Apple Pay 和 Swift 如何将 PKPaymentToken 发送到服务器

Posted

技术标签:

【中文标题】Apple Pay 和 Swift 如何将 PKPaymentToken 发送到服务器【英文标题】:Apple Pay and Swift how do I send PKPaymentToken to server 【发布时间】:2017-06-16 11:00:00 【问题描述】:

我正在使用 Swift 3 在我的应用程序中实现 Apple Pay,并尝试将我在paymentAuthorizationViewController 中收到的 PKPaymentToken 发送到银行的 API 以处理付款,但没有成功。我发送的数据总是被拒绝。

银行支持建议我发送整个 payment.token 而不是 payment.token.PaymentData 但我不知道怎么做,因为 payment.tokenPKPaymentToken 的实例,据我所知不能转换为字符串或编码为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。获得付款令牌后,我们必须将其发送到哪个网关..? 【参考方案1】:

好的,我现在清楚了。

let paymentDataDictionary: [AnyHashable: Any]? = try? JSONSerialization.jsonObject(with: payment.token.paymentData, options: .mutableContainers) as! [AnyHashable : Any]
var paymentType: String = "debit"

var paymentMethodDictionary: [AnyHashable: Any] = ["network": "", "type": paymentType, "displayName": ""]

if #available(ios 9.0, *) 
    paymentMethodDictionary = ["network": payment.token.paymentMethod.network ?? "", "type": paymentType, "displayName": payment.token.paymentMethod.displayName ?? ""]

    switch payment.token.paymentMethod.type 
        case .debit:
            paymentType = "debit"
        case .credit:
            paymentType = "credit"
        case .store:
            paymentType = "store"
        case .prepaid:
            paymentType = "prepaid"
        default:
            paymentType = "unknown"
        


let cryptogramDictionary: [AnyHashable: Any] = ["paymentData": paymentDataDictionary ?? "", "transactionIdentifier": payment.token.transactionIdentifier, "paymentMethod": paymentMethodDictionary]
let cardCryptogramPacketDictionary: [AnyHashable: Any] = cryptogramDictionary
let cardCryptogramPacketData: Data? = try? JSONSerialization.data(withJSONObject: cardCryptogramPacketDictionary, options: [])

// in cardCryptogramPacketString we now have all necessary data which demand most of bank gateways to process the payment

let cardCryptogramPacketString = String(describing: cardCryptogramPacketData)

【讨论】:

以上是关于Apple Pay 和 Swift 如何将 PKPaymentToken 发送到服务器的主要内容,如果未能解决你的问题,请参考以下文章

Apple Pay - 如何将商家公钥与支付令牌中的 publicKeyHash 进行比较?

WKWebView 中的 Apple Pay JS

如何将 ios 中的 Elavon 与在 obj-c 中使用 Apple Pay 集成?

如何在 Google Pay 和 Apple Pay 中为 Authorize.net 生成支付令牌(在移动应用交易中)?

Flutter:如何根据 Apple 指南添加 Apple Pay 按钮

apple pay 集成