In App Purchase - iOS Integration

You'll need to import the javascript file in your website before starting from this .

If you have not done setup for In App Purchase in your apple account click here to know how to setup IAP in iOS.

To initiate In app purchase in your app call following method from javascript

window.WTN.inAppPurchase({ 
    productId : ‘Product Id of IAP’, 
    callback : function(data){ 
        var receiptData = data.receiptData;
        if(data.isSuccess){
            // use this receipt data to verify transaction from app store 
            // refer : https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
        }
     } 
 })
 
 

Note: If the run was unsuccessful and you didn’t see any products, then there are a number of things to check.

  1. Does the project’s Bundle ID match the App ID from the iOS Development Center?

  2. Is the full product ID being used when calling purchase method.

  3. Is the Paid Applications Contract in effect on iTunes Connect? It can take hours to days for them to go from pending to accepted from them moment you submit them.

  4. Have you waited several hours since adding your product to App Store Connect? Product additions may be active immediately or may take some time.

  5. Check Apple Developer System Status. Alternatively, try this link. If it doesn’t respond with a status value, then the iTunes sandbox may be down. The status codes are explained in Apple’s Validating Receipts With the App Store documentation.

  6. Have IAPs been enabled for the App ID? (Did you select Cleared for Sale earlier?)

  7. Have you tried deleting the app from your device and reinstalling it?

  8. Still stuck? Contact Us

Verify Transaction

There are two methods available to fulfill your user’s purchases: server-side, and on-device. Server-side verification is generally recommended if purchases are to be associated with a user account, as it is more secure. When an in-app purchase is made, the purchase data will be sent to your web server, which will credit or fulfill the purchased item after it has verified the purchase with Apple. During this process, you should associate the purchase with the logged-in user in your system.

For example, your website may have user logins with a free membership tier, and a premium membership tier. In this case, you should only display the purchase page within the logged-in section of your website. When the purchase is made, callback function will be called with receiptData

Your web server needs to create a post to https://buy.itunes.apple.com/verifyReceipt with the contents:

{
    "receipt-data": "xxxxxxxxxxxxxxx",
    "password": "shared secret from iTunes connect",
    "exclude-old-transactions": true
}

If exclude-old-transactions is set to true, Apple will only return the latest transaction for auto-renewing subscriptions. Otherwise, you will get back the entire history of subscriptions.

Apple's server should return 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 the following url: https://sandbox.itunes.apple.com/verifyReceipt.

Assuming the response from Apple’s server has status 0, verify the receipt's bundle_id matches your app, and what products have been purchased. Additionally, save the receipt-data in your database so that you can verify successful auto-renews. The receipt-data serves as a “token” you can use to get updated subscription information. At this point, your server should provide whatever it is the user has purchased (premium content, virtual currency, etc.)

If the status in the JSON is any value other than 0, or Apple’s endpoint does not return an HTTP status 200, or the request to Apple fails, do not fulfill the purchase. See https://developer.apple.com/documentation/appstorereceipts/status for other possible JSON status values. Your web server should respond with a JSON object with success set to false. We recommend logging the response from Apple for troubleshooting purchases, especially the status field.

An example response from App Store Connect will look as follows:

{
    "receipt": {
        "receipt_type": "ProductionSandbox",
        "adam_id": 0,
        "app_item_id": 0,
        "bundle_id": "com.webtonative.com",
        "application_version": "1",
        "download_id": 0,
        "version_external_identifier": 0,
        "receipt_creation_date": "2022-04-27 11:20:28 Etc/GMT",
        "receipt_creation_date_ms": "1651058428000",
        "receipt_creation_date_pst": "2022-04-27 04:20:28 America/Los_Angeles",
        "request_date": "2022-07-07 13:52:28 Etc/GMT",
        "request_date_ms": "1657201948298",
        "request_date_pst": "2022-07-07 06:52:28 America/Los_Angeles",
        "original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
        "original_purchase_date_ms": "1375340400000",
        "original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
        "original_application_version": "1.0",
        "in_app": [
            {
                "quantity": "1",
                "product_id": "com.webtonative.com.extralives",
                "transaction_id": "2000000042206189",
                "original_transaction_id": "2000000042206189",
                "purchase_date": "2022-04-27 11:20:28 Etc/GMT",
                "purchase_date_ms": "1651058428000",
                "purchase_date_pst": "2022-04-27 04:20:28 America/Los_Angeles",
                "original_purchase_date": "2022-04-27 11:20:28 Etc/GMT",
                "original_purchase_date_ms": "1651058428000",
                "original_purchase_date_pst": "2022-04-27 04:20:28 America/Los_Angeles",
                "is_trial_period": "false",
                "in_app_ownership_type": "PURCHASED"
            }
        ]
    },
    "environment": "Sandbox",
    "status": 0
}

Auto-renewable Subscriptions

Apple will automatically bill users who have purchased auto-renewable subscriptions. To check on the status of a user’s subscription, POST the receipt again to Apple’s endpoint and check the latest_receipt_info field. You may wish to set up a regular job to go through all active subscriptions.

Apple can also notify you of subscription status changes by posting to an endpoint you have set up 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 the In-App Purchase Programming guide.

Last updated