Skip to content

Instantly share code, notes, and snippets.

@jcleblanc
Created September 19, 2015 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcleblanc/c45cc349e746e2dfb597 to your computer and use it in GitHub Desktop.
Save jcleblanc/c45cc349e746e2dfb597 to your computer and use it in GitHub Desktop.
Braintree Drop-In UI Express Server Sample (Node)
var express = require('express'),
bodyParser = require('body-parser'),
app = express(),
util = require('util'),
braintree = require('braintree');
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: 'YOUR MERCHANT ID',
publicKey: 'YOUR PUBLIC KEY',
privateKey: 'YOUR PRIVATE KEY'
});
app.get("/client_token", function (req, res) {
gateway.clientToken.generate({}, function (err, response) {
res.send(response.clientToken);
});
});
app.post("/checkout", function (req, res) {
var nonce = req.body.payment_method_nonce;
gateway.transaction.sale({
amount: '10.00',
paymentMethodNonce: nonce,
}, function (err, result) {
res.send(result);
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment