The subscription model is a popular method for offering products or services on a recurring basis. Whether in streaming or SaaS services, businesses use this model to establish long-term relationships with customers by providing ongoing access to a product or service in exchange for regular payments.
Recurring billing is the backbone of the subscription model, enabling businesses to automatically charge customers at predefined intervals without manual intervention.
This tutorial shows you how to use North's Recurring Billing API to implement subscription billing in your Java application. You'll see how the API allows you to:
- Set the recurring period that works best for your business, whether weekly, monthly, or something else;
- Offer your users options for managing their subscriptions with features such as pausing, resuming, and canceling; and
- Refund payments easily and securely because every transaction is tokenized.
Let's dive in!

Prerequisites
Before you start the tutorial, you need to create an account on North Developer, sign up for the Recurring Billing API account, and generate the Custom Pay API credentials that include the EPI_id and the EPI_Key.
Since you'll use a Maven project to implement the Recurring Billing API in your application, you also need a Java IDE that supports Maven, such as Eclipse or IntelliJ . This tutorial will use Eclipse.
Create a New Maven Java Project
Maven provides a structured and standardized approach to managing Java projects by defining the project's structure, dependencies, and building processes in a project object model (POM) file. By automatically resolving and downloading dependencies, managing project configurations, and executing predefined build phases and goals, Maven simplifies the process of compiling, testing, packaging, and deploying Java applications.
To create a new Maven project, open Eclipse and click on File > Maven Project.
The New Maven project window will be opened.
Check Create a simple project (skip archetype selection) and click Next to see the Create new Maven project page. An archetype is a Maven project templating tool kit. You can select any archetype to create a Maven project based on the template. You can skip the archetype selection for this application because you'll create a simple project and configure it from scratch.
Click Next to see your new Maven project.
Enter the Group Id and Artifact Id for your project. The Group Id represents the project's package structure, and the Artifact Id is the project's name. When packed as a jar or war, the Artifact Id will be used as the project's name.
Enter the starting version for this project, and select war packaging. Because this is a web application, a WAR file is used to deploy a Java EE application into an application server such as Tomcat.
Click Finish to create the project.
Configuring Project Dependencies and Plugins
In this section, you'll learn how to configure the project dependencies necessary to create, build, and run the application.
The dependencies are handled in the pom.xml file. Open the pom.xml file and add the dependencies below in the dependencies section. (If the dependencies section is not available, you can add it using .)
- The Servlet dependency will be used to create a Java web application using servlets:
- The Google Cloud Firestore dependency will be used to store the subscription details in the document:
- The Gson library converts Java objects into their JSON representation. The Recurring Billing API accepts inputs as JSON. Hence, this library will be useful in converting Java objects into JSON:
You need to install an application server to run the application. You can use Apache Tomcat as the application server in this application.
Maven is a plugin execution framework, and every task is executed by plugins. To add plugins, create a build section using and add a section inside the build section in the pom.xml file.
To use Apache Tomcat as the application server, add the tomcat7-maven-plugin inside the plugins section of the pom.xml file:
Get in Touch
Talk to us about adding recurring subscription payments to your Node.js application today.
Using Google Cloud Firestore as a Database
In this tutorial, you'll use Google Cloud Firestore to store user profile information, credit card information, and the details about the user subscription.
You're using Google Cloud Firestore because it stores documents using a key-value format, and the data you'll handle in this application is in JSON format, which is a set of key-value pairs. However, you could also use any database of your choice with the Recurring Billing API, such as MySQL or PostgreSQL.
To create a database in Google Cloud, create a service account with the Cloud Datastore Owner and Firestore Service Agent roles. These roles let the service account create, update, and get documents from the Firestore data store. For detailed instructions on how to use a Firestore database, check out the Firestore documention .
After creating the service account, create a key, download the key file , and place it in the src/main/resources folder of your project. All the necessary details for authenticating the connection to the Firestore data store is available in the JSON file. Remember to update the name of the JSON file to your actual JSON file name.
Lastly, create a class called FireStoreHandler.java in the handlers package. Mention the package name handlers in the Package text box and place the following code into it:
These methods interact with the datastore to create, update, and retrieve the documents. It uses the credentials available in the JSON file to authenticate the connection.
Creating the Request Signature
The Recurring Billing API requires a signature to authenticate the API requests. The signature is the hash-based message authentication code (HMAC) of the endpoint and payload. HMAC ensures the authenticity and integrity of a request by verifying its source and detecting any alterations made during transmission.
To create an HMAC signature for the request, you will use the createSignature() method available in the Util class.
Create the following Utils.java class in the package utils:
It contains the method to generate a request signature based on the endpoint, the JSON payload, and the EPI key, a credential obtained from the North portal.
Creating a Recurring Billing App
This section explains how to create different servlets to handle requests sent by users, process them, and send a response back to the user.
You'll create three servlets:
- Subscription handler: a servlet to handle the subscription requests placed from the home and profile pages
- Home page: a servlet to serve the home page for the subscription. It serves the HTML page for subscribing to the application.
- Profile page: a servlet to serve the profile page of the subscribed users. It serves the HTML page for displaying the profile and the subscription information of the subscribed users. The user can also pause, resume, or cancel the subscription on this page.
Creating the Subscription Handler
Next, you'll create a SubscriptionHandler.java class in the handlers package. This class includes the methods to handle the subscription requests placed from the home page.
To create a new class, select the File > New menu to open the Select a Wizard page. Select the Class option in the wizard and click Next. The New Java Class wizard will be opened. Use handlers as the package name and SubscriptionHandler.java as the class name and click Finish.
To create the subscription handler, place the following code in the class you just created. Remember to update the EPI key and the EPI ID in the appropriate fields:
The subscription handler receives the Post request from the home and profile pages and handles them as follows:
- If the subscription ID is null or 0, it adds a new subscription using the /subscription endpoint.
- If the subscription ID is not null and has a proper numeric value, it checks the change requested to the subscription, whether pause, resume, or cancel. Based on this request parameter, it calls the appropriate API endpoint.
All the API endpoints, request specifications, and response models are available in North's documentation for subscriptions.
/subscription, /subscription/pause, and /subscription/cancel are invoked using the HTTP post requests as follows:
- A request URL is created using the API URL https://billing.epxuap.com and the appropriate endpoint.
- A request signature is created using the createSignature() method and the EPI_KEY, as explained above.
- An HttpRequest is built using the JSON payload, EPI-ID, and the EPI-Signature. The request is sent, and the response is received.
- The response is added to the Firestore database for displaying the information on the profile page.
For example, while creating a new subscription, you'll invoke the /subscription endpoint. If the subscription is successful, it returns a 200 response code in the HttpResponse. In the HttpResponse body is a JSON object called VerifyResult, and inside this JSON object is a nested JSON with a key code. Its response code is 00, denoting that the subscription was successfully created.
Add this response to Firestore and redirect the user to the profile page.
Creating the Home Page
Now you'll create a HomeServlet.java class in the demo package. This class includes the methods to create a home page and display the available plans as well as a button to subscribe to a plan. When the button is clicked, it'll send the appropriate request to the subscription handler to create a subscription.
Create another new class using the same steps as before, but use demo as the package name and HomeServlet.java as the class name. To create the home page, place the following code in this new class:
It creates the HTML form for getting the profile, payment, and subscription information from users. It also defines that a Post request should be sent to the /subscriptionhandler with the query parameter subId=0 when the form is submitted. Here, subId=0 denotes that a new subscription must be created with the available details.
This form allows the user to enter their profile information such as name and address as well as payment information such as credit card details. They can also select the subscription plan and payment intervals (weekly, biweekly, or monthly).
Creating the Profile Page
Now you'll create a ProfileServlet.java class in the demo package. This class includes the methods to display the subscription details such as the username, the subscribed plan, and a button to pause or cancel the subscription. When the cancel or pause button is clicked, it'll send the appropriate request to the subscription handler to modify the subscription.
Create a new class using the same steps as before but use demo as the package name and ProfileServlet.java as the class name. Place the following code in this class to create the profile page:
The profile servlet accepts a query parameter mailID. Based on this email ID, it will fetch the necessary documents from Firestore and display details such as name, address, credit card data, and other subscription details on the profile page.
It also creates buttons to pause, resume, and cancel subscriptions. When these buttons are clicked, a Post request is sent to the subscription handler with the parameters emailid, SubscriptionID, and the request type (pause, resume, or cancel). Based on these parameters, the subscription handler will process the request.
Running the Application
To run your Maven application, right-click on your Maven project and select Run As > Maven Build, which will open the Maven build Run configuration:
Enter a name for your configuration and enter tomcat7:run in the Goals.
Click Run to deploy your project on the Tomcat server.
You can reach your application on your browser using the URL http://localhost:8090/home, where http://localhost:8090/ is the local host address, 8090 is the Tomcat port number, and /home is the servlet path you have defined in the Home servlet class annotation.
When you enter the URL in the browser, you should see the home page.
Enter all the necessary details (most details are prefilled with test data) and click Submit.
The subscription will be created, and you'll be redirected to the profile page.
You can modify your subscription from here using the pause, resume, or cancel buttons.
How To Get Started
In this article, you learned how to let users sign up for and manage a subscription service in your Java application using North's Recurring Billing API. The code for this tutorial is available in [this GitHub repo {