How do I integrate Apple Pay in Swift?

Integrating Apple Pay in a Swift application allows users to make secure payments using their Apple devices. This guide provides a step-by-step example to help developers incorporate Apple Pay into their apps seamlessly.

import PassKit class PaymentViewController: UIViewController { var paymentRequest: PKPaymentRequest { let request = PKPaymentRequest() request.merchantIdentifier = "your.merchant.identifier" request.supportedNetworks = [.visa, .masterCard, .amex] request.merchantCapabilities = .capability3DS request.countryCode = "US" request.currencyCode = "USD" request.paymentSummaryItems = [ PKPaymentSummaryItem(label: "Product Name", amount: NSDecimalNumber(string: "10.00")) ] return request } func startPayment() { if PKPaymentAuthorizationViewController.canMakePayments() { let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) paymentVC.delegate = self present(paymentVC, animated: true, completion: nil) } else { print("Apple Pay is not available.") } } } extension PaymentViewController: PKPaymentAuthorizationViewControllerDelegate { func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) { // Handle payment processing completion(.success) } func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) { controller.dismiss(animated: true, completion: nil) } }

Apple Pay Swift integration iOS payments secure payments PKPaymentRequest mobile payments merchant capabilities