Skip to content

Fetch Receipt (encrypted)

Samuel Spencer edited this page Jan 22, 2021 · 1 revision

Use this method to get the updated receipt:

SwiftyStoreKit.fetchReceipt(forceRefresh: true) { result in
    switch result {
    case .success(let receiptData):
        let encryptedReceipt = receiptData.base64EncodedString(options: [])
        print("Fetch receipt success:\n\(encryptedReceipt)")
    case .error(let error):
        print("Fetch receipt failed: \(error)")
    }
}

This method works as follows:

  • If forceRefresh = false, it returns the local receipt from file, or refreshes it if missing.
  • If forceRefresh = true, it always refreshes the receipt regardless.

Notes

  • If the local receipt is missing or forceRefresh = true when calling fetchReceipt, a network call is made to refresh it.
  • If the user is not logged to the App Store, StoreKit will present a popup asking to # to the iTunes Store.
  • If the user enters valid credentials, the receipt will be refreshed.
  • If the user cancels, receipt refresh will fail with a Cannot connect to iTunes Store error.

If fetchReceipt is successful, it will return the encrypted receipt as a string. For this reason, a validation step is needed to get all the receipt fields in readable form. This can be done in various ways:

  1. Validate with Apple via the AppleReceiptValidator (see verifyReceipt below).
  2. Perform local receipt validation (see #101).
  3. Post the receipt data and validate on server.