Consuming a Purchase
You can use the In-app Billing Version 3 API to track the ownership of purchased in-app products in Google Play. Once an in-app product is purchased, it is considered to be "owned" and cannot be purchased from Google Play. You must send a consumption request for the in-app product before Google Play makes it available for purchase again.
Important: Managed in-app products are consumable, but subscriptions are not.
How you use the consumption mechanism in your app is up to you. Typically, you would implement consumption for in-app products with temporary benefits that users may want to purchase multiple times (for example, in-game currency or equipment). You would typically not want to implement consumption for in-app products that are purchased once and provide a permanent effect (for example, a premium upgrade).
To record a purchase consumption, send the consumePurchase
method to the In-app Billing service and pass in the purchaseToken
String value that identifies the purchase to be removed. The purchaseToken
is part of the data returned in the INAPP_PURCHASE_DATA
String by the Google Play service following a successful purchase request. In this example, you are recording the consumption of a product that is identified with the purchaseToken
in thetoken
variable.
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.
It's your responsibility to control and track how the in-app product is provisioned to the user. For example, if the user purchased in-game currency, you should update the player's inventory with the amount of currency purchased.
Security Recommendation: You must send a consumption request before provisioning the benefit of the consumable in-app purchase to the user. Make sure that you have received a successful consumption response from Google Play before you provision the item.
Implementing Subscriptions
Launching a purchase flow for a subscription is similar to launching the purchase flow for a product, with the exception that the product type must be set to "subs". The purchase result is delivered to your Activity'sonActivityResult
method, exactly as in the case of in-app products.
Bundle bundle = mService.getBuyIntent(3, "com.example.myapp", MY_SKU, "subs", developerPayload); PendingIntent pendingIntent = bundle.getParcelable(RESPONSE_BUY_INTENT); if (bundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_OK) { // Start purchase flow (this brings up the Google Play UI). // Result will be delivered through onActivityResult(). startIntentSenderForResult(pendingIntent, RC_BUY, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0)); }
To query for active subscriptions, use the getPurchases
method, again with the product type parameter set to "subs".
Bundle activeSubs = mService.getPurchases(3, "com.example.myapp", "subs", continueToken);
The call returns a Bundle
with all the active subscriptions owned by the user. Once a subscription expires without renewal, it will no longer appear in the returned Bundle
.