7

我正在处理应用内购买的样本。我的应用程序有 2 个按钮,默认情况下禁用第一个按钮,单击第二个按钮将购买并启用第一个按钮。购买按钮工作正常,但购买后,我检查了库存,但它总是返回 null,这意味着我还没有购买。

这是源代码:

设置:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buyButton = (Button) findViewById(R.id.buyButton);
    clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setEnabled(false);
    String base64EncodedPublicKey = "key";
    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed: " + result);
            } else {
                Log.d(TAG, "In-app Billing is set up OK");
            }
            // Query to detect user was buy this item or not
            mHelper.queryInventoryAsync(mReceivedInventoryListener);
        }
    });

    buyButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (clickButton.isEnabled()) {
                mHelper.queryInventoryAsync(mReceivedInventoryListener);
            } else {
                buyClick(v);
            }
        }
    });
}

购买商品点击:

public void buyClick(View view) {
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
            mPurchaseFinishedListener, "");
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            // Handle error
            return;
        } else if (purchase.getSku().equals(ITEM_SKU)) {
            // consumeItem();
            // buyButton.setEnabled(false);
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
        }

    }
};

听者:

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

        if (result.isFailure()) {
            Toast.makeText(getApplicationContext(),
                    "Query Inventory Error!", Toast.LENGTH_SHORT).show();
            // Handle failure
        } else {
            Toast.makeText(getApplicationContext(),
                    "Query Inventory Success!", Toast.LENGTH_SHORT).show();
            // mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
            // mConsumeFinishedListener);
            // if (inventory.hasPurchase(ITEM_SKU)) {
            // clickButton.setEnabled(true);
            // }
            Purchase item = inventory.getPurchase(ITEM_SKU);
            if (item != null) {
                clickButton.setEnabled(true);
            } else {
                Toast.makeText(getApplicationContext(),
                        "This item was not buy yet!", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {

        if (result.isSuccess()) {
            Toast.makeText(getApplicationContext(), "Consume done!",
                    Toast.LENGTH_SHORT).show();
            clickButton.setEnabled(true);
        } else {
            Toast.makeText(getApplicationContext(), "Consume Error!",
                    Toast.LENGTH_SHORT).show();
            // handle error
        }
    }
};

第一次点击购买按钮时,显示支付对话框,支付成功后,点击按钮被启用。

但是当我第二次点击buyButton时,它会进入库存但inventory.getPurchase(ITEM_SKU)总是返回null。

任何想法?

谢谢!

4

1 回答 1

9

just comment or remove your code from OnIabPurchaseFinishedListener "mHelper.consumeAsync(purchase, mConsumeFinishedListener);"

如果您更喜欢 Android 默认的 Trivial 演示,请购买高级产品购买单次产品。

OnIabPurchaseFinishedListener()

// Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: "
                    + purchase);
            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                // setWaitScreen(false);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                // setWaitScreen(false);
                return;
            }

            Log.d(TAG, "Purchase successful.");


                if (purchase.getSku().equals(SKU_PREMIUM)) {
                          // bought the premium upgrade! 
                          Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
                  alert("Thank you for upgrading to premium!");
                    }

        }
    };  

解释为什么它给出空数据:

请记住,只有当您尝试购买一次性购买产品时,Google 才会从他们那里存储数据。但是,每当您购买消耗品时,Google Play 商店将不会在 Google Play 控制台中管理它的产品购买详细信息和其他内容。这就是为什么我们必须调用 consumeAsync() 方法。当我们购买商品时,Google Play 商店会记录已购买过一次的商品,并允许您进行第二次购买。

有关更多信息: android:Inapp billing:错误响应:7:Item Already Owned

但是,在这里您尝试购买产品作为单次购买,因此您不必在 onIabPurchasedFinishListener 中调用 consumeAsync() 方法。

希望它能解决你的问题。

于 2013-10-22T11:25:02.793 回答