During the presentation on the 9th of September 2014, Apple introduced Apple Pay — its own system of mobile payments.
Apple pay allows iPhone 6 and iPhone 6+ users, as well as those who have new versions of Apple Watch easily shop online now, and benefit from apple pay app integration as well as make payments with the help of NFC (Near Field Communication) Technology. Touch ID or Face ID technologies are used for payment authorization.
How to Set Up Apple Pay in The App
To set up your environment to implement Apple Pay in your apps, you must complete these steps:
- Create a Merchant ID
- Configure Apple Pay capabilities in Xcode for your project
- Create a sandbox user in iTunes Connect
- Add a test card
- Create a payment request in your project
- Handle the result
Step 1: Create a Merchant ID from your Apple Developer Account.
Steps 2: Configure Apple Pay capabilities in Xcode for your project
Steps 3: Create a sandbox user in iTunes Connect
Login this sandbox account in the device or simulator.
Steps 4: Add a test card in the account
Please click here for demo card details.
Steps 5: Create a payment request for the project.
First, create UI For apple pay.
Import the PassKit Library in ViewController
1 |
import PassKit |
Create a Payment Request in the ViewController
1 2 3 4 5 6 7 8 9 10 11 |
private var paymentRequest: PKPaymentRequest = { let request = PKPaymentRequest() request.merchantIdentifier = "merchant.com..." request.supportedNetworks = [.visa, .masterCard,.amex,.discover] request.supportedCountries = ["UA"] request.merchantCapabilities = .capability3DS request.countryCode = "UA" request.currencyCode = "UAH" request.paymentSummaryItems = [PKPaymentSummaryItem(label: "App test", amount: 10.99)] return request }() |
Handle the Apple Pay button action and make payment request
1 2 3 4 5 6 |
@IBAction func purchase(_ sender: Any?) { if let controller = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) { controller.delegate = self present(controller, animated: true, completion: nil) } } |
Steps 6: Handle the result
Confirm the PKPaymentAuthorizationViewControllerDelegate in the View Controller
1 2 3 4 5 6 7 8 9 10 11 |
extension ViewController: PKPaymentAuthorizationViewControllerDelegate { func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) { completion(PKPaymentAuthorizationResult(status: .success, errors: nil)) } func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) { controller.dismiss(animated: true, completion: nil) } } |
Now run your app.
In this way, you can integrate Apple pay in your app. I hope you understand the whole process. If you have any queries or suggestions please comment below.
Thank you