question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

purchaseUpdatedListener is repeatedly called

See original GitHub issue

Version of react-native-iap

4.3.0

Version of react-native

0.60.5

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

Both

Expected behavior

Following the example I want to call a custom method after my subscription has been purchased. It is my understanding that after acknowledging the purchase the listener should be removed? I have tried explicitly calling remove but had no success.

Example

 const purchaseListener = purchaseUpdatedListener(purchase => {
      const receipt = purchase.transactionReceipt;
      if (receipt) {
        callCustomEndpoint().then(result => {
          if(result){
            // Subscription is valid
            finishTransaction(purchase);
          }
        }) 
      };
      finishTransaction(purchase);
    });

Actual behavior

The callCustomEndpoint() method is being called multiple times. How can I stop this from happening? As I only need to hit this endpoint once.

I’m using a functional component instead of class component if it’s important.

Tested environment (Emulator? Real Device?)

Real Device

Steps to reproduce the behavior

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
heidjicommented, Aug 21, 2020

I am going to put down my solution for hooks for those who will need it later on.

outside your state component (though probably works also inside):

var purchaseUpdateSubscription;
var purchaseErrorSubscription;
const loadIAPListeners = () => {
  initConnection(); // important, or else it won't trigger before a random state change
  purchaseUpdateSubscription = purchaseUpdatedListener(
    async (
      purchase: InAppPurchase | SubscriptionPurchase | ProductPurchase,
    ) => {
      console.log('purchaseUpdatedListener', purchase);
      let receipt = purchase.transactionReceipt;
      if (receipt) {
        apis.checkreceipt({data: purchase, platform: Platform.OS}); // I personally don't care about callback
        if (Platform.OS === 'ios') {
          await RNIap.finishTransactionIOS(purchase.transactionId);
        } else if (Platform.OS === 'android') {
          await RNIap.acknowledgePurchaseAndroid(purchase.purchaseToken);
        }
        await RNIap.finishTransaction(purchase, true);
        await RNIap.finishTransaction(purchase, false);
      } else {
        // Retry / conclude the purchase is fraudulent, etc...
      }
    },
  );
  purchaseErrorSubscription = purchaseErrorListener((error: PurchaseError) => {
    console.log('purchaseErrorListener', error);
  });
};

inside your state component:

useEffect(() => {
    loadIAPListeners();
    return () => {
      purchaseUpdateSubscription.remove();
      purchaseErrorSubscription.remove();
    };
  }, []);
0reactions
aravi365commented, Jun 16, 2020

@wootwoot1234 @osmantuna Can you please provide the event handler codes if you managed to fix it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

purchaseUpdatedListener called multiple times even after ...
Any solution I am facing the same issue of calling both purchaseUpdatedListener purchaseErrorListener multiple times (more than 15 times).
Read more >
java - onPurchasesUpdated called multiple times
When I call launchBillingFlow on the BillingClient : BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder() .
Read more >
purchaseUpdatedListener called multiple times even after ...
purchaseUpdatedListener called multiple times even after purchase.
Read more >
react-native-iap - npm
On Android, it can be called at app launch, but on iOS, only at restoring purchase is ... purchaseUpdatedListener will receive the result....
Read more >
React Native: Subscriptions with In-App Purchases - Ross Bulat
The way react-native-iap keeps track of incoming transactions is via ... processNewPurchase is called inside the purchaseUpdatedListener ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found