Apple Pay iOS Implementation

How to implement Apple Pay in your iOS application through Merchant Warrior. This is a living document, and subject to change

Prerequisites

Before you start:

  • Please ensure you have an Apple Developer Account which is enrolled in either the Apple Developer Program or the Apple Developer Enterprise Program.
  • In your Developer account, you should have proper permission to register Apple Merchant ID and related certifications.
  • For testing the full feature of native iOS ApplePay, you need to have a physical device and have an existing card in your Apple Wallet.

Step 1: Create a Merchant Identifier

  1. In Certificates, Identifiers & Profiles, select Identifiers from the sidebar, then click the Add button in the upper-left corner.
  2. Select Merchant IDs, then click Continue.
  3. Enter the merchant description and identifier name, then click Continue.
  4. Review the settings, then click Register.

NOTE: Make sure your identifier name follows this format:

  • Production: merchant.com.merchantwarrior.<merchantUUID>.prod
  • Sandbox: merchant.com.merchantwarrior.<merchantUUID>.sandbox

Step 2: In Apple Developer Account - Apple Pay Payment Processing Certificate

  1. In Certificates, Identifiers & Profiles, select Identifiers from the sidebar.
  2. Under Identifiers, select Merchant IDs using the filter in the top-right.
  3. On the right, select your merchant identifier.
  4. Upload CSR file
  5. Click Continue.

NOTES:

  1. You can download the CSR file in Barracks - Setting tab, Digital Wallet Section
  2. Updating an existing Apple Pay Processing Certificate will take approximately 10 ~ 15 minutes to come into effect.
  3. Whenever you download a new CSR in Barracks, the old private key will be removed and a new private key will be associated with your merchant account.

Step 3: Integrate Merchant Identifier with XCode

In XCode project settings section, click the Signing & Capabilities tab, and add the ApplePay Capability.

XCode

Merchant Warrior iOS SDK Integration

The easiest way to integrate Apple Pay is by using the Merchant Warrior iOS SDK; a simple interface designed to streamline PassKit for use with Merchant Warrior. For more information on integrating without the SDK, we have manual integration instructions as well.

The Merchant Warrior iOS SDK is available here.

A sample project using the Merchant Warrior iOS SDK is available here. This is adapted from the official Apple sample project.

Steps

  1. Download the SDK.

  2. Import PassKit and the SDK into your application.

import PassKit
import MerchantWarrior
  1. Create a MWPaymentHandler using your Merchant Warrior UUID and API Key, your Apple Merchant Identifier, and a UIViewController. For use with our testing environment, also set environment to "sandbox".
// Production
let MWPay = MWPaymentHandler(MWuuid: "MWABCD1234", apiKey: "abcd1234", uiViewController: self)

// Development
let MWPay = MWPaymentHandler(MWuuid: "MWABCD1234", apiKey: "abcd1234", environment: "sandbox",  uiViewController: self)
  1. Create an array of PKPaymentSummaryItem's to be associated with a purchase. Please note that the final PKPaymentSummaryItem in the array is considered to be the grand total for the transaction.
let fare = PKPaymentSummaryItem(label: "Minimum Fare", amount: NSDecimalNumber(string: "9.99"), type: .final)
let tax = PKPaymentSummaryItem(label: "Tax", amount: NSDecimalNumber(string: "1.00"), type: .final)
let total = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: "10.99"), type: .pending)
let paymentSummaryItems = [fare, tax, total]
  1. Create a MWPaymentButton with a success or failure callback. This button is set up to automatically send your token to Merchant Warrior for processing.
// Create through MWPaymentHandler
var button = MWPay.createButton(paymentSummaryItems: paymentSummaryItems) { (success) in
  if (success) {
   // Handle Transaction Success
  } else {
   // Handle Transaction Failure
  }
}

// Create manually
button = new MWPaymentButton(handle: MWPay, paymentSummaryItems: paymentSummaryItems) { (success) in
  if (success) {
   // Handle Transaction Success
  } else {
   // Handle Transaction Failure
  }
}

If ApplePay is not available on a user's device, MWPaymentHandler.createButton() will return nil. If the user has not set up their wallet, the button will automatically display "Set Up Apple Pay" rather than "Pay With Apple Pay". The .canMakePayments and .canSetupCards properties on the MWPaymentHandler item are accessible to check prior to attempting to set up a button.

let MWPay = MWPaymentHandler(MWuuid: "MWABCD1234", apiKey: "abcd1234", uiViewController: self)

if (MWPay.canMakePayments || MWPay.canSetupCards) {
 // Apple Pay is available on this device
}

The MWPaymentButton class has a public property onPress which can be set to a function that will run when the button is pressed. If this function returns false, the payment process will not begin. This also provides an opportunity to set the paymentSummaryItems value after instantiation of the payment button.

// Add paymentSummaryItems at a later point
var button = MWPay.createButton() { (success) in
 if (success) {
 // Handle Transaction Success
 } else {
 // Handle Transaction Failure
 }
}

button.onPress = {
 if (cannotStartPayment) { 
   // If the payment can't start, return false
   return false
 }

 let fare = PKPaymentSummaryItem(label: "Minimum Fare", amount: NSDecimalNumber(string: "9.99"), type: .final)
 let tax = PKPaymentSummaryItem(label: "Tax", amount: NSDecimalNumber(string: "1.00"), type: .final)
 let total = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: "10.99"), type: .pending)
 button.paymentSummaryItems = [fare, tax, total]

 return true // Must return true to proceed with payment
}
Manual Integration

Integrate Apple Pay manually into your app using Apple PassKit

For direct use of PassKit, examining the ApplePay Sample Code Project and its associated documentation will give a clear idea of how to integrate with your app. In the section containing the comments

// Here you would send the payment token to your server or payment provider to process
// Once processed, return an appropriate status in the completion handler (success, failure, etc)

You will need to send us your token in a Digital Wallet processCard request.

MWPaymentHandler

MWPaymentHandler has one method, createButton, which is used to create an MWPaymentButton.

Constructor

Parameter Description
MWuuid

Your Merchant Warrior UUID.
Example: 123456789abcd

apiKey

Your Merchant Warrior API Key.
Example: 1a3b5c

merchantIdentifier

Your Apple Merchant Identifier.
Example: com.example.test

uiViewController

The UIViewController being used in your app.

createButton

Parameter Description
type

The PKPaymentButtonType to display. Defaults to .buy or .setUp, depending on if the user has already set up a card.
Example: .order.
This parameter is optional

style

The PKPaymentButtonStyle to display. Defaults to .white.
Example: .black
This parameter is optional

paymentSummaryItems

An array of PKPaymentSummaryItems to attach to the button. Please note that the final PKPaymentSummaryItem in the array is considered to be the grand total for the transaction.

completion

The callback for the completion of the transaction, in the format (Bool) -> Void. Boolean value represents success or failure.

MWPaymentButton

MWPaymentButton is an extension of PKPaymentButton which links with an MWPaymentHandler to complete and process an Apple Pay transaction.

The MWPaymentButton has no methods attached to it, it does have two properties.

Constructor

Parameter Description
handler

An MWPaymentHandler instance.

paymentButtonType

The PKPaymentButtonType to display. Defaults to .buy or .setUp, depending on if the user has already set up a card.
Example: .order.
This parameter is optional

paymentSummaryItems

An array of PKPaymentSummaryItems to attach to the button. Please note that the final PKPaymentSummaryItem in the array is considered to be the grand total for the transaction.

completion

The callback for the completion of the transaction, in the format (Bool) -> Void. Boolean value represents success or failure.

Properties

Property Description
paymentSummaryItems

An array of PKPaymentSummaryItems to attach to the button. Please note that the final PKPaymentSummaryItem in the array is considered to be the grand total for the transaction.

onPress

A function called prior to the start of the Apple Pay process, used to confirm or cancel the transaction or set paymentSummaryItems after the creation of the MWPaymentButton. Return false to cancel the transaction.