Initial Setup for Apple Pay
- Ensure your app's entitlements include "Apple Pay" by configuring the app's capabilities in the Xcode project settings.
- Implement a Merchant ID within your developer account settings, and connect it to your app by configuring your Apple Pay capabilities.
Create the Payment Request
- Import the necessary frameworks in your Swift file: `PassKit` and `UIKit`.
- Start by creating a PKPaymentRequest object. This object will contain all necessary information about the transaction, such as supported networks (Visa, MasterCard, etc.), the required contact fields, and the total amount to be charged.
import PassKit
import UIKit
let paymentNetworks = [PKPaymentNetwork.amex, .masterCard, .visa]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
let paymentRequest = PKPaymentRequest()
paymentRequest.currencyCode = "USD"
paymentRequest.countryCode = "US"
paymentRequest.merchantIdentifier = "your.merchant.identifier"
paymentRequest.supportedNetworks = paymentNetworks
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.requiredShippingContactFields = [.postalAddress, .name]
paymentRequest.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Cool Product", amount: NSDecimalNumber(decimal: 99.99))
]
}
Present Payment Sheet
- Check if the device can make payments with `PKPaymentAuthorizationViewController`. If it can, instantiate this view controller with your `PKPaymentRequest` object and present the payment sheet to the user.
- Set `self` as the delegate to handle user interaction with the payment sheet.
if let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) {
paymentVC.delegate = self
present(paymentVC, animated: true, completion: nil)
}
Handle Payment Authorization
- Conform to the `PKPaymentAuthorizationViewControllerDelegate` protocol by implementing its methods to handle payment authorization, cancellation, and completion events.
- The primary method, `paymentAuthorizationViewController(_:didAuthorizePayment:completion:)`, handles details of the authorized payment. Use this method to send payment data to your server for processing.
extension YourViewController: PKPaymentAuthorizationViewControllerDelegate {
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
// Process the authorized payment here
// For demo purposes, let's always succeed
let status = PKPaymentAuthorizationStatus.success
completion(PKPaymentAuthorizationResult(status: status, errors: nil))
// Optionally, communicate with your server to process the payment
// sendPaymentToServer(payment)
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
dismiss(animated: true, completion: nil)
}
}
Test and Debug
- Ensure to conduct tests on a device compatible with Apple Pay. Simulators do not support Apple Pay functionalities.
- Use the sandbox environments provided by Apple for testing transactions without incurring actual charges.