SDK Reference - buy
The buy method allows you to trigger the purchase of a product.
It returns the transaction details associated with the product that the user has purchased.
If you want to get alerts on your server when something happens, you can use webhooks. When you do, you can check if the purchase notification has reached your server by looking at the webhookStatus property of the transaction returned by the method. If your server responds with a '200' code, the webhookStatus property should say 'success'.
Please note that if you buy a product from the same subscription group as your current active subscription, it will replace the one you currently have.
Example
try {
var transaction = await Iaphub.buy(sku);
console.log("Transaction successful: ", transaction);
}
catch (err) {
console.log(`Error: Transaction failed, message: ${err.message}, code: ${err.code}, subcode: ${err.subcode}`);
}
Parameters
options
(Optional)prorationMode
ValuesError
If you come across an error, checking the code and subcode properties will assist you in identifying the issue.
Below, you'll find a comprehensive list of potential errors, each accompanied by a description and a suggested message for displaying to the user. Feel free to tailor these messages to suit your needs.
Expected Errors:
Code | Description |
---|---|
user_cancelled | The payment has been cancelled. No need to display any message |
deferred_payment | The payment has been deferred. Message to display: Please wait, the payment is pending. |
Errors caused by a configuration issue with IAPHUB:
Code | Description |
---|---|
receipt_failed | IAPHUB couldn't validate the payment receipt, double check you've configured your Google Play API credentials, App Store shared secret and App Store Server API correctly. Message to display: We're having trouble validating your transaction. Please give us some time; we'll retry validating your transaction as soon as possible |
server_error | Check the error subcode; it can indicate various reasons, but typically arises from incorrect configurations on the IAPHUB dashboard. Message to display: We were not able to process your purchase, please try again later or contact the support. |
product_not_available | The product sku doesn't exist, make sure your product configuration is correct. Message to display: The product is currently not available for purchase. |
anonymous_purchase_not_allowed | The purchase failed because the user isn't logged in. Enable the allowAnonymousPurchase option of the start method to allow anonymous purchases. Message to display: Please log in to your account before making any purchase. |
Errors not caused by you:
Code | Description |
---|---|
transaction_not_found | The purchase failed. Message to display: We were not able to process your purchase, please try again later. |
network_error | A network error has happened. Message to display: Network error, please try again later. |
billing_unavailable | The Apple/Google billing system isn't available. Message to display: In-app purchase unavailable on your device, please try again later. |
cross_platform_conflict | The user already has an active subscription on a different platform. Message to display: It seems like you already have a subscription on a different platform, please use the same platform to change your subscription or wait for your current subscription to expire. |
product_already_purchased | The user already own the product. Message to display: Product already owned, if you do not have access to the product please restore your purchases. |
product_change_next_renewal | The user tried to purchase a product that is already going to be active on next renewal. Message to display: Subscription change already effective, your subscription will be updated on the next renewal date. |
user_conflict | The product is currently owned by a different user id. Message to display: Product owned by a different user, please use the account with which you originally bought the product or restore your purchases. |
buy_processing | A purchase is currently processing. Message to display: Please wait, a purchase is currently processing. |
Other errors:
If you receive the error code 'unexpected' or encounter any other error not listed above, simply display a basic error message:
We were not able to process your purchase. Please try again later or contact support.
The error subcode property might help you figure out what went wrong.
Response
type
Valuesplatform
ValueswebhookStatus
ValuessubscriptionDuration
Valuestype
ValuescycleDuration
ValuessubscriptionState
ValuessubscriptionPeriodType
Values