简体   繁体   中英

Android in-app subscription: Inventory call returns purchase as null even if purchase exists

This question is very similar to Purchased subscription not returned in inventory & Android billing - error you own this item but the answers provided in them did not solve my problem.

I have the latest code from https://code.google.com/p/marketbilling/ . My signed apk is installed on the device and is uploaded as a draft on the Play Store. My subscription purchase exists in Google Wallet Merchant Center and shows up in queryPurchases and queryInventoryAsync but it shows up as null in my PurchaseActivity.

This code is from queryPurchases method in IabHelper.java and displays purchase information:

        for (int i = 0; i < purchaseDataList.size(); ++i) {
            String purchaseData = purchaseDataList.get(i);
            String signature = signatureList.get(i);
            String sku = ownedSkus.get(i);

            logDebug("Purchase Data: "+purchaseData);

            if (Security.verifyPurchase(mSignatureBase64, purchaseData, signature)) {
                logDebug("Sku is owned: " + sku);
                Purchase purchase = new Purchase(itemType, purchaseData, signature);

                logDebug("Purchase Data: "+purchase);

                if (TextUtils.isEmpty(purchase.getToken())) {
                    logWarn("BUG: empty/null token!");
                    logDebug("Purchase data: " + purchaseData);
                }

                // Record ownership and token
                inv.addPurchase(purchase);

                logDebug("Get Purchase Data: "+ inv.getPurchase(sku));
            }
            else {
                logWarn("Purchase signature verification **FAILED**. Not adding item.");
                logDebug("   Purchase data: " + purchaseData);
                logDebug("   Signature: " + signature);
                verificationFailed = true;
            }
        }

This method "queryInventoryAsync" is from the IabHelper.java class and returns inventory information:

public void queryInventoryAsync(final boolean querySkuDetails,
                           final List<String> moreSkus,
                           final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            }
            catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                        logDebug("Result: "+ result_f.getResponse() + " inventory: "+ inv_f.getAllOwnedSkus().get(0));
                    }
                });
            }
        }
    })).start();
}

This is from my PurchaseActivity:

IabHelper.QueryInventoryFinishedListener mInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {

        // Is it a failure?
        if (result.isFailure()) {
            Log.i(TAG, "Failed to query inventory: " + result);
            return;
        }else{
            //This returns false
            Log.i(TAG, "Query inventory was successful: " + inventory.hasPurchase(sku)); 
        }

        // Do we have the premium upgrade?
        //This returns null
        Purchase premiumPurchase = inventory.getPurchase(sku);

Although queryPurchases and queryInventoryAsync show that a purchase exists for my device and email account, getPurchase(sku) shows Purchase as null and hasPurchase(sku) returns false.

Does anyone have any ideas why this is happening? Thanks!

I had the same problem and solved it in this way

  • Upload draft application as alpha or beta with some version code.
  • Login on device with account which has active subscription.
  • Install signed application on this device with same version as in alpha/beta release.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM