iOS In-App Purchase completes successfully but currentPurchase remains undefined - getAvailablePurchases() always returns empty array #2997
Unanswered
shuvajitmaitra
asked this question in
Q&A
Replies: 2 comments 2 replies
-
We also face same problem. How we fix this problem. First time it can not success. If user buy it again then work. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Could you kindly compare your codebase to https://github.com/hyochan/react-native-iap/blob/main/example/screens/PurchaseFlow.tsx and try the example code and here is useIAP version https://github.com/hyochan/react-native-iap/blob/main/example/screens/SubscriptionFlow.tsx.
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
**I'm implementing in-app purchases for iOS using react-native-iap. The purchase flow appears to work (App Store sheet shows, payment processes successfully), but the purchase is never detected by the React Native layer. Both currentPurchase from useIAP hook and getAvailablePurchases() remain empty/undefined.
When I am entering the component for the second time and click in the buy now button then in-app purchase bottomsheet is not showing but get the current purchases.**
Version:
"react-native": "0.79.2",
"react-native-iap": "^14.0.1",
"react-native-nitro-modules": "^0.29.2",
Code:
import {Alert, TouchableOpacity} from 'react-native';
import React, {useEffect, useState} from 'react';
import {cn} from '../../utils/cn';
import HeadingTextV2 from '../common/HeadingTextV2';
import {useIAP, finishTransaction, initConnection} from 'react-native-iap';
// Interface for component props
interface IapButtonProps {
containerStyle?: string;
title?: string;
textStyle?: string;
courseId: string;
iapProductId: string;
slug?: string;
}
const IapButton: React.FC = ({
containerStyle,
title,
textStyle,
iapProductId,
slug,
}) => {
const [isLoading, setIsLoading] = useState(false);
const {connected, requestPurchase, currentPurchase} = useIAP();
useEffect(() => {
const initializeIAP = async () => {
try {
await initConnection();
} catch (error) {
console.error(error);
}
};
console.log(
'Current Purchase -----------------------------------------',
currentPurchase?.productId,
);
!currentPurchase && initializeIAP();
}, []);
const completePurchase = async () => {
if (!currentPurchase) return;
try {
// Grant the purchase to user here
};
useEffect(() => {
if (currentPurchase) {
completePurchase();
}
}, [currentPurchase]);
const handlePurchase = async (productId: string) => {
if (!connected) {
Alert.alert(
'Not Connected',
'Store connection unavailable. Please try again later.',
);
return;
}
};
return (
<>
<TouchableOpacity
onPress={() => {
handlePurchase(iapProductId);
}}
disabled={isLoading}
className={cn(
'bg-black h-14 mt-2 rounded justify-center items-center',
containerStyle,
)}>
<HeadingTextV2 className={cn('text-white', textStyle)}>
{isLoading ? 'Processing...' : title || 'Buy Now'}
</>
);
};
export default IapButton;
When I am entering for the first time on the screen and click on the Buy Now button:
When I am entering for the second time on the screen and click on the Buy Now button:
Beta Was this translation helpful? Give feedback.
All reactions