Swiftpack.co -  tikhop/TPInAppReceipt as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
tikhop/TPInAppReceipt
Reading and Validating In App Purchase Receipt Locally.
.package(url: "https://github.com/tikhop/TPInAppReceipt.git", from: "Validation-1.2.3")

TPInAppReceipt

Swift 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
  • ☑ Bundle Version and Identifiers Verification
  • ☑ Signature Verification
  • ☑ Works with StoreKitTest

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

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: .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()

Essential Reading

License

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

GitHub

link
Stars: 338
Last commit: 6 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

3.1.0
6 weeks ago
  • Use Date type instead of String for all variables containing dates
  • Parse app's age rating

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API