gpt4 book ai didi

ios - iOS Swift 中的 Braintree 插件 : Where to get variable PaymentMethodNonce:

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:12 24 4
gpt4 key购买 nike

我一直在按照此处的说明操作 https://developers.braintreepayments.com/start/hello-client/ios/v4在 iOS Swift 中将直接支付表单与 braintree 集成。我的代码如下所示,据我所知,它与文档完全匹配。

func fetchClientToken() {
// TODO: Switch this URL to your own authenticated API
let clientTokenURL = NSURL(string: "https://braintree-sample-merchant.herokuapp.com/client_token")!
let clientTokenRequest = NSMutableURLRequest(url: clientTokenURL as URL)
clientTokenRequest.setValue("text/plain", forHTTPHeaderField: "Accept")

URLSession.shared.dataTask(with: clientTokenRequest as URLRequest) { (data, response, error) -> Void in
// TODO: Handle errors
let clientToken = String(data: data!, encoding: String.Encoding.utf8)

// As an example, you may wish to present Drop-in at this point.
self.showDropIn(clientTokenOrTokenizationKey: clientToken!)

// Continue to the next section to learn more...
}.resume()
}

func postNonceToServer(paymentMethodNonce: String) {
// Update URL with your server
let paymentURL = URL(string: "https://your-server.example.com/payment-methods")!
let request = NSMutableURLRequest(url: paymentURL)
request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8)
request.httpMethod = "POST"
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
// TODO: Handle success or failure
}.resume()
}

func showDropIn(clientTokenOrTokenizationKey: String) {
let request = BTDropInRequest()
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
{ (controller, result, error) in
if (error != nil) {
print("ERROR")
} else if (result?.isCancelled == true) {
print("CANCELLED")
} else if let result = result {
// Use the BTDropInResult properties to update your UI
// result.paymentOptionType
// result.paymentMethod
// result.paymentIcon
// result.paymentDescription
}
controller.dismiss(animated: true, completion: nil)
}
self.present(dropIn!, animated: true, completion: nil)
}

现在,我可以调用 fetchClientToken 函数,如您所见,它又调用 showDropIn,一切正常。

但是,要将 postNonceToServer 作为下一个函数运行,我需要一个变量 paymentMethodNonce 并且我不知道从哪里获取,我认为文档中遗漏了这一点。因此,我无法在运行 showDropIn 后继续......

我很困惑,我确信有一个简单的修复方法,但我找不到关于这一点的任何文档。

最佳答案

函数 showDropIn 中的 result.paymentMethod.nonce 包含所需信息。

func showDropIn(clientTokenOrTokenizationKey: String) {
let request = BTDropInRequest()
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
{ (controller, result, error) in
if (error != nil) {
print("ERROR")
} else if (result?.isCancelled == true) {
print("CANCELLED")
} else if let result = result {

let out = result.paymentMethod!
print(out.nonce)
self.postNonceToServer(paymentMethodNonce: out.nonce)

// Use the BTDropInResult properties to update your UI
// result.paymentOptionType
// result.paymentMethod
// result.paymentIcon
// result.paymentDescription
}
controller.dismiss(animated: true, completion: nil)
}
self.present(dropIn!, animated: true, completion: nil)
}

关于ios - iOS Swift 中的 Braintree 插件 : Where to get variable PaymentMethodNonce:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com