cordova-plugin-purchase icon indicating copy to clipboard operation
cordova-plugin-purchase copied to clipboard

Upon verified App should redirect you - not working

Open pmosedale opened this issue 2 years ago • 2 comments

I have the following:

iap.validator = "https://validator.fovea.cc/v1/validate?appName=XXX";

  //initiate initInAppPurchase function 
  useEffect(() => {
      const init = async () => {
          await initInAppPurchase();
      }
      init();

  }, []);

  const initInAppPurchase = async () => {
      if ((isPlatform('ios')) || (isPlatform('android'))) {
        document.addEventListener(
          'deviceready',
          () => {
            iap.autoFinishTransactions = true;
            iap.refresh();
          })

          iap.verbosity = iap.DEBUG;

          iap.register([
            {
              id: "tpmonthly",
              alias: "Monthly",
              type: iap.PAID_SUBSCRIPTION
            }, {
            id: "tpannual",
            alias: "Annual",
            type: iap.PAID_SUBSCRIPTION
            },
          ])

          iap.ready(() => {
              let product = iap.get('Monthly');
              let productA = iap.get('Annual');
              setPrice(product.price)
              setProduct(product)
              setPriceA(productA.price)
              setProductA(productA)
          })
      }
  }

  const purchaseProduct = () => {
      if (product.owned || productA.owned) {
          alert('A subscription is currently active.')
      } else {
          iap.order('Monthly').then(() => {
              iap.when("tpmonthly").approved((p: IAPProduct) => {
              setLoading(true);
              setDuration('monthly');
              p.verify();
              }).verified((p: IAPProduct) => {
              p.finish();
              history.push("/ios-signup/?duration=monthly");
              });
          })
          
          setLoading(false);
      }
  }

This is a section of the code, but now I am also having trouble with the plugin even accepting a payment (in sandbox mode).

The following is from XCode:

To Native Cordova ->  InAppPurchase purchase InAppPurchase37537468 ["options": [tpmonthly, 1, , {
}]]
⚡️  [log] - [store.js] DEBUG: state: tpmonthly -> requested
2022-10-27 16:19:56.130425+0800 App[1449:149756] 8.15.0 - [Firebase/Analytics][I-ACS023141] Purchase is a duplicate and will not be reported. Product ID: tpmonthly
To Native Cordova ->  InAppPurchase appStoreReceipt InAppPurchaseXXX ["options": []]

When it does work it always does not redirect the user either - clearly I am missing something - is anyone able to offer advice?

pmosedale avatar Oct 27 '22 08:10 pmosedale

not missing anything, that's how the plugin code works

cozyop avatar Jan 01 '23 00:01 cozyop

you can check like this and redirect user for better understand see this example here

      CdvPurchase.store.when()
        .approved(transaction => transaction.verify())
        .verified(receipt => receipt.finish())
        .finished(transaction => console.log('Products owned: ' + transaction.products.map(p => p.id).join(',')))
        .receiptUpdated(r => this.updatePurchases(r)) //update purchase - do something here
        .productUpdated(p => this.updateUI(p)); //if already purchased then you can also update in this  - do something here
updatePurchases(receipt: CdvPurchase.Receipt) {
    console.log("", receipt);
    receipt.transactions.forEach(transaction => {
      console.log("transaction", transaction);
      transaction.products.forEach(trProduct => {
        console.log(`product owned: ${trProduct.id}`);  
      });
    });
  }

sksk008 avatar Jan 19 '23 08:01 sksk008