Question

Repurchase on reinstall only works if user restarts app


Badge +3

I have an App with a single In app purchase that I would like to see restored on reinstall or device switch. I am using anonymous id’s with the ‘Transfer to new App user ID’ setting. Upon app startup I call the ‘syncPurchases’ function immediately after ‘configure’ and wait for the response before proceeding with other revenuecat API calls.

if a user reinstalls the app, then he will not have the in app purchase available. He has to restart the app in order to see it take affect. I have only tested this out on Android, so I am not sure it’s an issue with IOS.

This is my react-native code:

useEffect(() => {
const fetchPurchaseData = async () => {
// ... omitted
if (Platform.OS === 'android') {
await Purchases.configure({ apiKey: APIKeys.google });
} else {
await Purchases.configure({ apiKey: APIKeys.apple });
}
await Purchases.syncPurchases();
const offerings = await Purchases.getOfferings();
const customerInfo = await Purchases.getCustomerInfo();
// ... omitted
};

fetchPurchaseData();

// ... omitted
}, []);

It seems to me that the sync have not taken affect when ‘getOfferings’ and ‘getCustomerInfo’ is called right after.


3 replies

Userlevel 4
Badge +6

Hey There! 

 

You might want to call getCustomerInfo() first, to make sure that you dont want/need to syncPurchases. If it fails, then I would recommend calling syncPurchases() . I am not completely sure on the best option for flow here, but that might help simplify the call in this case. The SDK will update the cache if it's older than 5 minutes, but only if you call getCustomerInfo(), make a purchase, or restore purchases, so it's a good idea to call getCustomerInfo() any time a user accesses premium content.

 

This could also be happening because of the cache looking like this user doesn't have a subscription maybe, as a suggestion I would try to maybe call invalidateCustomerInfoCache(), this method is used to invalidate the current cache if you want to force the SDK to fetch the data again.

 

More info on this method can be found here: https://revenuecat.github.io/react-native-purchases-docs/5.14.0/classes/default.html#invalidateCustomerInfoCache

Badge +3

Hello Michael

 

Thanks for responding. Your suggestions definitely gave me some options to try and made me rewrite my code a bit. I’ve managed to restore purchases using the ‘restorePurchases’ function. The documentation suggests to only use it on a button click, but it works if I call it automatically on startup. So basically replacing ‘syncPurchases’ with ‘restorePurchases’ fixes the issue: 

// Runs on app startup. A lot of code is omitted
useEffect(() => {
const initRevenueCat = async () => {
if (Platform.OS === 'android') {
await Purchases.configure({ apiKey: APIKeys.google });
} else {
await Purchases.configure({ apiKey: APIKeys.apple });
}

const customerInfo = await Purchases.getCustomerInfo();
if (!userHasUnlockedPremiumAccess(customerInfo)) {
await Purchases.restorePurchases();
}

setHasInitializedRevenueCat(true);
};
initRevenueCat();
}, []);

Using ‘syncPurchases’, even when invalidating the customerInfo cache will not work:

await Purchases.syncPurchases();
await Purchases.invalidateCustomerInfoCache();

So the main problem with the working implementation is that it goes directly against the documentation found here: https://www.revenuecat.com/docs/restoring-purchases

Userlevel 4
Badge +6

Hey There, 

 

Sometimes syncPurchases can act different if the id is anonymous, are your ids anonymous in this phase of the app? If so, that might be the reason why this is happening. 

Reply