Arda Kaplan
Arda Kaplan

Reputation: 1758

Buying same item over and over - In App Purchase Android

i want to buy same item over and over but code gives me null pointer exception when try to buy second time.

Here is the code;

try {
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), "ucret", "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();
    }

When this code script works again pendinIntent coming null.

We can ask to google if user has already purchased like above code

 inventory.hasPurchase(sku_id); 

But I want to sell same item whenever i open the screen.

Thanks for helping.

Upvotes: 0

Views: 148

Answers (1)

Arda Kaplan
Arda Kaplan

Reputation: 1758

Google prevents users to buy same items. So if you do this, you must tell the google "let me". And this is named "consuming".

Here it's all consuming method

private void consumeBilling() {

    try {

        Bundle ownedItems = iInAppBillingService.getPurchases(3, getPackageName(), "inapp", null);

        int response = ownedItems.getInt("RESPONSE_CODE");

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

            for (int i = 0; i < purchaseDataList.size(); ++i) {

                String purchaseData = purchaseDataList.get(i);

                //   String signature = signatureList.get(i);

                //    String sku = ownedSkus.get(i);

                //    RDALogger.info("purchaseData " + purchaseData + " signature " + signature + " sku " + sku);

                RDALogger.info("purchaseData " + purchaseData);

                String purchaseTokenString = new JSONObject(purchaseData).getString("purchaseToken");

                int responsee = iInAppBillingService.consumePurchase(3, getPackageName(), purchaseTokenString);

                RDALogger.info("Consuming response " + responsee);
            }
        }

    } catch (Exception e) {

        e.printStackTrace();

        ErrorHandlers.handle(PaymentActivity.this, new HoustonWeGotAProblemException());
    }
}

PurschaseToken from purchaseData will go to this code

  int responsee = iInAppBillingService.consumePurchase(3, getPackageName(), purchaseTokenString);

after these you told google, this item can be bought again.

Upvotes: 1

Related Questions