react-native-iap: purchaseUpdatedListener called only once on app start, and never calls after requestSubscription call

Version of react-native-iap

4.4.6

Version of react-native

0.61.5

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

ios

Expected behavior

purchaseUpdatedListener called on each time, after requestSubscription call

Actual behavior

purchaseUpdatedListener called only once on app start, and never calls after requestSubscription call

Tested environment (Emulator? Real Device?)

Real Device

Steps to reproduce the behavior

Just follow readme and try to request subscription

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 25 (4 by maintainers)

Most upvoted comments

Had the same issue as I didn’t add the initConnection() call. The README seems to be just updated with the bold part here:

initConnection() On iOS, it will simply call canMakePayments method and return value which is required for the listeners to work properly.

I think this part “On iOS, it will simply call canMakePayments” should be removed, also initConnection() should be added to the basic example

I was really convinced it was optional because it wasn’t in the example

Adding the initConnection() before registering the purchase listener/s fixed this for me, so this is probably related to #1002 and #756

I had the same issue for a few days with RN 0.62.2 and react-native-iap 4.4.8 I somehow missed the initConnection() probably because it is missing from the example in the README

If I may add, 4.3.4 through 4.4.3 work fine, the problem begins in 4.4.4

Glad it helped. We have this in production since June. It did change from Observable to this at some point (we’re not using typescript)

      const purchaseEvent = new NativeEventEmitter(NativeModules.RNIapIos);
      const subscription = purchaseEvent.addListener(
        'purchase-updated',
        transactionData => {
          console.log('IAP-LOG purchase-updated');
          dispatch(validateRecepit(transactionData));
        }
      );
      const errorSubscription = purchaseEvent.addListener(
        'purchase-error',
        data => {
          crashlytics().log(`Purchase error ${JSON.stringify(data)}`);
          console.log('IAP-LOG purchase-error', data);
        }
      );
    };

@Sophrinix I’ve just tried requestPurchase in my side and everything seems to be working. Could you please share some of your code?

Also, please check again if you’ve called initConnection. This is critical changes in a recent update that it is now necessary to be called in iOS as in android.

I’m on 4.4.9 and the issue described by this ticket is happening to me as well. The listener is only invoked when started, but if I set it and then perform the purchase, it is not called. I solved it by subscribing to the native event directly, after looking at react-native-iap/index.ts and figuring out that’s what happens internally. This can be done at any time and will persist. (Just make sure it’s done only once)

import { NativeModules, NativeEventEmitter } from 'react-native';
import { Observable } from 'rxjs/Observable';

const purchaseEvent = Observable.fromEvent(
  new NativeEventEmitter(NativeModules.RNIapIos),
  'purchase-updated'
);
const purchaseSubscription = purchaseEvent.subscribe((transactionData) => {
  // Trigger server receipt validation here...
});
const errorEvent = Observable.fromEvent(
  new NativeEventEmitter(NativeModules.RNIapIos),
  'purchase-error'
);
const errorSubscription = errorEvent.subscribe((errorData) => {
  // Handle errors here...
});

I confirm that reverting to 4.3.0 works. I was going nuts with the latest version, I’ve been debugging for over 1h trying to find the issue.