Reputation: 3953
When a user purchases a subscription we run the code that calls purchase(). After the subscription is successful, the app runs correctly, which navigates to the sign in page and after the user signs in, goes to our home view.
Then, if I quit the app and go back in, the paywall comes back up again. Instead it should go to the home view.
I thought by checking Purchases.getCustomerInfo()
I would get an updated status from RevenueCat
const [isActive = false, setIsActive] = useMMKVBoolean(
storageKeys.purchases.isActive
); //to store on device
const [userId, setUserId] = useMMKVString(storageKeys.user.id); //to store on device
const [isReady, setIsReady] = useState(false);
const [sawPaywall, setSawPaywall] = useState(false);
const [isLoadingRestoreData, setIsLoadingRestoreData] = useState(false);
// initial configuration
useMySWR({
key: 'init_purchases',
fetcher: async () => {
try {
if (!APIKEY) {
throw new Error('Missing API Key for purchases');
}
if (isReady) return;
await Purchases.setLogLevel(Purchases.LOG_LEVEL.DEBUG);
Purchases.configure({ apiKey: APIKEY, appUserID: userId });
await getInfo();
} catch (e) {
console.error((e as Error).message);
!__DEV__ &&
Bugsnag.notify({
name: 'initPurchaseConfig Error',
message: (e as Error).message,
});
Bugsnag.leaveBreadcrumb('initPurchaseConfig Error', {
country: JSON.stringify(getLocales()?.[0]),
});
setMessage(['error', (e as Error).message]);
} finally {
setIsReady(true);
}
},
});
const getInfo = useCallback(async () => {
try {
const info = await Purchases.getCustomerInfo();
packageRef.current =
info?.entitlements?.active?.[entitlementName]?.productIdentifier;
const { isActive: isEntitled } =
info?.entitlements?.active?.[entitlementName] || {};
setIsActive(!!isEntitled);
if (isEntitled) {
setSawPaywall(true);
}
return info;
} catch (e) {
throw new Error((e as Error).message);
}
}, [setIsActive]);
const purchase = async (purchasePackage: PurchasesPackage) => {
try {
await Purchases.purchasePackage(purchasePackage);
await getInfo();
} catch (e) {
if ((e as Error).message !== 'Purchase was cancelled.') {
throw new Error((e as Error).message);
}
}
};
I have traced this to an issue with the info?.entitlements?.active?.[entitlementName].isActive
part of the response of getCustomerInfo
: it is returning false.
If I press “restore purchase” and the app functions fine after that.
What is causing isActive
to be false yet the restore purchase updates. I know there’s a cache on getCustomerInfo
, but it feels like something I am doing is wrong?
How do you initialize & check purchase entitlements?
EDIT: I think this is a cache issue with RevenueCat getCustomerInfo
because if I let my app sit for 5+ minutes and refresh, it seems to work as expected. So how can I work around this?
Upvotes: 0
Views: 128
Reputation: 3953
This may have been an issue with identifying/syncing logged in customer. Adding Purchases.syncPurchase
after user logs in solves the problem.
Upvotes: 0