This is Chargebee’s Android Software Development Kit (SDK). This SDK makes it efficient and comfortable to build a seamless subscription experience in your Android app.
Post-installation, initialization, and authentication with the Chargebee site, this SDK will support the following process.
-
Sync In-App Subscriptions with Chargebee: Integrate Chargebee with Google Play Store to process in-app purchase subscriptions, and track them on your Chargebee account for a single source of truth for subscriptions across the Web and Google Play Store. Use this if you are selling digital goods or services, or are REQUIRED to use Google Play's in-app purchases. For SDK methods to work, ensure that prerequisites are configured in Chargebee.
-
Tokenisation of credit card: Tokenize credit card information while presenting your user interface. Use this if you are selling physical goods or offline services or are NOT REQUIRED to use Google's in-app purchases.
The following requirements must be set up before installing Chargebee’s Android SDK.
- Android 5.0 (API level 21) and above
- Android Gradle Plugin 4.0.0
- Gradle 6.1.1+
- AndroidX
- Java 8+ and Kotlin
The Chargebee-Android
SDK can be installed by adding below dependency to the build.gradle
file:
implementation 'com.chargebee:chargebee-android:1.0.25'
This is an optional step that helps you verify the SDK implementation using this example project. You can download or clone the example project via GitHub.
To run the example project, follow these steps.
-
Clone the repo - https://github.com/chargebee/chargebee-android.
-
Run build.gradle from the Example directory.
There are two types of configuration.
-
Configuration for In-App Purchases
-
Configuration for credit card using tokenization
To configure the Chargebee Android SDK for completing and managing In-App Purchases, follow these steps.
-
Integrate Google Play Store with your Chargebee site.
-
On the Sync Overview page of the web app, click Set up notifications and use the generated App ID value as SDK Key.
-
On the Chargebee site, navigate to Settings > Configure Chargebee > API Keys to create a new Publishable API Key or use an existing Publishable API Key. Note: During the publishable API key creation you must allow read-only access to plans/items otherwise this key will not work in the following step. Read more.
-
Initialize the SDK with your Chargebee site, Publishable API Key, and SDK Key by including the following snippets in your app delegate during app startup.
import com.chargebee.android.Chargebee
Chargebee.configure(site= "your-site",
publishableApiKey= "api_key",
sdkKey= "sdk_key",packageName = "packageName")
To configure SDK only for tokenizing credit card details, follow these steps.
-
Initialize the SDK with your Chargebee Site and Publishable/Full Access Key.
-
Initialize the SDK during your app startup by including this in the Android application class' onCreate method.
import com.chargebee.android.Chargebee
Chargebee.configure(site = "your-site", publishableApiKey = "api_key")
This section describes the SDK integration processes.
-
Integrating In-App Purchases
-
Integrating credit card tokenizationConfigure
The following section describes how to use the SDK to integrate In-App Purchase information. For details on In-App Purchase, read more here.
Every In-App Purchase subscription product that you configure in your Play Store account, can be configured in Chargebee as a Plan. Start by retrieving the Google IAP Product IDs from your Chargebee account.
CBPurchase.retrieveProductIdentifers(queryParam) {
when (it) {
is CBProductIDResult.ProductIds -> {
Log.i(TAG, "List of Product Identifiers: $it")
}
is CBProductIDResult.Error -> {
Log.e(TAG, " ${it.exp.message}")
// Handle error here
}
}
}
For eg. query params above can be "limit": "100".
The above function will determine your product catalog version in Chargebee and hit the relevant APIs automatically, to retrieve the Chargebee Plans that correspond to Google IAP products, along with their Google IAP Product IDs.
Retrieve the Google IAP Product using the following function.
CBPurchase.retrieveProducts(this, productIdList= "[Product ID's from Google Play Console]",
object : CBCallback.ListProductsCallback<ArrayList<Products>> {
override fun onSuccess(productDetails: ArrayList<Products>) {
Log.i(TAG, "List of Products: $productDetails")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
// Handle error here
}
})
You can present any of the above products to your users for them to purchase.
Pass the CBProduct
and CBCustomer
objects to the following function when the user chooses the product to purchase.
CBCustomer
- Optional object. Although this is an optional object, we recommend passing the necessary customer details, such as customerId
, firstName
, lastName
, and email
if it is available before the user subscribes to your App. This ensures that the customer details in your database match the customer details in Chargebee. If the customerId
is not passed in the customer's details, then the value of customerId
will be the same as the SubscriptionId
created in Chargebee.
Note: The customer
parameter in the below code snippet is an instance of CBCustomer
class that contains the details of the customer who wants to subscribe or buy the product.
CBPurchase.purchaseProduct(product=CBProduct, customer=CBCustomer, object : CBCallback.PurchaseCallback<PurchaseModel>{
override fun onSuccess(result: ReceiptDetail, status:Boolean) {
Log.i(TAG, "$status")
Log.i(TAG, "${result.subscription_id}")
Log.i(TAG, "${result.plan_id}")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
}
})
The above function will handle the purchase against Google Play Store and send the IAP token for server-side token verification to your Chargebee account. Use the Subscription ID returned by the above function, to check for Subscription status on Chargebee and confirm the access - granted or denied.
The showManageSubscriptionsSettings()
function is designed to invoke the Manage Subscriptions in your app using Chargebee's Android SDKs. Chargebee.showManageSubscriptionsSettings()
, opens the Play Store App subscriptions settings page.
The purchaseNonSubscriptionProduct
function handles the one-time purchase against Google Play Store and sends the IAP receipt for server-side receipt verification to your Chargebee account. Post verification a Charge corresponding to this one-time purchase will be created in Chargebee. There are two types of one-time purchases consumable
and non_consumable
.
CBPurchase.purchaseNonSubscriptionProduct(product = CBProduct, customer = CBCustomer, productType = OneTimeProductType.CONSUMABLE, callback = object : CBCallback.OneTimePurchaseCallback{
override fun onSuccess(result: NonSubscription, status:Boolean) {
Log.i(TAG, "invoice ID: ${result.invoiceId}")
Log.i(TAG, "charge ID: ${result.chargeId}")
Log.i(TAG, "customer ID: ${result.customerId}")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
}
})
The given code defines a function named purchaseNonSubscriptionProduct
in the CBPurchase class, which takes four input parameters:
product
: An instance ofCBProduct
class, initialized with aSkuDetails
instance representing the product to be purchased from the Google Play Store.customer
: Optional. An instance ofCBCustomer
class, initialized with the customer's details such ascustomerId
,firstName
,lastName
, andemail
.productType
: An enum instance ofproductType
type, indicating the type of product to be purchased. It can be either .consumable
, ornon_consumable
.callback
: TheOneTimePurchaseCallback
listener will be invoked when product purchase completes.
The function is called asynchronously, and it returns a Result
object with a success
or failure
case, which can be handled in the listener.
- If the purchase is successful, the listener will be called with the
success
case, it returnsNonSubscriptionResponse
object. which includes thecustomerId
,chargeId
, andinvoiceId
associated with the purchase. - If there is any failure during the purchase, the listener will be called with the
error
case, it returnsCBException
. which includes an error object that can be used to handle the error.
The restorePurchases()
function helps to recover your app user's previous purchases without making them pay again. Sometimes, your app user may want to restore their previous purchases after switching to a new device or reinstalling your app. You can use the restorePurchases()
function to allow your app user to easily restore their previous purchases.
To retrieve inactive purchases along with the active purchases for your app user, you can call the restorePurchases()
function with the includeInActiveProducts
parameter set to true
. If you only want to restore active subscriptions, set the parameter to false
. Here is an example of how to use the restorePurchases()
function in your code with the includeInActiveProducts
parameter set to true
.
CBCustomer
- Optional object. Although this is an optional object, we recommend passing the necessary customer details, such as customerId
, firstName
, lastName
, and email
if it is available before the user subscribes to your App. This ensures that the customer details in your database match the customer details in Chargebee. If the customerId
is not passed in the customer's details, then the value of customerId
will be the same as the subscriptionId
created in Chargebee. Also, the restored subscriptions will not be associate with existing customerId.
CBPurchase.restorePurchases(context = current activity context, customer = CBCustomer, includeInActivePurchases = false, object : CBCallback.RestorePurchaseCallback{
override fun onSuccess(result: List<CBRestoreSubscription>) {
result.forEach {
Log.i(javaClass.simpleName, "Successfully restored purchases")
}
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
}
})
The restorePurchases()
function returns an array of subscription objects and each object holds three attributes subscription_id
, plan_id
, and store_status
. The value of store_status
can be used to verify the subscription status such as Active
, InTrial
, Cancelled
and Paused
.
In the event of any failures while finding associated subscriptions for the restored items, The SDK will return an error, as mentioned in the following table.
These are the possible error codes and their descriptions:
Error Code | Description |
---|---|
BillingErrorCode.SERVICE_TIMEOUT |
The request has reached the maximum timeout before Google Play responds. |
BillingErrorCode.FEATURE_NOT_SUPPORTED |
The requested feature is not supported by the Play Store on the current device. |
BillingErrorCode.SERVICE_UNAVAILABLE |
The service is currently unavailable. |
BillingErrorCode.DEVELOPER_ERROR |
Error resulting from incorrect usage of the API. |
BillingErrorCode.ERROR |
Fatal error during the API action. |
BillingErrorCode.SERVICE_DISCONNECTED |
The app is not connected to the Play Store service via the Google Play Billing Library. |
BillingErrorCode.UNKNOWN |
Unknown error occurred. |
Receipt validation is crucial to ensure that the purchases made by your users are synced with Chargebee. In rare cases, when a purchase is made at the Google Play Store, and the network connection goes off or the server not responding, the purchase details may not be updated in Chargebee. In such cases, you can use a retry mechanism by following these steps:
- Add a network listener, as shown in the example project.
- Save the product identifier in the cache once the purchase is initiated and clear the cache once the purchase is successful.
- When the network connectivity is lost after the purchase is completed at Google Play Store but not synced with Chargebee, retrieve the product from the cache once the network connection is back and initiate
validateReceipt() / validateReceiptForNonSubscriptions()
by passing activityContext
,CBProduct
andCBCustomer(optional)
as input. This will validate the receipt and sync the purchase in Chargebee as a subscription or one-time purchase. For subscriptions, use the function tovalidateReceipt()
;for one-time purchases, use the functionvalidateReceiptForNonSubscriptions()
.
Use the function available for the retry mechanism.
CBPurchase.validateReceipt(context = current activity context, product = CBProduct, customer = CBCustomer, object : CBCallback.PurchaseCallback<String> {
override fun onSuccess(result: ReceiptDetail, status: Boolean) {
Log.i(TAG, "$status")
Log.i(TAG, "${result.subscription_id}")
Log.i(TAG, "${result.plan_id}")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
}
})
CBPurchase.validateReceiptForNonSubscriptions(context = current activity context, product = CBProduct, customer = CBCustomer, productType = OneTimeProductType.CONSUMABLE, object : CBCallback.OneTimePurchaseCallback {
override fun onSuccess(result: NonSubscription, status: Boolean) {
Log.i(TAG, "invoice ID: ${result.invoiceId}")
Log.i(TAG, "charge ID: ${result.chargeId}")
Log.i(TAG, "customer ID: ${result.customerId}")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
}
})
The following are methods for checking the subscription status of a subscriber who already purchased the product.
Use query parameters - Subscription ID, Customer ID, or Status for checking the Subscription status on Chargebee and confirm the access - granted or denied.
Chargebee.retrieveSubscriptions(queryParam= "[Array of String]") {
when(it){
is ChargebeeResult.Success ->{
Log.i(TAG, "subscription list in array: ${it}")
Log.i(TAG, "subscription status: ${it?.get(0)?.cb_subscription?.status}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
For example, query parameters can be passed as "customer_id" : "id", "subscription_id": "id", or "status": "active".
Use only Subscription ID for checking the Subscription status on Chargebee and confirm the access - granted or denied.
Chargebee.retrieveSubscription(subscriptionId) {
when(it){
is ChargebeeResult.Success ->{
Log.i(TAG, "subscription status: ${it.status}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
Use the Subscription ID for fetching the list of entitlements associated with the subscription.
Chargebee.retrieveEntitlements(subscriptionId) {
when(it){
is ChargebeeResult.Success -> {
Log.i(TAG, "Response: ${(it.data)}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
The following section describes how to use the SDK to directly tokenize credit card information if you are NOT REQUIRED to use Google's in-app purchases.
If you are using Product Catalog 2.0 in your Chargebee site, then you can use the following functions to retrieve the product to be presented for users to purchase.
You can retrieve the list of items by using the following function.
Chargebee.retrieveAllItems(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "list items : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
For example, query parameters can be passed as "sort_by[desc]" : "name" OR "limit": "100".
You can retrieve specific item details by using the following function. Use the Item ID you received from the previous function - Get all items.
Chargebee.retrieveItem(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "item details : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
If you are using Product Catalog 1.0 in your Chargebee site, then you can use any of the following relevant functions to retrieve the product to be presented for users to purchase.
You can retrieve the list of plans by using the following function.
Chargebee.retrieveAllPlans(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "list Plans : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
For example, query parameters can be passed as "sort_by[desc]" : "name" OR "limit": "100".
You can retrieve specific plan details by passing plan ID in the following function.
Chargebee.retrievePlan(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "Plan details : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
You can retrieve specific addon details by passing addon ID in the following function.
Chargebee.retrieve("addonId") { addonResult ->
try {
val addon = addonResult.getData()
Log.d("success", addon.toString())
// Use addon details here
} catch (ex: CBException) {
Log.d("error", ex.getMessage());
// Handle error here
}
}
Once the user selects the product to purchase, and you collect the credit card information, use the following function to tokenize the credit card details against Stripe. You need to have connected your Stripe account to your Chargebee site.
val card = Card(
number = "4321567890123456",
expiryMonth = "12",
expiryYear = "29",
cvc = "123")
val paymentDetail = PaymentDetail(
currencyCode = "USD",
type = PaymentMethodType.CARD,
card = card)
Chargebee.createTempToken(paymentDetail) { tokenResult ->
try {
val cbTempToken = tokenResult.getData()
Log.d("success", cbTempToken)
// Use token here
} catch (ex: PaymentException) {
Log.d("error payment", ex.toString())
// Handle PaymentException here
} catch (ex: InvalidRequestException) {
Log.d("error invalid", ex.toString())
// Handle InvalidRequestException here
} catch (ex: OperationFailedException) {
Log.d("error operation", ex.toString())
// Handle OperationFailedException here
}
}
Once your customer’s card data is processed and stored, and a Chargebee token reference is returned to you, you can use the token in subsequent API calls to process transactions. The following are some endpoints that accept Chargebee tokens for processing.
Please refer to the Chargebee API Docs for subsequent integration steps.
Chargebee is available under the MIT license. See the LICENSE file for more info.
Click here to expand...
This is Chargebee's Android Software Development Kit (SDK). This SDK makes it efficient and comfortable to build a seamless subscription experience in your Android app.
Post-installation, initialization, and authentication with the Chargebee site, this SDK will support the following process.
-
Sync In-App Subscriptions with Chargebee: Integrate Chargebee with Google Play Store to process in-app purchase subscriptions, and track them on your Chargebee account for a single source of truth for subscriptions across the Web and Google Play Store. Use this if you are selling digital goods or services, or are REQUIRED to use Google Play's in-app purchases as per their app review guidelines. For SDK methods to work, ensure that prerequisites are configured in Chargebee.
-
Tokenisation of credit card: Tokenize credit card information while presenting your user interface. Use this if you are selling physical goods or offline services or are NOT REQUIRED to use Google's in-app purchases as per their app review guidelines.
The following requirements must be set up before installing Chargebee's Android SDK.
- Android 5.0 (API level 21) and above
- Android Gradle Plugin 4.0.0
- Gradle 6.1.1+
- AndroidX
- Java 8+ and Kotlin
To install Chargebee's Android SDK, add the following dependency to the build.gradle file.
implementation 'com.chargebee:chargebee-android:1.0.25'
This is an optional step that helps you verify the SDK implementation using this example project. You can download or clone the example project via GitHub.
To run the example project, follow these steps.
- Clone the repo - https://github.com/chargebee/chargebee-android.
- Run build.gradle from the Example directory.
There are two types of configuration.
- Configuration for In-App Purchases
- Configuration for credit card using tokenization
To configure the Chargebee Android SDK for completing and managing In-App Purchases, follow these steps.
- Integrate Google Play Store with your Chargebee site.
- On theSync Overview pageof theweb app, clickSet up notificationsand use the generatedApp IDvalueas SDK Key.
- On the Chargebee site, navigate to Settings > Configure Chargebee > API Keys to create a new Publishable API Key or use an existing Publishable API Key. Note: During the publishable API key creation you must allow read-only access to plans/items otherwise this key will not work in the following step. Read more.
- Initialize the SDK with your Chargebee site, Publishable API Key, and SDK Key by including the following snippets in your app delegate during app startup.
import com.chargebee.android.Chargebee
Chargebee.configure(site= "your-site",
publishableApiKey= "api_key",
sdkKey= "sdk_key",packageName = "packageName")
To configure SDK only for tokenizing credit card details, follow these steps.
- Initialize the SDK with your Chargebee Site and Publishable/Full Access Key.
- Initialize the SDK during your app startup by including this in the Android application class' onCreate method.
import com.chargebee.android.Chargebee
Chargebee.configure(site = "your-site", publishableApiKey = "api_key")
This section describes the SDK integration processes.
- Integrating In-App Purchases
- Integrating credit card tokenizationConfigure
The following section describes how to use the SDK to integrate In-App Purchase information. For details on In-App Purchase, read more here.
Every In-App Purchase subscription product that you configure in your Play Store account, can be configured in Chargebee as a Plan. Start by retrieving the Google IAP Product IDs from your Chargebee account.
CBPurchase.retrieveProductIdentifers(queryParam) {
when (it) {
is CBProductIDResult.ProductIds -> {
Log.i(TAG, "List of Product Identifiers: $it")
}
is CBProductIDResult.Error -> {
Log.e(TAG, " ${it.exp.message}")
// Handle error here
}
}
}
For eg. query params above can be "limit": "100".
The above function will determine your product catalog version in Chargebee and hit the relevant APIs automatically, to retrieve the Chargebee Plans that correspond to Google IAP products, along with their Google IAP Product IDs.
Retrieve the Google IAP Product using the following function.
CBPurchase.retrieveProducts(this, productIdList= "[Product ID's from Google Play Console]",
object : CBCallback.ListProductsCallback<ArrayList<Products>> {
override fun onSuccess(productDetails: ArrayList<Products>) {
Log.i(TAG, "List of Products: $productDetails")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
// Handle error here
}
})
You can present any of the above products to your users for purchase.
Pass the CBProduct
and CBCustomer
objects to the following function when the user chooses the product to purchase.
CBCustomer
- Optional object. Although this is an optional object, we recommend passing the necessary customer details, such as customerId
, firstName
, lastName
, and email
if it is available before the user subscribes to your App. This ensures that the customer details in your database match the customer details in Chargebee. If the customerId
is not passed in the customer's details, then the value of customerId
will be the same as the SubscriptionId
created in Chargebee.
Note: The customer
parameter in the below code snippet is an instance of CBCustomer
class that contains the details of the customer who wants to subscribe or buy the product.
CBPurchase.purchaseProduct(product=CBProduct, customer=CBCustomer, object : CBCallback.PurchaseCallback<PurchaseModel>{
override fun onSuccess(result: ReceiptDetail, status:Boolean) {
Log.i(TAG, "$status")
Log.i(TAG, "${result.subscription_id}")
Log.i(TAG, "${result.plan_id}")
}
override fun onError(error: CBException) {
Log.e(TAG, "Error: ${error.message}")
// Handle error here
}
})
The above function will handle the purchase against Google Play Store and send the IAP token for server-side token verification to your Chargebee account. Use the Subscription ID returned by the above function, to check for Subscription status on Chargebee and confirm the access - granted or denied.
This function also returns the plan ID associated with a subscription. You can associate JSON metadata with the Google Play Store plans in Chargebee and retrieve the same by passing plan ID to the SDK method - retrievePlan(PC 1.0) or retrieveItem(PC 2.0).
The following are methods for checking the subscription status of a subscriber who already purchased the product.
Use query parameters - Subscription ID, Customer ID, or Status for checking the Subscription status on Chargebee and confirm the access - granted or denied.
Chargebee.retrieveSubscriptions(queryParam= "[Array of String]") {
when(it){
is ChargebeeResult.Success ->{
Log.i(TAG, "subscription list in array: ${it}")
Log.i(TAG, "subscription status: ${it?.get(0)?.cb_subscription?.status}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
For example, query parameters can be passed as "customer_id" : "id", "subscription_id": "id", or "status": "active".
Use only Subscription ID for checking the Subscription status on Chargebee and confirm the access - granted or denied.
Chargebee.retrieveSubscription(subscriptionId) {
when(it){
is ChargebeeResult.Success ->{
Log.i(TAG, "subscription status: ${it.status}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
These functions return the plan ID associated with a subscription. You can associate JSON metadata with the Google Play Store plans in Chargebee and retrieve the same by passing plan ID to the SDK method - retrievePlan(PC 1.0) or retrieveItem(PC 2.0).
Use the Subscription ID for fetching the list of entitlements associated with the subscription.
Chargebee.retrieveEntitlements(subscriptionId) {
when(it){
is ChargebeeResult.Success -> {
Log.i(TAG, "Response: ${(it.data)}")
}
is ChargebeeResult.Error ->{
Log.e(TAG, "Error : ${it.exp.message}")
// Handle error here
}
}
}
Note: Entitlements feature is available only if your Chargebee site is on Product Catalog 2.0.
The following section describes how to use the SDK to directly tokenize credit card information if you are NOT REQUIRED to use Google's in-app purchases.
If you are using Product Catalog 2.0 on your Chargebee site, then use the following functions to retrieve the product to be presented for users to purchase.
Retrieve the list of items by using the following function.
Chargebee.retrieveAllItems(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "list items : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
For example, query parameters can be passed as "sort_by[desc]" : "name" or "limit": "100".
Retrieve specific item details by using the following function. Use the Item ID you received from the previous function - Get all items.
Chargebee.retrieveItem(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "item details : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
If you are using Product Catalog 1.0 on your Chargebee site, then use any of the following relevant functions to retrieve the product to be presented for users to purchase.
Retrieve the list of plans by using the following function.
Chargebee.retrieveAllPlans(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "list Plans : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
For example, query parameters can be passed as "sort_by[desc]" : "name" or "limit": "100".
Retrieve specific plan details by passing plan ID in the following function.
Chargebee.retrievePlan(queryParam) {
when (it) {
is ChargebeeResult.Success -> {
Log.i(javaClass.simpleName, "Plan details : ${it.data}")
}
is ChargebeeResult.Error -> {
Log.d(javaClass.simpleName, "Error : ${it.exp.message}")
}
}
}
You can retrieve specific addon details by passing addon ID in the following function.
Chargebee.retrieve("addonId") { addonResult ->
try {
val addon = addonResult.getData()
Log.d("success", addon.toString())
// Use addon details here
} catch (ex: CBException) {
Log.d("error", ex.getMessage());
// Handle error here
}
}
Once the user selects the product to purchase, and you collect the credit card information, use the following function to tokenize the credit card details against Stripe. You need to have connected your Stripe account to your Chargebee site.
val card = Card(
number = "4321567890123456",
expiryMonth = "12",
expiryYear = "29",
cvc = "123")
val paymentDetail = PaymentDetail(
currencyCode = "USD",
type = PaymentMethodType.CARD,
card = card)
Chargebee.createTempToken(paymentDetail) { tokenResult ->
try {
val cbTempToken = tokenResult.getData()
Log.d("success", cbTempToken)
// Use token here
} catch (ex: PaymentException) {
Log.d("error payment", ex.toString())
// Handle PaymentException here
} catch (ex: InvalidRequestException) {
Log.d("error invalid", ex.toString())
// Handle InvalidRequestException here
} catch (ex: OperationFailedException) {
Log.d("error operation", ex.toString())
// Handle OperationFailedException here
}
}
Once your customer's card data is processed and stored and a Chargebee token reference is returned to you, use the token in subsequent API calls to process transactions.
The following are some endpoints that accept Chargebee tokens for processing.
Please refer to the Chargebee API Docs for subsequent integration steps.
Chargebee is available under the MIT license. For more information, see the LICENSE file.