Apple Pay Integration in iOS using swift

1. First step would be to get the Apple Merchant Id.

a)?Login to your Apple Developer Account.

b) Go to “Certificates, Identifiers & Profiles”. Choose iOS, tvOS, watchOS from the Navigation menu on the left.

  • Select Merchant Id section in the same left menu.
  • Cliquez sur "+" bouton.

apple-merchant-id

c) A new tab will open. Add Merchant Id description and a unique Identifier and continue?until the same is added. Save the Merchant Id you created.

merchant-id

2. Payment Processing Certificate:

a) Tap on the merchant ID that you created earlier. A tab will open up, select “Edit” option.

b) A new tab will open. Tap on Create Certificate.

paiement-traitement

c) Open Keychain Access in you Mac using “cmd + Space”.
d) Request a new certificate from authority.

porte-clé-accès

e) Fill the Email and Common Name fields. Check “Save to disk” and “Let me specify Key Pair Information”. Click continue.

certificate-information

f) Select Algorithm to “ECC” and Key Size to “256 bits”. Click Continue.

g) Continue with step 2. Upload the certificate that you have created.

h) After upload is complete, please download the Apple generated certificate.

download-certificate

3. Configure Apple Pay :

a) Setup Apple pay in Xcode project.

tourte aux pommes

4. Add below line into podfile and install pods.

# Pods for Apple Pay Demo
pod ‘Stripe/ApplePay’

5. After pod installation, make sure that you are using “project.xcworkspace”.
Remarque: Please don’t use “project.xcodeproj”

6. Add below code at the top of your Controller class to import pod libraries. There are chances that you don’t find these SDKs in suggestion. Don’t worry it will take some time. Or you can type the code manually.

import Stripe
import PassKit

7. Extend your view controller class with “PKPaymentAuthorizationViewControllerDelegate” class.

class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate

8. Add below code in your controller class.

var paymentRequest: PKPaymentRequest!
var transactionId = “”
var status = “”
var amount = 100

/**
* This function is called on tap of ApplePay Payment option in Payment Page.

* **Author:** Asharma

* **Date:** 01-Oct-2018
*/
func setupApplePay()
{
paymentRequest = PKPaymentRequest()
paymentRequest.currencyCode = “USD”
paymentRequest.countryCode = “US”
paymentRequest.merchantIdentifier = “Your Merchant Id”

// Payment networks array
let paymentNetworks = [PKPaymentNetwork.amex, .visa, .masterCard, .discover]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks:? ? ? ? paymentNetworks)
{
paymentRequest.supportedNetworks = paymentNetworks
paymentRequest.merchantCapabilities = .capability3DS

let item = PKPaymentSummaryItem(label: “Order Total”, amount: NSDecimalNumber(string: “\(amount)”))

paymentRequest.paymentSummaryItems = [item]

let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
applePayVC.delegate = self
self.present(applePayVC, animated: true, completion: nil)
}
else
{
// Notify the user that he/she needs to set up the Apple Pay on the device
// Below code calls a common function to display alert message. You can either create an alert or? can just print something on console.
CommonMethods.displayAlert(message: “Apple Pay is not available on this device.”, viewController: self)
print(“Apple Pay is not available on this device”)
}
}

/**
* This function is called when Apple Pay payment will be authorized.

* **Author:** Asharma

* **Date:** 01-Oct-2018
*/
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void)

{
// Payment token can be found like this
print(payment.token)
// Get response from the server and set the PKPaymentAuthorizationStatus
let status = PKPaymentAuthorizationStatus(rawValue: 0)!

self.transactionId = payment.token.transactionIdentifier

switch status.rawValue {
case 0:
self.status = “approved”
// perform Functionality on Apple Pay Successfull Payment
default:
self.status = “failed”
}

}
/**
* This function is called when Apple Pay payment authorization is finished.

* **Author:** Asharma

* **Date:** 01-Oct-2018
*/
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController)

{
self.dismiss(animated: true, completion: nil)
}

9.?You can call the function setupApplePay() whenever you want to start Apple Pay transaction. Either you can call the same function in viewDidLoad() or you can call this function on tap of a button.

Example: In case of button tap, you need to add a button in UI, draw IBAction of button. IBAction is a function that will be called on a tap of the button from which is referenced. In this action, you can call this function.

@IBAction func onTapBtnPay(_ sender: Any) {
setupApplePay()

}

Apple Pay Sandbox Testing

1. Create sandbox tester account.

a) Login to https://appstoreconnect.apple.com/
b) Select Users and Access.

sandbox-testing

c) Go to testers section in left navigation Menu. Then click on “+” button.

user-access

d) A popup will appear. Please fill that form.
Note: Please use mail that is not registered with Apple.

pop-up

f) Sign out of your Apple ID on all testing devices and sign back in with your new sandbox tester account.

2. Adding a test card number :

a) Make sure you have logged out from iCloud and iTunes.
b) Sign into your test device using the sandbox tester account you created.
c) Add a sandbox card.
Example :
FPAN: 4761 1200 1000 0492
Expiration Date: 11/2022
CVV: 533

You can find more cards here.

Note : The issue you might face that card will not be added. It will shoe some error. Possible resolution maybe – you might have not logged out of device completely. Please logout form iCloud and iTunes.

d) After adding the card, you can proceed with Apple Pay transaction.

Create your own eCommerce mobile app with Knowband’s?constructeur d'applications mobiles?and integrate PayU just by following these simple steps.

GTranslate Your license is inactive or expired, please subscribe again!

Akash Sharma

Akash Sharma

I'm Akash Sharma, an engineering graduate and software developer by profession. Challenges fascinate me and I like solving them.

Leave a Reply

Your email address will not be published. Required fields are marked *