Set up Apple Pay payments
Last updated: Aug 15th, 7:09am
Complete the steps to check out using Apple Pay.
Know before you code
Apple Pay has specific user experience and identity guidelines, which Apple might enforce during the App Store review process. Read these guidelines as well as Apple's developer information when designing your Apple Pay payment flow.
Step 1: Set up Apple Pay certificate and merchant ID
To use Apple Pay on a mobile device, you must configure an Apple Pay merchant ID and an Apple Pay certificate in Apple's Developer Center. Follow the Apple Pay Programming Guide to learn how to create a merchant ID and a Payment Processing certificate.
Step 2: Display the Apple Pay button
Before presenting the Apple Pay option to the buyer, determine whether Apple Pay is available. If so, present a PKPaymentButton.
Note: PayPal supports only Visa and Mastercard through Apple Pay.
1// Conditionally show Apple Pay button based on device availability2if (PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [.visa, .masterCard])) {3 let applePayButton = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .whiteOutline)4 applePayButton.addTarget(self, action: #selector(applePayCheckoutTapped(_:)), for: .touchUpInside)5 self.view.addSubview(button)6}
Step 3: Create a PKPaymentRequest
Before you can start a user-facing Apple Pay experience, initialize a payment request:
1func setupPaymentRequest() -> PKPaymentRequest {2 let paymentRequest = PKPaymentRequest()3 // We recommend collecting billing address information, at minimum4 // billing postal code, and passing that billing postal code with all5 // Apple Pay transactions as a best practice.6 paymentRequest.requiredBillingContactFields = [.postalAddress]78 // Set other PKPaymentRequest properties here9 paymentRequest.merchantCapabilities = .capability3DS10 paymentRequest.paymentSummaryItems = [11 PKPaymentSummaryItem(label: "Subtotal", amount: NSDecimalNumber(string: "12.75")),12 PKPaymentSummaryItem(label: "Discount", amount: NSDecimalNumber(string: "-2.00")),13 // Last entry should be the total amount to be charged:14 PKPaymentSummaryItem(label: "<Your Company Name>", amount: NSDecimalNumber(string: "10.75"))15 ]1617 return paymentRequest18}
Construct a PKPaymentRequest.
Tip: Consider setting these values dynamically based on a response from your server to avoid re-releasing your app with each change.
paymentSummaryItems: Make sure the total amount for this required value does not exceed the amount that you authorized or submitted for settlement.supportedNetworks: This SDK supports only Visa and MasterCard networks.requiredBillingContactFields: IncludePKContactFieldPostalAddress.
If you don't set the following properties on your PKPaymentRequest, the SDK populates them with default values based on your merchant account setup:
currencyCodecountryCodemerchantCapabilitiesmerchantIdentifier
Step 4: Check out with Apple Pay
Use the PYPLClient to call checkoutWithApplePay with a valid PayPal orderID. The SDK notifies your app when the Apple Pay checkout UI is ready to be presented and dismissed using the BTViewControllerPresentingDelegate protocol.
Call the applePayResultHandler with a boolean to indicate the success or failure of your payment authorization or capture. This is so the SDK can notify the user of the payment's status.
1func tappedApplePay() {2 // Create a PKPaymentRequest3 let paymentRequest = setupPaymentRequest()45 // Call checkoutWithApplePay6 client?.checkoutWithApplePay("orderID", paymentRequest: paymentRequest) { (result, error, applePayResultHandler) in7 guard let result = result else {8 // handle error9 applePayResultHandler(false)10 return11 }1213 // Send orderID to your server to process the payment14 // Capture or authorize the orderID1516 applePayResultHandler(true)17 }18}