PeterStaev / nativescript-purchase

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

transactionState: 'purchasing' in Android #57

Closed sandoche closed 5 years ago

sandoche commented 5 years ago

Hello,

I am on Android.

The first time I ran it triggered the payement and I didn't consume it (I added the consume function later). And now everytime I execute the purchase.buyProduct it goes to the else with the console.log(transaction). Also I don't know how to reset it, how to remove the purchase from the phone. I removed it from the Play Store only.

    purchase.on(purchase.transactionUpdatedEvent, (transaction) => {
      if (transaction.transactionState === TransactionState.Purchased) {
        alert(`Congratulations you just bought ${transaction.productIdentifier}!`)
        console.log(transaction.transactionDate)
        console.log(transaction.transactionIdentifier)
        purchase.consumePurchase(transaction.transactionReceipt) /* 3 */
        .then((responseCode) => {
          if (responseCode === 0) {
            this.toggleDarkmode()
          }
        })
        .catch((e) => console.log(e))
      }
      else if (transaction.transactionState === TransactionState.Restored) {
        console.log(`Purchase of ${transaction.productIdentifier} restored.`)
        console.log(transaction.transactionDate)
        console.log(transaction.transactionIdentifier)
        console.log(transaction.originalTransaction.transactionDate)
        this.toggleDarkmode()
      }
      else if (transaction.transactionState === TransactionState.Failed) {
        alert(`Purchase of ${transaction.productIdentifier} failed!`)
      }
      else {
        console.log(transaction)
      }

The last console.log is triggered and here is the result:

JS: { nativeValue: null,
JS:   transactionState: 'purchasing',
JS:   productIdentifier: 'com.sandoche.gitnews.darkmode',
JS:   developerPayload: undefined }

I am a bit stuck what can I do? How can I go back to the "buy" window? How do I reset the previous purchase?

sandoche commented 5 years ago

I tried in another device and everytime I click on trigger the action it asks me to pay. And triggering purchase.restorePurchases() do not do anything I don't see the purchase already done :/ But I can see them in the developper console of Google Play.

sandoche commented 5 years ago

So one of the phone the purchase.restorePurchases() triggers the right event but the purchase.buyProduct() triggered only once the first time and never again.

And in the other phone it's the other way around. So I am a bit confused. There are both on Android 8.0. One is a Xiaomi mi5s and the other a galaxy S7.

PeterStaev commented 5 years ago

Hey @sandoche , I'm not sure how purchases should work in your app, but I do not think how purchases work in general. So couple of points that should point you in the right direction:

  1. If you consume a purchase that will NOT be restored via restorePurchases. Such purchases should be used when the user can buy multiple times of an item in your app.
  2. If you DON'T consume a purchase it can be bought ONLY once. If you try to buy it another time you will get an exception like reports by users in other GH issues (for example: https://github.com/PeterStaev/nativescript-purchase/issues/48 , https://github.com/PeterStaev/nativescript-purchase/issues/45). Such purchases should be used for products that are bought only once by the user (for example unlocking some app functionality). Those purchases will be returned when you call restorePurcahses

I suggest you read in details all the possible purchases on both platforms as they differ quite a bit and you decide which one will fit you best for the in-app products you offer in your app. Only then to start implementing your purchase workflow and testing with your actual products, because you might end up messing your test GoogleID.

sandoche commented 5 years ago

THanks a lot that was the problem !