TPInAppReceipt 3.4.1

TPInAppReceipt 3.4.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2024
SPMSupports SPM

Maintained by Pavel T.



  • By
  • tikhop

TPInAppReceipt

Swift CocoaPods Compatible Swift Package Manager compatible Platform GitHub license

TPInAppReceipt is a lightweight, pure-Swift library for reading and validating Apple In App Purchase Receipt locally.

Features

  • Read all In-App Receipt Attributes
  • Validate In-App Purchase Receipt (Signature, Bundle Version and Identifier, Hash)
  • Determine Eligibility for Introductory Offer
  • Use with StoreKitTest
  • Use in Objective-C projects

Installation

Note: TPInAppReceipt in Objective-C project - If you want to use TPInAppReceipt in Objective-C project please follow this guide.

CocoaPods

To integrate TPInAppReceipt into your project using CocoaPods, specify it in your Podfile:

platform :ios, '9.0'

target 'YOUR_TARGET' do
  use_frameworks!

  pod 'TPInAppReceipt'
end

Then, run the following command:

$ pod install

In any swift file you'd like to use TPInAppReceipt, import the framework with import TPInAppReceipt.

Swift Package Manager

To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift:

.package(url: "https://github.com/tikhop/TPInAppReceipt.git", .upToNextMajor(from: "3.0.0"))

Then, specify "TPInAppReceipt" as a dependency of the Target in which you wish to use TPInAppReceipt.

Lastly, run the following command:

swift package update

Requirements

  • iOS 10.0+ / OSX 10.11+
  • Swift 5.3+

Usage

Working With a Receipt

The InAppReceipt object encapsulates information about a receipt and the purchases associated with it. To validate In-App Purchase Receipt you must create an InAppReceipt object.

Initializing Receipt

To create InAppReceipt object you can either provide a raw receipt data or initialize a local receipt.

do {
  /// Initialize receipt
  let receipt = try InAppReceipt.localReceipt() 
  // let receipt = try InAppReceipt() // Returns local receipt 
  
  // let receiptData: Data = ...
  // let receipt = try InAppReceipt.receipt(from: receiptData)
  
} catch {
  print(error)
}

Validating Receipt

TPInAppReceipt provides a variety of convenience methods for validating In-App Purchase Receipt:

/// Verify hash 
try? receipt.verifyHash()

/// Verify bundle identifier
try? receipt.verifyBundleIdentifier()

/// Verify bundle version
try? receipt.verifyBundleVersion()

/// Verify signature
try? receipt.verifySignature()

/// Validate all at once 
do {
  try receipt.verify()
} catch IARError.validationFailed(reason: .hashValidation) {
  // Do smth
} catch IARError.validationFailed(reason: .bundleIdentifierVerification) {
  // Do smth
} catch IARError.validationFailed(reason: .signatureValidation) {
  // Do smth
} catch {
  // Do smth
}

NOTE: Apple recommends to perform receipt validation right after your app is launched. For additional security, you may repeat this check periodically while your application is running. NOTE: If validation fails in iOS, try to refresh the receipt first.

Determining Eligibility for Introductory Offer

If your App offers introductory pricing for auto-renewable subscriptions, you will need to dispay the correct price, either the intro or regular price.
The InAppReceipt class provides an interface for determining introductory price eligibility. At the simplest, just provide a Set of product identifiers that belong to the same subscription group:

// Check whether user is eligible for any products within the same subscription group 
var isEligible = receipt.isEligibleForIntroductoryOffer(for: ["com.test.product.bronze", "com.test.product.silver", "com.test.product.gold"])

Note: To determine if a user is eligible for an introductory offer, you must initialize and validate receipt first and only then check for eligibility.

Reading Receipt

/// Initialize receipt
let receipt = try! InAppReceipt.localReceipt() 

/// Base64 Encoded Receipt
let base64Receipt = receipt.base64
  
/// Check whether receipt contains any purchases
let hasPurchases = receipt.hasPurchases

/// All auto renewable `InAppPurchase`s,
let purchases: [InAppPurchase] = receipt.autoRenewablePurchases 

/// all ACTIVE auto renewable `InAppPurchase`s,
let activePurchases: [InAppPurchase] = receipt.activeAutoRenewableSubscriptionPurchases 

Useful methods

// Retrieve Original TransactionIdentifier for Product Name
receipt.originalTransactionIdentifier(ofProductIdentifier: subscriptionName)

// Retrieve Active Auto Renewable Subscription's Purchases for Product Name and Specific Date
receipt.activeAutoRenewableSubscriptionPurchases(ofProductIdentifier: subscriptionName, forDate: Date())

// Retrieve All Purchases for Product Name
receipt.purchases(ofProductIdentifier: subscriptionName)

Refreshing/Requesting Receipt

When necessary, use this method to ensure the receipt you are working with is up-to-date.

InAppReceipt.refresh { (error) in
  if let err = error
  {
    print(err)
  } else {
    initializeReceipt()
  }
}

Essential Reading

License

TPInAppReceipt is released under an MIT license. See LICENSE for more information.