Inventory.getPurchase () always returns null, although already purchased

I am working with an in-ap purchase sample. My application has 2 buttons, the first button was disabled by default, click the second button to buy and enable the first button. The buy button works correctly, but after the purchase I check the inventory, but it always returns null, which means that I have not purchased yet.

Here is the source code:

Setup:

@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); } } }); } 

Click the buy button:

 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); } } }; 

Listener:

 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 } } }; 

When the first click on the purchase button, it displays a payment dialog, after the payment is successful, the clickButton button has been enabled.

But when I click on buyButton a second time, it goes into inventory, but inventory.getPurchase (ITEM_SKU) always returns null.

Any idea?

Thanks!

+6
source share
1 answer

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

If you prefer the default Android standard demo, go to the product purchase premium for a one-time purchase of the product.

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!"); } } }; 

Explanation why it gives null data:

Keep in mind that Google will only store data on its part if you are trying to purchase a one-time product for purchase. But whenever you purchased the google play consumer store will not be controlled the product has purchased parts and other things in the Google Play console. Therefore, we need to call the consumeAsync () method. when we bought, a record item was saved in the Google Play store that was purchased for once and will allow you to purchase a second time.

for more information: android: Inapp binary: Error response: 7: item already belongs

But here you are trying to purchase the product as a one-time purchase, so you do not need to call the consumeAsync () method in onIabPurchasedFinishListener.

Hope this solves your problem.

+9
source

Source: https://habr.com/ru/post/956463/


All Articles