j3k0 / cordova-plugin-purchase

In-App Purchase for Cordova on iOS, Android and Windows
https://purchase.cordova.fovea.cc
1.3k stars 537 forks source link

Ionic React: Ready event not firing, product status canPurchase = false, loaded = false #1257

Open AlvinStefanus opened 2 years ago

AlvinStefanus commented 2 years ago

system info

Windows 11 PRO 22000.282 Ionic React 5.5.0 InAppPurchase2 5.36.0

Google Play Status

Internal testing

Testing from

Android emulator PIXEL_3_API_30

Command to run the debugging

ionic cap run android -l --external

Expected behavior

Observed behavior

Steps to reproduce

Here is my code:

import { IAPProduct, InAppPurchase2 as iap } from "@ionic-native/in-app-purchase-2";

const App: React.FC = () => {

  useEffect(() => {
    iap.verbosity = iap.DEBUG;

    iap.validator = (product: string | IAPProduct, callback: Function) => {
      callback(true, { transaction: 'dummy' });
    }

    iap.register({
      id: "com.mysoftwares.posapp.basic_1_month",
      alias: "Basic 1 month",
      type: iap.PAID_SUBSCRIPTION
    });

    iap.when("com.mysoftwares.posapp.basic_1_month").updated((product: IAPProduct) => {
      if (product.owned)
        console.log('Product owned')
      else
        console.log('Product not owned')
    });

    iap.when("com.mysoftwares.posapp.basic_1_month").approved(function (product: any) {
      product.finish();
    });

    iap.ready(() => {
      alert("Product ready!")
      let product = iap.get('Basic 1 month');
      alert(product);
      if (product.canPurchase) {
        iap.order('Basic 1 month');
      }
    })

    iap.refresh()
  },[]}

Here is the log from console:

[store.js] DEBUG: state: com.mysoftwares.posapp.basic_1_month -> registered
VM3:4724 [store.js] DEBUG: store.trigger -> triggering action refreshed
VM3:5548 InAppBilling[js]: setup ok
VM3:5548 InAppBilling[js]: load ["com.mysoftwares.posapp.basic_1_month"]
VM3:5548 InAppBilling[js]: listener: {"type":"ready","data":{}}
VM3:4724 [store.js] DEBUG: store.trigger -> triggering action refresh-finished
VM3:5548 InAppBilling[js]: setup ok
VM3:5548 InAppBilling[js]: load ["com.mysoftwares.posapp.basic_1_month","com.mysoftwares.posapp.basic_1_month"]
VM3:5548 InAppBilling[js]: listener: {"type":"ready","data":{}}
VM3:5548 InAppBilling[js]: setup ok
VM3:5548 InAppBilling[js]: load ["com.mysoftwares.posapp.basic_1_month","com.mysoftwares.posapp.basic_1_month","com.mysoftwares.posapp.basic_1_month"]
VM3:5548 InAppBilling[js]: listener: {"type":"ready","data":{}}

For some reason the load is called multiple times. I put breakpoints to check whether my code called multiple times because of react components, but they are not. The code only run once.

AlvinStefanus commented 2 years ago

My bad I was testing on emulator which is dumb. On my smart phone it works fine.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.