react-native-iap icon indicating copy to clipboard operation
react-native-iap copied to clipboard

purchaseUpdateSubscription runs multiple times which cause multiple times DB query

Open webexpert889 opened this issue 3 years ago • 7 comments

Please use Discussion board if you want get some help out of it. Please report issue if there is a bug.

Version of react-native-iap : 6.0.6

Version of react-native : 0.62

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

Expected behavior :

Actual behavior

Tested environment (Emulator? Real Device?) Real Device

Steps to reproduce the behavior

Code :

const getProducts = useCallback(async () => { await RNIap.clearProductsIOS(); await RNIap.clearTransactionIOS(); await RNIap.flushFailedPurchasesCachedAsPendingAndroid();

try {
  const result = await RNIap.initConnection();
  console.log('result', result);
} catch (err) {
  console.warn(err.code, err.message);
}

purchaseUpdateSubscription = purchaseUpdatedListener(
  async (purchase) => {
    const receipt = purchase.transactionReceipt;
    if (receipt) {
      try {
		
		if (purchase.purchaseStateAndroid === 1 && !purchase.isAcknowledgedAndroid) {
			//  RNIap.consumePurchaseAndroid(purchase.purchaseToken)
			const ackResult = await RNIap.acknowledgePurchaseAndroid(purchase.purchaseToken);
			console.log("ackResultpurchase", purchase);
		}
		  // IOS
		if (Platform.OS == "ios") {
			// alert(purchase.transactionId)
			await RNIap.finishTransactionIOS(purchase.transactionId);
		}

        const ackResult = await finishTransaction(purchase);
        console.log('ackResult', ackResult);
      } catch (ackErr) {
        console.warn('ackErr', ackErr);
      }
    //   Alert.alert('purchase error', JSON.stringify(receipt));
    }
  },
);

purchaseErrorSubscription = purchaseErrorListener(
  (error) => {
    console.log('purchaseErrorListener', error);
    Alert.alert('purchase error', JSON.stringify(error));
  },
);

const productss = await RNIap.getProducts(itemSkus);
setProducts(productss);
productss.forEach((product) => {
  products.type = 'inapp';
});
// console.log('products', JSON.stringify(productss));

}, []);

useEffect(() => { getProducts();

return () => {
  if (purchaseUpdateSubscription) {
    purchaseUpdateSubscription.remove();
  }
  if (purchaseErrorSubscription) {
    purchaseErrorSubscription.remove();
  }
};

}, []);

webexpert889 avatar Jul 06 '21 11:07 webexpert889

One of the things I notice is that you didn't add getProducts as a dependency to the useEffect hook. I'm not quite sure how this would affect your code but make sure that your listeners are never registered more than once

StefanNedelchev avatar Jul 08 '21 18:07 StefanNedelchev

I'm also facing this issue, if the previous purchase was not acknowledged then it appears every app opening

adar2378 avatar Jul 11 '21 05:07 adar2378

I'm also facing this issue, if the previous purchase was not acknowledged then it appears every app opening

if you didn't acknowledge the purchase - then this is not an issue. I's supposed to appear again so you can proceed acknowledge it. Once you complete validating your receipt you should consume or acknowledge the purchase depending on its type.

StefanNedelchev avatar Jul 12 '21 07:07 StefanNedelchev

If the server is down and from the app we could not validate the receipt and the user tried multiple times then it will trigger multiple purchases using the purchaseUpdatedListener. Is there any way to avoid that?

adar2378 avatar Jul 12 '21 08:07 adar2378

If the server is down and from the app we could not validate the receipt and the user tried multiple times then it will trigger multiple purchases using the purchaseUpdatedListener. Is there any way to avoid that?

Hello sir, have you found any solution for the same, purchaseUpdatedListener is firing multiple time

nirbhaisingh1313 avatar Aug 17 '21 12:08 nirbhaisingh1313

purchaseUpdatedListener is being fired multiple times on opening the app and is not being fired when the actual purchase is being made.

sakshya73 avatar Sep 22 '21 12:09 sakshya73

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Apr 18 '22 18:04 stale[bot]

Same issue here. Although the transaction is finished correctly, the listener keeps firing in some cases..don't know how to reproduce unfrotuantly.

nes123 avatar Aug 16 '22 18:08 nes123

Please open a new issue if this is still happening on the latest version. Please include what kind of transactions are being sent. Are they cancelled for example? They should be easy to filter

andresesfm avatar Sep 01 '22 23:09 andresesfm