Pavel Tikhonenko 38464e48a0 Update Readme
2021-05-27 11:08:33 +03:00
2020-12-11 00:40:18 +03:00
2021-05-26 10:05:28 +03:00
2019-12-11 19:37:42 +03:00
2020-09-21 00:34:22 +03:00
2019-12-10 01:53:23 +03:00
2020-05-06 19:15:55 +03:00
2021-05-24 23:40:39 +03:00
2021-05-27 11:08:33 +03:00
2021-05-26 16:37:53 +03:00

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)
  • 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)
}


Refreshing/Requesting Receipt

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

NOTE: If validation fails in iOS, try to refresh the receipt first.

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

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 

Validating Receipt


/// Verify all at once

do {
try r.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
}

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

/// Verify bundle identifier and version
try? r.verifyBundleIdentifierAndVersion()

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

Determining Eligibility for Introductory Offer


// Initialize receipt
let receipt = try InAppReceipt()

// 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"])


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)

Essential Reading

License

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

Languages
Swift 99.4%
Ruby 0.6%