react-native-iap: Doesn't let rebuy Android item on test account

Version of react-native-iap

0.3.10

Platforms you faced the error (IOS or Android or both?)

Android

Expected behavior

Should be able to rebuy an item as many times as I need since I’m just testing

Actual behavior

Returns

Error: You already own this item.
    at createErrorFromErrorData (59522b9c-ca17-4e00-9e00-05cc989b1199:2016)
    at 59522b9c-ca17-4e00-9e00-05cc989b1199:1968
    at MessageQueue.__invokeCallback (59522b9c-ca17-4e00-9e00-05cc989b1199:2410)
    at 59522b9c-ca17-4e00-9e00-05cc989b1199:2155
    at MessageQueue.__guardSafe (59522b9c-ca17-4e00-9e00-05cc989b1199:2323)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (59522b9c-ca17-4e00-9e00-05cc989b1199:2154)
    at t (RNDebuggerWorker.js:1)

Steps to reprodue the behabior

Buy an item more than once using a test account

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

@kaushil111 I was able to consume/flush them like this

  try {
    await RNIap.initConnection();
    const availablePurchases = await RNIap.getAvailablePurchases();

    availablePurchases.forEach((purchase) => {
      finishTransaction(purchase, true);
    });
  } catch (error) {
    console.warn(
      "Failed to connect to IAP and finish all available transactions"
    );
  }

I was running into the same issue as @ilyakar, and using the code below I was able to clear the non-consumable purchase and test it again.


async componentDidMount() {
    const { itemSkus } = this.props;
    try {
      const result = await RNIap.initConnection()
      console.log('initiated connection?', result)
      const consumed = await RNIap.consumeAllItems();
      console.log('consumed all items?', consumed)
      let products = await RNIap.getProducts(itemSkus);
      console.log('loaded products', products)
    } catch (err) {
      console.warn(err);
    }
  };

I ran into this earlier today and after digging through the code I found that a pretty straight-forward option is to call finishTransaction(purchase, true)

There is a more complete code snippet below.

import * as IAP from "react-native-iap"

...

const App = () => {
  
  ...

  useEffect(() => {
    const checkCurrentPurchases = async (purchase) => {
      if (purchase) {
        const receipt = purchase.transactionReceipt
        if (receipt) {
          try {
            const isConsumable = true // if we were dealing with non-consumables this would be false but we aren't.
            const ackResult = await IAP.finishTransaction(purchase, isConsumable)
            console.info(`Transaction Completed Successfully ${JSON.stringify(ackResult)}`)
          } catch (ackError) {
            console.warn(`Error finishing transaction ${ackError}`)
          }
        }
      }
    }
    checkCurrentPurchases(currentPurchase)
  }, [currentPurchase])

  ...
  
}

I hope this helps someone. 🙂

@TylerNoblett This solution isn’t working right now.

  • I tried with consumeAllItemsAndroid instead of consumeAllItems. Any further help is appreciated.

@ilyakar Is this a consumable product? You should consumeItem to rebuy it. Please read some guides on how IAP works in android.

Hi @dooboolab, I was just responding to @deadcoder0904 with my previous knowledge. I’m afraid I have not tried your solution and probably won’t for some time since it’s not an urgent fix. But I will let you know in the future when I have.

Yes @deadcoder0904, that’s the whole concept of Android test accounts (https://developer.android.com/google/play/billing/billing_testing.html). Which do work fine with the plugin, but as I have pointed out, you cannot make the same purchase twice.