VA Entertaiment VA Entertaiment - 5 months ago 68
Android Question

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() {
public void onServiceDisconnected(ComponentName name) {
mService = null;

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 =
ArrayList<String> purchaseDataList =
ArrayList<String> signatureList =
String continuationToken =

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");
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
} catch (RemoteException e) {
} catch (IntentSender.SendIntentException e) {

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' on a null object reference

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?


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.