Search code examples
iosswiftapplepay

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?


Solution

  • 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