Android应用内结算购买的商品

时间:2016-07-28 11:59:01

标签: android in-app-billing

我正在按照一个教程来编写应用内结算项目,我设法让一切都很好,但是当我想知道用户是否购买了一个项目时,它总是假的,即使我用其他设备测试它有beta测试者帐户。

这是我用来购买物品的方式:

mHelper = new IabHelper(MainActivity.this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
       public void onIabSetupFinished(IabResult result) {
           IabHelper.QueryInventoryFinishedListener mGotInventoryListener
                   = new IabHelper.QueryInventoryFinishedListener() {
               public void onQueryInventoryFinished(IabResult result,
                                                    Inventory inventory) {

                   if (result.isFailure()) {
                       // handle error here
                   }
                   else {
                       // does the user have the premium upgrade?
                       boolean mIsPremium = inventory.hasPurchase(ITEM_SKU);
                       // update UI accordingly
                       Toast.makeText(getApplicationContext(), "" + mIsPremium, Toast.LENGTH_LONG).show();
                       if(mIsPremium){
                           buy.setVisibility(View.INVISIBLE);
                       }
                   }
               }
           };
           mHelper.queryInventoryAsync(mGotInventoryListener);
       }
    });

这是购买该项目的代码:

buy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mHelper.launchPurchaseFlow(MainActivity.this, ITEM_SKU, 10001,
                    new IabHelper.OnIabPurchaseFinishedListener() {
                        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                            if (result.isFailure()) {
                                return;
                            } else if (purchase.getSku().equals(ITEM_SKU)) {
                                mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
                                    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                                        if (result.isFailure()) {
                                        } else {
                                            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                                                    new IabHelper.OnConsumeFinishedListener() {
                                                        public void onConsumeFinished(Purchase purchase, IabResult result) {
                                                            if (result.isSuccess()) {
                                                                buy.setVisibility(View.GONE);
                                                            } else {
                                                            }
                                                        }
                                                    });
                                        }
                                    }
                                });
                            }
                        }
                    }, "mypurchasetoken");
        }
    });

当我使用我的Beta版测试人员在真实设备中测试应用时,代码在购买项目时工作正常,但是当我关闭应用并再次打开时,第一个代码显示该帐户没有购买项目

2 个答案:

答案 0 :(得分:0)

购买后,您可能正在使用该产品。

请参阅此处有关消耗品/非消耗品的信息: https://developer.android.com/google/play/billing/api.html#consumetypes

答案 1 :(得分:0)

好的伙计们,我已经解决了这个问题,但是如果没有阅读有关耗材/非耗材的商品,我将永远无法解决问题。我的商品是一次性购买,所以没有必要:

mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                                                new IabHelper.OnConsumeFinishedListener() {
                                                    public void onConsumeFinished(Purchase purchase, IabResult result) {
                                                        if (result.isSuccess()) {
                                                            buy.setVisibility(View.GONE);
                                                        } else {
                                                        }
                                                    }
                                                });

在代码的第二部分中,我所要做的就是删除它,然后一切都没问题,这样,代码的第二部分就是:

buy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mHelper.launchPurchaseFlow(MainActivity.this, ITEM_SKU, 10001,
                    new IabHelper.OnIabPurchaseFinishedListener() {
                        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                            if (result.isFailure()) {
                                return;
                            } else if (purchase.getSku().equals(ITEM_SKU)) {
                                mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
                                    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                                        if (!result.isFailure()) {
                                            buy.setVisibility(View.GONE);
                                        }
                                    }
                                });
                            }
                        }
                    }, "mypurchasetoken");
        }
    });

希望这将有助于将来的某些人,谢谢!。