Here is my code for IOS:-
const handleBuySubscription = async (productId: any) => {
try {
setLoading(true);
await clearTransactionIOS();
console.log('in handle buy transaction');
if (token?.appAccountToken) {
const purchaseRequest: SubscriptionPurchase = await requestSubscription(
{
sku: productId,
appAccountToken: token?.appAccountToken,
}
);
console.log('purchaseRequestIAP', purchaseRequest);
const purchaseUpdateSubscription = purchaseUpdatedListener(
async (purchase: SubscriptionPurchase) => {
const { transactionReceipt: receipt, transactionId } = purchase;
console.log('purchase receipt:', receipt);
console.log('purchase details', purchase);
if (transactionId === purchaseRequest.transactionId) {
// verify transactionId before success response.
console.log('purchase successfull');
navigation.navigate(APP_PATH.ONPAYMENTSUCCESS, {
plan: selectedPlan,
});
purchaseUpdateSubscription.remove(); // removing event listeners after work done.
}
},
(err) => {
console.log('Error in purchase', err);
purchaseUpdateSubscription.remove(); // removing event listeners on failure.
}
);
} else {
/*
* if appAccountToken is not available it will show a toast not available.
* that's an impossible case because each user will have appAccountToken but did this to prevent app crash if this happens.
*/
console.log('appAccountToken not set for this user');
showToast('Not available!');
}
console.log('After handle buy transaction');
} catch (error) {
setLoading(false);
setCurrentTransaction(null);
if (error.code === 'E_NETWORK_ERROR') {
showToast('Network unavailable'); // This will show toast if internet is not available while purchasing a plan.
} else if (error.code === 'E_UNKNOWN') {
navigation.navigate(APP_PATH.ONPAYMENTFAILED); // This will navigate to the payment failed screen if the failure reason is E_UNKNOWN. E_UNKNOWN comes when there is payment failure due to several external reason like payment method declined or not added etc.
}
if (error instanceof PurchaseError) {
console.log({ message: `[${error.code}]: ${error.message}`, error });
} else {
console.log({
message: 'handleBuySubscription',
error: JSON.stringify(error),
});
}
}
};
Development:-
Testing works in the sandbox environment.
There is no feature to add or view balance in the sandbox environment, but there's a toggle button to simulate success or failure cases in iOS.
Production :-
- In production, you can add money to the Apple Wallet using UPI, cards, or net banking.
Problem :-
Unable to test UPI payment results in the development environment.
The
requestSubscription
function fails even after a successful UPI payment.
Any solutions would be greatly appreciated. Thank you in advance!
How can I handle the UPI payment scenario using the react-native-iap npm module?