I’ve recently been trying to implement InAppPurchase2 in an ionic capacitor app and all the callback functions are prompting the error “x is not a function” in my chrome debugger.
I get this error when writing all the callbacks - .approved, .order, .owned.
The code compiles fine, am I missing a dependency or?
example error:
ERROR Error: Uncaught (in promise): TypeError: this.store.when(...).approved is not a function
TypeError: this.store.when(...).approved is not a function
at HomePage.setupListeners (home.page.ts:56)
at home.page.ts:24
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:27437)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at zone-evergreen.js:857
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27425)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at resolvePromise (zone-evergreen.js:798)
at zone-evergreen.js:864
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27425)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at drainMicroTaskQueue (zone-evergreen.js:569)
template:
import { Component } from '@angular/core';
import { InAppPurchase2, IAPProduct } from "@ionic-native/in-app-purchase-2/ngx";
import { Platform, AlertController} from '@ionic/angular';
const REMOVE_ADS = 'remove_ads';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
isPro = false;
constructor(private store: InAppPurchase2, private plt: Platform,
private alertControllers: AlertController) {
this.plt.ready().then(() =>{
this.store.verbosity = this.store.DEBUG;
this.registerProducts();
this.setupListeners();
this.store.ready(() => {
console.log('Store is ready');
this.presentAlert('Products: ' ,JSON.stringify(this.store.products));
});
})
}
async presentAlert(header, message){
const alert = await this.alertControllers.create({
header,
message,
buttons: ['OK']
})
await alert.present();
}
registerProducts(){
this.store.register({
id: REMOVE_ADS,
type: this.store.NON_CONSUMABLE
});
this.store.refresh();
}
setupListeners(){
// Subscribe in-app purchase event listeners
this.store.when(REMOVE_ADS).approved((p: IAPProduct) =>{
//
});
this.store.when(REMOVE_ADS).owned((p: IAPProduct) =>{
//
});
this.store.refresh();
}
purchase() {
this.store.refresh();
this.store.order(REMOVE_ADS).then(x => {this.presentAlert('Success', 'yey');}, e => {this.presentAlert('Failed', '${e}');});
}
restore(){
this.store.refresh();
}
}
.