purchaseErrorListener is not working.
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 });
}
};
}
}
}
I encountered the same problem, or "purchaseUpdatedListener" which is called multiple times, so I put my validation logic just after the "requestSubscription"
I’m closing all issues reported in versions below 14, as the library now supports the new architecture with NitroModules and has been completely revamped.