UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Apple pay doesn't trigger delegate

Forums > SwiftUI

@PGF  

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 = "merchant.myshop.domain.com"
        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

applePay().payWithApplePay(total: currentCart.total)

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

1      

@PGF  

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

applePay().payWithApplePay(total: currentCart.total)

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

paymentHandler.payWithApplePay(total: currentCart.total)

Good coding

1      

@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.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.