How to Build a Subscription Box With Stamplay

Final product image
What You'll Be Creating

Subscription boxes are an increasingly popular business these days. You can find them for everything from grooming products to healthy snacks, kids games and even ice cream. In this tutorial, I’ll show you how to quickly build a web app to test if you can find the first 1,000 customers for your subscription box idea!

Our app is inspired by cocktail subscription service Shaken and includes the following features:

  • Sign up users via Facebook.
  • Activate recurring payments via Stripe.
  • Pause and reactivate subscriptions.
  • Send transactional emails.
  • Add users to a MailChimp list.

This tutorial will show you how to configure a full-featured back-end and how to build the AngularJS front-end that hooks up to it.

The full codebase of the front-end can be found on GitHub.

Coupon: Stamplay has provided Tuts+ readers an exclusive coupon to provide 6 months of the plus plan for free ($600 value). The coupon code is CODETUTSPLUS and it expires on October 10. Sign up now for free to redeem it.

1. Stamplay: API Lego for Developers

Stamplay is a web-based development platform that gives developers an incredibly fast way to build fully integrated applications. Stamplay handles the trouble of talking to many different APIs so you don’t have to.

Stamplay is built around the concept of components; components are the building blocks of our application. We can see the different tools available to us under Tasks > Components.

Components available in Stamplay

Once logged in, you are greeted with this simple and elegant dashboard. We’ll create an application here. Name it whatever you please, but remember that this name is what we’ll use to identify our application. This will also be used as the URL for our application when it’s hosted.

In our case, we are using "tutsplus", and our application will be hosted at https://tutsplus.stamplayapp.com.

Now let’s start configuring the back end.

1.1 Configuring the Back End: Integrating Facebook Signup

This app will feature a Facebook login. We’ll be able to interact with the Facebook API and handle login and signup, all without a single line of code. Since we want our users to sign up with Facebook, we’ll need to head over to the Facebook Developers page and create a new developer app.

Add a New App on Facebook

Select Website, and once you get your app created, we’ll be able to grab two things that we need from Facebook to integrate with Stamplay:

  • App ID
  • App Secret

These two things can be found under Settings > Basic.

Dashboard showing App ID and App Secret

Now that we have our App ID and App Secret, we can plug those into Stamplay.

Head back to Stamplay and select Users from the main menu, and then Authentication. Add Facebook as the signup service, copy and paste the App ID and App Secret, and click Save.

You’re now able to register users via Facebook!

User authentication

Now that we’ve hooked up our Facebook credentials, Stamplay provides a convenient API URL to handle logging in with Facebook. Let’s go there now and see our Facebook login in action. 

Go to https://tutsplus.stamplayapp.com/auth/v1/facebook/connect.

And boom! You have your Facebook login up and running.

1.2 Configuring the Back End: Managing Data With Objects

Not everything can be handled with components, so Stamplay provides us with an easy way to handle custom data. Think of this as the back-end database interface for your application.

Orders will be tracked with an Object called orders. It has the following five properties:

  • active (boolean) // to track whether the subscription is active or not
  • address (plain_object) // user’s address info
  • plan (string) // the plan the user subscribed to
  • stripesubscription (string) // the subscription Id generated by Stripe
  • user (user_id) // the unique _id of our user
Orders page

Stamplay will instantly expose RESTful APIs as soon as you create the object. The endpoint will look like this: https://APPID.stamplayapp.com/api/cobject/v1/orders.

1.3 Configuring Stripe

We’ll need to bill our customers periodically, and thankfully this is easy to do with Stripe! Its recurring payment processing is implemented within the Plan concept. Stripe Plans are objects representing the cost and billing cycle of your subscriptions.

To set this up you’ll need to create your plan in the Stripe console > Plans:

Create new plan popup

Once done, head back to Stamplay and connect the Stripe component in Task > Components. Select the Stripe module and click the Connect button.

Now the Stripe account will be linked to the Stamplay app, which can perform API requests on our behalf.

After a successful Stripe connection you’ll end up on a view like this one.

Stripe account info and settings

1.4 Configuring MailChimp

To configure MailChimp, the process is pretty similar; simply connect your MailChimp account as you previously did with Stripe, and you’ll see a page like this:

Mailchimp connection page

2. Adding Automation With Tasks

This is the server-side part of our application, where the bulk of the magic happens! Let’s say that we want do a couple of things:

  • Email a “Welcome” message when a user signs up with Facebook.
  • Add users to a MailChimp list when they sign up.

These two things would normally require a good chunk of code, but not with Stamplay...

2.1 Welcome Email

A wizard will walk you through the configuration of the task. Under Tasks > Manage, create a new Task and set it up the way you want. Here’s how we’ve set up the welcome email task:

New Task window

Then you can configure the action leveraging the variables coming out from the trigger (in this case, the user). Here’s how we configured ours:

Welcome email configuration

2.2 Push Users to a MailChimp List

Pushing users’ emails to your mailing list on MailChimp is straightforward.

New task window for MailChimp signup

Under Tasks > Manage, create a new Task and set it up the way you want. Here is how we’ve set up the MailChimp subscribe task.

MailChimp task configuration

3. The Front End

To start working on our front end, we’ll need to use the Stamplay CLI tool.

3.1 Install the Stamplay CLI

This tool will give us the ability to work with our Stamplay applications. It allows us to:

  • deploy applications
  • start a local server for development
  • initialize a Stamplay project
  • roll back versions
  • download the latest version

Let’s create a new folder and initialize our Stamplay application to that folder.

You’ll be prompted to enter your appId (name of your app) and your apiKey. In this case, they are tutsplus and 4****0 respectively.

This will create a stamplay.json file in the root of your app so that when deploying, Stamplay will know your credentials.

3.2 Building the Front End

To hit the ground running with a good design, we started from one of the many cool free HTML5 templates powered by Templated.co. The one we used is called Retrospect.

Let’s download it and unzip it to our project folder. Then let’s install some additional libraries here using Bower. Run the following command and we’ll be good to go:

In order to use Stamplay with Angular, we will need to:

  • load the Stamplay SDK
  • configure the Stamplay application
  • load Angular
  • load Angular UI Router
  • load the Angular-Stamplay library (just a service wrapper for the SDK)

Here are the lines required to do all of that. We’ll add them below the </footer> tag of our index.html file:

Make sure you’ve typed your own AppId into the Stamplay.init function. We can now start our local server to make sure everything’s loading correctly. Run the following:

You will see that your application is ready to go at http://localhost:8080.

3.3 The Angular Application

We’ll be creating some files for our Angular application:

List of files and folders for Angular app

The two services files will use the Stamplay SDK and angular-stamplay libraries to help our application interact with our Stamplay data.

The most important is userService.js. It has a few functions and helps us grab data or create data. For a more in-depth look at what the Stamplay SDK is capable of, be sure to check out the documentation.

UserService.js leverages the Stamplay JS SDK to provide to our Angular app the following functionalities:

  • login
  • logout
  • isLogged
  • saveAddress
  • getUserModel
  • createCard
  • subscribe
  • unsubscribe

/assets/js/services/userService.js

Our application will have three pages:

  • home.html, the main landing page where the user will be able to sign up
  • subscriptions.html, where users can view the boxes available for a subscription
  • profile.html, where users can edit their information and cancel a subscription

These pages are managed by three controllers (as suggested by best practices), which are homeCtrl.js, subscriptionCtrl.js, and profileCtrl.js.

Page templates and controllers are tied together by the main Angular app.

Here’s the foundation of our app.js:

Now we just need to load our new files in index.html below the libraries we added earlier.

/index.html

3.4 Front-End Authentication

We wired up our Facebook application earlier, and now we need to provide our users a way to use it! All of the authentication is handled by Stamplay. We don’t need to code anything up; we just need to link our users to the authentication path that Stamplay has provided for us.

The authentication process looks like this:

  • Link users to our authentication URL.
  • Authentication happens on Facebook.
  • Facebook sends a user back to our application with a token.
  • Stamplay handles storing that token and our user is authenticated.

We only need to manually configure the first of those steps. Let’s apply our Angular app with ng-app and ng-controller to index.html and add some Angular directives to connect the homepage with our homeCtrl.js. We are also going to remove everything between the <nav> and the <footer> of the original template and replace it with a  <div ui-view></div> tag element.

This element will dynamically display the content of the current page.

/index.html

When the application starts, the router will check the URL and load the appropriate view. Inside the first <section> of home.html, we can easily spot all the Angular directives that paired with the homeCtrl.js functions.

/pages/home.html

/assets/js/controllers/homeCtrl.js

Now if a user clicks the “Become a member" button, they will be asked to log in with their Facebook account. We had to deploy because authentication won’t work from localhost:8080. Facebook wants to return a user to a full qualified domain.

At this point, deploy your application again using:

After a user successfully logs in, we want them to be able to subscribe to one of our plans.

3.5 Creating Stripe Customers

Customers are the other main component of subscriptions. In a broad sense, a customer is just a generic way to associate your own users with your Stripe account. Typically you'll want to associate some metadata, like an email address, with a customer. Customer objects can also store a credit card, which is how they'll be billed later on.

Creating a new customer via the API is easy, as the Stamplay JS SDK already provides all the required support. Our app associates a Stripe customer to our own users right after the signup or login—this is managed via userService.js in the getUserModel function.

/assets/js/services/userService.js

The code above verifies whether the user is logged in or not, and if they already have a stripeCustomerId property. If not, it creates a Stripe customer with $stamplay.Stripe().createCustomer(user.get('id')) and stores the stripeCustomerId as an additional property of the logged user.

This control is triggered every time the app starts by the app.js Angular module.

/assets/js/modules/app.js

3.6 Subscribing to a Plan

Now we need to finally charge our user when they purchase a plan. The template subscriptions.html, tied to the controller subscriptionCtrl.js, displays to our users the subscription plans they can choose from.

Join Your Spirit Way

When the user selects one of the subscriptions, the payment form will be displayed, allowing the user to finalize their purchase. The subscription page lists the articles at the very top and contains two more sections (the payment and thank-you sections), both hidden.

/pages/subscription.html

In order to pass the information related to the selected subscription we’ll use a function called openSubscribe(‘planName’) from subscriptionCtrl.js.

/assets/js/controllers/subscriptionCtrl.js

This function makes the payment form visible that will be displayed along with user data and the selected plan.

This form collects the user’s information, the selected subscription, and the credit card values.

To collect credit card values we need to add Stripe’s JavaScript client to get a token for the card, and then we use Stamplay’s Stripe function to finalize the plan. In our index.html, let’s import the Stripe.js library and initialize it with our test key.

/index.html

Finally, the subscription can be processed. When Confirm Subscription is clicked, our subscriptionCtrl.js starts the subscription process, leveraging userService.js.

Payment Information

/assets/js/controllers/subscriptionCtrl.js

Before saving the credit card, let’s update the user with their address information. We’ll see later why this is useful at this stage.

A user can be bound to only one credit card. This is why the createCard function checks whether the user already has one with $stripe.getCreditCard(user.get('id')). If no card is present, then we proceed to add the card.

Before binding the card to the user, we need to add the Stripe client-side JavaScript library to securely convert the credit card values to a token. Once we have the token, we can pass it to Stamplay JS SDK and bind the card to the user $stamplay.Stripe().createCreditCard.

/assets/js/service/userService.js

To test the Stripe implementation, we prefilled the credit card information with the test values below. For information on testing in Stripe, check out the documentation.

  • n.4242 4242 4242 4242
  • date: 07/2020
  • cvc: 424

The subscription flow then continues by calling the userService.js subscribe function. This completes the process by submitting the subscription request to Stripe.

/assets/js/services/userService.js

The subscription function is simple. Once we ensure that the user exists and has a credit card, we only need to call $stamplay.Stripe().createSubscription.

The last step is to keep track of the subscriptions by creating orders and receiving notifications for each new subscription that we sell via the website. Let’s go back to Stamplay and set up some tasks.

4. Adding Final Automation

We want to bind subscription data to an order and vice versa. This will allow our users to deactivate their subscription if they choose.

4.1 When a New Subscription Is Submitted, Create an Order

On new subscription then create an instance of order

When a subscription has been submitted, we want to create a new instance of an order object. The order will keep track of the subscriptionId that has just been created by Stripe, the userId, the plan, and the address.

When a task is triggered by a logged-in user, we can use any of their properties to feed the action that follows. This is why we tied the address to our users in subscriptionCtrl.js. This lets us store them in the order object even if the subscription trigger’s data doesn’t provide this information. So you can see the order’s address property being filled with data fetched from the user, e.g. {{user.address.city}}.

Create a new order when a user subscribes to a plan

4.2 When a New Order Is Made, Bind It With the Related Subscription

To have a reference of the order also in the subscription on Stripe, we can update the subscription’s Metadata field as follows. First let’s select the appropriate trigger and the action from the Objects and Stripe component.

On create order add id to subscription
Configure trigger

Since we saved our order to the Subscription Id, we can now edit Stripe’s subscriptions by Id.

Configure action

4.3 When a New Order Is Made, Notify Both the User and the App Administrator With an Email

Confirmation email for plan subscription
Configure action

Conclusion

In this tutorial, we reviewed how to get started with creating a web application using Stamplay. We implemented the social sign-in functionality and successfully authenticated the user against the Stamplay database. We created RESTful APIs to manage orders. We’ve been able to connect our app to the MailChimp and Stripe APIs and glue them together with a few back-end automations.

Coupon: Stamplay has provided Tuts+ readers an exclusive coupon to provide 6 months of the plus plan for free ($600 value). The coupon code is CODETUTSPLUS and it expires on October 10. Sign up now for free to redeem it.

Manage your tasks

Code from the above tutorial is available on GitHub. Do let us know your thoughts in the comments below.

Tags:

Comments

Related Articles