braintree / braintree_node

Braintree Node.js library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
335 stars 104 forks source link

What value should i pass for the field paymentMethodNonce from the client ? #138

Closed kelvinngom closed 6 years ago

kelvinngom commented 6 years ago

Hi,

I am using the javascript SDK to setup the client and server. Not able to understand what value I need to pass for the payment_method_nonce. app.post("/checkout", function (req, res) { console.log(req.body); nonceFromTheClient = req.body.payment_method_nonce; // Use payment method nonce here });

quinnjn commented 6 years ago

HI @kelvinngom

You can follow our "Hello Server" getting started tutorial. That should explain how to make a simple credit card transaction from start to finish.

This is the relevant bit that you're asking for, after receiving the payment method nonce on your server: Create a Transaction

Note that braintree_node is the server SDK. You can use braintree-web-drop-in as the client SDK.

quinnjn commented 6 years ago

If the question was around passing information from the client to the server, here is an example codepen showcasing how to pass data from client to server via an HTTP POST request.

If you have more questions around the tutorial please contact: support@braintreepayments.com

kelvinngom commented 6 years ago

Thanks @quinnjn Question is more on how to create the nonce. I got the below error, pls let me know.

Error: TypeError: Cannot read property 'paymentMethodNonce' of undefined

Code: const express = require('express'); const app = express(); var bodyParser = require('body-parser'); var braintree = require("braintree"); var nonceFromTheClient; var clientToken; var gateway = braintree.connect({ environment: braintree.Environment.Sandbox, merchantId: "", publicKey: "", privateKey: "" }); app.use(bodyParser.json()); gateway.clientToken.generate({ customerId: "*" }, function (err, response) { clientToken = response.clientToken console.log("client token is :",clientToken); }); app.listen(3000, () => console.log('Example app listening on port 3000!')) app.get("/client_token", function (req, res) { gateway.clientToken.generate({}, function (err, response) { res.send(response.clientToken); });

});

app.post("/checkout", function (req, res) { gateway.paymentMethodNonce.create(clientToken, function(err, response) { console.log("log*",clientToken); var nonce = response; console.log("log***",response.paymentMethodNonce); }); nonceFromTheClient = req.body.payment_method_nonce; console.log(nonceFromTheClient); // Use payment method nonce here res="submitted"; return res; });

gateway.transaction.sale({ amount: "12.00", paymentMethodNonce: nonceFromTheClient, options: { submitForSettlement: true } }, function (err, result) { });

crookedneighbor commented 6 years ago

I see a few problems here. You have this snippet:

app.post("/checkout", function (req, res) {
  gateway.paymentMethodNonce.create(clientToken, function(err, response) {
    console.log("log*",clientToken);
    var nonce = response;
    console.log("log***",response.paymentMethodNonce);
  });
  nonceFromTheClient = req.body.payment_method_nonce;
  console.log(nonceFromTheClient);
  // Use payment method nonce here
  res="submitted";
  return res;
});

gateway.transaction.sale({
  amount: "12.00",
  paymentMethodNonce: nonceFromTheClient,
  options: {
    submitForSettlement: true
  }
}, function (err, result) {
});

There's no reason to have this in your snippet:

gateway.paymentMethodNonce.create(clientToken, function(err, response) {
    console.log("log*",clientToken);
    var nonce = response;
    console.log("log***",response.paymentMethodNonce);
  });

It's used for something entirely different than what you are trying to accomplish. This is likely where the error is being thrown, because you are passing a client token (or, in this case, an undefined variable) into it. Therefore, it's going to respond with an error and the response variable will be undefined.

The following section is pulling a payment_method_nonce property from the body of the post request to your checkout endpoint. This assumes your client side page is making a post request to your checkout endpoint and sending the payment method nonce along as payment_method_nonce in the post body parameters.

nonceFromTheClient = req.body.payment_method_nonce;

Finally, your transaction.sale call is outside of the callback function for the post request. That will need to be inside the function to be called when making the post request.

For further help with your integration, please contact our support team. https://articles.braintreepayments.com/forms/contact