NEW: Learn SwiftData for free with my all-new book! >>

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


@PGF, thanks for sharing your solution. It helped me resolve an identical problem yesterday that I was having with Apple Pay sheet not calling its delegate functions and being largely non-responsive (not closing, not completing payment, etc.) I would have expected some debug messages to show up in XCode, but there were none.

In my case, I stored reference to an instance of the delegate class into a let constant associated with my SwiftUI view, but I still had an issue. What resolved it for me was to store the reference into a @State variable.

So, this was the problem:

struct ShoppingCart: View {
  let paymentHandler = applePay()

And this was the solution:

struct ShoppingCart: View {
  @State var paymentHandler = applePay()

Just sharing in case is helps the next person.


Hacking with Swift is sponsored by Swiftable

SPONSORED An iOS conference hosted in Buenos Aires, Argentina – join us for the third edition from November 29th to December 1st!

Get your ticket

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.