-
Notifications
You must be signed in to change notification settings - Fork 0
Android
-
Purchases
-
Verification
To setup billing and launch billing flow you must implement BillingListener and define all callbacks.
class BillingExample : BillingListener {
override fun onPurchaseConsumed(purchaseToken: String) {
// your code here
}
override fun onPurchaseConsumptionFailed(statusCode: Int, description: String) {
// your code here
}
override fun onPurchaseLoaded(purchases: List<Purchase>) {
// your code here
}
override fun onPurchaseLoadingFailed(statusCode: Int, description: String) {
// your code here
}
override fun onSkuDetailsLoaded(skuDetails: List<SkuDetails>) {
// your code here
}
override fun onSkuDetailsLoadingFailed(statusCode: Int, description: String) {
// your code here
}
override fun onBillingClientError(statusCode: Int, description: String) {
// your code here
}
}You must call initialization function and pass your BillingListenerimplementation and application Context before other SDK calls.
GoogleBillingHelper.init(applicationContext, billingListener)If initialization of the Billing client was failed you'll receive error code and description in onBillingClientError callback.
Before starting purchase flow you must load a list of available SkuDetails. For do that just pass to loadSkuDetails function SkuType and list of items of this type. If SkuDetails will be loaded without any errors onSkuDetailsLoaded(skuDetails) function will be calleed in your implemented BillingListener and provide list of obtained SkuDetails. If any error occured - onSkuDetailsLoadingFailed(statusCode, description) will be called and provide status code of an error and detailed description of the error. SkuDetails is a class that represents an in-app product's or subscription's listing details.
You can call loadSkuDetailsForAllTypes function to obtain purchases for INAPP and SUBS types together and receive result in onSkuDetailsLoaded(skuDetails) function.
SKU details loading example:
// load SkuDetails list with 'in-app' SKU type
GoogleBillingHelper.loadSkuDetails(BillingClient.SkuType.INAPP, inAppSkuDetailsList)
// load SkuDetails list with 'subscribtion' SKU type
GoogleBillingHelper.loadSkuDetails(BillingClient.SkuType.SUBS, subsSkuDetailsList)
// load SkuDetails for INAPP and SUBS types by one call
// the first parameter of skus is sku and the second is SkuType of this sku
GoogleBillingHelper.loadSkuDetailsForAllTypes(skus: Map<String, String>)BillingListener functions for handling result of loadSkuDetails function call:
override fun onSkuDetailsLoaded(skuDetails: List<SkuDetails>) {
// your code here
}
override fun onSkuDetailsLoadingFailed(statusCode: Int, description: String) {
// your code here
}To launch purchase flow you must use GoogleBillingHelper.purchase(activity, skuDetails). For this method it's important to set valid SkuDetails and pass Activity reference.
After calling this function you will be able to get list of Purchase items in onPurchaseLoaded(purchases: List<Purchase>) function or if something went wrong - onPurchaseLoadingFailed(statusCode: Int, description: String) will be called in your implemented BillingListener. Purchase is a class that represents an in-app billing purchase.
Start purchase flow example:
...
// start a purchase flow for item
GoogleBillingHelper.purchase(activity, skuDetails)
...
// handle a purchase flow result
override fun onPurchaseLoaded(purchases: List<Purchase>) {
// your code here
}
override fun onPurchaseLoadingFailed(statusCode: Int, description: String) {
// your code here
}GoogleBillingHelper.restorePurchases(skuType) will call onPurchasesRestored(purchases) in your implemented BillingListener and provide list of the owned purchases as List<Purchase>. If you want to restore purchases for both SkuType together you can call restorePurchasesForAllTypes() function and receive owned purchases for both types. It's important to keep in mind that this method will not return consumed items!
Load purchases example:
...
GoogleBillingHelper.restorePurchases(skuType)
// or load owned purchases for both SkuTypes by one call
GoogleBillingHelper.restorePurchasesForAllTypes()
...
// hadle result
override fun onPurchasesRestored(purchases: List<Purchase>) {
// your code here
}To consume one of your owned purchases you must call GoogleBillingHelper.consume(purchaseItem) and pass valid Purchase. After calling this function you will be able to get purchases token in onPurchaseConsumed(purchaseToken: String) function or if something went wrong - onPurchaseConsumptionFailed(statusCode: Int, description: String) will be called in your implemented BillingListener.
Consume purchase code example:
...
// call consume function
GoogleBillingHelper.consume(purchase)
...
override fun onPurchaseConsumed(purchaseToken: String) {
// your code here
}
override fun onPurchaseConsumptionFailed(statusCode: Int, description: String) {
// your code here
}To validate purchase on your device you can call validatePurchases(appPublicKey, purchases) function. Applicatin public key is a public key associated with the developer account. After calling this function you will be able to get list of valid Purchase items in onPurchaseLoaded(purchases: List<Purchase>). If all of purchases items are invalid you'll receive an empty list. It's important to keep in mind that it's strongly recommended to perform such check on your backend to prevent replacing this method with "constant true" if hackers decompile/rebuild your app.
...
GoogleBillingHelper.validatePurchases(appPublicKey, purchases)
...
//handle result with list of valid purchases
override fun onPurchaseLoaded(purchases: List<Purchase>) {
// your code here
}You can find more info how to set up your SKU items in Google Console here: https://developer.android.com/google/play/billing/billing_overview