Swiftpack.co - Package - tikhop/TPInAppReceipt

TPInAppReceipt

Carthage compatible CocoaPods Compatible Swift Package Manager compatible Platform GitHub license

A lightweight library for reading and validating Apple In App Purchase Receipt locally.

Features

  • ☑ Extract all In-App Receipt Attributes
  • ☑ Hash Verification
  • ☑ Verify Bundle Version and Identifiers
  • ☑ Signature Verification

Installation

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

Carthage

Make the following entry in your Cartfile:

github "tikhop/TPInAppReceipt" 

Then run carthage update.

If this is your first time using Carthage in the project, you'll need to go through some additional steps as explained over at Carthage.

Requirements

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

Usage

Working With a Receipt

InAppReceipt is an object to incapsulate all necessary getters from a receipt payload and provides a comprehensive API for reading and validating in app receipt and related purchases.

Initializing Receipt

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


Refreshing/Requesting Receipt

Use this method to request a new receipt if the receipt is invalid or missing.

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

Reading Receipt

/// Base64 Encoded Receipt
let base64Receipt = receipt.base64
  
/// Initialize receipt
let receipt = try! InAppReceipt.localReceipt() 

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

Validating Receipt


/// Verify all at once

do {
    try r.verify()
} catch IARError.validationFailed(reason: .hashValidation) 
{
    // Do smth
} catch IARError.validationFailed(reason: .bundleIdentifierVefirication) 
{
    // 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()

Essential Reading

License

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

Github

link
Stars: 224

Dependencies

Used By

Total: 0