Making Purchases
When you want to make purchase transaction, you can simply call purchase method of the SDK. SDK will handle the rest of the transaction process regardless device has supported HMS In-App Purchase or GMS In-App Billing. With just giving product type and product id to the purchase method, you can make purchase for the specified product.
important
Calling getProducts method at least one time is mandatory before calling purchase.
important
To test purchase method on a GMS supported device, a test or release app bundle which includes the appmate SDK must be uploaded to Google Play Store.
important
To test purchase method on a HMS supported device, SHA-256 fingerprint must be added to AppGallery Connect (My Projects -> Select Project -> Project Settings -> General Information -> App Information) and agconnect-services.json file in this section should be downloaded to Android module directory. Also In-App Purchases must be enabled over My Projects -> Select Project -> Project Settings -> Manage APIs.
#
RequestParameter Name | Type |
---|---|
activity | Activity |
PurchaseRequest | PurchaseRequest(String productId, ProductType productType) |
listener | PurchaseResultListener<PurchaseResultInfo, GenericError> |
#
ResponseParameter Name | Type |
---|---|
onSucceeded | PurchaseResultInfo |
onFailed | GenericError |
onQueryPurchasesResponse | BillingResult , List<Purchase> |
note
The onQueryPurchasesResponse is called to notify that the query purchase is finished.
- Kotlin
- Java
PurchaseClient.getInstance().purchase( this, new PurchaseRequest("sampleId", ProductType.CONSUMABLE), new PurchaseResultListener<PurchaseResultInfo, GenericError>() { @Override public void onError(GenericError error) { }
@Override public void onSuccess(PurchaseResultInfo data) { }
@Override public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> list) { } })
PurchaseClient.instance.purchase( activity = this, purchaseRequest = PurchaseRequest("sampleId", ProductType.CONSUMABLE), listener = object : PurchaseResultListener<PurchaseResultInfo, GenericError> { override fun onSuccess(data: PurchaseResultInfo) { }
override fun onError(error: GenericError) { }
override onQueryPurchasesResponse(p0:BillingResult, p1:MutableList<Purchase>) { } })
Also add below code to your Activity's onActivityResult() method where you call purchase method, in order to obtain the execution result back to your Activity or Fragment after store checkout screen closed.
note
If you are calling purchase method from one of your fragments, still add below code to Activity where your Fragment attached.
- Kotlin
- Java
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQ_CODE_BUY) { PurchaseClient.getInstance().getResultListener().onActivityResult(data); }}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQ_CODE_BUY) { PurchaseClient.instance.getResultListener().onActivityResult(data) }}
After user completed transaction with the Google Play Store or Huawei App Gallery, PurchaseResultInfo will be returned. Purchase transaction will be synced to your appmate console application automatically.
If an error occurs during the purchase process, error object will be returned with the error code and description to specify the reason.
#
Consume PurchaseOnce purchase is completed, as a part of purchase process you need to deliver a content with granting entitlement as a result of the user's purchase. For one-time consumable product, you need to call consume method of the SDK to indicate that your app has granted entitlement to the user. With consumption of the one-time consumable product, the product can be purchased again.
note
Because of the consume request can be failed, you might want to grant entitlement after consume request succeded to prevent granting entitlement multiple times for the same purchase.
If you try to call purchase method again before consuming purchased one-time consumable product, you will get an AlreadyOwnedButNotConsumed error.
#
RequestParameter Name | Type |
---|---|
purchaseToken | String purchaseToken |
listener | ReceivedDataListener<String, GenericError> |
#
ResponseParameter Name | Type |
---|---|
onSucceeded | String |
onFailed | GenericError |
- Kotlin
- Java
PurchaseClient.getInstance().consumePurchase( "samplePurchaseToken", new ReceivedDataListener<String, GenericError>() { @Override public void onSucceeded(String data) { }
@Override public void onError(GenericError error) { } });
PurchaseClient.instance.consumePurchase(purchaseToken, object : ReceivedDataListener<String, GenericError> { override fun onSucceeded(data: String) { }
override fun onError(error: GenericError) { } })
#
Acknowledge Delivery of The ContentSimilarly to the consumption of one-time consumable product, Google Play wants you to acknowledge non-comsumable and subscription purchases to understand that you have granted
entitlement to user for the purchase. If you do not acknowledge a purchase within three days, the user automatically receives a refund, and Google Play revokes the purchase.
SDK is automatically handle acknowledgement of the non-consumable and subscription purchases. You don't need to do anything in order to fulfil that requirement.