Integrating Apply Pay on iOS

By Brian Telintelo on 25 Mar 2017
  • iOS

Back in the early 17th century, people in England stored their gold with the royal mint.  But King Charles, needing funds, raided the gold in the royal mint as a “loan”.  He took the gold along with the country’s pepper stock to fund the royal crown.   A few years later he was executed for treason.  After that incident, individuals began storing gold with goldsmiths.  The goldsmiths had private, secured vault’s where individuals could feel confident about their golds safekeeping.  This was the beginning of the banking industry.

Today we have a similar problem: credit cards are prone to fraud and failure.  Last year alone, 16 billion dollars was lost to fraud and theft of credit cards, not to mention billions more being spent to prevent fraud.  With consumers wanting virtual wallets on their smartphones, the need for alternative payment methods began to form.

Enter Apple Pay

Apple pay enables payments for goods and services like traditional credit cards, but with some unique advantages.

  • More Secure Payments - No one other than Apple has a record of your credit card number
  • Digital Wallets - No longer need to carry a credit card which could be lost or stolen
  • Payments made quicker - with a single touch, reducing friction for consumer purchases

Many retailers have already jumped on board with Apple Pay.  Consumers will begin to make buying decisions based on retailers that offer forms of payment that please them.  Starting with the iPhone 6 and 6+, consumers could take advantage of the NFC chip to make payments at available retail outlets.

At the time of this post, Apple Pay has now been introduced nearly two years ago.  So why write about Apple Pay now?  Because now app developers are finally seeing mainstream acceptance of these forms of payments.  It has taken some time for companies and hardware manufacturers to react to Apple’s announcement.  Now the building blocks are in place, developers and companies companies of all sizes should make a priority to implement these new an alternative payment methods for their customers.

Now to have a look on how to go about implementing Apple Pay in an app.

Apple Pay in an iOS app

Log in to the Apple developer portal to complete the Apple Pay app setup steps.

  1. Create a new App ID
  2. Enable Apple Pay service
  3. Create a merchant ID

Create Merchant ID

Enable Apple Pay in Xcode

Enable Apple Pay

Apple Pay Assets

Apple Pay has branding requirements to for uses in an app.  Apple provides photoshop graphics to identify Apple Pay payment methods within an app.  Or you can just use the built in Apple Pay button with the PassKit framework:

PKPaymentButton(type: PKPaymentButtonType.Buy, style: PKPaymentButtonStyle.Black)
Apple Pay Button

Getting Money From A to B

Now that the app is configured for Apple Pay, it needs to get money from point A(the customer) to point B(the merchant).  Here is a diagram that shows how an order and payment will occur.

Payment Process

Payment Processors

To make the transaction of money from A to B, there needs to be a payment provider.  There are many payment providers that developers can integrate with their API to make these transactions happen. A few popular ones are:

  • Stripe
  • Braintree Payments
  • Square
Each offer unique benefits, for example Braintree gives you your first 50k in transactions free - a nice starter option for smaller apps just beginning the payment process.  Square offers more physical hardware to small businesses to help them get started quickly with mobile payments and product inventories.  Square also has an API for developers.  Stripe claims to be a mobile and web payment processing provider built for developers which is a good sign for getting something done quickly.

How to use a payment processor

The steps to implement payment processors within a mobile app are pretty similar across providers.

Step 1: Client Token

The first step for the client mobile app is to request a client token from the backend server.  The backend server has a key and uses a provided SDK to generate a token.

Step 1: Client Token

Step 2: Use Client SDK to Add a Payment Method

Now that the client has an authorized client token from the application server, the client can use that token to create a payment method with the payment provider.  For a credit card transaction, this means sending the credit card number, expiration date, name, and cvv number over to the payment provider.  For Apple Pay, there are a few extra steps that have to be done to integrate with apple to make this happen.

Check to see if iPhone has Apple Pay Capability

if PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks([PKPaymentNetworkVisa, 
         PKPaymentNetworkAmex]) { 
     //show/allow Apple Pay Button

Make an Apple Pay payment request

func paymentRequest() -> PKPaymentRequest { 
     let paymentRequest = PKPaymentRequest() 
     paymentRequest.merchantIdentifier = "<#YOUR_APPLE_PAY_MERCHANT_ID#>"; 
     paymentRequest.supportedNetworks = [PKPaymentNetworkAmex, PKPaymentNetworkVisa, PKPaymentNetworkMasterCard]; 
     paymentRequest.merchantCapabilities = PKMerchantCapability.Capability3DS; 
     paymentRequest.countryCode = "<#COUNTRY_CODE#>"; // e.g. US 
     paymentRequest.currencyCode = "<#CURRENCY_CODE#>"; // e.g. USD   
     // Add payment summary items... 
     paymentRequest.paymentSummaryItems = [PKPaymentSummaryItem(label: "<#ITEM_NAME#>", amount: NSDecimalNumber(string: "<#PRICE#>")), 
          PKPaymentSummaryItem(label: "<#COMPANY_NAME#>", amount: NSDecimalNumber(string: "<#GRAND_TOTAL#>"))] 
     return paymentRequest 

Show the Apple Pay Screen to the User for Authorization

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

When the user accepts the purchase, appropriate delegate methods( from PKPaymentAuthorizationViewControllerDelegate) will be called.

Sending the Apple Pay payment token to the Payment Processor

So now that the user has accepted the Apple Pay payment, we need to send the resulting token to the payment processor.  The payment processor will respond with a token of its own, which we can then send to the application server for any further reference.  For example, say we need to refund the purchase.

Step 2: Sending the token


No matter which payment provider you choose to accept mobile payments, the steps are very similar for accepting credit cards or Apple Pay within your app.  By accepting Apple Pay, you can open up new avenues for customer payments without requiring them to enter credit cards or expecting them to implicitly trust where the credit card number is going.  Apple Pay is continuing to gain traction and it will be more important to consumers to support this new an emerging technology.

Join Our Newsletter

Stay up to date with the latest mobile app dev news, technologies and events with Atomic Robot.