stripe / stripe-ios

Stripe iOS SDK
https://stripe.com
MIT License
2.09k stars 975 forks source link

Apple Pay sheet always showing 0 as amount. #1792

Closed dan-precup closed 3 years ago

dan-precup commented 3 years ago

Summary

If the payment method hasn't been created and the paymentRequest.requiredShippingContactFields in not an empty array the ApplePay sheet show a grand total of 0. If I change this:

paymentRequest.requiredShippingContactFields = [.name, .emailAddress, .postalAddress, .phoneNumber]

to

 paymentRequest.requiredShippingContactFields = []

The correct amount is shown.

Also replacing:

 applePayContext.presentApplePay(from: application.window)

with:

 let paymentViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
 viewController.present(paymentViewController!, animated: true, completion: nil)

Fixes the issue as well

Code to reproduce

Both these methods present the ApplePay sheet:

    let paymentRequest = PKPaymentRequest()
        paymentRequest.merchantIdentifier = "############"
        paymentRequest.supportedNetworks = [PKPaymentNetwork.visa, PKPaymentNetwork.masterCard, PKPaymentNetwork.amex]
        paymentRequest.merchantCapabilities = PKMerchantCapability.capability3DS
        paymentRequest.countryCode = "GB"
        paymentRequest.currencyCode = "GBP"
        paymentRequest.requiredBillingContactFields = [.postalAddress]
        paymentRequest.requiredShippingContactFields = [.name, .emailAddress, .postalAddress, .phoneNumber]
        paymentRequest.paymentSummaryItems = [
            PKPaymentSummaryItem(label: "Some Product", amount: 9)
        ]

        guard let applePayContext = STPApplePayContext(paymentRequest: paymentRequest, delegate: delegate) else {
            return
        }
        DispatchQueue.main.async {
            guard let application = UIApplication.shared.delegate as? AppDelegate else {
                return
            }
            applePayContext.presentApplePay(from: application.window)
        }

iOS version

14.4

Installation method

Cocoapods

SDK version

21.4

Other information

yuki-stripe commented 3 years ago

Hi @dan-precup,

What simulator are you using? I haven't been able to reproduce this with the code provided with an iPhone 11 - 14.4 simulator and v21.4 of the SDK, I see: image

dan-precup commented 3 years ago

Hi Yuki,

Thanks for the quick reply.

That is a great point you brought up. I was using an iPhone 12 simulator. I've tried on the iPhone 11 simulator and it looks exactly like the screenshot above. I then switched on 12 pro max actual device, same bug.

I then realised that it was the presence of required data that is doing the offending so I proceeded to erase the iPhone 12 simulator and surely enough I got the same screenshot as you did on this one as well.

If I added the required details back it reverted to the 0 amount.

dan-precup commented 3 years ago

I found the issue, it was completely on my end. I've foolishly tried to updated the paymentSummaryItems from an STPPaymentContext inside the applePayContext(_ context:, didSelectShippingContact) delegate method and that was always 0. Sorry for the confusion and thank you for the support!