When React native RNIap.purchaseErrorListener is called?

Viewed 201

I have integrated react-native-iap for in app purchase. Android Payment flow works properly(payment success, payment failed and user cancel payment). But facing issue when purchase card is shown and I click outside the card, card is dismissed but not getting event inside RNIap.purchaseErrorListener . Hence my state variable did not updated.

here is sample: steps to dismiss card

Listener code :

  useEffect(() => {
  initPurchase()

  purchaseErrorSubscription = RNIap.purchaseErrorListener(

    (error) => {
      console.log('purchaseErrorListener INAPP>>>>', error);
    
      if(error.code == "E_USER_CANCELLED") {
        //for cancelled or refund sku
        setSelectedPackage(null)
        RNToasty.Show({
          title: error.message,
        });
      } else if(error.code == "E_ITEM_UNAVAILABLE") {
        //item not found
        setSelectedPackage(null)
      } else {
        setSelectedPackage(null)
      }
      
    },

  );

 

}, [])  

Listener not called when I perform above action.

Can anyone help me to get this event? Thanks in advance!!

1 Answers

you need to put this in a function, and in useEffect call this function

const checkCurrentPurchaseError = async () => {
    purchaseErrorListener(async currentPurchaseError => {
      if (currentPurchaseError) {
        Here is your code...
      }
    });
  };
  
  useEffect(() => {
    checkCurrentPurchaseError(currentPurchaseError);
  }, []);

Related