Handle Payment
Add Callback when purchase
fun setBillingListener(listener: IKNewBillingHandlerListener)
Example
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
ikBilling.setBillingListener(
listener = object : IKNewBillingHandlerListener {
override fun onBillingError(error: IKBillingError) {
}
override fun onBillingInitialized() {
}
override fun onProductPurchased(
productId: String,
orderId: String?,
purchaseToken: String?
) {
isPurchaseNow = true
}
override fun onPurchaseHistoryRestored() {
}
}
)
val checkIAP = isPurchaseNow || ikBilling.isIabServiceAvailable(this)
One-time Purchase Package (Lifetime Purchase)
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.
Example
Multi-purchase Package (Buy multiple times, recharge, etc.)
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.
Example
Subscription
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.
Example
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.
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.