2

I can buy only one item ("productitem1"). If I had purchase this item, I can't purchase it again. But I need it to buy it several times. In my Google Play Console, I can only choose between "Managed In-app Products" and "subs". I have setting it up to "Managed In-app Products".

@Override
protected void onActivityResult(int request, int response, Intent data) {
    if (request == 42) {
        int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
        String dataSignature = data.getStringExtra("INAPP_DATE_SIGNATURE");
        if (response == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String productId = jo.getString("productId");
                Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                Log.e(getClass().getSimpleName(), "JSONException", e);
            }
        }
    }
}

btnBuy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String name = "productitem1";
            try {
                Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), name, "inapp", "");
                if(buyIntentBundle.getInt("RESPONSE_CODE")==0) {
                    PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                    startIntentSenderForResult(
                            pendingIntent.getIntentSender(), 42, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
                }
            } catch (Exception e) {
                Log.e(Start.this.getClass().getSimpleName(),"Exception:",e);
            }

        }
    });
SilverBlue
  • 239
  • 2
  • 13

2 Answers2

2

Before you can buy another item with the same SKU, you need to consume it using one of the methods available from the In-App Billing API like IabHelper.consumeAsync()

mHelper.consumeAsync(inventory.getPurchase(SKU_ITEM), mConsumeFinishedListener);

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
           new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        if (result.isSuccess()) {
            // provision the in-app purchase to the user
            // (for example, credit 50 gold coins to player's character)
        } else {
            // handle error
        }
    }
};

More info here: https://developer.android.com/training/in-app-billing/purchase-iab-products.html#Consume

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • I get the errors: Cannot resolve symbol 'mHelper' and Cannot resolve symbol 'IabHelper' – SilverBlue May 31 '17 at 14:30
  • How are you purchasing items? Please add some more code to your question se we can see which version of the purchase library you are using (Obviously mHelper is a `IabHelper` instance) – MatPag May 31 '17 at 14:41
  • I purchase items with the code in my question post. I have imported the actually files from google. All purchases work fine, but I can purchase all items only one time, not multiple. – SilverBlue May 31 '17 at 14:50
  • If I add your code, I get the error Cannot resolve symbol 'mHelper' and Cannot resolve symbol 'IabHelper' – SilverBlue May 31 '17 at 14:51
  • Anything to import that I can use the IabHelper and mHelper ? I have imported "import com.android.vending.billing.IInAppBillingService;" – SilverBlue May 31 '17 at 14:52
  • Post the link of the sample you imported in your project for IAP flow – MatPag May 31 '17 at 14:54
  • I use the android SDK, downloading the Google Play Billing Library (version 5), an import the 9 java files into my project (SDK/extras/google/play_billing/samples/TrivialDrive/src/com/xample/android/trivialdrivesample/util) – SilverBlue May 31 '17 at 14:58
  • If you added all the classes, you will see that at [this link](https://github.com/googlesamples/android-play-billing/tree/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/util) you have 8 classes to add to your project, one of them is `IabHelper` then you need to handle all the purchase procedure using a private var mHelper in your code [like the example](https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java#L139) in the MainActivity class – MatPag May 31 '17 at 15:04
  • I've added the code below in my answer-post. But I get the Errors "Variable 'mHelper' might not have been initialized – SilverBlue May 31 '17 at 15:14
  • Man you need to initialize the property first... if you search for "mHelper" string in the MainActivity.class i linked you in the previous comment you will see that at some point the property [is initialized](https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java#L177), i will suggest you to make some base understanding of object oriented programming before proceding on in-app purchases flow because your are probably lacking in it. Good luck :) – MatPag May 31 '17 at 15:18
  • thank you, but now I get a new error :-( Look at my edited answer post... – SilverBlue May 31 '17 at 15:23
0
IabHelper.OnConsumeFinishedListener onConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        @Override
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            try {
                Log.d("Tag", "this is onConsumeFinished - "+result.toString()+" and purchased"+purchase.toString());
                mHelper.consumeAsync(purchase,onConsumeFinishedListener);
            } catch (IabHelper.IabAsyncInProgressException e) {

                Log.d("Tag", "this is onConsumeFinished Error -  "+e.toString());
                e.printStackTrace();
            }
        }
    };
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 18:26