Search code examples
androidin-app-purchasegoogle-play-servicespurchase-orderandroid-inapp-purchase

In-App Purchase Android,Buy same item over and over without consumption


Good day.I am implementing in-app purchase into android.The only items which shall be bough are coins.And by idea coins can be bought over and over.But as we know the google play in-app purchase keeps the item as purchased,and you can buy it only after consumption.So what is an best practice for this situation?Like i want to let user buy same coin pack over and over.What is the code for it? This is the code i am using for in-app purchase flow.

 private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name,
                                   IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);

        try {
            Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
            Log.d("Fasfsafasfasfa", "onServiceConnected: "+ownedItems);

            int response = ownedItems.getInt("RESPONSE_CODE");
            Log.d("Fasfsafasfasfa", "onServiceConnected: "+response);

            if (response == 0) {
                ArrayList<String> ownedSkus =
                        ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
                ArrayList<String> purchaseDataList =
                        ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
                ArrayList<String> signatureList =
                        ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE_LIST");
                String continuationToken =
                        ownedItems.getString("INAPP_CONTINUATION_TOKEN");


                Log.d("Fasfsafasfasfa", "onServiceConnected: "+ownedSkus.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+purchaseDataList.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+signatureList.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+continuationToken);

                for (int i = 0; i < purchaseDataList.size(); ++i) {
                    String purchaseData = purchaseDataList.get(i);
                    String signature = signatureList.get(i);
                    String sku = ownedSkus.get(i);

                    // do something with this purchase information
                    // e.g. display the updated list of products owned by user
                    Log.d("Fasfsafasfasfa", "onServiceConnected: " + "purchased items are" + sku + " " + signature + " " + purchaseData);
                }

                // if continuationToken != null, call getPurchases again
                // and pass in the token to retrieve more items
            }


            Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                    "small_pack", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
            PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
            startIntentSenderForResult(pendingIntent.getIntentSender(),
                    1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
                    Integer.valueOf(0));
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    }
};

I have noticed this issue because on very first purchase everything is going fine,and whenever i try to buy same sku named item again,i get the fatal exception like this

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.IntentSender android.app.PendingIntent.getIntentSender()' on a null object reference
                                                 at ink.va.activities.BuyCoins$1.onServiceConnected(BuyCoins.java:103)

Which i can reckon means that i must first consume purchase and only after that again let user buy it..Am i right?Anyway who have a suggestion of this issue please?


Solution

  • Consume your item to make it available to be purchased again:

    int response = mService.consumePurchase(3, getPackageName(), token);
    

    Warning: Do not call the consumePurchase method on the main thread. Calling this method triggers a network request which could block your main thread. Instead, create a separate thread and call the consumePurchase method from inside that thread.

    Consumable products can be purchased multiple times by same the user.

    Non-consumable can be purchased only once.