Present Apple Pay yourself
Build the session's Apple Pay request, show it with your own PassKit controller, and pay with the payment the customer authorizes.
checkout.applePayPaymentRequest(_:) builds the Apple Pay request for the session, for you to present with your own PKPaymentAuthorizationController. When the customer authorizes, pass the payment to pay(.applePay(_:)) and complete the sheet with the result. Use it when your app runs the Apple Pay sheet itself. Otherwise, payWithApplePay() does all of this for you, and its requirements apply here too.
let request = try checkout.applePayPaymentRequest(applePay)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
configuration | PayApplePayConfiguration | Yes | Your merchant ID, country, and total label. See Pay with Apple Pay. |
Returns
A PKPaymentRequest built from the session, the way Pay.com.js builds it on the web:
| Property | Value |
|---|---|
merchantIdentifier | configuration.merchantIdentifier. |
countryCode | configuration.merchantCountryCode, else your Pay.com account's country, in upper case. |
currencyCode | The session's currency, in upper case. |
paymentSummaryItems | One final item for the session's amount, labeled with configuration.merchantDisplayName, else the name your account shows on statements, else Total. |
supportedNetworks | The card brands the session accepts, as PassKit networks. Brands PassKit has no network for are left out. Visa, Mastercard, American Express, and Discover when the session doesn't restrict brands. |
merchantCapabilities | 3DS, plus debit and credit as the session's card funding types allow. Both when the session doesn't restrict them. |
supportedCountries | The card issuer countries the session accepts, when it restricts them. |
requiredBillingContactFields | The billing name, unless the session turned name collection off for Apple Pay. |
The payment is charged only if the session's total is still the one in the last request you built. If the amount may have changed, call refreshSession() first, then build the request. After .failed(.amountChanged), dismiss your sheet, which can only show the old total, and build a new request.
canPayWithApplePay(_:) is true when this request can be built and the device can pay with one of its networks.
Errors
applePayPaymentRequest(_:) throws one error.
| Error | When |
|---|---|
unsupported | The session doesn't offer applepay, isn't a sale or authorization session, or has no amount or currency, or your country isn't known (set merchantCountryCode). Inspect the associated message for the reason. |
Paying with the authorized payment then works like any payment. It fails in the same ways as with payWithApplePay(), for example with .failed(.unsupported) for a token from the iOS Simulator.
Example
import PassKit
import PayCheckout
import PayCore
import UIKit
@MainActor
final class ApplePayController: NSObject, @preconcurrency PKPaymentAuthorizationControllerDelegate {
private let checkout: PayCheckout
private weak var presenter: UIViewController?
private var controller: PKPaymentAuthorizationController?
private(set) var result: PayResult = .canceled
init(checkout: PayCheckout, presenter: UIViewController) {
self.checkout = checkout
self.presenter = presenter
}
func show(_ configuration: PayApplePayConfiguration) async throws(PayError) {
try await checkout.refreshSession() // So the sheet shows the current total
let request = try checkout.applePayPaymentRequest(configuration)
let controller = PKPaymentAuthorizationController(paymentRequest: request)
controller.delegate = self
self.controller = controller
_ = await controller.present()
}
func paymentAuthorizationController(
_ controller: PKPaymentAuthorizationController,
didAuthorizePayment payment: PKPayment
) async -> PKPaymentAuthorizationResult {
guard let presenter else { return PKPaymentAuthorizationResult(status: .failure, errors: nil) }
do throws(PayError) {
result = try await checkout.pay(.applePay(payment), presenter: presenter)
} catch {
result = .failed(error)
}
switch result {
case .succeeded, .processing:
return PKPaymentAuthorizationResult(status: .success, errors: nil)
case .canceled, .failed:
return PKPaymentAuthorizationResult(status: .failure, errors: nil)
}
}
func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
controller.dismiss(completion: nil)
self.controller = nil
// Read `result` here: it's `.canceled` if the customer closed the sheet without paying.
}
}pay doesn't close your sheet. If the payment needs a 3DS challenge or a redirect, which is rare with Apple Pay, it opens on presenter while your sheet is still up. payWithApplePay() closes the sheet first.
Next steps
- Pay with Apple Pay: Let the SDK show the sheet.
- Pay: What
payreturns and throws.