Search code examples
node.jsswiftbraintreenonce

can't send nonce to Braintree


I'm trying ti integrate with Brain tree sandbox,

I'm trying to send my nonce from the ios client to my server but I get this error in my console : TypeError: Cannot read property 'payment_method_nonce' of undefined

my server side code is :

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);
}
});});

in my Ios side :

    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()
}

Solution

  • TypeError: Cannot read property 'payment_method_nonce' of undefined

    This means your req.body is empty/not parsed. Did you import body-parser? This module will parse the body and make it available to you.