Deep Integration of Apple Pay In‑App Payments on iOS: Technical Guide and Best Practices
This article explains how to integrate Apple Pay’s in‑app payment feature into an iOS application, compares it with traditional third‑party payment SDKs, and provides detailed implementation steps, code samples, server‑side decryption, transaction handling, and practical tips for a seamless user experience.
Apple Pay’s in‑app payment offers a new online payment model for iOS apps. By tightly coupling Apple Pay’s native capabilities with an app’s product flow, developers can significantly improve the user’s checkout experience.
Compared with third‑party solutions such as Alipay or WeChat Pay, Apple Pay provides system‑level support without requiring a separate SDK, and it can retrieve user information (e.g., phone number, shipping address) directly.
Deep integration example – ENJOY : ENJOY, one of the first apps in China to support Apple Pay, demonstrates features like purchasing without logging in, one‑click buying on the home page and flash sales, and completing transactions earlier than third‑party payments.
Key integration steps :
Check Apple Pay availability using PKPaymentAuthorizationViewController.canMakePayments() and PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks([PKPaymentNetworkChinaUnionPay]) . The APIs require iOS 8+, with the China UnionPay network available from iOS 9.2.
Configure a PKPaymentRequest with merchant identifier, capabilities, country and currency codes, supported networks, and payment summary items. Example:
let request = PKPaymentRequest()
request.merchantIdentifier = "merchant.xxxxx"
request.merchantCapabilities = [.Capability3DS, .CapabilityEMV, .CapabilityCredit, .CapabilityDebit]
request.countryCode = "CN"
request.currencyCode = "CNY"
request.supportedNetworks = [PKPaymentNetworkChinaUnionPay]
// set paymentSummaryItems and requiredShippingAddressFields as neededPresent the payment sheet with:
let vc = PKPaymentAuthorizationViewController(paymentRequest: request)
vc.delegate = self
presentViewController(vc, animated: true, completion: nil)The payment sheet has a high view priority; when it appears the app behaves like it is in the background, so developers must handle related lifecycle callbacks.
Server‑side decryption : After the user authenticates, the callback provides a PKPaymentToken whose paymentData is an encrypted JSON blob. Developers must verify the signature , decrypt the wrappedKey with the merchant’s private key, then decrypt data to obtain the card information, which is forwarded to UnionPay for settlement.
Transaction status confirmation : UnionPay returns both synchronous and asynchronous results. If no asynchronous callback arrives within a few seconds, poll the transaction‑status API using the synchronous flow number.
Failure handling : For PIN‑related errors (missing, incorrect, lockout), the payment sheet stays visible; for other failures the sheet dismisses and the app must inform the user. The status is set via the completion closure with values such as PKPaymentAuthorizationStatus.PINRequired , PINIncorrect , or PINLockout .
Cancellation and revocation : Users can cancel the sheet at any time. The app notifies the server, which decides whether to call UnionPay’s revocation API (if the charge has not yet been settled) or a refund API (if revocation is unsupported).
In summary, while Apple Pay’s native in‑app payment is not conceptually complex, it involves many detailed steps on the client and server sides. For projects with modest customization needs, third‑party SDKs (e.g., CUPSDK) can abstract much of this logic, but developers seeking the best native experience can follow the guidelines presented here.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.