无法将随机数发送给Braintree

时间:2018-03-24 16:00:39

标签: node.js swift braintree nonce

我正在尝试与Brain tree sandbox集成,

我正在尝试将我的nonce从ios客户端发送到我的服务器,但是我在控制台中收到此错误:TypeError: Cannot read property 'payment_method_nonce' of undefined

我的服务器端代码是:

app.post("/checkout", function (req, res) {
var nonceFromTheClient = req.body.payment_method_nonce;
gateway.transaction.sale({
amount: "10.00",
paymentMethodNonce: nonceFromTheClient,
options: {
  submitForSettlement: true
}
}, function (err, result) {
if (err) {
  print(err)
}
else if (result.success) {
  console.log(result);
}
});});

在我的Ios方面:

    func createTransaction(paymentMethodNonce:String) {
    let paymentURL = postUrl!
    var request = URLRequest(url: paymentURL)
    request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8)
    request.httpMethod = "POST"

    URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
        if let err = error {
            print(err.localizedDescription)
            return}
        else {
            print(response)}
        }.resume()
}

1 个答案:

答案 0 :(得分:2)

TypeError: Cannot read property 'payment_method_nonce' of undefined

这意味着您的req.body为空/未解析。你导入了body-parser吗?该模块将解析正文并使其可供您使用。