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 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 |
---|---|
product | Product |
#
ResponseParameter Name | Type |
---|---|
purchaseInfo | PurchaseInfo? |
error | GenericError? |
- React Native
PurchaseClient.makePurchase(product) .then((purchaseInfo) => { console.log('Purchase successful. Purchase token: ' + purchaseInfo.purchaseToken); })) .catch((error) => { console.log('Failure: ' + error.message); });
After user completed transaction with Google Play Store, Huawei App Gallery or Apple App Store, MakePurchaseResponse 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 |
#
ResponseParameter Name | Type |
---|---|
success | bool |
error | GenericError? |
- React Native
PurchaseClient.consumePurchase(purchaseInfo.purchaseToken) .then((success) => { console.log(success ? 'Consume successful.' : 'Consume error.'); }) .catch((error) => { console.log('Failure: ' + error.message); });
#
UnsubscribeOnce users subscribe to a product, they will want to be able to unsubscribe whenever they want. Developer can open Apple's subscription page to show all subscription of users. So, users can easily unsubscribe from a product. To open subscription page, you need to call unsubscribe() method of the SDK.
#
RequestParameter Name | Type |
---|---|
purchaseToken | String |
#
ResponseParameter Name | Type |
---|---|
success | boolean |
error | GenericError? |
- React Native
PurchaseClient.unsubscribe(purchaseInfo.purchaseToken) .then((success) => { console.log(success ? 'Unsubscribe Success' : 'Unknown error!'); }) .catch((error) => { console.log(error.message ?? 'Unknown error!'); });