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

purchaseErrorListener is not working. #2878

Open
Karthik-V26 opened this issue Nov 5, 2024 · 1 comment
Open

purchaseErrorListener is not working. #2878

Karthik-V26 opened this issue Nov 5, 2024 · 1 comment

Comments

@Karthik-V26
Copy link

Karthik-V26 commented Nov 5, 2024

Hi,
I have used purchaseErrorListener to get the updates of any error. I am not getting any errors through this listener if any error occurs. Am I using it right or any other way to do it ?
I have added requestSubscription method in the try catch block, where if any errors occurs its getting in this catch method, but not in listener.
Am i missing anything here ?

useEffect(() => {
    console.log("Setting up purchase listeners");

    const handleFinishTransaction = async (purchase: Purchase) => {
      try {
        const acknowledgeResult = await finishTransaction({purchase});
        console.log("Transaction finished:", acknowledgeResult);
      } catch (error) {
        errorLog({ message: "Error finishing transaction", error });
        Alert.alert("Error", "There was an issue completing your purchase.");
      }
    }
    const membershipValidationHandler = async (purchase: Purchase) => {
      try {
            const response = await MembershipValidationApi(params);
            return response;
          } catch (error){
              console.log("An error occured");
        } 
    }
    
    const purchaseListener = purchaseUpdatedListener(async (purchase: Purchase) => {
      try {
        console.log("New Purchase Update!!");
          const receipt =  purchase.transactionId;
          const isTransactionPurchaseReason = purchase.transactionReasonIOS;
          if (receipt) {
            if (isTransactionPurchaseReason === "PURCHASE") {
              const validationResponse = await membershipValidationHandler(purchase);
              if (validationResponse.ok) {
                console.log("Membership validation successfull")
                await handleFinishTransaction(purchase);
              } else {
                console.log("Membership validation failed:", validationResponse);
              }
            } else {
              console.log("This is Renewed transaction: ", isTransactionPurchaseReason);
            }
          } else {
            throw new Error("Receipt is missing in the purchase.");
          }
        }
      } catch (error) {
        errorLog({ message: "Error processing purchase update", error })
      }
    });

    const purchaseErrListener = purchaseErrorListener((error: PurchaseError) => {
      console.log("purchaseError", error);
      if (error.code === 'E_USER_CANCELLED') {
        Alert.alert("Purchase Cancelled ", "You cancelled the sign-in/purchase process.");
        return;
      } else {
        console.log("purchase error console");
        errorLog({ message: `[${error.code}]: ${error.message}`, error });
      }
    });
    return () => {
      purchaseListener.remove();
      purchaseErrListener.remove();
    };
  }, []);
  
  const handleBuySubscription = async (productId: any) => {
      try {
        const subscriptionPurchaseResult = await requestSubscription({
          sku: productId
        });
        setPurchaseDetails(subscriptionPurchaseResult);
      } catch (error) {
        if (error instanceof PurchaseError) {
          errorLog({ message: `[${error.code}]: ${error.message}`, error });
        } 
        else {
          if (error.code === 'E_USER_CANCELLED') {
            console.log("error", error.code, error.message, error.name);
            Alert.alert("Purchase Cancelled", "You cancelled the payment process. Please try again");
            return;
          } else {
            errorLog({ message: `Error purchasing subscription - ErrorCode: ${error.code}, ErrorMsg: ${error.message}`, error });
          }
        };
      } 
    } 
  }
@mlcpro
Copy link

mlcpro commented Nov 6, 2024

I encountered the same problem, or "purchaseUpdatedListener" which is called multiple times, so I put my validation logic just after the "requestSubscription"

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