Integrating Apple Pay in iOS App


Apple pay is a new way of mobile payment technology. It is easy to use and secure to make payments.

To set up an iOS app for Apple Pay we need to follow below steps.

-Create new App Id
*Login to Apple developer account and select Certificate, Identifiers, and Profiles from the left pane.
*Select App IDs from Identifiers section. Tap on Add button (+)  to create new App ID. Make sure to select Explicit App Id because Apple Pay does not work with wildcard App IDs.

-Register a merchant ID
*Select Merchant IDs in Identifiers section. Tap on Add button (+) located in the upper-right corner.
*Enter the description, identifier, and tap on Continue. Merchant IDs are created in a similar way as Bundle ID with reverse domain style.
*Tap on Register and then Done.

-Create payment processing certificate
*Select the merchant I’d created above and tap on Edit
*Tap on Create certificate in Payment Processing Certificates section. Follow the steps to obtain certificate signing request(CSR) and tap on Continue.
*Tap on Choose File, select your CSR, and tap on Generate.
*Download the certificate and tap on Done.

-Enable Apple Pay in Xcode
*Create a new Single View Application in Xcode. Update the bundle Identifier to the one created above.
*Open Capabilities tab in Project target settings. Enable Apple Pay and select the merchant Id created above. Select the checkbox next to the merchant ID as shown in below image.



Now the Apple Pay has been enabled in the project. To start with we can have a simple app with list of items to be purchased. Next step is to add Apple Pay Button and set up payment requests.

Apple pay makes use of the PassKit framework. So include this in the project and import it in required classes by adding below the line at the top.

Import Passkit

Adding Apple Pay button
Create a button instance programmatically or through storyboard in the required view controller class. Make sure the apple pay button adheres to the guidelines mentioned in You can also create a button instance using PKPaymentButton. Below is the screenshot from simulator preview for reference.



Creating Payment Request

Payment requests are created using instances of PKPaymentRequest class. A request object consists of a list of items such as available shipping methods, information about the merchant and payment processor.

Let supportedNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkMasterCard]
We can check whether the device is able to handle the payments and accordingly hide or show the apple pay button by calling canMakePaymentsUsingNetworks: method of PKPaymentAuthorizationViewController class
payButton.hidden = !PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks(supportedNetworks)

// creating request object and presenting apple payment sheet
let merchantId = “” // the one created in the above step
let paymentRequest = PKPaymentRequest()
paymentRequest.merchantIdentifier = merchantId
paymentRequest.supportedNetworks = supportedNetworks
paymentRequest.merchantCapabilities = PKMerchantCapability.Capability3DS //(EMV, Credit, Debit)
paymentRequest.countryCode = “US”
paymentRequest.currencyCode = “USD”
paymentRequest.requiredShippingAddressFields = PKAddressField.PostalAddress | PKAddressField.Phone //Add the required shipping address field

//Creating shipping method
let shippingMethod = PKShippingMethod(label: “Free Shipping”, amount: NSDecimalNumber(double: 0.00))
shippingMethod.identifier = “free”
shippingMethod.detail = “Arriving by tomorrow”
paymentRequest.shippingMethods = [shippingMethod]

//creating payment summary
let item = PKPaymentSummaryItem(label: item.title, amount: item.price) //the item here indicates the one selected from the list
let shippingCharge = PKPaymentSummaryItem(label: shippingMethod.label, amount: shippingMethod.amount)
let itemTotal = item.price.decimalNumberByAdding(shippingMethod.amount)
let total = PKPaymentSummaryItem(label: “items”, amount: itemTotal)

paymentRequest.paymentSummaryItems = [item, shippingCharge, total]

let paymentController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
paymentController.delegate = self
self.presentViewController(paymentController, animated: true, completion: nil)

PKPaymentAuthorizationViewController is used for displaying the Apple pay payment sheet. Below is the image from apple developer site which shows the apple payment sheet.


Setting Up the delegate to handle Payment Requests

To listen to the callbacks while processing the information, add the delegate to the receiving class as below
class ItemsViewController: UIViewController,PKPaymentAuthorizationViewControllerDelegate

We can handle the Payment completion status using below delegate method. In this method the payment request object is sent to the backend server for further processing.

func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {
The below delegate method is called when the payment request is completed. At this point the view controller(ie the apple payment sheet) can be dismissed.
func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController!) {
controller.dismissViewControllerAnimated(true, completion: nil)
Below is the optional delegate which is called when the user changes shipping method if there are multiple options provided. We can implement this delegate to re-calculate the shipping cost and item total.
func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController,
didSelectShippingMethod shippingMethod: PKShippingMethod,
completion: @escaping (PKPaymentAuthorizationStatus, [PKPaymentSummaryItem]) -> Void)

By following all the above steps it is easy to integrate Apple Pay and make online transactions.





about the author


  1. Subhash R.P

    May 4, 2017

    Hi, can we have Apple Pay integration in India?

    • Rahul Vyas

      May 19, 2017

      No, It currently supports only 4 countries