Set Up Your Server
Install and configure
Install the Braintree node package:
- bash
npm install braintree
In your code, configure the environment and API credentials:
- Node
const braintree = require("braintree");
const gateway = new braintree.BraintreeGateway({
environment: braintree.Environment.Sandbox,
merchantId: "useYourMerchantId",
publicKey: "useYourPublicKey",
privateKey: "useYourPrivateKey"
});
See the Braintree Node Version Changelog.
Generate a client token
customerId
when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts.- Callback
- Promise
gateway.clientToken.generate({
customerId: aCustomerId
}, (err, response) => {
// pass clientToken to your front-end
const clientToken = response.clientToken
});
If the customer can't be found, the response will contain a message stating "Customer specified by customer_id does not exist".
Set Up Your Client covers the client side of the exchange.
Send a client token to your client
Here is an example of how your server would generate and expose a client token:
- Callback
- Promise
app.get("/client_token", (req, res) => {
gateway.clientToken.generate({}, (err, response) => {
res.send(response.clientToken);
});
});
How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, while in mobile apps the client token must be requested. These methods are discussed in the client token setup section.
Receive a payment method nonce from your client
Once your client successfully obtains a customer payment method, it receives a payment_method_nonce
representing customer payment authorization, which it then sends to your server.
Your server implementation is then responsible for receiving the payment_method_nonce
and using it appropriately.
- Node
app.post("/checkout", (req, res) => {
const nonceFromTheClient = req.body.payment_method_nonce;
// Use payment method nonce here
});
Create a transaction
- Callback
- Promise
gateway.transaction.sale({
amount: "10.00",
paymentMethodNonce: nonceFromTheClient,
deviceData: deviceDataFromTheClient,
options: {
submitForSettlement: true
}
}, (err, result) => {
});
The sale call returns a Transaction Result Object which contains the transaction and information about the request.
Test your integration
Always develop and test your code against your sandbox account before processing live transactions against a production account.
Transition to production
At this point, you should be able to accept a payment method nonce and create a transaction in our sandbox. When you're ready to start charging real money, transition over to our production environment. We'll explain that process next.