React Native - Making Purchases
Learn how to make and restore mobile purchases using Adapty React Native SDK. Adapty handles server-side validation, including renewals and grace periods
To make the purchase, call adapty.purchases.makePurchase()
method:
try {
const {
receipt,
purchaserInfo,
product
} = await adapty.purchases.makePurchase(<product>, { ios: { offerId: <offerId> } });
}
catch (error: AdaptyError) {}
Request parameters:
product
—AdaptyProduct
retrieved from a paywallofferId
(optional) – a string identifier of promotional offer from App Store Connect. Adapty signs the request according to Apple guidelines, please make sure you've uploaded Subscription Key in Adapty Dashboard when using promotional offers.
Response parameters:
purchaserInfo
— Purchaser info: anAdaptyPurchaserInfo
object. It contains info about access levels, subscriptions, and non-subscription purchases. Generally, you need to check only an access level status to determine whether the user has premium access to the appreceipt
— Receipt: a string representation of a receiptproduct
— Product: anAdaptyProduct
object you've passed to this method
Make sure you've added App Store Shared Secret for iOS and uploaded Service Account Key File for Android in Adapty Dashboard, without them, we can't validate purchases.
Example
Below is a complete example of making the purchase and checking the user's access level.
try {
const { purchaserInfo } = await adapty.purchases.makePurchase(product);
// "premium" is an identifier of default access level
if (purchaserInfo?.accessLevels['premium'].isActive) {
// grant access to premium features
}
} catch (error: AdaptyError) {}
Make sure to set up App Store Server Notifications for iOS and Real-time Developer Notifications (RTDN) for Android to receive subscription updates without significant delays.
Restoring purchases
To restore purchases call adapty.purchases.restore()
method:
try {
const {
purchaserInfo
receipt,
googleValidationResults,
} = await adapty.purchases.restore()
} catch (error: AdaptyError) {}
Response parameters:
purchaserInfo
— Purchaser info: anAdaptyPurchaserInfo
object. It contains info about access levels, subscriptions, and non-subscription purchases. Generally, you have to check only access level status to determine whether the user has premium access to the app.receipt
— String representation of Apple receipts. Might be null.googleValidationResults
— Array of stringed representation of PlayMarket receipts. Might be null.
Redeeming an Offer Code
Since iOS 14.0 your iOS users can redeem Offer Codes. To allow them to do so, you can present the Offer Code redemption sheet by calling the related SDK method.
Purchases performed within this page would be tracked via an info update listener.
adapty.promo.presentCodeRedemptionSheet()
Updated about 1 year ago