BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Apple pay doesn't trigger delegate

Forums > SwiftUI


Hello I've wrote, taking example from internet, a simple apple pay integration to my app. I've successful complete the Xcode configuration adding apple pay capability, then I've configured all apple store profile with certs and identifier reporting this to xcode.

This is my class code:

class applePay: NSObject {
  func payWithApplePay( total: Double) {

        // Create a payment request and add all data to it
        let paymentRequest = PKPaymentRequest()
        paymentRequest.merchantIdentifier = ""
        paymentRequest.merchantCapabilities = .capability3DS // A security protocol used to authenticate users
        paymentRequest.countryCode = "US"
        paymentRequest.currencyCode = "USD"
        paymentRequest.supportedNetworks = [.visa, .masterCard, .amex]
        paymentRequest.shippingType = .delivery
        paymentRequest.requiredShippingContactFields = [.name, .postalAddress]

        paymentRequest!.paymentSummaryItems =
                    PKPaymentSummaryItem(label: "Total Amount", amount: NSDecimalNumber(string: String(format: "%.2f", total)))
        // Display the payment request in a sheet presentation
        let paymentController = PKPaymentAuthorizationController(paymentRequest: paymentRequest!)
        paymentController.delegate = self
        paymentController.present(completion: nil)

extension applePay: PKPaymentAuthorizationControllerDelegate{    

    func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController,
                             didAuthorizePayment payment: PKPayment,
                                      handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
        print("test apple pay successful")

    func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
        // Apple Payment finished
        print("test apple close")
        controller.dismiss(completion: nil)

This is how I show the view


It's seem that everything works well, I can set shipping detail, changing cart an so on, but I can't complete payment or closing the view. It is like no delagate is called from the PKPaymentAuthorizationController. I think that my code is right I've tested it on a minimal xcode project and delegate works well, so I conclude that there is something wrong in my app. Where I should check to resolve my issue? Is there a way to avoid delegates with apple pay? Regards



Ok if you are in trouble with same problem here the solution that worked for me. The problem is in the way I use to call the view


Launch the PKPaymentController in such way, make the delegate unable to trigger so here the solution

You need to create a "persistent" variable that survive to launch itself

let paymentHandler = applePay()

and then launch controller


Good coding


Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.