-
Notifications
You must be signed in to change notification settings - Fork 0
Java 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:
public class Configurations {
public static final String RSA_KEY = "YOUR_RSA_PUBLIC_KEY";
public static final String PRODUCT_ID_1 = "sku_p1";
...
}Set up the payment and connection objects in the onCreate of your Activity:
private Payment payment;
private Connection paymentConnection;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
SecurityCheck securityCheck = new SecurityCheck.Enable(Configurations.RSA_KEY);
PaymentConfiguration paymentConfig = new PaymentConfiguration(securityCheck);
payment = new Payment(this, paymentConfig);
paymentConnection = payment.connect(connectionCallback -> {
connectionCallback.connectionSucceed(() -> {
updateStatus("Connected");
return Unit.INSTANCE;
});
connectionCallback.connectionFailed(throwable -> {
updateStatus("Failed: " + throwable.getMessage());
return Unit.INSTANCE;
});
connectionCallback.disconnected(() -> {
updateStatus("Disconnected");
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
}
private void updateStatus(String status) {
Log.d("In-App-Billing-Bazaar", "New Status: " + status);
}Disconnect from the payment service when the activity is destroyed to avoid resource leaks:
@Override
public void onDestroy() {
paymentConnection.disconnect();
super.onDestroy();
}Add logic to initiate a purchase when specific product purchase buttons are clicked:
public void onClickPurchaseProduct1() {
purchase(new PurchaseRequest(Configurations.PRODUCT_ID_1, "SKU_P1", null));
}private void purchase(PurchaseRequest request) {
payment.purchaseProduct(getActivityResultRegistry(), request, purchaseCallback -> {
purchaseCallback.purchaseFlowBegan(() -> {
updateStatus("Purchase Began");
return Unit.INSTANCE;
});
purchaseCallback.failedToBeginFlow(throwable -> {
updateStatus("Failed to Begin: " + throwable.getMessage());
return Unit.INSTANCE;
});
purchaseCallback.purchaseSucceed(purchaseEntity -> {
updateStatus("Purchase Succeed: " + purchaseEntity.getPayload());
consume(purchaseEntity.getPurchaseToken(), purchaseEntity.getPayload());
return Unit.INSTANCE;
});
purchaseCallback.purchaseCanceled(() -> {
updateStatus("Purchase Canceled");
return Unit.INSTANCE;
});
purchaseCallback.purchaseFailed(throwable -> {
updateStatus("Purchase Failed: " + throwable.getMessage());
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
}For consumable products, implement the consume method (if required):
private void consume(String purchaseToken, String payload) {
payment.consumeProduct(purchaseToken, consumeCallback -> {
consumeCallback.consumeSucceed(() -> {
updateStatus(payload + " Consume Succeed");
// Grant the purchased item to the user
return Unit.INSTANCE;
});
consumeCallback.consumeFailed(throwable -> {
updateStatus(payload + " Consume Failed: " + throwable.getMessage());
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
}- 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 Java.
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.