Response: An array of product objects with the following fields:
Field
Type
Description
identifier
string
The product identifier
price
number
The price as a number
currencySymbol
string
The currency symbol, i.e. "
quot; or "SEK"
currencyCode
string
The currency code, i.e. "USD" of "SEK"
priceString
string
Localised string of price, i.e. "$1,234.00"
countryCode
string
Country code of the price, i.e. "GB" or "FR"
downloadable
boolean
Whether the purchase is downloadable
description
string
Description string
title
string
Title string
Troubleshooting: If you do not get back your product(s) then there's a good chance that something in your iTunes Connect or Xcode is not properly configured. Take a look at this StackOverflow Answer to determine what might be the issue(s).
Checking if payments are allowed
InAppUtils.canMakePayments((canMakePayments) => {
if(!canMakePayments) {
Alert.alert('Not Allowed', 'This device is not allowed to make purchases. Please check restrictions on device');
}
})
NOTE: canMakePayments may return false because of country limitation or parental contol/restriction setup on the device.
Buy product
var productIdentifier = 'com.xyz.abc';
InAppUtils.purchaseProduct(productIdentifier, (error, response) => {
// NOTE for v3.0: User can cancel the payment which will be available as error object here.
if(response && response.productIdentifier) {
Alert.alert('Purchase Successful', 'Your Transaction ID is ' + response.transactionIdentifier);
//unlock store here.
}
});
NOTE: Call loadProducts prior to calling purchaseProduct, otherwise this will return invalid_product. If you're calling them right after each other, you will need to call purchaseProduct inside of the loadProducts callback to ensure it has had a chance to complete its call.
NOTE: Call canMakePurchases prior to calling purchaseProduct to ensure that the user is allowed to make a purchase. It is generally a good idea to inform the user that they are not allowed to make purchases from their account and what they can do about it instead of a cryptic error message from iTunes.
To test your in-app purchases, you have to run the app on an actual device. Using the iOS Simulator, they will always fail as the simulator cannot connect to the iTunes Store. However, you can do certain tasks like using loadProducts without the need to run on a real device.
Set up a test account ("Sandbox Tester") in iTunes Connect. See the official documentation here.
import iapReceiptValidator from 'iap-receipt-validator';
const password = 'b212549818ff42ecb65aa45c'; // Shared Secret from iTunes connect
const production = false; // use sandbox or production url for validation
const validateReceipt = iapReceiptValidator(password, production);
async validate(receiptData) {
try {
const validationData = await validateReceipt(receiptData);
// check if Auto-Renewable Subscription is still valid
// validationData['latest_receipt_info'][0].expires_date > today
} catch(err) {
console.log(err.valid, err.error, err.message)
}
}
This works on both react native and backend server, you should setup a cron job that run everyday to check if the receipt is still valid
Free trial period for in-app-purchase
There is nothing to set up related to this library.
Instead, If you want to set up a free trial period for in-app-purchase, you have to set it up at
iTunes Connect > your app > your in-app-purchase > free trial period (say 3-days or any period you can find from the pulldown menu)
The flow we know at this point seems to be (auto-renewal case):
FIRST, user have to 'purchase' no matter the free trial period is set or not.
If the app is configured to have a free trial period, THEN user can use the app in that free trial period without being charged.
When the free trial period is over, Apple's system will start to auto-renew user's purchase, therefore user can continue to use the app, but user will be charged from that point on.