Consumable In-app Purchases
suggest changeConsumable Managed Products are products that can be bought multiple times such as in-game currency, game lives, power-ups, etc.
In this example, we are going to implement 4 different consumable managed products "item1", "item2", "item3", "item4".
Steps in summary:
- Add the In-app Billing library to your project (AIDL File).
- Add the required permission in
AndroidManifest.xmlfile. - Deploy a signed apk to Google Developers Console.
- Define your products.
- Implement the code.
- Test In-app Billing (optional).
Step 1:
First of all, we will need to add the AIDL file to your project as clearly explained in Google Documentation here.
IInAppBillingService.aidl is an Android Interface Definition Language (AIDL) file that defines the interface to the In-app Billing Version 3 service. You will use this interface to make billing requests by invoking IPC method calls.
Step 2:
After adding the AIDL file, add BILLING permission in AndroidManifest.xml:
<!-- Required permission for implementing In-app Billing -->
<uses-permission android:name="com.android.vending.BILLING" />
Step 3:
Generate a signed apk, and upload it to Google Developers Console. This is required so that we can start defining our in-app products there.
Step 4:
Define all your products with different productID, and set a price to each one of them. There are 2 types of products (Managed Products and Subscriptions). As we already said, we are going to implement 4 different consumable managed products "item1", "item2", "item3", "item4".
Step 5:
After doing all the steps above, you are now ready to start implementing the code itself in your own activity.
MainActivity:
public class MainActivity extends Activity {
IInAppBillingService inAppBillingService;
ServiceConnection serviceConnection;
boolean billingBound;
// productID for each item. You should define them in the Google Developers Console.
final String item1 = "item1";
final String item2 = "item2";
final String item3 = "item3";
final String item4 = "item4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the views according to your layout file.
final Button buy1 = (Button) findViewById(R.id.buy1);
final Button buy2 = (Button) findViewById(R.id.buy2);
final Button buy3 = (Button) findViewById(R.id.buy3);
final Button buy4 = (Button) findViewById(R.id.buy4);
// setOnClickListener() for each button.
// buyItem() here is the method that we will implement to launch the PurchaseFlow.
buy1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buyItem(item1);
}
});
buy2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buyItem(item2);
}
});
buy3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buyItem(item3);
}
});
buy4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buyItem(item4);
}
});
// Attach the service connection.
serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
inAppBillingService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
inAppBillingService = IInAppBillingService.Stub.asInterface(service);
querySkuPrices(buy1, buy2, buy3, buy4);
}
};
// Bind the service. onServiceConnected runs later, so the service
// is still null if you query it on the next line.
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
billingBound = bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE);
}
// getSkuDetails() is a network call. Update the buttons on the UI thread.
private void querySkuPrices(final Button buy1, final Button buy2, final Button buy3, final Button buy4) {
new Thread(new Runnable() {
@Override
public void run() {
ArrayList<String> skuList = new ArrayList<>();
skuList.add(item1);
skuList.add(item2);
skuList.add(item3);
skuList.add(item4);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
Bundle skuDetails = inAppBillingService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response != 0) {
return;
}
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
if (responseList == null) {
return;
}
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
final String sku = object.getString("productId");
final String price = object.getString("price");
runOnUiThread(new Runnable() {
@Override
public void run() {
if (item1.equals(sku)) {
buy1.setText(price);
} else if (item2.equals(sku)) {
buy2.setText(price);
} else if (item3.equals(sku)) {
buy3.setText(price);
} else if (item4.equals(sku)) {
buy4.setText(price);
}
}
});
}
} catch (RemoteException | JSONException e) {
e.printStackTrace();
}
}
}).start();
}
// Launch the PurchaseFlow passing the productID of the item the user wants to buy as a parameter.
private void buyItem(String productID) {
if (inAppBillingService != null) {
try {
Bundle buyIntentBundle = inAppBillingService.getBuyIntent(3, getPackageName(), productID, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
if (buyIntentBundle.getInt("RESPONSE_CODE") != 0) {
return;
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
if (pendingIntent == null) {
return;
}
startIntentSenderForResult(pendingIntent.getIntentSender(), 1003, new Intent(), 0, 0, 0);
} catch (RemoteException | IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
// Unbind even when onServiceConnected has not run yet. bindService
// already holds the connection. Unbinding only when the binder is
// non-null leaks that connection if the activity is destroyed first.
@Override
public void onDestroy() {
super.onDestroy();
if (billingBound) {
unbindService(serviceConnection);
billingBound = false;
}
}
// Check here if the in-app purchase was successful or not. If it was successful,
// then consume the product, and let the app make the required changes.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1003 && resultCode == RESULT_OK) {
final String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
// Attention: You need to create a new thread here because
// consumePurchase() triggers a network request, which can
// cause lag to your app if it was called from the main thread.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
JSONObject jo = new JSONObject(purchaseData);
// Get the productID of the purchased item.
final String sku = jo.getString("productId");
final String productName;
// increaseCoins() stands in for granting the purchase on the UI thread.
switch (sku) {
case item1:
productName = "Item 1";
break;
case item2:
productName = "Item 2";
break;
case item3:
productName = "Item 3";
break;
case item4:
productName = "Item 4";
break;
default:
productName = null;
break;
}
// Consume first. Grant the item only when Play returns BILLING_RESPONSE_RESULT_OK (0).
// A grant before a failed consume can be awarded twice, or block the next purchase.
final int consumeResult = inAppBillingService.consumePurchase(3, getPackageName(), jo.getString("purchaseToken"));
final String purchasedName = productName;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (consumeResult != 0 || purchasedName == null) {
Toast.makeText(MainActivity.this, "Failed to consume the purchase.", Toast.LENGTH_LONG).show();
return;
}
switch (sku) {
case item1:
increaseCoins(2000);
break;
case item2:
increaseCoins(8000);
break;
case item3:
increaseCoins(18000);
break;
case item4:
increaseCoins(30000);
break;
}
Toast.makeText(MainActivity.this, purchasedName + " is successfully purchased. Excellent choice, master!", Toast.LENGTH_LONG).show();
}
});
} catch (JSONException | RemoteException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Failed to parse purchase data.", Toast.LENGTH_LONG).show();
}
});
}
}
});
thread.start();
}
}
}
Step 6:
Upload the APK to an internal, alpha, or beta track and install it from that track. License-tester accounts can buy these products without a production release, and Play does not charge them. A draft APK that was never uploaded cannot complete a real SKU purchase.
More info on testing In-app Billing can be found here.