我一直在按照這裏的說明https://developers.braintreepayments.com/start/hello-client/ios/v4 在iOS Swift中集成一個支付形式與braintree。我的代碼如下所示,據我所知,它完全匹配文檔。現在iOS中的布蘭特裏嵌入式Swift:從哪裏獲得可變PaymentMethodNonce:
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安裝布倫特裏我得到這個錯誤。 「無法找到BraintreeDropIn的規範」 – VJVJ