0

I am Implementing In App purchase in my app, using some online tutorial. But the tutorial is for Consumable In App Purchase. But in my case, Users need to buy only once.

I modified the Code, to disable the "Buy Button" after buying the In App Purchase. Now it's working fine. But the problem is if I close and open the app, "Buy button" getting enabled.

This is my xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".InAppBillingActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click_string"
        android:id="@+id/clickButton"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="113dp"
        android:onClick="buttonClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buy_string"
        android:id="@+id/buyButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="buyClick" />
</RelativeLayout>

This is my In App Billing Activity

public class InAppBillingActivity extends AppCompatActivity {

    private static final String TAG =
            "InAppBilling";
    IabHelper mHelper;
    static final String ITEM_SKU = "com.example.buttonclick";

    private Button clickButton;
    private Button buyButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_app_billing);

        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton = (Button)findViewById(R.id.clickButton);
        clickButton.setEnabled(false);
        String base64EncodedPublicKey =
                "<place your public key here>";

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

    public void buttonClicked (View view)
    {
        Intent Quiz = new Intent(getApplicationContext(), QuestionYearwises.class);
        startActivity(Quiz);
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data)
    {
        if (!mHelper.handleActivityResult(requestCode,
                resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

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

        }
    };
    public void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
    }

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

            if (result.isFailure()) {
                // Handle failure
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
            }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        clickButton.setEnabled(true);
                    } else {
                        // handle error
                    }
                }
            };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

2

Every time your users open the app, the first thing you want to do is to check for the status of the purchase. If the user already owns the purchase, release the features in your app related to the purchase, and then just set your button visibility to View.GONE or something meaningful to you, personally I just hide it.

You must not consume your purchase if it is a one time purchase, seems like in your mPurchaseFinishedListener you are consuming it, then when you check for ownership of the item when the user opens the app it seems like is a recursive purchase as you already consumed the item, it will allow you to buy again.

You may want to consider using the Google Play Billing Library, if the one you are using is the one with AIDL it will be deprecated see.

This is a Codelab to get you started with the Google Play Billing Library

Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38
  • I saw the sample tutorials, but it's containing Subscription and consumable also. I am very new to coding, so I am not able get from that. If possible Can you edit my code and post it here. – Suresh Srinivasalu Sep 28 '19 at 19:26
  • Being very new to coding is a very good reason to go through the codelab I shared in the answer. If you plan to do more android apps and use In App Billing understanding how the Play Billing Library works is the best you can start doing. ALL `one time` purchases are consumable and eventually you will wanna go with subscriptions too. I can not edit your code. If this answer solved your problem or help you, please consider upvoting/accept. – Guanaco Devs Sep 28 '19 at 19:58