iosreact-nativein-app-purchasemobile-developmentreact-native-iap

react-native-iap requestPurchase not working properly


I'm gonna make in-app purchase payment for ios in react-native app.

This is my code.

const items = Platform.select({
  ios: ["item_25", "item_55", "item_155"],
  android: [""],
})
  const {
    connected,
    subscriptions, //returns subscriptions for this app.
    getSubscriptions, //Gets available subsctiptions for this app.
    getProducts,
    products,
    currentPurchase, //current purchase for the tranasction
    finishTransaction,
    purchaseHistory, //return the purchase history of the user on the device (sandbox user in dev)
    getPurchaseHistory, //gets users purchase history
    requestPurchase
  } = useIAP();

  const handleGetProducts = async () => {
    setGetPlaning(true);
    try {
      await getProducts({ skus: items });
    } catch (error) {
      errorLog({ message: "handleGetProducts", error });
    }
  };

  useEffect(() => {
    handleGetProducts();
  }, [connected]);

  const handlePurchase = async (productId) => {
    setLoading(true);
    try {
      await requestPurchase({
        sku: productId,
      });
      setLoading(false);
    } catch (error) {
      setLoading(false);
      if (error instanceof PurchaseError) {
        console.error({ message: `[${error.code}]: ${error.message}`, error });
      } else {
        console.error({ message: "handlePurchase", error });
      }
    }
  };
<Button
  color={colors.black}
  size="large"
  loading={loading}
  style={{ marginTop: theme.SIZES.BASE }}
  onPress={() => handlePurchase('item_25')}>
    <Text color={colors.white} size={20} bold>Purchase</Text>
</Button>

Above code is showing payment modal

Modal

But after click purchase button, this modal closed and open again.

I don't know the reason about this.

Anyone help me with this issue?

Thanks.


Solution

  • Try it with a physical device, not a simulator.