As SaaS products continue to takeover the world, web apps which enable users to make periodical billing transactions with ease are becoming more and more prevalent. This online processing of payments is a perfect use case for an event driven architecture like the one provided by a serverless setup.

In a two part series, I’ll give a walk through on how to setup a lambda function using the Serverless framework and AWS Lambda . We’ll then be able to post a request to our lambda service to create the same kind of functionality we would expect when subscribing to a product online. Our lambda function will carry out the following tasks:

Create a Stripe customer account for a user Subscribe the created customer to a product plan

Create A Stripe Account

As you might have guessed, you’re going to need a Stripe account that you can work with. Getting setup shouldn’t be a hassle, just the usual sign up stuff. As soon as you’re done signed in, head over to the Billing section in the menu on the left hand side. You can go ahead and click on Billing, after which you’ll see a dropdown of options with a link to the Products page. Navigate to Products page and create some products and product plans. In case you run into some issues, this short article should provide guidance.

After creating a couple of products and their respective product plans, click on the Developers section in the same left menu. You’ll notice a link to the API keys in the dropdown options that appear. Take note of both your Publishable key and your Secret key as you will need them in different parts of this small application.

Setting Up Our Lambda Service

Let’s start by globally installing the Serverless framework:

$ npm install -g serverless

If you don’t already have an AWS account, you will need to create one. As soon as you’re all setup with that, create an IAM user profile and store the user credentials in a secure place. You can then configure that profile with serverless like so:

$ sls config credentials --provider aws --key 1234 --secret 5678

Once that is done, you can head over to the local directory or folder where your lambda will live. The Serverless framework comes with a number of cool templates that we can use to generate some boilerplate code for our service. I opted to use the aws-nodejs-typescript template. So our next step is to run the serverless command (serverless or sls) that will create a new service:

# Create a new Serverless Service/Project $ sls create --template aws-nodejs-typescript --path react-stripe-subscriptions-backend # Change into the newly created directory $ cd react-stripe-subscriptions-backend

At this point, you should have a whole serverless app structure with a default handler.ts file that looks like this:

If you’re seeing some linting errors in your IDE or code editor, that’s probably because we’re missing a couple of dependencies so let’s take care of that now. We are also going to install the stripe node library which is the main module we’ll need for our service:

$ npm i --save aws-lambda node stripe

We want to be able to run our service offline before deploying it so while you’re at it, you can install serverless-offline:

$ npm i --save-dev serverless-offline

If you’re like me and you prefer to use familiar CLI commands (npm in this case), you can update your manifest file (package.json) by adding another script so that you can have the ability to start your service offline using the npm start command:

"scripts": { "start": "sls offline start --port 4000", "test": "echo \"Error: no test specified\" && exit 1" }

Next up, we need to update some configurations in our serverless.yml file by listing serverless-offline as one of our plugins, including some details of our handler function and specifying the region of our service.

service: name: react-stripe-subscriptions-backend # Add the serverless-webpack plugin plugins: - serverless-webpack - serverless-offline

provider: name: aws runtime: nodejs8.10 region: eu-west-1

functions: createCustomer: handler: handler.createCustomer events: - http: method: post path: create-customer cors: true

I know what you’re thinking “When are we going to get to the fun stuff?”, Hang on, we’re almost there.

Let’s update our current handler.ts file to have this createCustomer function we specified in out yaml config file. This function will receive three arguments:

stripeToken — a token containing the customer’s billing and address details email — email address of the user creating a Stripe customer account productPlan — id of the product plan that the customer is subscribing to

Next step, let’s create a folder a with a file that will contain the function createCustomerAndSubscribeToPlan which will make use of the stripe library and handle the creation of the customer and the product subscription.

Once you’ve got that function in, our createCustomer lambda should be good to go 🎉

To make sure that all is in order, you can give it a bash and try running it locally with npm start