我已经使用Passkit框架在我的iOS应用程序中实现了apple pay。我完全完成了所有these things来设置苹果工资。 我正在使用沙盒帐户。我在电子钱包应用程序中添加了卡片,这些卡片是我从this link复制的测试卡。我正在使用的代码:
print("\(((self.grandTotalLabel.text!).replacingOccurrences(of: "$", with: "")))")
let paymentNetworks = [PKPaymentNetwork.amex]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks){
paymentRequest.supportedNetworks = paymentNetworks
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.requiredShippingAddressFields = [.all]
paymentRequest.paymentSummaryItems = self.itemToSell(shipping: 10.0)
let sameDayShipping = PKShippingMethod(label: "Same day divilery", amount: 12.99)
sameDayShipping.detail = "Same day divilery Detail"
sameDayShipping.identifier = "sameDay"
let twDaysShipping = PKShippingMethod(label: "Two days divilery", amount: 4.99)
twDaysShipping.detail = "Two days divilery Detail"
twDaysShipping.identifier = "twoDays"
let freeShipping = PKShippingMethod(label: "Free shipping divilery", amount: 0.0)
freeShipping.detail = "Free shipping divilery Detail"
freeShipping.identifier = "freeShipping"
// paymentRequest.shippingMethods = [sameDayShipping,twDaysShipping, freeShipping]
let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
applePayVC?.delegate = self
self.present(applePayVC!, animated: true) {
print("ApplePayViewcontroller")
}
}
else{
print("Please set up for apple pay")
}
func itemToSell(shipping:Float) -> [PKPaymentSummaryItem]{
print(Double("\(((self.grandTotalLabel.text!).replacingOccurrences(of: "$", with: "")))") as Any)
let dishItems = PKPaymentSummaryItem(label: "FoodKonnect", amount: NSDecimalNumber(string: "21.00"))
let discount = PKPaymentSummaryItem(label: "Discount", amount: 1.0)
let shipping = PKPaymentSummaryItem(label: "Shipping", amount: NSDecimalNumber(string: "\(shipping)"))
let totalAmount = dishItems.amount.adding(discount.amount)
let totalPrice = PKPaymentSummaryItem(label: "FoodKonnect application", amount: totalAmount)
return [dishItems, discount,shipping, totalPrice]
}
我使用的所有代表 PKPaymentAuthorizationViewControllerDelegate
:
extension CartViewController:PKPaymentAuthorizationViewControllerDelegate{
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
controller.dismiss(animated: true, completion: nil)
}
// @available(iOS 11.0, *)
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelectShippingContact contact: PKContact, completion: @escaping (PKPaymentAuthorizationStatus, [PKShippingMethod], [PKPaymentSummaryItem]) -> Void) {
print("\(#function)")
}
@available(iOS 11.0, *)
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect shippingMethod: PKShippingMethod, handler completion: @escaping (PKPaymentRequestShippingMethodUpdate) -> Void) {
print("\(#function)")
}
@available(iOS 11.0, *)
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
print("\(#function)")
}
}
Apple pay viewcontroller正在显示,此屏幕显示处理圆圈视图。
但几秒钟后我收到此错误消息:
答案 0 :(得分:1)
在具有PKPaymentAuthorizationViewControllerDelegate
/ completion
的{{1}}的每个已实现的委托函数中,您必须使用适当的参数调用该块,最重要的是使用适当的状态。
在iOS 11上(大约)15-20秒内没有调用该块,iOS正在查看您看到的错误的付款。在iOS 10上,它将允许您无限期地旋转handler
,直到调用Processing
块。
我遇到了同样的问题,事实证明我在其中一个边缘案例中根本没有调用completion
块。
答案 1 :(得分:1)
最近我遇到了同样的问题,并且找到了解决方案。
一切都很好,只有我们必须用成功或失败来更新处理程序,如下所示:
func paymentAuthorizationViewController(_ controller:
PKPaymentAuthorizationViewController, didAuthorizePayment payment:
PKPayment, handler completion: @escaping
(PKPaymentAuthorizationResult) -> Void) {
如果付款数据为零或不检查模拟器,则始终为零:
do {
let jsonResponse = try JSONSerialization.jsonObject(with: paymentStatus.paymentData, options: .mutableContainers)
print(jsonResponse as! NSDictionary)
completion(PKPaymentAuthorizationResult(status: .success, errors: nil))
}
catch let error
{
print(error)
completion(PKPaymentAuthorizationResult(status: .failure, errors: nil))
}
应该可以解决问题。
答案 2 :(得分:0)
在某些情况下,您确实确实希望PKPaymentAuthorizationViewControllerDelegate
函数中正确调用了完成程序/处理程序,但期望您的内部付款API的超时时间长于ApplePay对话框的预期(自iOS以来) 13会在30秒左右超时)。
在这些情况下,您可能会在慢速网络中收到“ Apple pay not complete”错误(尝试人为地阻止您的API以模拟这种情况)。
一个解决方案可能是在放弃并显示错误之前自行关闭ApplePay对话框。有关如何执行此操作的更多详细信息,请参见this answer和正在讨论的similar question。