【发布时间】:2017-06-16 11:00:00
【问题描述】:
我正在使用 Swift 3 在我的应用程序中实现 Apple Pay,并尝试将我在paymentAuthorizationViewController 中收到的 PKPaymentToken 发送到银行的 API 以处理付款,但没有成功。我发送的数据总是被拒绝。
银行支持建议我发送整个 payment.token 而不是 payment.token.PaymentData 但我不知道怎么做,因为 payment.token 是 PKPaymentToken 的实例,据我所知不能转换为字符串或编码为base64。
发送令牌的正确方法是什么?
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping ((PKPaymentAuthorizationStatus) -> Void)) {
self.processPayment(payment: payment, completion: completion)
}
func processPayment(payment: PKPayment, completion: @escaping ((PKPaymentAuthorizationStatus) -> Void)) {
print("Payment token: \(payment.token)")
let paymentData=String(data: payment.token.paymentData.base64EncodedData(), encoding: .utf8)
var request = URLRequest(url: URL(string: "https://bankapi.com/method")!)
request.httpMethod = "POST"
let postString = "orderid=\(orderid)&token=\(String(describing: paymentData))&amount=\(price)"
print("POST: \(postString)")
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
//@TODO check response is failure or success
//completion(PKPaymentAuthorizationStatus.failure)
}
task.resume()
}
更新。 银行服务为我提供了 Objective C 中的示例。 我现在尝试在 Swift 中重现它。
【问题讨论】:
-
您使用的是哪个支付网关?
-
@moogeek。获得付款令牌后,我们必须将其发送到哪个网关..?