2
votes

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?

2
Are you seeing the "Processing" message before you touch ID / enter a passcode? Did you implement any other delegate methods for the PKPaymentAuthorizationViewController, such as didSelectShippingAddress?lxt
@lxt I implemented didSelectShippingAddress. I commented it and the "Pay with Passcode" shows up. I guess I have done sth wrong in didSelectShippingAddress. Thanks a lot for that!Shali Liu
No problem! Perhaps you weren't calling the completion handler in that delegate method?lxt
@lxt You saved me another day. Thx :)Shali Liu

2 Answers

8
votes

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

4
votes

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.