Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop for requestPurchase #2725

Closed
jyoonsong opened this issue Apr 12, 2024 · 2 comments
Closed

Infinite loop for requestPurchase #2725

jyoonsong opened this issue Apr 12, 2024 · 2 comments

Comments

@jyoonsong
Copy link

jyoonsong commented Apr 12, 2024

Description

The purchase window keeps appearing again and again, creating an infinite loop.
I didn't click the purchase button again. It just keeps popping up again and again even after saying it's done.

Expected Behavior

The purchase should be done after being executed once.

Environment:

  • react-native-iap: ^12.13.1
  • react-native: 0.73.6
  • Platforms: iOS Simulator - iPhone SE (3rd generation) (iOS 17.4)

To Reproduce

My code is as follows:

import { Platform, Text, TouchableOpacity, View } from "react-native";
import {
  initConnection,
  purchaseErrorListener,
  purchaseUpdatedListener,
  flushFailedPurchasesCachedAsPendingAndroid,
  requestPurchase,
  getProducts,
} from "react-native-iap";
import { useEffect, useState } from "react";

const Shop = () => {
  const [purchaseUpdateSubscription, setPurchaseUpdateSubscription] =
    useState(null);
  const [purchaseErrorSubscription, setPurchaseErrorSubscription] =
    useState(null);
  const [products, setProducts] = useState([]);

  useEffect(() => {
    initIAP();

    return () => {
      if (purchaseUpdateSubscription) {
        purchaseUpdateSubscription.remove();
        setPurchaseUpdateSubscription(null);
      }
      if (purchaseErrorSubscription) {
        purchaseErrorSubscription.remove();
        setPurchaseErrorSubscription(null);
      }
    };
  }, []);

  const initIAP = async () => {
    try {
      const initialized = await initConnection();
      if (initialized) {
        console.log("initialized"); // THIS IS SUCCESSFULLY PRINTED

        const products = await getProducts({
          skus: ["product_id_1", "product_id_2", "product_id_3", "product_id_4", "product_id_5"],
        });
        setProducts(products); // THIS IS SUCCESSFULLY PRINTED

        if (Platform.OS === "android") {
          // we make sure that "ghost" pending payment are removed
          // (ghost = failed pending payment that are still marked as pending in Google's native Vending module cache)
          const flushed = await flushFailedPurchasesCachedAsPendingAndroid();
          console.log(flushed);
        }

        const updatedListener = purchaseUpdatedListener((purchase) => {
          console.log("purchaseUpdatedListener", purchase); // THIS IS NEVER GETTING PRINTED
          const receipt = purchase.transactionReceipt;
          if (receipt) {
            console.log(receipt); 
          }
        });
        setPurchaseUpdateSubscription(updatedListener);

        const errorListener = purchaseErrorListener((error) => {
          console.log("purchaseErrorListener", error);
          if (error.code !== "E_USER_CANCELLED") {
            alert(error.message);
          }
        });
        setPurchaseErrorSubscription(errorListener);
      } else {
        alert("Could not initialize payment");
      }
    } catch (error) {
      console.log(error);
      alert(error);
    }
  };

  const purchase = async (sku) => {
    try {
      const result = await requestPurchase({
        sku,
        andDangerouslyFinishTransactionAutomaticallyIOS: false,
      });
      console.log(result); // THIS IS NEVER GETTING PRINTED
    } catch (err) {
      console.warn(err.code, err.message);
    }
  };

  return (
    <>
      {products.map((product) => (
        <TouchableOpacity onPress={() => purchase(product.productId)}>
          <Text>{product.title}</Text>
        </TouchableOpacity>
      ))}
    </>
  );
};

export default Shop;

The console.log(result) inside purchase method never gets printed

@jyoonsong
Copy link
Author

#2603

Solved when I used physical device

@pradhyumansinh9849
Copy link

console.log(result); only got true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants