ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - tikhop/TPInAppReceipt: Reading and Validating In App Purchase Receipt Locally.
Reading and Validating In App Purchase Receipt Locally. - tikhop/TPInAppReceipt
Visit Site

GitHub - tikhop/TPInAppReceipt: Reading and Validating In App Purchase Receipt Locally.

GitHub - tikhop/TPInAppReceipt: Reading and Validating In App Purchase Receipt Locally.

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, '12.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 12.0+ / OSX 10.13+
  • Swift 5.9+

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory