In app purchase with capacitor not update product's states

Viewed 11

I've implemented an application with Ionic/AngularJS and setted up In-app purchase 2 (with capacitor plugin : link). When user land on a view that require a subscription, a checkout modal is displayed with the price and the description of the product.

To make it work, I've implemented a backend service to handle receipt validation. But the problem is : When the checkout modal is displayed, request are made to validate receipt two or three times (even if is the first time that the modal is displayed).

I've spend too many time on this feature and nothing works as expected. So here my questions :

  1. Why so many requests are sent to my backend from the store.validator when modal is displayed ?
  2. Why the property canPurchase of the product is always to false ?

The documentation about the capacitor In-App purchase plugin is so unclear... I think that I am missing something but I don't know what and I really need help.

Here an example of my code:

// On init : resgiter product, setup listeners, refresh store
// On destroy : Off all listeners
ngOnInit() {
  this.iap.verbosity = this.iap.DEBUG;
  this.store.register([
   { id : com.aaaa.app.premium, type: 
     this._store.PAID_SUBSCRIPTION }
   ]);

  this._store.validator = "https://URL_TO_MY_BACKEND";

  this._store.when(productId).owned(this.purchaseOwned);
  this._store.when(productId).updated(this.purchaseUpdated);
  this._store.when(productId).approved(this.purchaseApproved);
  this._store.when(productId).verified(this.purchaseVerified);
  this._store.when(productId).cancelled(this.purchaseCancelled);
  this._store.when(productId).expired(this.purchaseExpired);

  this._store.error(this.handleError);
  this._store.applicationUsername = userID;

  this._store.refresh();
}

purchaseExpired = (p: IAPProduct) => {
  console.log(`Product expired: `, p);
}

purchaseCancelled = (p: IAPProduct) => {
  this.isLoading = false;
  this._changeDetector.detectChanges();
}

purchaseOwned = (p: IAPProduct) => {
  // When subscription is owned...
  // Perform request to the backend to save informations
  // In the database
}

purchaseUpdated = async (p: IAPProduct) => {
  if (p.loaded && p.valid && p.transaction != null && p.canPurchase && !p.owned) {
    // User can purchase the product. Display it in the modal view
    this.subscriptions = [p];
    this.isLoading = false;
  }
}

purchaseApproved = (p: IAPProduct) => {
  p.verify();
}

purchaseVerified = (p: IAPProduct) => {
  p.finish();
}

purchaseProduct(product: IAPProduct) => {
  this.iap.order(product.id);
}

Thank you for answers.

Best regards.

0 Answers
Related