j3k0 / cordova-plugin-purchase

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

Check if user has an active subscription #1512

Open moblizeit opened 5 months ago

moblizeit commented 5 months ago

I want to show user subscribe option only if they dont have an active subscription.

my code is like below but i am getting undefined.

` constructor( public plt: Platform) {

this.plt.ready().then(() => {
  const store: CdvPurchase.Store = new window.CdvPurchase.Store(); 
  this.registerFunction()

  const activeSubscription = store.verifiedPurchases.find(purchase => {
    const product = store.get(purchase.id, purchase.platform);
    console.log("Found purchase at:", product)
    return product?.type === CdvPurchase.ProductType.PAID_SUBSCRIPTION && product.owned;
  });

  console.log("active subscripntion####", JSON.stringify(activeSubscription))
});

}

registerFunction() { const { store, ProductType, Platform, AppleAppStore } = CdvPurchase; store.verbosity = 4; //to debug

store.register([
  {
    id: PRODUCT_PRO_KEY,
    type: CdvPurchase.ProductType.PAID_SUBSCRIPTION,

    platform: Platform.GOOGLE_PLAY,
  }
]);

store.when()
.productUpdated((product) =>{
  if(product?.getOffer()?.canPurchase === true){
    this.products.push({
      "title": product.title,
      "price": product.pricing?.price,
      "currency": product.pricing?.currency
    })
  }
})
.approved(transaction => {
  console.log("txn approved.", JSON.stringify(transaction))
  transaction.verify()
})
.verified((receipt) => {
  console.log("transaction is verified", receipt)
  receipt.finish();
})

  if (this.plt.is('ios')) {
    console.log('init apple');
    store.initialize([{ platform: Platform.APPLE_APPSTORE }]);
  } else if (this.plt.is('android')) {
    console.log('init google');
    store.initialize([{ platform: Platform.GOOGLE_PLAY }]);
  } 

}//end constructor`

i am not using any receipt validator at this point.

orhan-swe commented 4 months ago

@moblizeit I am trying to do the same thing. Is owned() working for you?

moblizeit commented 4 months ago

Well i am seeing another issue in case of android that, the subscription is done but then going to the app subscriptions in play store, it says i should open the app to confirm subscription or else it will be canceled in 3 days. this is on testing a production account. so i think i need to resolve that too

RubioN commented 4 months ago

@orhan-swe owned is not working in my app too.

It's only working when I try to order a second time the product

j3k0 commented 3 months ago

When using a validator, owned depends on the result of the validator. If not, ownership cannot be determined on Apple devices (because the expiry date isn't made available locally).

Purchases cancelled after 3 days mean you do not call "finish", which acknowledges processing of the transaction.

j3k0 commented 3 months ago

Notice that this is well tested, maybe you could try first with using a validator (which is more than recommended with subscriptions)... You can try with https://iaptic.com first if that helps.