Apple Pay lets you use iPhone 6 to pay in stores and within apps in an easy, secure, and private way. There are two ways you can use Apple Pay to purchase items: at the point of sale using Near Field Communication (NFC), or within an iOS app. This tutorial will teach you how to allow your users to pay with apple pay through the app.

First off we need to setup some basics in the Apple Developer Portal :

  • Create an App ID
  • Give it a Bundle ID similar to that of your project
  • Make sure that Explicit App ID is selected (wildcard App IDs aren’t able to make payments with Apple Pay)
  • Check the Apple Pay checkbox in App Services
  • Create Merchant ID

Once this has been completed, you will need to go into your Project Target settings and turn on Apple Pay under the Capabilities Tab. Once turned on you will see the Merchant ID you just created. If you don’t see the Merchant ID, just select the refresh button so that Xcode can repopulate the list. Now your app is setup to accept Apple Pay.

Apple Pay Settings

To follow along with this tutorial, open the View Controller that will have the Apple Pay Button. In that View controller, we will need to import the PassKit framework so add the following line at the top:

    import PassKit

Next you must add the following under the IBOutlets:

    let SupportedPaymentNetworks = [ PKPaymentNetworkMasterCard,
        PKPaymentNetworkVisa, PKPaymentNetworkAmex]
    let merchantID = "merchant.com.YOURMERCHANT.ID" //Use Merchant ID you created above

Create a PKPaymentRequest

Now that we have the import and the settings, we need to add a button on the view controller and link it to an IBAction that we will call purchase. Apple has some guidelines and a wide range of buttons, that can be used as a button, over on their website(https://developer.apple.com/apple-pay/). Now that the button is setup we need to place the following code inside of the purchase method:

    let request = PKPaymentRequest()
    request.merchantIdentifier = merchantID
    request.supportedNetworks = SupportedPaymentNetworks
    // This property constrains the payment cards that the user can select to fund the payment.
    request.merchantCapabilities = PKMerchantCapability.Capability3DS
    request.countryCode = "US"
    request.currencyCode = "USD"
    let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)
    applePayController.delegate = self
    self.presentViewController(applePayController, animated: true, completion: nil)

This code creates a basic PKPaymentRequest with your Merchant ID, the networks you want to support(Visa, AMEX, MasterCard), and what country and currency. It then passes it into a new PKPaymentAuthorizationViewController which loads the standard Apple Pay modal. The final line then presents that view controller.

Now that we have the view controller ready, we need to send it a list of items so that we can show the user a list of what they are buying. To do this we will pass in an array of summary items. Add this code before initializing the applePayController:

    request.paymentSummaryItems = [
        PKPaymentSummaryItem(label: "Sticker", amount: 5.0),
        PKPaymentSummaryItem(label: "Make & Build", amount: 5.0)
    ]

The last line in paymentSummaryItems array is how much will actually be charged and the label will have PAY TO added before it.

PKPaymentRequest Delegates

The final step to getting the Apple Pay view controller is to setup the delegates that will handle the interactions. To do this you must add the following required methods of PKPaymentAuthorizationViewControllerDelegate in your view controller:

    extension YourViewController: PKPaymentAuthorizationViewControllerDelegate {
        //change this to your view controller
        func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!,
                didAuthorizePayment payment: PKPayment!, 
                completion: ((PKPaymentAuthorizationStatus) -> Void)!) {
            //This handles the user authorization to complete the purchase.
            completion(PKPaymentAuthorizationStatus.Success)
        }
        func paymentAuthorizationViewControllerDidFinish(controller:
                PKPaymentAuthorizationViewController!) {
            //This is called when the payment request is completed.
            controller.dismissViewControllerAnimated(true, completion: nil)
        }
    }

Common Questions:

Which Payment Providers support this service? Here are some of the solution providers you can start working with today.*

Which payment networks are supported?
Visa, MasterCard, and American Express

Which card types are supported?
Both credit and debit cards from the major issuing banks are supported.

How much does it cost to accept Apple Pay?
Apple does not charge users, merchants or developers to use Apple Pay for payments. Your credit and debit transactions will continue to be handled by the payment networks.

Conclusion

You are officially done setting up the basic Apple Pay View. The current configuration gives an automatic confirmation but is still not permitted to use Apple Pay because you still need to setup your Payment Provider. To complete the setup you need to follow the instructions of your payment processor. Apple Pay is just getting started, but seems to be very powerful. Not only will you be able to pay in-app but you will also be able to pay in store with NFC hardware if you have iPhone 6, 6+, or the new Apple Watch being announced on monday.