Shali Liu
Shali Liu

Reputation: 1174

ApplePay PKPaymentAuthorizationViewController always shows processing

enter image description here I am trying to use Apple Pay in my app. It works fine to present PKPaymentAuthorizationViewController. However, after I filled up with credit card and billing information, it just shows a spinning wheel and 'processing' as shown in the picture.

The code for presenting Apple Pay view controller is as follows:

let request = Stripe.paymentRequestWithMerchantIdentifier(DH_APPLEPAY_ID)
request.paymentSummaryItems = [PKPaymentSummaryItem(label: label, amount: amountDecimal)]

request.requiredBillingAddressFields = PKAddressField.All
request.requiredShippingAddressFields = PKAddressField.PostalAddress | PKAddressField.Email
request.countryCode = "US"
request.currencyCode = "USD"
request.merchantCapabilities = PKMerchantCapability.CapabilityEMV | PKMerchantCapability.Capability3DS
request.supportedNetworks = [PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]

if Stripe.canSubmitPaymentRequest(request) {

    let paymentController = PKPaymentAuthorizationViewController(paymentRequest: request)

    paymentController.delegate = self

    self.navigationController?.presentViewController(paymentController, animated: true, completion: nil)
} else {
    //popup
    DHUtils.alert("Apple Pay", message: "Please add your credit card to Passbook.", inViewController: self)
}

The func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) delegate never get called.

The certificate status looks all right. What do I miss here to make it work?

Upvotes: 4

Views: 5566

Answers (3)

yoAlex5
yoAlex5

Reputation: 34471

You are able to see processing UI if you use next PKPaymentAuthorizationViewControllerDelegate method without calling completion

func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect paymentMethod: PKPaymentMethod, handler completion: @escaping (PKPaymentRequestPaymentMethodUpdate) -> Void)

To fix it just call completion:

extension SomeViewController: PKPaymentAuthorizationViewControllerDelegate {
    public func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect paymentMethod: PKPaymentMethod, handler completion: @escaping (PKPaymentRequestPaymentMethodUpdate) -> Void) {
        //as an example
        let paymentMethodUpdate = PKPaymentRequestPaymentMethodUpdate(paymentSummaryItems: [
            PKPaymentSummaryItem(label: "someLabel", amount: 0.01)
        ])
        completion(paymentMethodUpdate)
    }
}

As you can see here you have PKPaymentMethod which actually provides you PKPaymentMethodType (Credit, Debit...) all others variable will be nil at this moment. They are will be filled when you authorize(didAuthorizePayment) - PKPayment.PKPaymentToken.PKPaymentMethod

Please note that it is a place for extra logic that is why use it when you need it for reaching your goals. It means if you don't need this extra logic just remove this method

Upvotes: 0

Whitney Foster
Whitney Foster

Reputation: 721

My issue was having this delegate method:

func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didSelectPaymentMethod paymentMethod: PKPaymentMethod, completion: ([PKPaymentSummaryItem]) -> Void) {

}

Remove it and it should stop the endless spinning.

Upvotes: 5

Rushabh
Rushabh

Reputation: 661

There should be a completion block that you need to call. It should be in the delegate method didAuthorizePayment on the PKPaymentAuthorizationViewController, you need to use this completion block and pass the success or failure message to it. It should look somewhat like this

extension BuySwagViewController: PKPaymentAuthorizationViewControllerDelegate {
  func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {
    completion(PKPaymentAuthorizationStatus.Success)
  }

  func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController!) {
    controller.dismissViewControllerAnimated(true, completion: nil)
  }
}

Here in completion block you should pass PKPaymentAuthorizationStatus.Success or PKPaymentAuthorizationStatus.Failure depending on whether you were able to process the payment.

Also, you can refer the Ray Wenderlich tutorial here. http://www.raywenderlich.com/87300/apple-pay-tutorial

Upvotes: 9

Related Questions