The paywall in Masareef, my expense tracker, marks the plan you already pay for. On Android it never did. Every monthly and yearly subscriber saw no current plan. Only the lifetime purchase matched.
There was no error. The code compared two strings that name the same subscription in different ways.
Two names for one subscription#
A Google Play subscription is a product plus a base plan. RevenueCat reports the two halves in different shapes depending on where you read them:
| Where | Field | Value |
|---|---|---|
| Entitlement | productIdentifier |
pro_annual |
| Entitlement | productPlanIdentifier |
annual |
| Package | product.identifier |
pro_annual:annual |
So this is false for every subscription on Android:
entitlement.productIdentifier === pkg.product.identifier;A one-time purchase has no base plan on either side, which is why lifetime was the only one that worked. On iOS there is no base plan either, so testing there shows nothing wrong.
RevenueCat's typings do say so, in separate places. productPlanIdentifier is "Android subscriptions only, null on consumables and iOS", and an Android subscription option's storeProductId "will be subId:basePlanId". Nothing puts the two next to each other.
A comparison that works#
Split both sides into a subscription id and a base plan, and compare the parts:
function subscriptionIdOf(productId: string): string {
const colon = productId.indexOf(':');
return colon === -1 ? productId : productId.slice(0, colon);
}
function basePlanOf(productId: string): string | null {
const colon = productId.indexOf(':');
return colon === -1 ? null : productId.slice(colon + 1);
}
export function isOwned(
packageProductId: string,
ownedId: string | null,
ownedPlan: string | null = null
): boolean {
if (!ownedId || !packageProductId) return false;
if (subscriptionIdOf(ownedId) !== subscriptionIdOf(packageProductId)) return false;
const mine = ownedPlan ?? basePlanOf(ownedId);
const theirs = basePlanOf(packageProductId);
if (!mine || !theirs) return true;
return mine === theirs;
}Call it with the entitlement's two fields:
isOwned(pkg.product.identifier, entitlement.productIdentifier, entitlement.productPlanIdentifier);The base plan decides only when both sides name one. An app that caches the entitlement may hold a record from before it saved the plan. Treating "unknown" as "different" would hide the plan from a paying user again.
The limit of that choice: if one subscription has several base plans and the cached record has none, every one of its packages matches. Save productPlanIdentifier along with the product id and the question goes away after the next refresh.
The same string in an upgrade#
The second place the combined id leaks into is a plan change. To replace a subscription you pass the old product:
await Purchases.purchasePackage(pkg, null, {
oldProductIdentifier: subscriptionIdOf(ownedId),
replacementMode,
});The typing says only "The old product identifier to upgrade from", which does not tell you which of the two forms it wants. Google Play lists a user's active purchases by the plain subscription id, without the base plan, so I pass the plain id.
I want to be exact about the evidence here. I did not see a user billed twice. I found the combined id going into this field while fixing the comparison, and a replacement that matches nothing risks being treated as a new purchase. If you find the same in your code, test the upgrade with a licence tester and check in Play that the old subscription ended.
Leave the field out when the user buys a lifetime product. A one-time purchase does not replace a subscription, so the app has to tell the user to cancel it.
Two more things that kept the bug alive#
The comment was wrong. A comment above the old helper described the shapes the other way round. It was confident and nobody checked it. Three lines in Node with real identifiers printed false and settled it.
The value was read once. The screen read the owned plan in a useMemo with an empty dependency list. RevenueCat's cache can be written after the screen opens, so even a correct comparison would have used an old answer. Read it from state that updates.
Test it with real strings#
The old helper had no test, and a test with made-up ids like monthly on both sides would have passed. Use the identifiers as each API returns them:
expect(isOwned('pro_annual:annual', 'pro_annual', 'annual')).toBe(true);
expect(isOwned('pro_annual:annual', 'pro_annual')).toBe(true);
expect(isOwned('pro_annual:monthly', 'pro_annual', 'annual')).toBe(false);
expect(isOwned('pro_monthly:monthly', 'pro_annual', 'annual')).toBe(false);
expect(isOwned('pro_lifetime', 'pro_lifetime')).toBe(true);A promotional grant is one more case to handle. Its product id starts with rc_promo and matches no package, so show it as its own state instead of an unknown plan.
I checked the typings quoted here in react-native-purchases 10.15.1. For the subscription side of a different provider, see which Lemon Squeezy statuses should keep access.