iosswiftstorekitreceipt-validation

Receipt-Validation code for iOS app, not working


I have implemented Receipt-Validation code for the first time in an iOS app, following the Raywenderlich tutorial

Even though it was working fine during the test, using Sandbox. In reality, it does not work as expected.

When the app is downloaded, all features are available from start; meaning there is no check on the purchase of the iap contents.

In the case of Sandbox I needed to make a purchase to get the receipt and "making a purchase" meant "getting the content of the iap".

And as far as I know, in the real world the receipt comes with the app. My interpretation is that I am probably not checking what I should in the code.

Any guidance by an expert in the field would be very much appreciated.

Looking at the code, it seems that there is (indeed) not much check, other than making sure that their is a valid (well formed) receipt. But I guess I need more to check that the very contents of the IAP had been paid for.

Below is what I think is the relevant code:

func validateReceipt() {

    receipt = Receipt()

    if let receiptStatus = receipt?.receiptStatus {
        guard receiptStatus == .validationSuccess else {
            return
        }

        // If verification succeed, we show information contained in the receipt.
        // print("Bundle Identifier: \(receipt!.bundleIdString!)")
        // print("Bundle Version: \(receipt!.bundleVersionString!)")

        if let originalVersion = receipt?.originalAppVersion {
            //print("originalVersion: \(originalVersion)")
        } else {
            //print("originalVersion: Not Provided")
        }

        if let receiptExpirationDate = receipt?.expirationDate {
            //print("Expiration Date: \(formatDateForUI(receiptExpirationDate))")
        } else {
            //print("Expiration Date: Not Provided")
        }

        if let receiptCreation = receipt?.receiptCreationDate {
            //print("receiptCreation: \(formatDateForUI(receiptCreation))")
        } else {
            //print("receiptCreation: Not Provided")
        }

        // At this point we should enable full features if it is not yet the case.
        .... code to unlock full features .....
    }
}

Solution

  • You should check fields with type 17xx where you can find product identifier (field type 1702) and purchase date (field type 1704). This is described at Raywenderlich tutorial you mentioned in part Reading In-App Purchases.

    So, you can check if user purchased IAP with product identifier you interested in. Product identifier is the same as you choose at App Store Connect at MyApps -> Feature -> IAP.