Nilesh Zare
Nilesh Zare

Reputation: 21

How to consume product inapp billing to purchase same product multiple time android

I am unable to purchase same product multiple time, i am not getting where to write the code of consuming product in following code. Can anyone give me any advice?

This is my code:

//-- onCreate

inAppPurchase = new InAppPurchase(this);


//--- calling on button click

inAppPurchase.purchase(this, "android.test.purchased", "123");


//--- class InAppPurchase

public class InAppPurchase {

    public static final String BASE64_ENCODED_PUBLIC_KEY = "XXXXXXXXXXX"; // CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE
    public static final int RC_REQUEST = 10001;
    String playStoreProductId;
    IabHelper mHelper;
    Activity activity;

    public InAppPurchase() {
        // TODO Auto-generated constructor stub
    }

    public InAppPurchase(Activity activity) {
        // TODO Auto-generated constructor stub
        mHelper = new IabHelper(activity, BASE64_ENCODED_PUBLIC_KEY);
        mHelper.enableDebugLogging(true);

        // Start setup. This is asynchronous and the specified listener will be called once setup completes.
        Log.d("", "Starting setup.");
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 
        {
            public void onIabSetupFinished(IabResult result) 
            {
                 if (!result.isSuccess()) 
                 {
                     Log.e("", "Problem setting up in-app billing: " + result);
                 }
                 else
                 {
                     Log.e("", "Setup successful. Querying inventory.");     
                 }
            }
        });
    }

    public void purchase(Activity activity, String playStoreProductId, String orderId)
    {
        this.activity = activity;
        this.playStoreProductId = playStoreProductId;
        String payload = orderId;
        mHelper.launchPurchaseFlow(activity, playStoreProductId, RC_REQUEST, mPurchaseFinishedListener, payload);       
    }

    // Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() 
    {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
        {
            Log.e("", "Purchase finished: " + result + ", purchase: " + purchase);
            if (result.isFailure()) 
            {
                Log.e("", "Error purchasing: " + result);
            }
            else if(purchase.getSku().equals(playStoreProductId))
            {
                Log.e("", "Purchase product. Starting product consumption." + playStoreProductId);
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        }
    };

    // Listener that's called when we finish querying the items and subscriptions we own
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() 
    {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) 
        {
            Log.e("", "Query inventory finished.");
            if (result.isFailure()) 
            {
                Log.e("", "Failed to query inventory: " + result);
            }
            else //if(result.isSuccess()) 
            {
                Log.e("", " else We have product. Consuming it." + playStoreProductId);
                Purchase purchase = inventory.getPurchase(playStoreProductId);
                if (purchase != null) 
                {
                    Log.e("", "We have product. Consuming it.");
                    mHelper.consumeAsync(inventory.getPurchase(playStoreProductId), mConsumeFinishedListener);
                }
            }
        }
    };

    // Called when consumption is complete
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() 
    {
        public void onConsumeFinished(Purchase purchase, IabResult result) 
        {
            Log.e("", "Consumption finished. Purchase: " + purchase + ", result: " + result);
            if (result.isSuccess()) 
            {
                // Save Data
                Log.e("", "Consumption successful. Provisioning.");
            }
            else 
            {
                Log.e("", "Error while consuming: " + result);
            }
        }
    };
}



protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.e("", "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == InAppPurchase.RC_REQUEST) 
    {
        int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
        String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
        Log.e("", ""+responseCode + " " + purchaseData + " " + dataSignature);
        if (resultCode == RESULT_OK) 
        {
            if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_OK)
            {
                try 
                {
                    JSONObject jo = new JSONObject(purchaseData);
                    String mOrderId = jo.getString("orderId");
                    String mPackageName = jo.getString("packageName");
                    String mSku = jo.getString("productId");
                    long mPurchaseTime = jo.getLong("purchaseTime");
                    int mPurchaseState = jo.getInt("purchaseState");
                    String mDeveloperPayload = jo.getString("developerPayload");
                    String mToken = jo.optString("token", jo.getString("purchaseToken"));
                }
                catch (JSONException e) 
                {
                    Log.e("", "Failed to parse purchase data.");
                    e.printStackTrace();
                }   
            }
            else if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_USER_CANCELED)
            {

            }
            else if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_SERVICE_UNAVAILABLE)
            {
                Toast.makeText(getApplicationContext(), "Network connection is down", 1).show();
            }
        }
        else if (resultCode == RESULT_CANCELED) 
        {
            Toast.makeText(getApplicationContext(), "Payment Canceled", 1).show();
        }
    }
}

Upvotes: 2

Views: 435

Answers (1)

Abhriya Roy
Abhriya Roy

Reputation: 1438

Please take a look at this article

To consume a purchase you have to do something like this

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

Please take a look at the link i have posted above. It provides step by step instructions. Following them, you will have no problem to consume the purchase.

Upvotes: 1

Related Questions