0

I am trying to implement In App Purchase Receipt validation in an iOS app. I followed this tutorial and use the sample project provided as a base to work on my own app.

Here is an issue I am facing at this point. In a function called validateReceipt():

  func validateReceipt() {
    ......

    receipt = Receipt()

    if let receiptStatus = receipt?.receiptStatus { // Critical point.
        ...... // A lot of important things happening here.
    }
    ......
  }

Following what is going on in the debugger, I see that when reaching the line commented: // Critical point. I have, receipt not nil but:

receipt?.receiptStatus == nil

What could be the reason for this? I supposed I did something wrong on the way, but don't know what.

To make things clearer, here follows the definition of the Receipt class, taken from the tutorial:

/// Copyright (c) 2018 Razeware LLC
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
/// distribute, sublicense, create a derivative work, and/or sell copies of the
/// Software in any work that is designed, intended, or marketed for pedagogical or
/// instructional purposes related to programming, coding, application development,
/// or information technology.  Permission for such use, copying, modification,
/// merger, publication, distribution, sublicensing, creation of derivative works,
/// or sale is expressly withheld.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.

import UIKit

enum ReceiptStatus: String {
  case validationSuccess = "This receipt is valid."
  case noReceiptPresent = "A receipt was not found on this device."
  case unknownFailure = "An unexpected failure occurred during verification."
  case unknownReceiptFormat = "The receipt is not in PKCS7 format."
  case invalidPKCS7Signature = "Invalid PKCS7 Signature."
  case invalidPKCS7Type = "Invalid PKCS7 Type."
  case invalidAppleRootCertificate = "Public Apple root certificate not found."
  case failedAppleSignature = "Receipt not signed by Apple."
  case unexpectedASN1Type = "Unexpected ASN1 Type."
  case missingComponent = "Expected component was not found."
  case invalidBundleIdentifier = "Receipt bundle identifier does not match application bundle identifier."
  case invalidVersionIdentifier = "Receipt version identifier does not match application version."
  case invalidHash = "Receipt failed hash check."
  case invalidExpired = "Receipt has expired."
}

class Receipt {
  var receiptStatus: ReceiptStatus?
  var bundleIdString: String?
  var bundleVersionString: String?
  var bundleIdData: Data?
  var hashData: Data?
  var opaqueData: Data?
  var expirationDate: Date?
  var receiptCreationDate: Date?
  var originalAppVersion: String?
  var inAppReceipts: [IAPReceipt] = []

  static public func isReceiptPresent() -> Bool {
    if let receiptUrl = Bundle.main.appStoreReceiptURL,
      let canReach = try? receiptUrl.checkResourceIsReachable(),
      canReach {
        return true
    }

    return false
  }
}

In fact checking the state of the receipt variable (when it exists) in the debugger I can see the following:

(lldb) p receipt
(MyApp).Receipt?) $R0 = 0x0000000281516ac0 {
  receiptStatus = nil
  bundleIdString = nil
  bundleVersionString = nil
  bundleIdData = nil
  hashData = nil
  opaqueData = nil
  expirationDate = nil
  receiptCreationDate = nil
  originalAppVersion = nil
  inAppReceipts = 0 values {}
}

In other words it shows up filled up with nil(s).

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

0

Receipt() isn't a native Swift class so hard to tell from that code what's going on. A few common problems with testing receipts in Sandbox are:

  1. Need to be on a physical device
  2. Need to have already made a purchase after installing in Sandbox to have a receipt. In production, there will be a receipt on the device after installation, in Sandbox, a receipt isn't created until a purchase is made.
enc_life
  • 4,973
  • 1
  • 15
  • 27
  • Yes, I am testing on a physical device. And I have made a purchase using Sandbox. To make things more explicit I updated the post to include the code for the Receipt class. – Michel Mar 29 '19 at 02:45