I'm using Capacitor (but not Ionic) to package a SvelteKit application for iOS and am trying to get an in-app-purchase working.
Capacitor's page on in-app-purchases is surprisingly unhelpful. I've done my best and:
I have the products set up in appstoreconnect and they're status is "ready to submit"
I've installed
cordova-plugin-purchaseand runnpx cap updateandnpx cap syncand it's installing
[info] Found 1 Cordova plugin for ios: [email protected]
- I've tried to make the simplest test I could just to see what's going on:
import 'cordova-plugin-purchase'; // This seems to add `CdvPurchase` to the global scope.
function buy() {
const {store, ProductType, Platform} = CdvPurchase;
store.verbosity = store.DEBUG;
store.register([{
type: ProductType.CONSUMABLE,
id: "my-product-id",
platform: Platform.APPLE_APPSTORE,
}]);
store.error(e => {
console.log('error', e);
});
store.when()
.productUpdated(() => {
console.log('product updated', product);
})
.approved(value => {
console.log('approved', value);
})
.verified(value => {
console.log('verified', value);
})
.finished(value => {
console.log('finished', value);
});
store.ready(() => {
console.log('ready', store.products);
store.order('my-product-id');
});
store.initialize(Platform.APPLE_APPSTORE)
.then(() => {
console.log('initialize resolved', store.products);
store.order('my-product-id');
});
}
But I run the buy function, all I get is:
[log] - [CordovaPurchase] INFO: initialize()
The store never reports as ready. None of the listeners are triggered, not even .error().
Have I missed something? How do I debug this?
You need to set up and initialize the store when the device is ready.
You can't
buy()a product and then initialize the store; it has to be done on device startup.For example, in Cordova, this is the
onDeviceReadyevent (example).For Capacitor, if you're using Ionic + Angular, you can use the
this.platform.ready()event.For React, Vue, and other frameworks, you can use: