Creating Charges for Stripe with React and Netlify functions Part 3

Part 3 of a 3 part tutorial - create a lambda to access stripes api and process charges

Sun, 05 Aug 2018

This is the third part of the a three part series that explains how to set up stripe react elements and process charges using Stripes API. If you want to read the first part which sets up your front end, click here. This part will focus on processing charges using a lambda. Lets hop right in!

Setting up

The only package that we will need for this lambda is the official Stripe package.

npm install stripe --save

or

yarn add stripe

Code

Here is the full code example

var stripe = require("stripe")("sk_test_key");

module.exports.handler = (event, context, callback) => {

  const requestBody = JSON.parse(event.body);
  const token = requestBody.token.id;
  const amount = requestBody.charge.amount;
  const currency = requestBody.charge.currency;
  const email = requestBody.charge.email;

  return stripe.charges.create({ // Create Stripe charge with token
    amount,
    currency,
    receipt_email: email,
    description: 'Serverless Stripe Test charge',
    source: token,
  })
    .then((charge) => { // Success response
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: `Charge processed succesfully!`,
          charge,
        }),
      };
      callback(null, response);
    })
    .catch((err) => { // Error response
      const response = {
        statusCode: 500,
        body: JSON.stringify({
          error: err.message,
        }),
      };
      callback(null, response);
    })
};

starting off with the first line, it just initializes Stripe with your API key. Make sure you are using the test key while you’re developing your app.

var stripe = require("stripe")("sk_test_key");

The next block will start to deconstruct our charge object that was passed to us from our front end.

  const requestBody = JSON.parse(event.body);
  const token = requestBody.token.id;
  const amount = requestBody.charge.amount;
  const currency = requestBody.charge.currency;
  const email = requestBody.charge.email;

The charge object contains a charge token that was created from Elements, the amount we would like to charge, the currency country code(USD for example) and an email address to send a receipt to our customer on a successful charge. The next block of code will attempt to create a new charge.

  return stripe.charges.create({ // Create Stripe charge with token
	    amount,
	    currency,
	    receipt_email: email,
	    description: 'Serverless Stripe Test charge',
	    source: token,
})

After we get our new charge, either one of two things will happen. If it was successful, we would return an object with a 200(OK) status code and message stating the charge was successful. If the charge wasn’t succesful, we return 500 and the error message that the Stripe API returned.

Loading...
Edward Beazer

Edward Beazer - I just like to build shit. Sometimes I get stuck for hours, even days while trying to figure out how to solve an issue or implement a new feature. Hope my tips and tutorials can save you some time.