Integrating Apple Pay

Implement Apple Pay.

Adding the Apple Pay payment method to your iOS application follows the same concepts as for any other payment method. You first create a Unzer Payment Type and then trigger the charge for that Payment Type (see also Using the Mobile SDK).

You use the iOS Apple Pay API to show the Apple Pay UI to the user and to retrieve the encrypted payment information.

// configure the Apple Pay Payment Request for the Apple Pay UI

let paymentRequest = PKPaymentRequest()
paymentRequest.currencyCode = "EUR"
paymentRequest.countryCode = "FR"
paymentRequest.requiredBillingContactFields = [ .name, .emailAddress ]
paymentRequest.requiredShippingContactFields = [ .postalAddress ]
paymentRequest.shippingType = .delivery
let bikeShipping = PKShippingMethod(label: "Bike", amount: 10)
bikeShipping.detail = "30 min"
bikeShipping.identifier = "bike"
let truckShipping = PKShippingMethod(label: "Truck", amount: 20.0)
truckShipping.identifier = "Truck"
truckShipping.detail = "60 min"
paymentRequest.shippingMethods = [ bikeShipping, truckShipping]
paymentRequest.paymentSummaryItems = [
    PKPaymentSummaryItem(label: "Your Product Label", amount: NSDecimalNumber(value: 123.30))
// merchant identifier as defined on the Apple Developer Portal
paymentRequest.merchantIdentifier = ""

// present the Apple Pay UI
if let paymentViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) {
   paymentViewController.delegate = self
   present(paymentViewController, animated: true, completion: nil)        

The Apple Pay UI is presented to the user and your PKPaymentAuthorizationViewControllerDelegate methods are being called for success or failure (e.g. cancel).

If the user did authorise the payment you then create a Unzer Payment Type with the encrypted payment data of the Apple Pay API. You are responsible to call the Apple Pay completion block with the result of the charge (success or failure) as the Apple Pay UI shows an activity indicator during the whole process and reports the status at the end of the process.

extension YourViewController: PKPaymentAuthorizationViewControllerDelegate {
    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true) {
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {

                // get the encrypted payment information
        let applePayPaymentData = ApplePayPaymentData(paymentToken: payment.token)
        // create the heidelpay payment type
        heidelPay.createApplePay(paymentToken: applePayData.paymentToken) { (paymentType, error) in
            if let error = error {
                completion(PKPaymentAuthorizationResult(status: .failure, errors: [error]))
            } else {
              // call your server with the payment type id which then triggers
              // a charge on that payment type. wait for the result and tell
              // apple pay if the charge was successfull.
              yourServerAPI.charge(paymentType) { error in 
                 if let error = error {
                   completion(PKPaymentAuthorizationResult(status: .failure, errors: [error]))
                 } else {
                   completion(PKPaymentAuthorizationResult(status: .success, errors: nil))