Skip to main content

SDK Reference - buy

React native
Flutter
Swift
Kotlin

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}`);
}
React Native
Flutter
Swift
Kotlin

Parameters

StringProduct SKU
options(Optional)
ObjectOptions
Object
StringProration mode when replacing a subscription (Android only)

Error

Object
StringError message
StringError code
StringError sub code

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:

CodeDescription
user_cancelledThe payment has been cancelled.
No need to display any message
deferred_paymentThe payment has been deferred.
Message to display: Please wait, the payment is pending.

Errors caused by a configuration issue with IAPHUB:

CodeDescription
receipt_failedIAPHUB 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_errorCheck 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_availableThe 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_allowedThe 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:

CodeDescription
transaction_not_foundThe purchase failed.
Message to display: We were not able to process your purchase, please try again later.
network_errorA network error has happened.
Message to display: Network error, please try again later.
billing_unavailableThe Apple/Google billing system isn't available.
Message to display: In-app purchase unavailable on your device, please try again later.
cross_platform_conflictThe 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_purchasedThe 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_renewalThe 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_conflictThe 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_processingA 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

Object
StringIAPHUB product ID
StringProduct type
StringProduct sku
NumberPrice amount
StringPrice currency
StringLocalized price (Ex: '$12.99')
StringProduct title (Ex: 'Membership')
StringProduct description (Ex: 'Join the community with a membership')
Only available if the product belongs to a group
StringIAPHUB group ID
StringName of the product group created on IAPHUB
StringIAPHUB purchase ID
StringPurchase date
platformValues
StringPlatform
BooleanIndicates if the purchase was made in the sandbox environment (testing)
BooleanIndicates if the purchase was made using a promo code
StringPromo code (Android: only available for subscriptions vanity codes, not available for one time codes) (iOS: the value is the offer reference name)
StringAndroid purchase token
StringWebhook status
Only available for a subscription
StringDuration of the subscription cycle specified in the ISO 8601 format
ArrayOrdered list of the subscription intro phases
Array element
typeValues
StringIntro phase type
NumberIntro phase price
StringIntro phase currency code (Ex: 'USD')
StringIntro phase localized price (Ex: '$2.99')
NumberNumber of cycles of the intro phase
StringDuration of a cycle specified in the ISO 8601 format
StringSubscription expiration date
StringSubscription original purchase id
StringIndicates if the subscription will renew automatically
BooleanIndicates if the subscription is shared by a family member (iOS subscriptions only).
StringSubscription product id of the next renewal (only defined if different than the current product)
StringSubscription product sku of the next renewal (only defined if different than the current product)
StringCurrent state of the subscription
StringPeriod type of the subscription
StringDate when the subscription will automatically resume after being paused (Android subscriptions only).