Once your Certificate and Merchant ID are set up, you can add Apple Pay to your app.

Apple Pay user experience

Apple Pay has specific user experience and identity guidelines, which Apple may enforce during the App Store review process. Please consult these guidelines as well as Apple's developer information when designing your Apple Pay user experience.

Get the SDK

For CocoaPods integrations, include Braintree/Apple-Pay and Braintree in your podfile:

Ruby
pod 'Braintree'
pod 'Braintree/Apple-Pay'

For Carthage integrations, include the BraintreeApplePay framework.

availability

Support for Swift Package Manager was added in v5 of the iOS SDK. See the migration guide for details.

Initialization

Like all Braintree SDK integrations, you will first need to initialize the Braintree SDK client:

var braintreeClient: BTAPIClient?

// BTAPIClient can be initialized in a couple different ways, here's one example:
braintreeClient = BTAPIClient(authorization: CLIENT_AUTHORIZATION)

Your app will submit an Apple Pay authorization to Braintree and receive a payment method nonce in return, which can be used for server-side processing.

PassKit integration and payment tokenization

Set up your Apple Pay button

Apple Pay is only available to a subset of iOS users. Before presenting the Apple Pay option to the current user, you should determine whether Apple Pay is available.

import PassKit
// Add BraintreeApplePay.h to bridging header

class MyCheckoutViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate {
    // ...

    override func viewDidLoad() {
        super.viewDidLoad()

        // Conditionally show Apple Pay button based on device availability
        if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [PKPaymentNetwork.visa, PKPaymentNetwork.masterCard, PKPaymentNetwork.amex, PKPaymentNetwork.discover]) {
            let button = self.applePayButton()
            // TODO: Set button constraints/frame...
            self.view.addSubview(button)
        }
    }

    // ...
}

Since iOS 8.3, Apple has provided an Apple Pay button, PKPaymentButton. If your app targets older versions of iOS, you will need to create your own button using Apple's Human Interface Guidelines and assets.

func applePayButton() -> UIButton {
    var button: UIButton?

    if #available(iOS 8.3, *) { // Available in iOS 8.3+
        button = PKPaymentButton(type: PKPaymentButtonType.buy, style: PKPaymentButtonStyle.black)
    } else {
        // TODO: Create and return your own apple pay button
        // button = ...
    }

    button?.addTarget(self, action: #selector(tappedApplePay), for: UIControlEvents.touchUpInside)

    return button!
}

Create a PKPaymentRequest

Before you can initiate a user-facing Apple Pay experience, you will need to initialize a payment request:

func setupPaymentRequest(completion: @escaping (PKPaymentRequest?, Error?) -> Void) {
    let applePayClient = BTApplePayClient(apiClient: self.braintreeApiClient)
    // You can use the following helper method to create a PKPaymentRequest which will set the `countryCode`,
    // `currencyCode`, `merchantIdentifier`, and `supportedNetworks` properties.
    // You can also create the PKPaymentRequest manually. Be aware that you'll need to keep these in
    // sync with the gateway settings if you go this route.
    applePayClient.paymentRequest { (paymentRequest, error) in
        guard let paymentRequest = paymentRequest else {
            completion(nil, error)
            return
        }

        // We recommend collecting billing address information, at minimum
        // billing postal code, and passing that billing postal code with all
        // Apple Pay transactions as a best practice.
        paymentRequest.requiredBillingContactFields = [.postalAddress]

        // Set other PKPaymentRequest properties here
        paymentRequest.merchantCapabilities = .capability3DS
        paymentRequest.paymentSummaryItems =
        [
            PKPaymentSummaryItem(label: "<#ITEM_NAME#>", amount: NSDecimalNumber(string: "<#PRICE#>")),
            // Add add'l payment summary items...
            PKPaymentSummaryItem(label: "<#COMPANY NAME#>", amount: NSDecimalNumber(string: "<#GRAND TOTAL#>")),
        ]
        completion(paymentRequest, nil)
    }
}

Here are some Braintree-specific recommendations about the various fields in the PKPaymentRequest:

  • countryCode: We suggest using the country where your business is located
  • currencyCode: This value must correspond to your Braintree merchant account's currency
  • merchantCapabilities: Please use PKMerchantCapability3DS (PKMerchantCapabilityEMV corresponds to in-store purchasing only)
  • merchantIdentifier: This value must match up with the merchantIdentifier and certificate on file in both Apple's developer center and Braintree's Control Panel
  • paymentSummaryItems: This value is required, and the grand total amount should not exceed the amount that is authorized or submitted for settlement
  • supportedNetworks: This value must match up with your merchant account's accepted payment methods
  • requiredBillingContactFields: We recommend including PKContactFieldPostalAddress as a best practice

In order to be prepared to make changes without re-releasing your app, you should consider setting these values dynamically based on a response from your server.

In particular, you should be careful about keeping the PKPaymentRequest in sync with server environment (production vs. sandbox), as well as with your merchant account configuration and Apple Pay configuration. Self-service certificate management can be found in the Control Panel by clicking the gear icon in the top right corner, selecting Processing from the drop-down menu, scrolling to Apple Pay, and clicking the Options link.

Present a PKPaymentAuthorizationViewController

When your user taps on your Apple Pay UI, present a PKPaymentAuthorizationViewController to initiate Apple Pay:

func tappedApplePay() {
    self.setupPaymentRequest { (paymentRequest, error)
        guard error == nil else {
           // Handle error
           return
        }

        // Example: Promote PKPaymentAuthorizationViewController to optional so that we can verify
        // that our paymentRequest is valid. Otherwise, an invalid paymentRequest would crash our app.
        if let vc = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
            as PKPaymentAuthorizationViewController?
        {
            vc.delegate = self
            present(vc, animated: true, completion: nil)
        } else {
            print("Error: Payment request is invalid.")
        }
    }
}

Implement PKPaymentAuthorizationViewControllerDelegate

Implement the PKPaymentAuthorizationViewControllerDelegate protocol methods. In your implementation of paymentAuthorizationViewController:didAuthorizePayment:handler:, create a nonce by tokenizing the PKPayment via the Braintree SDK:

func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController,
                         didAuthorizePayment payment: PKPayment,
                                  handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {

    // Tokenize the Apple Pay payment
    braintree!.tokenizeApplePay(payment) { (nonce, error) in
        if error != nil {
            // Received an error from Braintree.
            // Indicate failure via the completion callback.
            completion(PKPaymentAuthorizationResult(status: .failure, errors: nil)
            return
        }

        // TODO: On success, send nonce to your server for processing.
        // If requested, address information is accessible in `payment` and may
        // also be sent to your server.

        // Then indicate success or failure based on the server side result of Transaction.sale
        // via the completion callback.
        // e.g. If the Transaction.sale was successful
        completion(PKPaymentAuthorizationResult(status: .success, errors: nil))
    }
}

The delegate method paymentAuthorizationViewController:didAuthorizePayment:handler: is only available in iOS 11+. If you are targeting apps using iOS 10 or below, you must also implement paymentAuthorizationViewController:didAuthorizePayment:completion:.

func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
    // Tokenize the Apple Pay payment
    braintree!.tokenizeApplePay(payment) {
        (nonce, error) in
        if error != nil {
            // Received an error from Braintree.
            // Indicate failure via the completion callback.
            completion(PKPaymentAuthorizationStatus.failure)
            return
        }

        // TODO: On success, send nonce to your server for processing.
        // If requested, address information is accessible in `payment` and may
        // also be sent to your server.

        // Then indicate success or failure based on the server side result of Transaction.sale
        // via the completion callback.
        // e.g. If the Transaction.sale was successful
        completion(PKPaymentAuthorizationStatus.success)
    }
}

You must also implement the paymentAuthorizationViewControllerDidFinish(_:) delegate method to handle the dismissal of the Apple Pay sheet upon Apple Payment finishing.

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
    // Apple Payment finished
    dismiss(animated: true)
}

Next Page: Server-side →