In-App Purchase - iOS Integration
Integrate iOS in-app purchases using the WebToNative JavaScript API. Enable secure subscriptions and one-time purchases in your app.
Functions to sell and restore Apple In-App Purchases from your app. The WebToNative In-App Purchase plugin wraps Apple's StoreKit natively, giving you a single JavaScript API to initiate a purchase, return the App Store receipt to your page, and fetch previously completed transactions for verification.
Initiate a Purchase
Starts the StoreKit purchase flow for the given product ID. On completion, the callback receives the App Store receipt, which you then verify with Apple (see Verify a Transaction).
window.WTN.inAppPurchase({
productId: "Product Id of IAP",
accountToken: "11112222-3333-4444-5555-666677778888",
callback: function (data) {
if (data.isSuccess) {
// Send data.receiptData to your server to verify the transaction.
// refer: https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
console.log(data.receiptData);
}
},
});import { inAppPurchase } from "webtonative/InAppPurchase";
inAppPurchase({
productId: "Product Id of IAP",
accountToken: "11112222-3333-4444-5555-666677778888",
callback: (data) => {
if (data.isSuccess) {
// Send data.receiptData to your server to verify the transaction.
// refer: https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
console.log(data.receiptData);
}
},
});Parameters:
productId
String
Yes
The full product ID of the IAP as configured in App Store Connect.
accountToken
String
No
A UUID that associates the purchase with a user in your system. Surfaced in the verified receipt as app_account_token for server-side reconciliation. See availability note below.
callback
Function
No
Callback function invoked with the response.
Callback Response:
isSuccess
Boolean
true if the purchase completed successfully, false otherwise.
receiptData
String
The base64-encoded App Store receipt. Send this to your server for verification.
Verify a Transaction
There are two ways to fulfill a user's purchase: server-side and on-device. Server-side verification is recommended when purchases are tied to a user account, as it is more secure. When a purchase is made, the receipt is returned to your page through the callback; send it to your server, which verifies it with Apple and then credits or unlocks the purchased item. During this process, associate the purchase with the logged-in user in your system.
For example, your website may have a free membership tier and a premium tier. Display the purchase page only inside the logged-in section of your site. When the purchase completes, the callback fires with receiptData.
Your web server should POST to https://buy.itunes.apple.com/verifyReceipt with the following body:
If exclude-old-transactions is set to true, Apple returns only the latest transaction for auto-renewing subscriptions. Otherwise, you get the entire subscription history.
Apple's server returns HTTP status 200 with a JSON object (see example below). If the JSON object is {"status":21007}, the receipt was generated from the sandbox/test environment — in that case, re-do the POST to https://sandbox.itunes.apple.com/verifyReceipt.
Assuming the response has status 0, verify that the receipt's bundle_id matches your app and check what products were purchased. Save the receipt-data in your database so you can verify successful auto-renewals — it serves as a token you can reuse to get updated subscription information. At this point, your server should provide whatever the user purchased (premium content, virtual currency, etc.).
If status is any value other than 0, or Apple's endpoint does not return HTTP 200, or the request to Apple fails, do not fulfill the purchase. See the App Store receipt status codes for other possible values. Your web server should respond with a JSON object with success set to false. We recommend logging Apple's response — especially the status field — for troubleshooting.
An example response from App Store Connect looks as follows:
Auto-renewable Subscriptions
Apple automatically bills users who have purchased auto-renewable subscriptions. To check the status of a user's subscription, POST the receipt again to Apple's endpoint and inspect the latest_receipt_info field. You may wish to set up a regular job to walk through all active subscriptions.
Apple can also notify you of subscription status changes by posting to an endpoint you configure to handle the change events. Go to App Store Connect → Your App → App Information and enter the URL. See the "Status Update Notifications" section in Apple's In-App Purchase Programming Guide.
Fetch Previous Transactions
Returns the receipt for transactions previously completed on the device. Use this to restore purchases or re-verify an existing subscription.
Parameters:
callback
Function
No
Callback function invoked with the response.
Callback Response:
isSuccess
Boolean
true if a receipt was retrieved successfully, false otherwise.
receiptData
String
The base64-encoded App Store receipt. Send this to your server for verification.
Official References
Last updated