Premium - IAP
Initialization and Enabling IAP Features
This function configures and enables in-app purchase (IAP) features within the application. It resides in the Application class and must be updated whenever there are changes to IAP functionalities, such as enabling or disabling features, or modifying purchase packages.
The function returns an instance of SDKIAPProductIDProvider, which contains the following components:
- enableIAPFunction: A boolean that manages the activation or deactivation of IAP features.
- listProductIDsSubscription: Provides a list of product IDs for subscription products.
- listProductIDsPurchase: Provides a list of product IDs for one-time purchase products.
- listProductIDsRemoveAd: Provides a list of product IDs for products that remove advertisements after purchase.
- listProductIDsCanPurchaseMultiTime: Provides a list of product IDs for products that can be purchased multiple times. To maintain the accuracy of the IAP functionality, it’s essential to update this configuration whenever changes occur.
New update Billing Provider
From SDK version 3.00.490
or higher
Have other interface IKBillingProvider
, it will can config licenseKey
...
override fun licenseKey(): String {
return super.licenseKey()
}
...
Example
override fun configIAPData(): IKBillingProvider {
return object : IKBillingProvider {
override val enableIAPFunction: Boolean
get() = true
override fun licenseKey(): String {
return super.licenseKey()
}
override fun listProductIDsSubscription(): ArrayList<String> {
return arrayListOf()
}
override fun listProductIDsPurchase(): ArrayList<String> {
return arrayListOf()
}
override fun listProductIDsRemoveAd(): ArrayList<String> {
return arrayListOf()
}
override fun listProductIDsCanPurchaseMultiTime(): ArrayList<String> {
return arrayListOf()
}
}
}
Set global callback when purchase
It will receive all Purchase listener when use purchase in every where in your app.
New update Billing Handler Listener
From SDK version 3.00.490
or higher
Have other interface IKBillingDetailsHandlerListener
, it will return more data with PurchaseInfo
interface IKBillingDetailsHandlerListener : IKBillingHandlerListener {
fun onDetailProductPurchased(details: PurchaseInfo?)
}
Best practice
Because onBillingDataSave
delay about 5s, so when you need to check immediately it will not work now. You can pre save an variable cho check realtime.
var isPurchaseNow = false
IKBillingController.setBillingListener(object : IKBillingHandlerListener {
override fun onProductPurchased(productId: String, orderId: String?) {
isPurchaseNow = true
}
override fun onPurchaseHistoryRestored() {
//do some thing
}
override fun onBillingError(error: IKBillingError) {
}
override fun onBillingInitialized() {
//do some thing
}
override fun onBillingDataSave(isPaySuccess: Boolean) {
// It can delay about 5s to verify and save to server
}
})
fun checkIap(): Bolean{
return isPurchaseNow || isIabServiceAvailable()
}
Check for Google Play Service Availability for IAP
This function determines if the Google Play Billing (In-App Billing) service is available on the device. It is essential for ensuring that the device can handle in-app purchases before attempting any IAP operations.
Reference
com.ikame.android.sdk.billing.IKBillingController#isIabServiceAvailable
Function Definition
fun isIabServiceAvailable(context: Context?): Boolean
Sample usage
Retrieve Full Information of a Purchase Package
Inapp Purchase
The getPurchaseDetail
function retrieves detailed information about a specific in-app product from the Google Play Store. This is useful when you need to display product details (such as price, title, and description) to users before they make a purchase.
Reference
com.ikame.android.sdk.billing.IKBillingController#getPurchaseDetail
Function Definition
fun getPurchaseDetail(
productId: String,
callback: IKBillingDetailListener<SdkProductDetails>?
)
Parameters:
productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
: IKBillingDetailListener - Generic interface for handling success and error callbacks in billing processes, adaptable to various data types through generics.
Sample usage
Subscription
This function is used to obtain detailed information about a specific subscription product by its productId. This includes data such as the subscription’s price, title, description, and any other relevant details. This information can be useful for displaying subscription options to users before they make a purchase. Reference
com.ikame.android.sdk.billing.IKBillingController#getSubscriptionDetail
Function Definition
fun getSubscriptionDetail(
productId: String,
callback: IKBillingDetailListener<SdkProductDetails>?
)
Parameters:
productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
: IKBillingDetailListener - Generic interface for handling success and error callbacks in billing processes, adaptable to various data types through generics.
Sample usage
Retrieve Only the Price of a Purchase Package
Inapp Purchase
This function retrieves the price of an in-app product specified by its productId. The price information is useful for displaying to users before they make a purchase decision.
Reference
com.ikame.android.sdk.billing.IKBillingController#getPricePurchase(java.lang.String, com.ikame.android.sdk.listener.pub.IKBillingValueListener)
Function Definition
fun getPricePurchase(
productId: String,
callback: IKBillingValueListener?
)
Parameters:
productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
:IKBillingValueListener
Subscription
This function retrieves the price of an subscription product specified by its productId. The price information is useful for displaying to users before they make a subscription decision.
Reference
com.ikame.android.sdk.billing.IKBillingController#getPriceSubscribe(java.lang.String, com.ikame.android.sdk.listener.pub.IKBillingValueListener)
Function Definition
fun getPriceSubscribe(
productId: String,
callback: IKBillingValueListener?
)
Parameters:
productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
:IKBillingValueListener
Purchasing
One-time Purchase Package (Lifetime Purchase)
Reference
com.ikame.android.sdk.billing.IKBillingController#purchase(android.app.Activity, java.lang.String, com.ikame.android.sdk.listener.pub.IKBillingPurchaseListener)
Function Definition
fun purchase(
activity: Activity?,
productId: String,
listener: IKBillingPurchaseListener? = null
)
Parameters:
activity
: The activity context used to initiate the purchase flow. This is necessary for displaying the purchase dialog to the user.productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
: IKBillingPurchaseListener - Handles callbacks related to the purchase process, including successful purchases, failed transactions, and products already purchased.
Sample usage
Multi-purchase Package (Buy multiple times, recharge, etc.)
Reference
com.ikame.android.sdk.billing.IKBillingController#purchase(android.app.Activity, java.lang.String, com.ikame.android.sdk.listener.pub.IKBillingPurchaseListener, boolean)
Function Definition
fun purchase(
activity: Activity?,
productId: String,
listener: IKBillingPurchaseListener? = null,
hasBuyMultipleTime: Boolean
)
Parameters:
activity
: The activity context used to initiate the purchase flow. This is necessary for displaying the purchase dialog to the user.productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
: IKBillingPurchaseListener - Handles callbacks related to the purchase process, including successful purchases, failed transactions, and products already purchased.hasBuyMultipleTime
: Indicates whether the user is allowed to purchase the same product multiple times.
Sample usage
Subscription
Reference
com.ikame.android.sdk.billing.IKBillingController#subscribe
Function Definition
fun subscribe(
activity: Activity?,
productId: String,
listener: IKBillingPurchaseListener? = null
)
Parameters:
activity
: The activity context used to initiate the purchase flow. This is necessary for displaying the purchase dialog to the user.productId
: String: The ID of the product you want to retrieve details for. This is typically defined in the Google Play Console as part of your in-app products or subscriptions.callback
: IKBillingPurchaseListener - Handles callbacks related to the purchase process, including successful purchases, failed transactions, and products already purchased.
Sample usage
Update an existing subscription with a new package
This function facilitates updating an existing subscription by allowing users to switch from their current subscription (oldProductId
) to a new subscription (productId
). This is particularly useful for handling subscription upgrades or downgrades.
Reference
com.ikame.android.sdk.billing.IKBillingController#updateSubscription
Function Definition
fun updateSubscription(
activity: Activity?,
oldProductId: String,
productId: String,
listener: IKBillingPurchaseListener? = null
)
Parameters:
activity
: The activity context used to initiate the purchase flow. This is necessary for displaying the purchase dialog to the user.oldProductId
: The product ID of the current subscription that the user is upgrading from or downgrading from.productId
: The product ID of the new subscription that the user is switching to.callback
: IKBillingPurchaseListener - Handles callbacks related to the purchase process, including successful purchases, failed transactions, and products already purchased.
Check if the user has previously made a purchase
Used in cases where the user changes phones or has made a purchase before.
Reference
com.ikame.android.sdk.billing.IKBillingController#reCheckIAP
Function Definition
fun reCheckIAP(
listener: IKBillingListener?,
hasDelay: Boolean = false
)
Parameters
-
listener
: IKBillingListener - Used for general billing process callbacks, focusing on the successful or failed completion of billing operations. -
hasDelay
: This optional parameter determines whether the re-check process should be delayed. If set to true, the function will wait a specified time before starting the re-check (useful in some UI or background task scenarios). The default value is false, meaning no delay.
Sample usage
Get Purchase History
In-app Purchase
To retrieve the in-app purchase history, the SDK provides the queryPurchaseHistoryAsync()
method from the IKBillingController
class. This method asynchronously queries the history of past in-app purchases and provides the results through the IKOnQueryHistoryListener
.
Reference
com.ikame.android.sdk.billing.IKBillingController#queryPurchaseHistoryAsync
Function Definition
fun queryPurchaseHistoryAsync(listener: IKOnQueryHistoryListener)
Sample Usage
Subscription
For querying the history of subscription purchases, the SDK provides the querySubHistoryAsync()
method. This method works similarly to in-app purchases but is used specifically for retrieving subscription history.
com.ikame.android.sdk.billing.IKBillingController#querySubHistoryAsync
Function Definition
fun querySubHistoryAsync(listener: IKOnQueryHistoryListener)
Sample Usage
Purchase Check
The SDK provides methods to check whether a user has purchased any subscription or in-app purchase packages, as well as to verify if a specific package has been purchased.
Check if User Has Purchased Any In-app or Subscription Packages
Reference
com.ikame.android.sdk.utils.IKUtils#isUserIAPAvailableAsync
Function Definition
fun isUserIAPAvailableAsync(): Boolean
Sample Usage
Check if User Has Purchased a Specific Package
To check if the user has purchased a specific in-app purchase or subscription package, use the isProductIAPAsync()
method, which takes a product (of type SdkIapPackageDto
) as an argument.
Reference
com.ikame.android.sdk.utils.IKUtils#isProductIAPAsync(com.ikame.android.sdk.data.dto.pub.SdkIapPackageDto, kotlin.coroutines.Continuation<? super java.lang.Boolean>)
Function Definition
fun isProductIAPAsync(product: SdkIapPackageDto): Boolean
Sample Usage
Get All productId purchased
To retrieve a list of all purchased product IDs, you can use the getIapPackage()
function. This method returns an ArrayList
of SdkIapPackageDto
, representing all the purchased products.
Function Definition
suspend fun getIapPackage(): ArrayList<SdkIapPackageDto>
Sample Usage
Class and Listener Interfaces
-
IKBillingPurchaseListener: Handles callbacks related to the purchase process, including successful purchases, failed transactions, and products already purchased.
-
IKBillingDetailListener: Generic interface for handling success and error callbacks in billing processes, adaptable to various data types through generics.
-
IKBillingListener: Used for general billing process callbacks, focusing on the successful or failed completion of billing operations.
-
SdkProductDetails: Encapsulates detailed information about products, managing aspects like price, subscription type, and trial periods.
-
IKOnQueryHistoryListener: Manages callbacks for querying purchase history, addressing successful data retrieval and handling failures during the query process.