- Notifications
You must be signed in to change notification settings - Fork5
russell-archer/IAPDemo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Implementing and testing In-App Purchases with StoreKit1 in Xcode 12 and iOS 14.
See alsoStoreHelper for details on implementing and testing in-app purchases withStoreKit2
andStoreHelper
in Xcode 13, Swift 5.5, iOS 15.
Disclaimer. The source code presented here is for educational purposes. You may freely reuse and amend this code for use in your own apps.However, you do so entirely at your own risk.
See onIAPHelper on GitHub forIAPHelper
source.
Added notes onSupport for Strong Customer Authentication transactions in the European Economic Areawith reference to new aApple Support Document.
SeeIAPDemo onGitHub for source code.TheHelloIAPWorld source isalso available on GitHub.
References:
- In-App Purchase Overview (Apple)
- Receipt Validation Programming Guide (Apple archive but still useful)
- In-App Purchase (Apple)
- Choosing a Receipt Validation Technique (Apple)
- Validating Receipts with the App Store (Apple)
- In-App Purchases: Receipt Validation Tutorial (Ray Wenderlich)
- Local Receipt Validation for iOS in Swift From Start to Finish (Andrew Bancroft)
- Swifty Local Receipt Validator (Andrew Bancroft)
- Receipt Validation – Verifying a Receipt Signature in Swift (Andrew Bancroft)
- Receipt Validation (Laurent Etiemble, Objc)
- Overview
- Receipt validation options
- Sandbox accounts
- Basic Steps
- HelloIAPWorld Example
- How to Validate Receipts Locally
- IAPDemo Example
- StoreKit Automated Testing
- Support for Strong Customer Authentication transactions in the European Economic Area
- Future Enhancements
The code we write to manage in-app purchases is critically important to the success of our apps. However, if you’ve not tackled it before, implementing andtesting in-app purchases is daunting, complex and seemsway more involved than you’d expect!
Anybody wanting to support in-app purchases faces a similar set of challenges:
- How do you define the set of products that can be purchased in your app?
- Defining your in-app purchases in App Store Connect
- Working with
StoreKit
to requestlocalized product data from the App Store and initiate purchases - Implementing
StoreKit
delegate methods to process async notifications for purchase success, failure, restoring purchases, etc. - Handling edge-cases, like when a purchase is deferred because it requires parental permissions, or when entitlements for a user have changed and access to the specified IAPs has been revoked
- Should you handle App Store receipt validation on-device or server-side?
- Should you write your own receipt validation code or use a service likeRevenueCat?
- Working withOpenSSL and the arcanePKCS #7 andASN.1 data structures found in receipts
- Writing code to validate the receipt and read in-app purchase data
- Creating and managing sandbox accounts used for testing
When I first implemented in-app purchases in one of my iOS apps in 2016 the two main pain-points were:
The App Store issues an encrypted receipt when in-app purchases are made or restored (when an app’s first installed, no receipt is present).This receipt contains a complete list of all in-app purchases made in the app.
There are four receipt validation approaches available:
- Server-side receipt validation
- On-device receipt validation
- Third-party receipt validation service
- No receipt validation
This is probably the easiest option, but you need an app server to send requests to the App Store server. Apple specifically says youshould not create directconnections to the App Store server from your app because you can’t guard against man-in-the-middle attacks.
Despite this clear warning, the web has many examples (including commercial offerings) of using direct app-to-App Store connections. The advantage of usingserver-side validation is that you can retrieve easily decoded JSON payloads that include all the in-app purchase data you need. We don’t cover server-sidevalidation in this example.
On-device validation is somewhat tricky and requires use of the C-basedOpenSSL library to decrypt and read the receipt data.Note that including the required two OpenSSL libraries adds nearly 50MB to your app.
I first started supporting in-app purchases in 2016. I fully expectedStoreKit or some other Apple framework to provide ready-to-use abstractions allowingfor easy access to the low-level cryptographic data structures in the receipt. However, as I looked deeper into the “where’s the receipt processing framework?”conundrum the more the answer became clear: having a ready-to-use framework creates a security risk because “hackers” wishing to access your in-apppurchases for-free know in advance where and how to concentrate their attacks. Apple’s answer was (and still is): create your own custom receipt validationsolution because a unique solution will be harder to hack.
Clearly a custom solution (if done correctly) will be more secure. But, as all developers know that have attempted it, writing security-critical cryptographic-relatedcode ishard and if you get it wrong disasters will happen! In my opinion, surely it would be better for Apple to provide something that enables correct andreasonably secure receipt validation for the general app developer?
However, at present (November 2020) you have no choice if you want to validate and read receipt data on-device: you must develop your own OpenSSL-basedsolution. If you don’t feel confident doing this feel free to adapt (or use as-is) the code presented herein.
A number of third parties provide receipt validation services, normally as part of a larger in-app purchase, subscription and analytics service. I've not used anyof them in my apps so can't comment on their suitability. However,RevenueCat seems like a good option judging by theirdocumentation and sample code.
It’s perfectly possible to do no receipt validation at all, if you think that’s appropriate for your app’s business model. All you need to do is handle transactionsfrom the App Store using the following method:
paymentQueue(_:updatedTransactions:)
When you get a.purchased
or.restored
transaction simply add the product identifier for the product to a list of purchased products that your app maintains.The list should be persisted in a database, or evenUserDefaults
. Clearly, this is a far less secure approach than doing receipt validation. However, you maydecide that a particular app doesn’t warrant the greater protection and associated complexity provided by receipt validation. See theHelloIAPWorld example below for a discussion of this approach.
Prior to Xcode 12, in order to test in-app purchases you needed to create multiple sandbox test accounts in App Store Connect. Each sandbox account has to havea unique email address and be validated as an AppleID. In addition, tests must be on a real device, not the simulator.
On the test device you need to sign out of your normal AppleID and sign-in using the sandbox account. This really means you need a spare device to do testing on.To make things more painful, each time you make a purchase using a sandbox account that account becomes “used up” and can’t be used to re-purchase thesame product. There’s no way to clear purchases, so you need to use a fresh sandbox account for each set of product purchases.
There are a lot of pieces that fit together to enable you to support in-app purchases in your app:
The basic steps you need to take to support in-app purchases (IAP hereafter) in your app are as follows:
Create a class or struct that will contain all your IAP-related code. For the sake of example we’ll refer to this as theIAPHelper
code.
Define a set of Strings that holdProductIds for the products you want to sell. ProductIds are generally in reverse domain form (“com.your-company.your-product”).For example,com.rarcher.flowers-large
. These ids will match the product ids you define in App Store Connect.
To receive notifications from the App Store (when payments are successful, fail, are restored, etc.) add your IAPHelper to the StoreKit payment queue.This should be done as soon as possible in the app’s lifecycle.
For example inapplication(_:didFinishLaunchingWithOptions:)
, so that notifications from the App Store are not missed:
SKPaymentQueue.default().add(iapHelper)
TheSKProductsRequestDelegate
methodproductsRequest(_:didReceive:)
will be called asynchronously with a list ofSKProduct
objects.Note that you can’t simply use predefined product data because you need to display prices, etc. that arelocalized for each user.
When the user taps on “buy product” you should wrap the selectedSKProduct
in anSKPayment
object, then add it to theSKPaymentQueue
.The App Store will then send notifications to theSKPaymentTransactionObserver
methodpaymentQueue(_:updatedTransactions)
as thepurchase progresses. Note that the App Store presents the user with all the required purchase prompts and confirmations.
The App Store will create a newreceipt when a purchase has been made or restored. The receipt is available when thepaymentQueue(_:updatedTransactions)
method is called. This receipt, which is cryptographically signed and encrypted, contains a complete record of all the IAPs made by the user of your app.
The code discussed in theHelloIAPWorld example below provides a practical example of the above points (receipt validation is covered later).
Immediately before Apple’s WWDC 2020 keynote event I tweeted that I was hoping for something “magical and unexpected”. I followed this up with“How about an update to StoreKit that makes it really easy to do on-device validation of App Store receipts”.
Well, I didn’t get my wish with regard to receipt validation, but I certainly got something magical and unexpected related to StoreKit and in-app purchases!
Starting with Xcode 12, there’s a new localStoreKit
test environment that allows you to do early testing of IAPs in the simulator and without having to setanything up in App Store Connect. You can define your products locally in aStoreKit
Configuration file. Furthermore, you can view and delete transactions,issue refunds, and a whole lot more. There’s also a newStoreKitTest
framework that enables you to do automated testing of IAPs.TheHelloIAPWorld project below includes details on how to create and use a StoreKit configuration file.
These new features are a huge leap forward in terms of making testing substantially easier, quicker to setup, more flexible and less frustrating!
The following example shows how to create avery minimal IAP example (the IAP equivalent of “Hello World”) that makes use of the new StoreKit testingfeatures in Xcode 12.
Note that StoreKit testing requiresXcode 12 andiOS 14.
Although this is a bare-bones example, the project does demonstrate most of the essential requirements for handling in-app purchases in an iOS app.
You can find the code forHelloIAPWorldon GitHub.
Note that this example project is missing some features a real-world app would be expected to support:
- The App Store receipt is not validated, nor is IAP data read from the receipt
- Purchases are not persisted. So, if the app’s closed and restarted no purchases are remembered
- There’s no way to restore previous purchases
- Deferred purchases aren’t supported
- Edge cases are not supported (refunds, entitlements being revoked, store front changes, purchasing IAPs directly from the app store, etc.)
For this example we’ll assume you’re going to create a demo app from scratch using iOS 14 and Xcode 12. Here are the steps we'll go through:
- Create the App
- Add the StoreKit Framework
- Create the StoreKit configuration file
- Add the in-app purchase capability
- Enable StoreKit Testing via the Project Scheme
- Add the StoreKit public certificate
- Minimal IAPHelper Code
- Running the app
Create a new iOS app in Xcode named "HelloIAPWorld":
As we'll be creating the UI programmatically we need to remove the Storyboard.
Remove the name (“Main”) of the storyboard inTargets > General > Deployment Info:
DeleteMain.storyboard and move it to the trash:
OpenInfo.plist, search forMain and then remove theStoryboard Name entry entirely:
OpenSceneDelegate.swift and modify thescene(_:willConnectTo:options:)
method as follows:
classSceneDelegate:UIResponder,UIWindowSceneDelegate{varwindow:UIWindow?func scene( _ scene:UIScene, willConnectTo session:UISceneSession, options connectionOptions:UIScene.ConnectionOptions){ // Use this method to optionally configure and attach the UIWindow `window` to the // provided UIWindowScene `scene`. If using a storyboard, the `window` property will // automatically be initialized and attached to the scene. // Get the window sceneguardlet windowScene=(sceneas?UIWindowScene)else{return} // Create a window window=UIWindow(frame: windowScene.coordinateSpace.bounds)iflet w= window{ // Assign the window scene to the window's window scene w.windowScene= windowScene // Set the root view controller (to the default view controller) w.rootViewController=ViewController() // Make the window visible w.makeKeyAndVisible()}}::}
The first thing you need to do after creating your new app is to add theStoreKit framework. Select your appTarget and theGeneral tab, thenadd theStoreKit
framework:
Now create a StoreKit configuration file. SelectFile > New > File and choose theStoreKit Configuration File template:
Choose a location in your project to save the file.
Open the StoreKit configuration file and click+ to add an in-app purchase. For this example select theAdd Non-Consumable in-App Purchase option:
You can now define your products in the StoreKit configuration file:
In this example I set the following fields:
Reference Name
A descriptive name for the productProduct ID
This the unique code used to identify an IAP product. This same ID will be used in App Store Connect when setting up in-app purchases for production.Note that Product ID is a string that, by convention, uses the format “com.developer.product”, although it can be anything you likePrice
A hard-coded price for the product. In production your app will request localized price (and other) information from the App Store
By default, the first localization is for the US store. However, you can add as many localizations as required.
Note that none of the data defined in the .storekit file is ever uploaded to App Store Connect. It’s only used when testing in-app purchases locally in Xcode.
It’s easy to forget to do this! And you can successfully test in-app purchaseswithout adding the IAP capability. However, you will receive the followingerror when attempting to archive a project in preparation for uploading it to the App Store:
Add the in-app purchase capability by selecting the app target andSigning & Capabilities, then click+Capability to add a capability:
You now need to enable StoreKit testing in Xcode (it’s disabled by default).
SelectProduct > Scheme > Edit Scheme. Now selectRun and theOptions tab. You can now select your configuration file fromtheStoreKit Configuration list:
Should you wish to disable StoreKit testing then repeat the above steps and remove the StoreKit configuration file from theStoreKit Configuration list.
You need to add the StoreKit public test certificate to your project. This isn’t strictly necessary if you’re not going to be doing any receipt validation.However, we’ll include the details here for completeness.
StoreKit testing in Xcode generateslocally signed receipts that your app must validate locally against the StoreKit test certificate. In production yourapp will include theApple Root Certificate and use that when validating the App Store receipt.
In Xcode project navigator, select the StoreKit configuration file. Now selectEditor > Save Public Certificate.
Choose a location in your project to save the file.
You now need to ensure your app uses the correct certificate in all environments. The easiest way to do this is to create a simple helper which returns thecorrect certificate name for the runtime environment:
/// Constants used in support of IAP operations.publicstructIAPConstants{ /// The appropriate certificate to use for DEBUG and RELEASE builds. /// - Returns: Returns the appropriate certificate to use for /// DEBUG and RELEASE builds.publicstaticfunc Certificate()->String{#if DEBUG // This is issued by StoreKit for local testingreturn"StoreKitTestCertificate"#else // For release with the real App Storereturn"AppleIncRootCertificate"#endif}}
In this example we’ll put all IAP related code into a singleIAPHelper
class. We set this up as a singleton, ensuring there’s only ever a single instanceof the class:
publicclassIAPHelper:NSObject{ /// Singleton access. Use IAPHelper.shared to access all IAPHelper /// properties and methods.publicstaticletshared:IAPHelper=IAPHelper() /// Private initializer prevents more than a single instance of this class /// being created. See the public static 'shared' property. This helper /// must be initialized as soon as possible in the app's lifecycle. /// See application(_:didFinishLaunchingWithOptions:).privateoverrideinit(){ super.init() // Add ourselves to the payment queue so we get App Store // notificationsSKPaymentQueue.default().add(self)}::}
InAppDelegate
we initializeIAPHelper
:
classAppDelegate:UIResponder,UIApplicationDelegate{publicvariapHelper:IAPHelper?func application(_ application:UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{ // Make sure the IAPHelper is initialized early in the app's lifecycle // to ensure we don't miss any App Store notifications iapHelper=IAPHelper.sharedreturntrue}
Then in the initialViewController
we request a list of localized product information:
classViewController:UIViewController{privateletiap=IAPHelper.sharedoverridefunc viewDidLoad(){: configureProducts()}func configureProducts(){ iap.requestProductsFromAppStore{ notificationinif notification==IAPNotification.requestProductsSuccess{self.iap.processReceipt() // Validate the receipt // Update the UI with product info}}}}
When the user wants to purchase a product we callIAPHelper.buyProduct(_:completion:)
and handle the result in a closure:
extensionViewController:ProductCellDelegate{internalfunc requestBuyProduct(productId:ProductId){guardlet product= iap.getStoreProductFrom(id: productId)else{return} iap.buyProduct(product){ notificationinswitch notification{case.purchaseAbortPurchaseInProgress:IAPLog.event("Purchase aborted because another purchase is being processed")case.purchaseCancelled(productId:let pid):IAPLog.event("Purchase cancelled for product\(pid)")case.purchaseFailure(productId:let pid):IAPLog.event("Purchase failure for product\(pid)")case.purchaseSuccess(productId:let pid):IAPLog.event("Purchase success for product\(pid)")self.iap.processReceipt() // Validate the new receiptdefault:break} // Update the UI}}}
If you run the app you'll be able to tap on "buy" button and step through the purchase procedure:
While the app's running, click on theManage StoreKit Transactions button in Xcode's console toolbar:
You'll see transactions for purchases you've made:
From theManage StoreKit Transaction view you can select a transaction and then:
- Approve a transaction (if it's pending parental approval)
- Decline a transaction (if it's pending parental approval)
- Issue arefund
- Resolve transaction issues
- Delete a transaction
The ability todelete transactions is a huge boast to productivity! Previously, when working with App Store Connect sandbox accounts you couldtest purchase a product once. If you wanted to test purchasing the same product again you'd have to create a new sandbox account, complete withApple ID, email, etc.
The receipt issued to an app by the App Store contains a complete record of a user's in-app purchase history for that app. It is asigned andencryptedfile which is storedon the device in the app'smain bundle. The location of the receipt is given by the URLBundle.main.appStoreReceiptURL
.
When an app is first installed the receipt will bemissing. A new receipt will be issued automatically by the App Store when:
- An in-apppurchase succeeds
- The app isupdated (a receipt is issued for the new version)
- Previous in-app purchases arerestored
Receipts have the following structure:
The containing structure for the receipt is aPKCS #7struct
. PKCS #7 is a common cryptographic data format that OpenSSL handles for us.
ThePayload part of the receipt contains zero or moreAttributes in ASN.1 format (another common crypto format that OpenSSL works with).Each attribute is a record of an in-app purchase.
We use theCertificate Chain andSignature to validate that the receipt was genuinely issued by Apple.
You should validate the app's receipt:
Onstart-up
Your app should keep a "fallback" list of successfully purchased product ids that's stored either inUserDefaults (easy to work with, less secure) ortheKeychain (less easy to work with, more secure). This list will be useful if the receipt is missing and there's no network connection allowing a freshone to be requested from the App Store. At start up validate the receipt and then compare the fallback list against the IAP records in the receipt.If they differ, reset the fallback list to match the receiptWhen apurchase succeeds
A new receipt will be issued automatically by the App Store when a purchase is successfully completed. The new receipt will be available in the appbundle whenpaymentQueue(_:updatedTransactions:)
is called by StoreKitWhen purchases arerestored
This appears to the app like a succession of purchases. You should validate the receipt when the final transaction is completed(seepaymentQueue(_:updatedTransactions:)
)Edge cases
This includes when the storefront changes (e.g. the user changes from the US to UK App Store) and when rights to an IAP are revoked by the AppStore (e.g. the App Store has issued a refund), etc.
Although actual implementation details will vary, the same basic procedure is adopted by all apps. For example, the steps taken byIAPHelper
(seeIAPDemo Example below) when validating a receipt are as follows:
We'll go through an example of exactly how the validation process is accomplished in theIAPDemo Example below.
The following are the most importantStoreKit
protocols, classes and methods you'll encounter:
SKPaymentQueue
Allows us to observeStoreKit
transactionsSKProduct
Defines a product (id, title, etc.)SKPayment
Wrap anSKProduct
in anSKPayment
object when purchasingSKProductsRequest(productIdentifiers:)
Request localized product info from app store. Note that doing a product request does not result in a fresh receiptSKPaymentQueue.default().add()
Purchase a product usingSKPaymentQueue.default().add(SKPayment(product: myProduct))
restoreCompletedTransactions()
Restore previously made purchases withSKPaymentQueue.default().restoreCompletedTransactions()
productsRequest(_:didReceive:)
Called when localized product information is returned by the App Store. When this method returnsStoreKit
will immediately callrequestDidFinish(_:)
.
Protocol:SKProductsRequestDelegate
requestDidFinish(_:)
Called for bothSKProductsRequest(productIdentifiers:)
(request product info) andSKReceiptRefreshRequest()
(request receipt fresh).
Protocol:SKRequestDelegate
SKReceiptRefreshRequest()
Ask the App Store to issue new receipt.requestDidFinish(_:)
called when receipt availablepaymentQueue(_:updatedTransactions:)
Receive notifications when payments are successful, fail, are restored, etc.
Protocol:SKPaymentTransactionObserver
TheIAPDemo example provides a more complete, real-world treatment of handling in-app purchases. It has a similar structure to theHelloIAPWorldexample, however the scope ofIAPHelper
has been increased to cope with most non-subscription in-app purchase scenarios. Support for subscriptionswill be added shortly as an enhancement.
The main things to note are:
- On-device receipt validation is supported using OpenSSL
- In a debug build the StoreKit
Configuration.storekit
file is read by IAPHelper to create a set of supported product ids.In a release build theProductsRelease.plist
file is read to get product ids - Purchased product ids are persisted to
UserDefaults
as a "fallback" list, and then checked against IAP data in the receipt
TheIAPHelper.processReceipt()
method is used to validate App Store receipts. If you review this method you'll see the main validation flow:
publicfunc processReceipt(){ receipt=IAPReceipt()guard receipt.isReachable, receipt.load(), receipt.validateSigning(), receipt.read(), receipt.validate()else{IAPLog.event(.receiptProcessingFailure)return}::}
IAPHelper usesOpenSSL to validate the App Store receipt and read its contents. Building OpenSSL for iOS is not totally straightforward.To make getting started easier,IAPDemo contains pre-built OpenSSL binaries that were built using version 1.1.1 of OpenSSL.
The OpenSSL binarieslibcrypto.a
andlibssl.a
need to work in the following environments:
As you can see from the above table, everything works as anticipated, except in the case of building with Xcode on an M1 Mac for running on the simulator.At the time of writing (a week after the first M1 Macs became available in November 2020 [edit: this is still an issue in mid-December following the release ofiOS 14.3 and Xcode 12.3]) the situation isn't totally clear. If we intend only to support recent devices on iOS 13 and higher, in theory our OpenSSL binaries only need tosupport two architectures in a "fat" or Universal Binary: x86 64-bit and ARM 64-bit.
The included builds of the OpenSSL binaries contain the following architectures as shown by using thelipo
utility:
% lipo -info libcrypto.a Architectures in the fat file: libcrypto.a are: armv7 armv7s x86_64 arm64 % lipo -info libssl.a Architectures in the fat file: libssl.a are: armv7 armv7s x86_64 arm64
Our IAPDemo app only supports devices runningiOS 13
and higher. So IAPDemo supports the iPhone 6s and upwards.
Thearm64
64-bit ARM CPU architecture has been used since the iPhone 5S and iPad Air, Air 2 and Pro, with the A7 and later chips.Thearmv7s
32-bit architecture is used in Apple's A6 and A6X chips on iPhone 5, iPhone 5C and iPad 4.Thearmv7
32-bit architecture is an older variant of the 32-bit ARM CPU.
If we build on an M1 Mac for the simulator we get the following error:
libcrypto.a(tasn_typ.o), building for iOS Simulator, but linking in object file built for iOS, for architecture arm64
Currently, I can't find a solution to this issue. I wonder if it's because the OpenSSL binaries were built for iOS arm64, which is in some way different for thearm64 architecture which the simulator running on the M1 Mac expects?
Note that if you build on an M1 Mac for a real device then everything builds, links and running as expected. The issue is purely with the simulator.
It is possible to build for the simulator on an M1 Mac if you exclude the arm64 architecture for simulator builds:
However, when you run the app on the simulator there are issues. The most notable one is that theBundle.main.appStoreReceiptURL
property, whichpoints to the location of the App Store receipt, is always nil.
Until a solution is found you will need to build and deploy IAPDemo to a real device if you use an M1-based Mac.
TheIAPReceipt
class encapsulates the main features and data of the App Store receipt. This includes aSet<ProductId>
that holds a collection ofpurchased product ids that have been validated against data in the App Store receipt.
Theload()
method of theIAPReceipt
class loads the receipt and performs basic validation:
extensionIAPReceipt{ /// Load the receipt data from the main bundle and cache it. Basic validation of the receipt is done. /// We check its format, if it has a signature and if contains data. After loading the receipt you /// should call validateSigning() to check the receipt has been correctly signed, then read its IAP /// data using read(). You can then validate() the receipt. /// - Returns: Returns true if loaded correctly, false otherwise.publicfunc load()->Bool{ // Get the URL of the receipt fileguardlet receiptUrl=Bundle.main.appStoreReceiptURLelse{IAPLog.event(.receiptLoadFailure)returnfalse} // Read the encrypted receipt container file as Dataguardlet data=try?Data(contentsOf: receiptUrl)else{IAPLog.event(.receiptLoadFailure)returnfalse} // Using OpenSSL create a buffer to read the PKCS #7 container intoletreceiptBIO=BIO_new(BIO_s_mem()) // The buffer we will write intoletreceiptBytes:[UInt8]=.init(data) // The encrytped data as an array of bytesBIO_write(receiptBIO, receiptBytes,Int32(data.count)) // Write the data to the receiptBIO bufferletreceiptPKCS7=d2i_PKCS7_bio(receiptBIO,nil) // Now convert the buffer into the required PKCS7 structBIO_free(receiptBIO) // Free the buffer // Check the PKCS7 container existsguard receiptPKCS7!=nilelse{IAPLog.event(.receiptLoadFailure)returnfalse} // Check the PKCS7 container has a signatureguardpkcs7IsSigned(pkcs7: receiptPKCS7!)else{IAPLog.event(.receiptLoadFailure)returnfalse} // Check the PKCS7 container is of the correct data typeguardpkcs7IsData(pkcs7: receiptPKCS7!)else{IAPLog.event(.receiptLoadFailure)returnfalse} receiptData= receiptPKCS7 // Cache the PKCS7 dataIAPLog.event(.receiptLoadSuccess)returntrue}func pkcs7IsSigned(pkcs7:UnsafeMutablePointer<PKCS7>)->Bool{ // Convert the object in the PKCS7 struct to an Int32 and compare it to the OpenSSL NID constantOBJ_obj2nid(pkcs7.pointee.type)== NID_pkcs7_signed}func pkcs7IsData(pkcs7:UnsafeMutablePointer<PKCS7>)->Bool{ // Convert the object in the PKCS7 struct to an Int32 and compare it to the OpenSSL NID constantOBJ_obj2nid(pkcs7.pointee.d.sign.pointee.contents.pointee.type)== NID_pkcs7_data}}
Theread()
method of theIAPReceipt
class reads the receipt's in-app purchase data and caches it:
extensionIAPReceipt{ /// Read internal receipt data into a cache. /// - Returns: Returns true if all expected data was present and correctly read from the receipt, false otherwise.publicfunc read()->Bool{ // Get a pointer to the start and end of the ASN.1 payloadletreceiptSign= receiptData?.pointee.d.signletoctets= receiptSign?.pointee.contents.pointee.d.datavarpointer=UnsafePointer(octets?.pointee.data)letend= pointer!.advanced(by:Int(octets!.pointee.length))vartype:Int32=0varxclass:Int32=0varlength:Int=0ASN1_get_object(&pointer,&length,&type,&xclass, pointer!.distance(to: end))guard type== V_ASN1_SETelse{IAPLog.event(.receiptReadFailure)returnfalse}while pointer!< end{ASN1_get_object(&pointer,&length,&type,&xclass, pointer!.distance(to: end))guard type== V_ASN1_SEQUENCEelse{IAPLog.event(.receiptReadFailure)returnfalse}guardlet attributeType=IAPOpenSSL.asn1Int(p:&pointer, expectedLength: length)else{IAPLog.event(.receiptReadFailure)returnfalse}guardlet _=IAPOpenSSL.asn1Int(p:&pointer, expectedLength: pointer!.distance(to: end))else{IAPLog.event(.receiptReadFailure)returnfalse}ASN1_get_object(&pointer,&length,&type,&xclass, pointer!.distance(to: end))guard type== V_ASN1_OCTET_STRINGelse{IAPLog.event(.receiptReadFailure)returnfalse}varp= pointerswitchIAPOpenSSLAttributeType(rawValue: attributeType){case.BudleVersion: bundleVersionString=IAPOpenSSL.asn1String(p:&p, expectedLength: length)case.ReceiptCreationDate: receiptCreationDate=IAPOpenSSL.asn1Date( p:&p, expectedLength: length)case.OriginalAppVersion: originalAppVersion=IAPOpenSSL.asn1String(p:&p, expectedLength: length)case.ExpirationDate: expirationDate=IAPOpenSSL.asn1Date(p:&p, expectedLength: length)case.OpaqueValue: opaqueData=IAPOpenSSL.asn1Data(p: p!, expectedLength: length)case.ComputedGuid: hashData=IAPOpenSSL.asn1Data(p: p!, expectedLength: length)case.BundleIdentifier: bundleIdString=IAPOpenSSL.asn1String(p:&pointer, expectedLength: length) bundleIdData=IAPOpenSSL.asn1Data(p: pointer!, expectedLength: length)case.IAPReceipt:variapStartPtr= pointerletreceiptProductInfo=IAPReceiptProductInfo(with:&iapStartPtr, payloadLength: length)iflet rpi= receiptProductInfo{ inAppReceipts.append(rpi) // Cache in-app purchase recordiflet pid= rpi.productIdentifier{ validatedPurchasedProductIdentifiers.insert(pid)}}default:break // Ignore other attributes in receipt} // Advance pointer to the next item pointer= pointer!.advanced(by: length)} hasBeenRead=trueIAPLog.event(.receiptReadSuccess)returntrue}}
Thevalidate()
method of theIAPReceipt
class performs the actual receipt validation:
extensionIAPReceipt{ /// Perform on-device (no network connection required) validation of the app's receipt. /// Returns false if the receipt is invalid or missing, in which case your app should call /// refreshReceipt(completion:) to request an updated receipt from the app store. This may /// result in the user being prompted for their App Store credentials. /// /// We validate the receipt to ensure that it was: /// /// * Created and signed using the Apple x509 root certificate via the App Store /// * Issued for the same version of this app and the user's device /// /// At this point a list of locally stored purchased product ids should have been loaded from the UserDefaults /// dictionary. We need to validate these product ids against the App Store receipt's collection of purchased /// product ids to see that they match. If there are no locally stored purchased product ids (i.e. the user /// hasn't purchased anything) then we don't attempt to validate the receipt. Note that if the user has previously /// purchased products then either using the Restore feature or attempting to re-purchase the product will /// result in a refreshed receipt and the product id of the product will be stored locally in the UserDefaults /// dictionary. /// - Returns: Returns true if the receipt is valid; false otherwise.publicfunc validate()->Bool{guardlet idString= bundleIdString,let version= bundleVersionString,let _= opaqueData,let hash= hashDataelse{IAPLog.event(.receiptValidationFailure)returnfalse}guardlet appBundleId=Bundle.main.bundleIdentifierelse{IAPLog.event(.receiptValidationFailure)returnfalse}guard idString== appBundleIdelse{IAPLog.event(.receiptValidationFailure)returnfalse}guardlet appVersionString=Bundle.main.object(forInfoDictionaryKey:"CFBundleVersion")as?Stringelse{IAPLog.event(.receiptValidationFailure)returnfalse}guard version== appVersionStringelse{IAPLog.event(.receiptValidationFailure)returnfalse}guard hash==computeHash()else{IAPLog.event(.receiptValidationFailure)returnfalse}iflet expirationDate= expirationDate{if expirationDate<Date(){IAPLog.event(.receiptValidationFailure)returnfalse}} isValid=trueIAPLog.event(.receiptValidationSuccess)returntrue} /// Compare the set of fallback ProductIds with the receipt's validatedPurchasedProductIdentifiers. /// - Parameter fallbackPids: Set of locally stored fallback ProductIds. /// - Returns: Returns true if both sets are the same, false otherwise.publicfunc compareProductIds(fallbackPids:Set<ProductId>)->Bool{ fallbackPids== validatedPurchasedProductIdentifiers}}
We now have an Xcode project (IAPDemo) which contains app code, plus a discreet group of files that form a helper for supporting in-app purchases.
Our aim is to:
- Create a separateframework project for
IAPHelper
- Move the helper files into that project and then remove them from theIAPDemo project
- The framework should be useable byIAPDemo and sharable by others in the future
- Combine the mainIAPDemo project and the
IAPHelper
framework project in a single XcodeWorkspace so we can easily work on, debug and test both projects - AddUnit Tests for
IAPHelper
to the framework
An Xcode workspace is a collection of projects:
- Any project in the workspace has access to all the content from any other project in that same workspace, including compiled content
- You can set up dependencies between projects so that a single build command builds all required pieces for the chosen target
- You can include frameworks, modules, or static libraries, either your own or those of a third party
- You can break up large projects into smaller pieces, allowing easier maintenance and sharing of functionality
Open the originalIAPDemo project which contains the mixture of app code andIAPHelper
code.
Create new project of typeFramework forIAPHelper
:
Name the projectIAPHelper:
Save the new framework projectoutside the directory structure of IAPDemo project:
In the newIAPHelper framework project, create new group folders foropenssl-include andopenssl-lib:
FromFinder select all the header files in theIAPDemo project'sopenssl-include directory and drag them into theopenssl-include group folderin the newIAPHelper
framework project. Make sure to check"Copy items if needed" and select"Create folder references":
From Finder select the two OpenSSL libraries in theIAPDemo project'sopenssl-lib directory and drag them into theopenssl-lib group folder in thenewIAPHelper
framework project.
From Finder drag the remaining IAPHelper files from theIAPDemo project into theIAPHelper group folder in the newIAPHelper
framework project.
The new framework project should now look like this:
Switch back to theIAPDemo app project, select theIAPHelper group folder anddelete it. When prompted choseMove to Trash (you might wantback up the project files first, just in case of problems):
Switch to the newIAPHelper
framework project.
Copy the contents ofIAPDemo-Bridging-Header.h and paste it intoIAPHelper.h (which was generated for us by Xcode).
DeleteIAPDemo-Bridging-Header.h.
InBuild Settings for theIAPHelper target set theHeader Search Paths field to:
$(inherited) $(PROJECT_DIR)/IAPHelper/openssl-include
Check thatLibrary Search Paths is set to:
$(inherited) $(PROJECT_DIR)/IAPHelper/openssl-lib
Select all the header files in theopenssl-include group folder and make sure theirTarget Membership is set toIAPHelper Public (the default isProject):
If you don't do this you'll get an error for every header file included inIAPHelper.h when you build the project:
Include of non-modular header inside framework module
The project should now build.
Close theIAPHelper framework project and switch back to the originalIAPDemo project.
To convert the project into a workspace selectFile > Save As Workspace. Normally you'll want to save the workspace file in the same root folder foryour original project.
Add theIAPHelper framework project to the new workspace by selectingFile > Add Files toProject Name:
You should see thatIAPHelper has been added to the workspace:
Select the app project (IAPDemo in this case) targetBuild Settings and remove theBridging Header:
You can now embed theIAPHelper
framework in theIAPDemo app project:
Build the workspace. You may need to adjust the access levels from (e.g.internal
topublic
) for some elements in the IAPHelper framework.
The app should now run.
Close theIAPDemo workspace and then re-open theIAPHelper project.
Convert theIAPHelper project to a workspace withFile > Save As Workspace.
Save the workspace file in the root of theIAPHelper project folder:
SelectFile > New > Target.
Filter the templates by typing "test" and then selectUnit Testing Bundle:
Create the new target, which will be namedproject-nameTests.
The new test target is added to your project:
Open the unit test file (IAPHelperTests.swift) that Xcode just created.
Add the following to define a first simple test case (notice how we@testable import
theIAPHelper
module):
import XCTestimport StoreKitTest// Import the IAPHelper module.// This lets you write unit tests against *internal* properties and methods@testableimport IAPHelperclassIAPHelperTests:XCTestCase{ // Reference the IAPHelper singleton via the shared propertyprivatevariap=IAPHelper.shared // Create a test session that allows us to control StoreKit transactions // (e.g. disable the normal purchase confirmation dialogs, etc.)privatevarsession:SKTestSession!=try?SKTestSession( configurationFileNamed:IAPConstants.ConfigFile())func testConfiguration(){ // If this is true then the StoreKit config file has been successfully // read by IAPHelperXCTAssertTrue(iap.haveConfiguredProductIdentifiers)}}
Now build and run the test by clicking the button to the left of the class name (or hitCmd + U):
At this point you'll get an error if you try to run the tests on a real device:
The issue here is Xcode doesn't support running tests on aframework directly on a real device. As we want to support both the simulator and realdevices we'll need to create a minimal test host app that simply embeds theIAPHelper framework - it doesn't need to even reference it in code.
Add a new iOS app to theIAPHelper workspace withFile > New > Project:
Select toadd the project to yourIAPHelper workspace:
Then select theIAPHelperTests target and inTarget > General > Testing set the test host app as theHost Application:
The host app itself will run on either the simulator or real device.
CopyAppleRootCertificate.cer,StoreKitTestCertificate.cer andConfiguration.storekit from theIAPHelper framework and add themto theIAPHelperTestHost project. Then add theIAPHelper framework toIAPHelperTestHost:
There's no need to add any code or referenceIAPHelper.
We should now be able to build and run the test by clicking the button to the left of the class name (or hitCmd + U):
We can now complete writing our tests inIAPHelperTests:
//// IAPHelperTests.swift// IAPHelperTests//// Created by Russell Archer on 28/11/2020.//import XCTestimport StoreKitTest// Import the IAPHelper module.// This lets you write unit tests against *internal* properties and methods@testableimport IAPHelperclassIAPHelperTests:XCTestCase{privatevariap=IAPHelper.shared // Create a test session that allows us to control StoreKit transactions // (e.g. disable the normal purchase confirmation dialogs, etc.)privatevarsession:SKTestSession!=try?SKTestSession( configurationFileNamed:IAPConstants.ConfigFile())func testConfiguration(){ // If this is true then the StoreKit config file has been successfully // read by IAPHelperXCTAssertTrue(iap.haveConfiguredProductIdentifiers)}func testGetProductInfo(){ // Create an expected outcome for an *asynchronous* testletproductInfoExpectation=XCTestExpectation() iap.requestProductsFromAppStore{ notificationinif notification==IAPNotification.requestProductsSuccess{XCTAssertNotNil(self.iap.products)}elseif notification==IAPNotification.requestProductsFailure{XCTFail()} productInfoExpectation.fulfill()} // Signal that we want to wait on one or more expectations for up // to the specified timeoutwait(for:[productInfoExpectation], timeout:10.0)}func testPurchaseProduct(){letproductId="com.rarcher.flowers-large"letpurchaseProductExpectation=XCTestExpectation() session.disableDialogs=trueguardlet product= iap.getStoreProductFrom(id: productId)else{XCTFail()return} iap.buyProduct(product){ notificationinswitch notification{case.purchaseSuccess(productId:let pid):XCTAssertNotNil(pid)case.purchaseFailure(productId:):XCTFail()default:break} purchaseProductExpectation.fulfill()}wait(for:[purchaseProductExpectation], timeout:10.0)}func testValidateReceipt(){XCTAssertTrue(iap.processReceipt())}}
Starting December 31, 2020, legislation from the European Union introducesStrong Customer Authentication (SCA) requirements.AnApple Support Document provides details.
As the Apple support document notes:
For in-app purchases that require SCA, the user is prompted to authenticate their credit or debit card. They’re taken out of the purchase flowto the bank or payment service provider’s website or app for authentication, then redirected to the App Store where they’ll see a message lettingthem know that their purchase is complete. Handling this interrupted transaction is similar to Ask to Buy purchases that need approval from afamily approver or when users need to agree to updated App Store terms and conditions before completing a purchase.
IAPHelper already provides support for handling SCA through its support of deferred purchases ("ask to buy"):
publicfunc paymentQueue(_ queue:SKPaymentQueue, updatedTransactions transactions:[SKPaymentTransaction]){fortransactionin transactions{switch(transaction.transactionState){case.purchasing:purchaseInProgress(transaction: transaction)case.purchased:purchaseCompleted(transaction: transaction)case.failed:purchaseFailed(transaction: transaction)case.restored:purchaseCompleted(transaction: transaction, restore:true)case.deferred:purchaseDeferred(transaction: transaction)default:return}}}privatefunc purchaseDeferred(transaction:SKPaymentTransaction){ isPurchasing=falseIAPLog.event(.purchaseDeferred(productId: transaction.payment.productIdentifier))DispatchQueue.main.async{self.purchaseCompletion?(.purchaseDeferred(productId: transaction.payment.productIdentifier))} // Do NOT call SKPaymentQueue.default().finishTransaction() for .deferred status}
So, initially when the user attempts to purchase a product that requires SCA or ask-to-buy, a transaction with a state ofdeferred
is generated. Then, when the user authenticates (or when a parent authorizes an ask-to-buy purchase) another transaction with a state ofpurchased
is generated.
Upcoming enhancements include:
- IAPHelper support for subscriptions
- A example of server-based (off device) receipt validation
- Using a service like RevenueCate for receipt validation
About
Implementing and testing In-App Purchases in Xcode 12 and iOS 14, including local receipt validation.