When I attempt to present a PKPaymentAuthorizationController with present(completion: _)
it fails without any further information.
Here's how I'm presenting it:
let paymentSummaryItems: [PKPaymentSummaryItem] = <HIDDEN>
let paymentRequest = PKPaymentRequest()
paymentRequest.paymentSummaryItems = paymentSummaryItems
paymentRequest.merchantIdentifier = <HIDDEN>
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.countryCode = "US"
paymentRequest.currencyCode = "USD"
paymentRequest.supportedNetworks = [.visa, .amex, .masterCard, .discover]
paymentRequest.requiredShippingAddressFields = [.email]
let paymentController = PKPaymentAuthorizationController(paymentRequest: paymentRequest)
paymentController.delegate = self
paymentController.present { success in
if success {
print("Presented payment controller")
} else {
print("Failed to present payment controller")
}
}
Every time I try, I get the message "Failed to present payment controller", and no Apple Pay Authorization modal appears.
I added a PKPaymentAuthorizationController.canMakePayments()
check, and that returns true. I'm not sure what could be going wrong. The same code (with a different merchantIdentifier
) works in my other Apple Pay projects with no problems.
If you have a typo at any of the properties of PKPaymentRequest
object, that will cause this behaviour. Please double check that following properties are correct:
merchantID
should match your merchantID in Capabilities. currencyCode
should be three-letter ISO 4217 currency code. countryCode
should be two-letter ISO 3166 country code. decimalNumbers
in paymentSummaryItems In my case, I was trying to just test the feature with hardcoded merchantID and it turned out it was misspelled. In that case PKPaymentRequest
and PKPaymentAuthorizationController
(or PKPaymentAuthorizationViewController
) will be initialised successfully but when you present controller, it simply wouldn't show up.
I figured it out, for anybody who encounters this issue in the future.
It turns out there was a problem with one of the NSDecimalNumbers in my array of PKPaymentSummaryItems, it didn't properly convert to a decimal and resulted in amount
being set to NaN
. Once I fixed it, the controller appeared properly.
I fixed it using following steps: 1 of which could be correct answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.