PeterStaev / nativescript-purchase

:moneybag: A NativeScript plugin for making in-app purchases!
Apache License 2.0
83 stars 28 forks source link

Getting Invalid sku type when attempting to consume a purchase on Android #111

Closed tangerinegit closed 3 years ago

tangerinegit commented 3 years ago

Hello,

I am testing on a physical device deploying from VS Code directly. Not sure if this could be the cause for this issue. The product I am exposing to the customer is a subscription. Here is my code:

purchase.on(purchase.transactionUpdatedEvent, (transaction: Transaction) => {
      if (transaction.transactionState === TransactionState.Purchased) {
          alert(`Congratulations you just bought ${transaction.productIdentifier}!`);
          if (isAndroid) {
              //consume the purchase
              purchase.consumePurchase(transaction.transactionReceipt)
                  .then((responseCode) => console.log('Purchase consumed'))
                  .catch((e) => console.log('Error consuming the purchase', e, transaction.transactionReceipt));
          }
      }
      else if (transaction.transactionState === TransactionState.Restored) {
          console.log(`Purchase of ${transaction.originalTransaction.productIdentifier} restored.`);
      }
      else if (transaction.transactionState === TransactionState.Failed) {
          alert(`Purchase of ${transaction.productIdentifier} failed!`);
      }
      else {
          console.log('Other transaction status: ', transaction);
      }
  });

Everything works well, except that the purchase.consumePurchase returns the Invalid sku type error. The transaction receipt is a string like llbggebdncolcmklgdebdeij.AO-J1OyYvj_OU8HJJ0e8tYtSeXRl8kKH6tOgaaaaaQNecXwRS3gTuX98VIvbKmvyPWbH0L9Mz-kJpC88G5tZIfnfd8c-axb3lIhKwqBAU9REkIDuOmL1Ao8.

I can see the subscription being purchased in my Google Play account.

I am not sure what I am doing wrong. Did I miss anything?

Thanks.

PeterStaev commented 3 years ago

Hey @tangerinegit, the problem is caused, becaused you are trying to consume a subscription. Subscriptions are non-consumable. Only product purchases can be consumed. So if you are working with subscriptions only, you should remove the consume call. If you have both subscriptions and products that need to be consumed, you need to have a check and only call consume for the in-app products and not the subscriptions. That's why in the readme i'm checking the product ID to have .consume before I make a call to the consume method:

purchase.on(purchase.transactionUpdatedEvent, (transaction: Transaction) => {
    if (transaction.transactionState === TransactionState.Purchased && transaction.productIdentifier.indexOf(".consume") >= 0) {
        purchase.consumePurchase(transaction.transactionReceipt)
            .then((responseCode) => console.log(responseCode)) // If responseCode === 0 the purchase has been successfully consumed
            .catch((e) => console.log(e));
    }    
});

But you can change this to whatever works in your case.

tangerinegit commented 3 years ago

Thank you @PeterStaev