简体   繁体   English

Android应用程序内订阅:即使购买存在,库存调用也会将购买返回为null

[英]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. 此问题非常类似于库存Android结算中 未返回的已购买订阅 - 您拥有此项目时出错,但其中提供的答案并未解决我的问题。

I have the latest code from https://code.google.com/p/marketbilling/ . 我有来自https://code.google.com/p/marketbilling/的最新代码。 My signed apk is installed on the device and is uploaded as a draft on the Play Store. 我签名的apk已安装在设备上,并作为草稿上传到Play商店。 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. 我的订阅购买存在于Google电子钱包商户中心,并显示在queryPurchases和queryInventoryAsync中,但在我的PurchaseActivity中显示为null。

This code is from queryPurchases method in IabHelper.java and displays purchase information: 此代码来自IabHelper.java中的queryPurchases方法,并显示购买信息:

        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: 此方法“queryInventoryAsync”来自IabHelper.java类并返回库存信息:

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: 这来自我的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. 虽然queryPurchases和queryInventoryAsync显示我的设备和电子邮件帐户存在购买,但getPurchase(sku)将Purchase显示为null并且hasPurchase(sku)返回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. 使用某些版本代码将草稿应用程序上载为alpha或beta。
  • Login on device with account which has active subscription. 使用有效订阅的帐户登录设备。
  • Install signed application on this device with same version as in alpha/beta release. 使用与alpha / beta版本相同的版本在此设备上安装已签名的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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