Hey everyone,
I know that there are lots of people out here that try to use the IAP2 plugin and there’s not a lot of documentation, so I’m hoping that someone here can help me figure this out and then I can write a small guide for future users or even add the sample code to the ionic-native IAP2 plugin github repo.
I have an app with a single non consumable IAP and I’m trying to setup a button that triggers ‘purchase()’ with the follow .ts code:
purchase() {
console.log('Starting Configurations');
let productId;
if (this.Platform.is('ios')) {
productId = 1325094585;
} else if (this.Platform.is('android')) {
productId = 1111111111;
}
try {
// Register Product
console.log('Registering Product ' + JSON.stringify(productId));
this.store.verbosity = this.store.DEBUG;
this.store.register({
id: productId,
alias: productId,
type: this.store.NON_CONSUMABLE
});
// Handlers:
this.store.ready().then((status) => {
console.log('store_ready', {});
console.log(JSON.stringify(this.store.get(productId)));
console.log('Store is Ready: ' + JSON.stringify(status));
console.log('Products: ' + JSON.stringify(this.store.products));
});
//Check Registration
this.store.when(productId).registered( (productId) => {
console.log('Config Registered: ' + JSON.stringify(productId));
});
//Config Load
this.store.when(productId).updated( (productId) => {
console.log('Config Loaded' + JSON.stringify(productId));
});
// Purchase
let product = this.store.get(productId);
console.log('Purchase Product Info:' + JSON.stringify(productId));
this.store.order(product).then( () => {
console.log('Purchase Successful');
this.storage.set('IAP_number', 100);
})
//Cancelled
this.store.when(productId).cancelled( (product) => {
console.log('Config purchase_cancelled' , {});
});
//Store Error
this.store.error( (err) => {
console.log('Config store_error', {});
});
//Product Error
this.store.when(productId).error( (error) => {
console.log('store_error', {});
alert('An Error Occured' + JSON.stringify(error));
});
}
//Catch
catch (err) {
console.log('Purchase Error Ordering ' + JSON.stringify(err));
let alert = this.alertCtrl.create({
title: 'Error:',
subTitle: 'There was an error with your attempted purchase. Please try again and contact the developer if the error persists.',
buttons: [
{
text: 'OK',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}}
]
});
alert.present();
}
}
My xcode debugging window readout:
11:18:21.382 Purchase page loaded
11:18:23.613 Starting Configurations
11:18:23.613 Registering Product 0000000000
11:18:23.613 [store.js] DEBUG: store.queries !! '0000000000 registered'
11:18:23.614 [store.js] DEBUG: store.queries !! 'non consumable registered'
11:18:23.614 [store.js] DEBUG: store.queries !! 'registered'
11:18:23.614 [store.js] DEBUG: ios -> product 0000000000 registered
11:18:23.615 [store.js] DEBUG: store.queries !! '0000000000 updated'
11:18:23.615 [store.js] DEBUG: store.queries !! 'non consumable updated'
11:18:23.615 [store.js] DEBUG: store.queries !! 'updated'
11:18:23.616 Purchase Error Ordering {"line":1677,"column":34,"sourceURL":"http://localhost:8080/var/containers/Bundle/Application/7C364E81-59EB-4D2D-8F16-8DE01C151D9E/Pocket%20Thai.app/www/plugins/cc.fovea.cordova.purchase/www/store-ios.js"}
So what’s happening is that my purchase page loads, and on button tap it does start to configure the purchase, but for some reason the store.ready console logs never appear and after registration and updating the IAP it goes to the catch(err) portion of the typescript.
If anyone has any ideas I would love to help get this solved and written up for both myself but also to share with others in the community.
Thanks so much,
Evan