I have purchased and cancelled the order. So it got refunded. After the refund the purchaseState is in 0 (UNSPECIFIED_STATE). Hereafter cancellation of the purchaseQuerySkus return the state as 0.
But the for loop returns the value as 1 for getPurchaseState.
But while processing it satisfying the condition. It is weird.
List returns for me [Purchase.Json: {
"orderId": "orderID",
"packageName": "com.android.app",
"productId": "in_app",
"purchaseTime": 29323923232,
"purchaseState": 0,
"purchaseToken": "weorehrhjewrhewirhewiruhewfbewfbweuyfbwehfbweyfbwefbwefweuyfhweyufhwe",
"quantity": 1,
"acknowledged": true
}]
Code:
private List<Purchase> purchaseQuerySkus;
private void queryPurchasesAsync() {
if(mBillingClient.isReady()) {
LogUtils.LOGI(TAG, "queryPurchasesAsync");
billingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
LogUtils.LOGI(TAG, "queryPurchasesAsync list" + list);
purchaseQuerySkus = list;
}
});
}
}
Checking the purchase is already made or not after the cancellation. It returning the purchaseState as 0. But the print values shows as 1.
if (billingClient.isReady()) {
queryPurchasesAsync();
LogUtils.LOGI(TAG, "QUE_PUR_LIST_SIZE " + purchaseQuerySkus.size());
//It returns the size of the list as 1
for (Purchase pur : purchaseQuerySkus) {
String thisSku = pur.getSkus().get(0); //my sku value
LogUtils.LOGI(TAG, "pur.getPurchase state " + pur.getPurchaseState()); //It prints the value as 1
But the returned list has the purchaseState as 0 which I have added above.
// So it satisfying the condition and showing the item was purchased
if (pur.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Acknowledging the user if purchased
}
I know I'm a bit late for the party but stil... This also surprised me. It's because list is using toString()
to show elements which in turns uses original json and purchaseState
from json isn't directly matched with Purchase.PurchaseState
enum. If you checkout Purchase.class
file there you'll find this enum:
@Retention(RetentionPolicy.SOURCE)
public @interface PurchaseState {
int UNSPECIFIED_STATE = 0;
int PURCHASED = 1;
int PENDING = 2;
}
and if you check Purchase.getPurchaseState()
you'll see how it's converting purchaseState from json int to Purchase.PurchaseState
enum. Please note that by default it's picking purchased state and it looks like Purchase.PurchaseState.UNSPECIFIED_STATE
from java class is never used.
public int getPurchaseState() {
switch(this.zzc.optInt("purchaseState", 1)) {
case 4:
return 2;
default:
return 1;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.