How to Build a Single-Page Sales Funnel App using Node.js, Cosmic JS and Stripe
Tags: node, stripe, cosmicjs, handlebars
Published: 2019-05-15 19:21:54

---
title: How to Build a Single-Page Sales Funnel App using Node.js, Cosmic JS and Stripe
published: true
description: Single page app that allows you to showcase you digital products and accepts credit card payment online. Very similar to the marketing sales funnel, but done in simple single page sales funnel.
tags: Node.js, Stripe, Cosmic JS, Handlebars
---

# How to Build a Single-Page Sales Funnel App using Node.js, Cosmic JS and Stripe

<img src="https://cosmic-s3.imgix.net/fba84950-7534-11e9-97c3-f3f149e9cf49-smartmockupsmain.jpg?w=1000" style="display: block; margin-left: auto; margin-right: auto;"/>

What is a sales funnel? And why is it important? A sales funnel is a path that your website's visitors take before purchasing a product. If you don’t understand your sales funnel, you can’t optimize it. Building a great sales funnel can influence how visitors move through the funnel and whether they eventually convert. In this little demo I will be showing you how to build a simple, one-page sales funnel using Node JS, Cosmic JS, and Stripe for handling credit card payments.

## TL;DR
* <a href="https://cosmicjs.com/apps/single-page-sales-funnel" target="_blank">Demo</a>
* <a href="https://github.com/cosmicjs/nodejs-sales-page" target="_blank">Source code</a>
* <a href="https://stripe.com/docs/stripe-js/elements/quickstart/" target="_blank">Stripe</a>
* <a href="https://cosmicjs.com/" target="_blank">Cosmic JS</a>

## Why did I pick Cosmic JS
If you need to build a sales page quickly and freely, you can simply sign up for a free Cosmic JS account, copy my app, customize it and you're done. You'll have you sale funnel page ready in minutes. If you need to add a section or features then you can simply download the source-code and change any of the markup or the JavaScript behind this application.

## How to clone your own version of this app:
In order for you to clone this application and have your own custom copy, you need to following these steps:

* Sign up for a free Cosmic JS account
* Login into Cosmic JS account
* Go to the <a href="https://cosmicjs.com/buckets">buckets</a> page
* Click on `Add New Bucket` button on the top right of the page
* Select `Choose an app below option` and scroll to the applications list
* Try to find the `Sales Funnel` application
* Once you open the application click on the `Install Free` button
* You can keep the same title and hit the `Install App` button to confirm
* Then you will be taken to the application bucket
* Then you need to clone the github repo. Open the terminal window and type:

```
git clone https://github.com/cosmicjs/nodejs-sales-page
cd crowd-pitch
npm install
```

* edit and change Cosmic JS and Stripe API keys located in `/crowd-pitch/.env.local` file

```
COSMIC_BUCKET=your-bucket-slug
COSMIC_READ_KEY=your-cosmic-read-key
COSMIC_WRITE_KEY=your-cosmic-write-key
STRIPE_KEY=your-stripe-api-key
```

* Run the app server on your local machine

```
# start the app
npm run server

```

* open the application from your browser on <a href="http://localhost:3000">http://localhost:3000</a>

## How to customize the application content
In order for you to change the text, images, and application content you need to follow these steps:

* Login to <a href="https://cosmicjs.com/buckets">Cosmic JS dashboard</a>
* Go to buckets --> crowd-pitch
* Go to Pages --> Google cash machine
* Change the pages section by editing the text for each part of the web page like the page title, header....
* Change the page images by adding your own images. Make sure to keep the same image title and slug.
* Hit save and publish
* This part works like any CMS system, where you make changes to the back-end and the site can change immediately.

## How to add new features to this application
This part and below would be an explanation on how the application front-end was developed and how you can dig deeper to customize more options like the layout, css, colors, and which fields to collect from the user. This application was build mainly using Node JS, and Stripe API. So let's take a look at the server.js file

{% gist https://gist.github.com/mtermoul/3ac8e7957a1edb9069a2dc8eff171798 %}

As you can see from the code above we are using the following Javascript components:

* <a href="https://www.npmjs.com/package/express">Express:</a> a light weight web server for node
* <a href="https://www.npmjs.com/package/express-handlebars">Express-Handlebars:</a> This html templating library for handlebars.js syntax
* <a href="http://handlebarsjs.com/">CosmicJs:</a> to handle interaction with Cosmic JS API from the server
* <a href="https://www.npmjs.com/package/stripe">Stripe:</a> to handle interaction with <a href="https://stripe.com/docs/payments">Stripe payment API</a>

Inside the server.js there is basically two function to handle server routing:

* app.get('/') to handle the get request when the user visits the application. Inside this function we simply call Cosmic JS to fetch all data from our bucket and inject it as a server response local variable. The second part is just rendering the home view which is just an HTML/Handlebars template page.
* app.post('/pay') to handle the payment form posting. Inside this function there is simply one call to Stripe charges API to add one charge to the specified credit card.

## How to customize the layout and CSS for the application?
As mentioned before, in this app we are using handlebars.js for the page templating. From the server get function we render `views/home.handlebars` page which is simply an html page with some handlebars tags to replace server variables with values from Cosmic JS CMS. Let's take a look:

{% gist https://gist.github.com/mtermoul/9cbf6fc2afaed53b37f82cd5f1acd111 %}

As you can see, we reference server variables within a double curly brackets. For instance `{{ cosmic.metadata.top_logo.url }}` means get the value of variable from Cosmic JS, which the logo image url and put it in the home view page. There are also similar syntax to handle if and loop using handlebars syntax. For a full syntax help, please refer to <a href="http://handlebarsjs.com/">Handlebars User Documentation</a>.

You can also change some of the styling of the page from within home view page because we are simply using <a href="https://getbootstrap.com/docs/4.3/getting-started/introduction/">Bootstrap framework</a>. For some other styling properties you can change it directly from `/public/css/styles.css`

For the application layout there the file `/views/layouts/main.handlebars`

{% gist https://gist.github.com/mtermoul/c236fb329dcb67a2d75841777c2a5ac0 %}

This is basically the main html template for every page in our application. It's worth mentioning that we are referencing a couple of libraries from the client side like:

* JQuery
* Stripe
* Bootstrap
* Font-awesome
* Axios.js to handle Ajax calls

## Handling credit card payment with Stripe and Axios
To accept credit card payments in our application we use three steps process.

first we use <a href="https://stripe.com/docs/stripe-js/elements/quickstart">Stripe card elements</a> to inject the credit card input element inside our html form.

{% gist https://gist.github.com/mtermoul/e20ee070d2d49673e60af22be432e48b %}

* Validate the credit card info by calling stripe.createToken when the user hit the submit payment button. This function will simply send the info to Strip API and get a valid token if success. Otherwise it will return an invalid token.

* Post the payment form form data to the server post method. This method will take data submitted from the client form and submit them again as a stripe charge by calling stripe.charges.create. Take a look:

{% gist https://gist.github.com/mtermoul/4e2c5adef4236256c1f907af2d100932 %}

As you can see, once the charge is sent to stripe, we will return success to the client if no errors from Stripe. Otherwise we will emit Stripe errors back to the client.

* The last step is on the client side, we will display the payment result on the client form if success, or the error message if the payment fails.

## How to add multiple page funnel to my application.
Sometimes the application will need more than one page to capture the user final action. If you want to do that, you can simply add more pages to the server view, more routes, and handle the post from one page to the other either via javascript functions or server post methods.

## What about the main.js file?
This file contains one function right now to handle count down counter. However you can use it if you any other javascript interactions with your user. For the count down counter we store a variable on Cosmic JS server called

```
const dealCountDown = {{ cosmic.metadata.deal_countdown }};

```

and then we calls the `initializeClock` function which will run the count down until this variable reaches zero.

Take a look at the main.js file for the full implementation details.

<img src="https://cosmic-s3.imgix.net/e8530730-7536-11e9-add3-5fb6959d341f-crowd-pitch-preview.gif" alt="Crowd-Pitch main view" style="display: block; margin-left: auto; margin-right: auto;">

## Check user engagement with A/B testing
As most marketers have come to realize, the cost of acquiring any quality traffic can be huge. A/B testing lets you make the most out of your existing traffic and helps you increase conversion without having to spend on acquiring new traffic. A/B testing can give you high ROI as sometimes, even the most minor changes can result in a significant increase in conversions.
For the purpose of this app, I will add two `style.css` files and once the user visits the site I will be randomly selecting one stylesheet. The stylesheet selection will affect how the color theme will look like. So basically the user can see either version A of the site or version B.

{% gist https://gist.github.com/mtermoul/80e8afc5fcc30ae6e0b58abb0f2df6d2 %}

Then we load either style A or B on the `main.handlebars` file like this:

{% gist https://gist.github.com/mtermoul/7075eb49b89e17c327858a201b12a599 %}

We also have a javascript variable that will be used during the payment process to capture which page the user is coming from. This info will be stored along Stripe charge information.

```
    const pageSource = {{#if pageB}} 'pageB' {{else}} 'pageA' {{/if}};
```

then we save the page source along the stripe charge on the server post method.

{% gist https://gist.github.com/mtermoul/92a1c9c9e50aded4c0e3d50afc25a996 %}

## Conclusion
In this application, I've demonstrated how you can build a page that displays product info and handles credit card payments by leveraging the power of the Cosmic JS CMS plus a few other libraries that handle the payment function. You can add a function that will send an email to the user after the payment is submitted. Or add a function to send a user to another secure page to all him to download the digital product. The Cosmic JS Community  has a lot of examples on how to handle integration with email functions, download functions, and third party platforms.

As always, I really hope that you enjoyed this little app, and please do not hesitate to send me your thoughts or comments about what could I have done better.

If you have any comments or questions about building apps with Cosmic JS, <a href="https://twitter.com/cosmic_js">reach out to us on Twitter</a> and <a href="https://cosmicjs.com/community">join the conversation on Slack</a>.