Google Play: Improve upgrade check reliability - #747
Merged
Conversation
Previously we used `orderId` to check for valid purchases but apparently this is not a reliable way to check state. While [documentation](https://developer.android.com/reference/com/android/billingclient/api/Purchase#getOrderId()) says: > The order ID will be null if the purchase is in the Purchase.PurchaseState.PENDING state and populated if the purchase has transitioned to the Purchase.PurchaseState.PURCHASED state. in reality I could reproduce cases where the `orderId` field was `null` despite `getPurchaseState() == PURCHASED`. What up Google? My best guess so far is that the cached data that `queryPurchasesAsync` sometimes returns, does not always contain all fields. Clearing Google Play cache can reset it and fix that, but it's unclear why and when the "half-cached" state returns. A normal, fully cached purchase entry looks like this: ```json { "orderId": "GPA.<id>", "packageName": "eu.darken.sdmse", "productId": "eu.darken.sdmse.iap.upgrade.pro", "purchaseTime": 1697351226769, "purchaseState": 0, "purchaseToken": "<token>", "quantity": 1, "acknowledged": true } ``` A half-cached entry looks like this: ```json { "packageName": "eu.darken.sdmse", "productId": "eu.darken.sdmse.iap.upgrade.pro", "purchaseTime": 1680023656958, "purchaseState": 0, "purchaseToken": "<token>" } ``` While the json payload contains `purchaseState == UNSPECIFIED`, the actual `Purchase.getPurchaseState()` function still returns `PURCHASED` in the half-cached state. Due to fallback handling: ```java public int getPurchaseState() { switch (this.zzc.optInt("purchaseState", 1)) { case 4: return 2; default: return 1; } } ``` So this PR changes the check to `getPurchaseState`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously we used
orderIdto check for valid purchases but apparently this is not a reliable way to check state.While documentation says:
in reality I could reproduce cases where the
orderIdfield wasnulldespitegetPurchaseState() == PURCHASED. What up Google?My best guess so far is that the cached data that
queryPurchasesAsyncsometimes returns, does not always contain all fields. Clearing Google Play cache can reset it and fix that, but it's unclear why and when the "half-cached" state returns.A normal, fully cached purchase entry looks like this:
{ "orderId": "GPA.<id>", "packageName": "eu.darken.sdmse", "productId": "eu.darken.sdmse.iap.upgrade.pro", "purchaseTime": 1697351226769, "purchaseState": 0, "purchaseToken": "<token>", "quantity": 1, "acknowledged": true }A half-cached entry looks like this:
{ "packageName": "eu.darken.sdmse", "productId": "eu.darken.sdmse.iap.upgrade.pro", "purchaseTime": 1680023656958, "purchaseState": 0, "purchaseToken": "<token>" }While the json payload contains
purchaseState == UNSPECIFIED, the actualPurchase.getPurchaseState()function still returnsPURCHASEDin the half-cached state. Due to fallback handling:So this PR changes the check to
getPurchaseState.