我正在尝试设置一个在node.js上运行的相对简单的Braintree服务器,我的客户端用Swift 2.1 for iOS 9编写,虽然我能够记录客户端令牌,但是我收到以下错误:
[BraintreeSDK] ERROR Unsupported client token version. Please ensure your server is generating a valid Braintree ClientToken with a server-side SDK that is compatible with this version of Braintree iOS.
以下是我的服务器代码(改编自BraintreeDev存储库):
'use strict';
var express = require('express');
var app = express();
var braintree = require('braintree');
var bodyParser = require('body-parser');
var parseUrlEnconded = bodyParser.urlencoded({
extended: false
});
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: 'xxxxxxxxxxxxx',
publicKey: 'xxxxxxxxxxxxxxx',
privateKey: 'xxxxxxxxxxxxxxxxxxxxxxx'
});
app.use(express.static('public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Creates a new token and returns it in the response.
app.get('/token', function (req, res) {
gateway.clientToken.generate({}, function (error, response) {
res.json(response);
});
});
app.post('/payment', function (req, res) {
var sale = {
amount: "10",
payment_method_nonce: req.param('payment_method_nonce')
};
gateway.transaction.sale(sale, function (error, response) {
if (!error && response.success) {
res.json(response);
} else {
// do something else
}
});
});
app.listen(process.env.PORT || 5000)
module.exports = app;
这是我的iOS代码:
class BaseViewController: UIViewController {
var braintreeClient: BTAPIClient?
override func viewDidLoad() {
super.viewDidLoad()
let clientTokenURL = NSURL(string: "https://my-heroku-app.herokuapp.com/token")!
let clientTokenRequest = NSMutableURLRequest(URL: clientTokenURL)
clientTokenRequest.setValue("text/plain", forHTTPHeaderField: "Accept")
NSURLSession.sharedSession().dataTaskWithRequest(clientTokenRequest) { (data, response, error) -> Void in
guard let data = data else {
return
}
let clientToken = String(data: data, encoding: NSUTF8StringEncoding)
print("Client Token: \(clientToken)")
self.braintreeClient = BTAPIClient(authorization: clientToken!)
print(response?.description)
}.resume()
}
...
这似乎应该是非常直接的,我很难理解为什么我会收到这个错误。
编辑:我使用的是最新版本的Braintree SDK for iOS和最新版本的Braintree node.js包。
答案 0 :(得分:1)
完全披露:我在Braintree工作。如果您有任何其他问题,请随时与我们的support小组联系。
您似乎正在序列化gateway.clientToken.generate
的整个回复并将其传递给BTAPIClient
。您应该从响应对象中删除clientToken
,并仅将clientToken字符串值传递给BTAPIClient
。
app.get('/token', function (req, res) {
gateway.clientToken.generate({}, function (error, response) {
res.json(response.clientToken);
});
});
有关详细信息,请参阅clientToken.generate文档。
答案 1 :(得分:0)
上面的答案解决了我的服务器端代码问题。但是,我仍然在iOS应用程序中收到错误:
ERROR BTClient could not initialize because the provided clientToken was invalid
我现在正在使用Alamofire,我的功能如下:
associatedType ClientTokenResult = (clientToken: String?) -> ()
func fetchToken(completion: ClientTokenResult) {
let parameters = [String: AnyObject]()
Alamofire.request(Router.Get("api/token", parameters))
.validate()
.response { (request, response, data, error) -> Void in
guard let data = data else {
return
}
if let error = error {
print("Error: \(error.localizedDescription)")
completion(clientToken: nil)
} else {
let clientToken = String(data: data, encoding: NSUTF8StringEncoding)!
// NOTE: This shouldn't be necessary, but as of right now the server is returning the token
// with quotation marks.
let strippedClientToken = clientToken.stringByReplacingOccurrencesOfString("\"",
withString: "")
print("Client Token: \(strippedClientToken)")
print(response?.debugDescription)
completion(clientToken: strippedClientToken)
}
}
}
更新:您可以在我的NOTE
评论中看到问题是我的服务器正在使用引号返回令牌(我还没弄清楚为什么)所以我不得不去除它们#34;来自令牌。现在一切都很好。