Integration of Apple Pay-Swift

Apple Pay is provided by Apple to get the amount from end-user for their order in an easy, robust, efficient manner. Its integration is easy for the developer too.

Before the Integration of Apple Pay in our iOS mobile application, we need to understand the user guidelines of Apple.

Why did we need to integrate?
When we get a requirement to get the money from the user they purchased from our Mobile application then we need to add the payment gateway to our app so that we can collect the money from the user’s bank account to our merchant account. Once we know that we need to integrate the payment gate to get this done then the next question is what we are sold to the user or what user going to be purchase from our Mobile Application. It may be a physical purchase or it may be a Digital purchase. If it’s a physical purchase then we can collect the amount using any 3rd party payment gateways as per the project requirement, documentation, or client concern. But, If it’s a Digital purchase then we need to integrate In-App purchase as per the Apple guidelines. In-App purchase is a different story we will come back to this in another story. Till now we understand that we need to integrate the 3rd party payment gateway If our mobile application collects the money for the Physical purchase. Now the real requirement of the Apple Pay integration starts. Apple says that if you integrate any 3rd party payment gateway to get the money from the user for their physical purchase then you need to integrate the Apple Pay in your app as an option to the user with the 3rd party payment gateway.

Note: Some 3rd party payment gateways already provide Apple pay with their payment gateway we need to just configure it as per their guidelines.

Implementation Now
Requirements:
1. You need a paid Apple Developer Account
2. Create New project in your latest Xcode and fill in all the details as per your requirement
3. Select the Bundle Id from the project target and copy it
4. Go to your Apple Developer account
5. Select Certificate and click on the (+)plus icon to create a certificate
6. Select Apple Development and create a CSR from your MAC, Upload it and continue
7. Go to Identifier and (+) plus, create a new identifier
8. App Id and Continue, Select your app and continue
9. Paste your Bundle Id as you copy in point(3) and Enable Apple Pay Payment Processing
10. Go to your Xcode project, Select Target, Go to Signing and Capabilities
11. Click (+) and search for Apple Pay and add the Merchant Key

Coding
import Passkit

let payableItem = PKPaymentSummaryItem.init(label: “addyourProductName”, amount: “addProductPrice”)

let network = [PKPaymentNetwork.amex] — — → add or delete network in this array as per your project requirement

Create a Apple Pay request
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: network) {

let paymentRequest = PKPaymentRequest()
paymentRequest.currencyCode = “Currency Code”
paymentRequest.countryCode = “Country Code”
paymentRequest.merchantIdentifier = “Merchant Id”
paymentRequest.merchantCapabilities = PKMerchantCapability.capability3DS
paymentRequest.supportedNetworks = network
paymentRequest.paymentSummaryItems = [payableItem]

guard let vc = PKPaymentAuthorizationViewController(paymentRequest:paymentRequest) else { return}
yourViewconteoller.delegate = self
self.present(vc, animated: true, completion: nil)} else {
//Show alert message to the user }

// Add Delegate methods to your controller

extension yourViewcontroller: PKPaymentAuthorizationViewControllerDelegate {

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {}

func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {

}}

Add the following code in your project and see the changes

Happy Coding!!
Thank you

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store