Giter VIP home page Giter VIP logo

tpinappreceipt's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.