A lightweight library for reading and validating Apple In App Purchase Receipt locally.
- Extract all In-App Receipt Attributes
- Hash Verification
- Verify Bundle Version and Identifiers
- Signature Verification
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
.
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
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.
- iOS 9.0+ / OSX 10.11+
- Swift 5.3+
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.
do {
/// Initialize receipt
let receipt = try InAppReceipt.localReceipt()
// let receiptData: Data = ...
// let receipt = try InAppReceipt.receipt(from: receiptData)
} catch {
print(error)
}
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()
}
}
/// 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
// 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)
/// 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()
- Apple - About Receipt Validation
- Apple - Receipt Validation Programming Guide
- Apple - Validating Receipts Locally
- objc.io - Receipt Validation
TPInAppReceipt is released under an MIT license. See LICENSE for more information.