How to do it...

Let's perform the following steps to get integrated with Stripe as a payment processor; we will add a simple test route to add customers to our account and return customer data from it:

  1. First, we will need to create a new /middleware/stripe.js middleware, which will contain all our configuration for Stripe. We will define two middleware methods, one to create a customer record in Stripe using our JWT's email property and another to return a full customer list from Stripe:
const keyPublishable = process.env.stripeAPIPublishableKey;
const keySecret = process.env.stripeAPISecretKey;
const stripe = require("stripe")(keySecret);

var self = module.exports = {
createCustomer: function(req, res, next) {
return stripe.customers.create({
email: req.jwt.payload.email
}).then(function(customer) {
req.stripe = { customer: customer };
next();
})
},

getCustomers: function(req, res, next) {
stripe.customers.list()
.then(function(customers) {
res.json(customers);
})
}
};
  1. We will want to provide thorough error handling whenever working with third-party APIs, so we'll add jsonapi-serializer's error serializer and add an errorHandler middleware, which we will use for parsing any errors returning from the Stripe API:
var JSONAPIError = require('jsonapi-serializer').Error;
const keyPublishable = process.env.stripeAPIPublishableKey;
const keySecret = process.env.stripeAPISecretKey;
const stripe = require("stripe")(keySecret);

var self = module.exports = {

errorHandler: function(error) {
return function(req, res) {
res.status(500).json(new JSONAPIError({
status: 500,
title: error.type,
detail: error.message
}));
};
}
,

createCustomer: function(req, res, next) {
return stripe.customers.create({
email: req.jwt.payload.email
}).then(function(customer) {
req.stripe = { customer: customer };
next();
}).catch(function(error) {
return self.errorHandler(error)(req, res);
})
;
},

getCustomers: function(req, res, next) {
stripe.customers.list()
.then(function(customers) {
res.json(customers);
})
.catch(function(error) {
return self.errorHandler(error)(req, res);
})
;
}
};
  1. Now that we have created our Stripe middleware, we can import it into our /routes/api.js route configuration. We'll create a route that checks whether there is an active JWT token first for authentication protection, before creating a customer and returning our customer list to us in a JSON format:
...
var stripe = require('../middleware/stripe');
...
router.use('/login', login);
router.use('/customers', jwt.active(), stripe.createCustomer, stripe.getCustomers);
router.use('/images', jwt.active(), jwt.require('role', '===', 'admin'), images);
router.use('/posts', jwt.active(), jwt.require('role', '===', 'admin'), posts);
...

module.exports = router;
  1. Now, we can log in to our API, and visit http://localhost:3000/api/customers to get a record from Stripe of all our customers. If we repeat this request, we will see our total number of customer entries increment by one, and if we attempt to access this endpoint without having a valid JWT, we will get a JWT unauthenticated error instead.
..................Content has been hidden....................

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