Setting up Braintree endpoint and authentication

The generator has already created the required order routes and controller for use. We are going to roll with that. If you remember from our Angular app, we are using a route called /api/braintree/client_token, which we haven't created yet. For that we are going to use the Braintree SDK. Let's get that set up:

npm install [email protected] --save

Next, let's generate the endpoint:

yo angular-fullstack:endpoint braintree
? What will the url of your endpoint be? /api/braintree
...

The API keys

We need to get the API keys in order to use the Braintree services. Go to https://www.braintreepayments.com/get-started and create a sandbox account.

Once you have the private/public keys and the merchant ID, add it to local.env.js:

/* server/config/local.env.js */

BRAINTREE_ID: '7hh… public key …rq',
BRAINTREE_SECRET: 'f1c… private key …028',
BRAINTREE_MERCHANT: 'gwz… merchant ID …g3m',

Later, let's create the config files to make use of it:

/* server/config/environment/index.js *excerpt */

  braintree: {
    clientID:       process.env.BRAINTREE_ID || 'id',
    clientSecret:   process.env.BRAINTREE_SECRET || 'secret',
    clientMerchant: process.env.BRAINTREE_MERCHANT || 'merchant'
  }

We have been using the same pattern for other services that need keys such as Facebook and Twitter. In the next section, we are going to make use of it.

Gateway

Open braintree.model.js, and replace the content with the following code:

/* server/api/braintree/braintree.model.js */

var braintree = require('braintree');
var config = require('../../config/environment');
var isProduction = config.env === 'production';

var gateway = braintree.connect({
  environment: isProduction ? braintree.Environment.Production : braintree.Environment.Sandbox,
  merchantId: config.braintree.clientMerchant,
  publicKey: config.braintree.clientID,
  privateKey: config.braintree.clientSecret
});

module.exports = gateway;

This model is very different, since it doesn't store anything in MongoDB like the others. Instead, it sets up communication with a remote service in the Braintree servers.

Controller

Now we can use the Braintree gateway in the controllers:

/*server/api/braintree/braintree.controller.js */

var _ = require('lodash');
var Braintree = require('./braintree.model');

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}

function handleResponse (res) {
  return function (err, result) {
    if(err) {
      return handleError(res)(err);
    }
    responseWithResult(res)(result);
  }
}

function responseWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

exports.clientToken = function(req, res){
  Braintree.clientToken.generate({}, function (err, data) {
    return handleResponse(res)(err, data.clientToken);
  });
}

exports.checkout = function(req, res){
  Braintree.transaction.sale({
    amount: req.body.total,
    paymentMethodNonce: req.body.nonce,
  }, function callback (err, result) {
    if(err) {
      return handleError(res)(err);
    }
    if(result.success){
      responseWithResult(res)(result);
    } else {
      handleError(res)(result.errors);
    }
  });
}

Router

We are using only two actions: POST checkout and GET clientToken. Let's define them:

/* server/api/braintree/index.js */

var express = require('express');
var controller = require('./braintree.controller');

var router = express.Router();

router.get('/client_token', controller.clientToken);
router.post('/checkout', controller.checkout);

module.exports = router;

Go ahead and delete everything else that the generator did for the Braintree endpoint; we are not going to use that. The only three required files are the Braintree model, controller, and index.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.40.177