【发布时间】:2017-04-24 09:20:20
【问题描述】:
我一直按照这里的说明进行操作https://developers.braintreepayments.com/start/hello-client/ios/v4 在 iOS Swift 中集成一个 drop-in 支付表单和 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 后无法继续...
我很困惑,我确信有一个简单的解决方法,但我找不到任何关于这一点的文档。
【问题讨论】:
-
我在通过 cocoapod 安装 Braintree 时遇到此错误。 “无法找到 BraintreeDropIn 的规范”
标签: ios swift paypal braintree braintree-sandbox