-
Notifications
You must be signed in to change notification settings - Fork 0
Kotlin Implementation
Add the required dependencies for the Poolakey SDK to your build.gradle file:
dependencies {
implementation "com.github.cafebazaar.Poolakey:poolakey:[latest_version]"
}
You also need to add jitpack as a maven repository to your project:
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
Create a configuration file or define a constant to store your RSA public key and product SKUs. For instance:
object Configurations {
const val RSA_KEY = "YOUR_RSA_PUBLIC_KEY"
const val PRODUCT_ID_1 = "sku_p1"
...
}
Set up the payment and connection objects in the onCreate of your Activity:
private lateinit var payment: Payment
private lateinit var paymentConnection: Connection
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
val securityCheck = SecurityCheck.Enable(rsaPublicKey = Configurations.RSA_KEY)
val paymentConfig = PaymentConfiguration(localSecurityCheck = securityCheck)
payment = Payment(context = this, config = paymentConfig)
paymentConnection = payment.connect {
connectionSucceed { updateStatus("Connected") }
connectionFailed { throwable -> updateStatus("Failed: ${throwable.message}") }
disconnected { updateStatus("Disconnected") }
}
}
fun updateStatus(status: String) {
Log.d("In-App-Billing-Bazaar", "New Status: $status")
}
Disconnect from the payment service when the activity is destroyed to avoid resource leaks:
override fun onDestroy() {
paymentConnection.disconnect()
super.onDestroy()
}
Add logic to initiate a purchase when specific product purchase buttons are clicked:
fun onClickPurchaseProduct1() {
purchase(
request = PurchaseRequest(
productId = Configurations.PRODUCT_ID_1,
payload = "SKU_P1",
)
)
}
private fun purchase(request: PurchaseRequest) {
payment.purchaseProduct(
registry = activityResultRegistry,
request = request,
) {
purchaseFlowBegan { updateStatus("Purchase Began") }
failedToBeginFlow { throwable -> updateStatus("Failed to Begin: ${throwable.message}") }
purchaseSucceed { purchaseEntity ->
updateStatus("Purchase Succeed: ${purchaseEntity.payload}")
consume(purchaseEntity.purchaseToken, purchaseEntity.payload)
}
purchaseCanceled { updateStatus("Purchase Canceled") }
purchaseFailed { throwable -> updateStatus("Purchase Failed: ${throwable.message}") }
}
}
For consumable products, implement the consume function (if required):
private fun consume(purchaseToken: String, payload: String) {
payment.consumeProduct(purchaseToken = purchaseToken) {
consumeSucceed {
updateStatus("$payload Consume Succeed")
// Grant the purchased item to the user
}
consumeFailed { throwable -> updateStatus("$payload Consume Failed: " + throwable.message) }
}
}
- Use consumable products when users are expected to purchase the same item multiple times over the course of app usage.
- Do not use consumable products for items or features that should persist across sessions or devices (e.g., subscriptions or premium content).
You’ve now successfully integrated Cafe Bazaar's In-App Billing SDK into your Android app using Kotlin.
Important: Ensure that the Cafe Bazaar application is installed on your device. The SDK communicates with the Cafe Bazaar app to process purchases, so it’s required for testing and using in-app billing features.