Serverless payments solution

Serverless payments solution

This post is for software developers and architects wondering how to build and deploy enterprise applications that are highly available, scalable, resilient and most importantly deploy it with the lowest possible cost.

But why?

The basic requirements for any web application are to have high availability, scalability, and resiliency, but these criteria come with a high cost to deploy and maintain. So the goal of the architect is to design application in such a way that cost is very minimal. In our previous Serverless Architecture post, we discussed different ways of designing web applications and concluded that serverless architecture suits most of the applications meeting the above-mentioned criteria.

In this post, we will build an entire application with both client and server-side demonstrating step by step usage of serverless architecture. Let’s take payment solution applications use cases such as Paypal, Google Pay, Paytm, etc. and build our own. And you can find source code for both client and server-side in the GitHub repository.

The following video demonstrates the complete flow of use cases.

Following is the abstract block diagram, as I am more comfortable with AWS, we will be using amazon web services such as API Gateway, Lambda, DynamoDB, S3.

Following is the table of content of this post

  1. Build client application 
  2. Deploy client webpage onto S3
  3. Back end setup with API Gateway, Lambda, DynamoDB
  4. Integrating client and server

Build client application

As we are building a demo application, we will not build a full-fledged client application, hence we will write a static web page with ajax calls within it to interact with the backend server. And we will host this static webpage into AWS S3. This way we don’t need to have a dedicated machine, hence reducing hosting cost.

In payment solutions such as GooglePay and PayTM, we transfer money by scanning the QR code of the payee which uniquely identifies a person’s identity. Similarly, we need to create a QR code that uniquely represents a person using a URL parameter. So in our client application, we will launch a webpage with a person’s information on a scan of QR code by parsing URL parameters. Below is the webpage which read user unique identity from URL and considered payee as “Mahantesh”

 

We will generate unique url for each payee and generate it’s corresponding QR code like below

The first QR code is generated using the URL mentioned next to it with parameter “user=1”, so when a user scans the first QR code, its URL will be detected and once you click on that link, above webpage will be launched. On load of the page, call update page() method and parse URL and update the payee like below

function updatePage(){
	var username = getName()	
	var userEle = document.getElementById("user");
	userEle.innerHTML = username;
}

function getName() {
	var user = getUrlParams("user", 1)
	if(user == 1) {
		user = "Mahantesh"
	} else if(user == 2) {
		user = "Shilpa"
	} else if(user == 3) {
		user = "Sneha"
	} else if(user == 4) {
		user = "Adarsh"
	} else {
		user = "Idiot"
	}
	return user;
}

function getUrlParams(param, defaultValue) {
	var urlparam = defaultValue
	if(window.location.href.indexOf(param) > -1) {
		urlparam = getUrlVars()[param]
	}
	return urlparam
}

function getUrlVars() {
	var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

Deploy client webpage onto S3

AWS S3 is an object storage service, which lets you host static webpages. It is a low-cost hosting option when compared to hosting it in EC2 instances. As our webpage is not dynamic, we don’t need to host it in EC2. The following video demonstrates how to host static web pages onto S3 and making them public. We also need to generate QR code for each of the payees in our solution, each QR code will be associated with a unique URL, but as we have a static webpage, the URL will be unique. Hence we will differentiate each payee based on the URL parameter. We will assign different IDs for each payee.

In the following video, you can find detailed steps of hosting webpage onto S3 and generating QR codes for each payee in our application

Back end setup with API Gateway, Lambda, DynamoDB

Now let’s start with setting up server-side for handling transaction details. Traditionally we use Elastic load balancer(ELB), Autoscaling Group(ASG), Elastic cloud compute(EC2) for hosting web tier and use RDS machine for data storage service. But these services require a lot of effort from the developer and also have a high cost to maintain. Major cost consuming service is EC2, and even though EC2 will be charged for what we use, we will have to keep the minimum required instances running all the time. But we can reduce that by moving our computing logic to AWS Lambda(You can find details about it in our previous Serverless Architecture post). 

As ELB is used for distributing and balancing load across EC2, we will use API Gateway for handling incoming requests and distributing them to lambda functions. We need to receive payment transaction details such as payee_id, payer_id, amount, the reason for the transfer. To process that request, we will expose the PUT method for handling requests and redirect them to the lambda function, then lambda function can validate and insert that information into a database. 

First, we will set up API Gateway for handling incoming requests by adding a resource and a PUT method. And then we need to expose it to everyone by deploying it. Then we will create a lambda function for handling server logic to parse the information in the request and inserting it into DynamoDB. Then we will create a table in DynamoDB service and link them together. You can find all these steps performed in the following demo video.

In the following demo video, we will write a lambda function in a python programming language, and use boto3(AWS SDK for python) for interacting with DynamoDB service, and test complete flow from a client sending information in PUT request till data being inserted into the database.

 

Integrate client and server

Now both client and server are ready, so now it’s time to integrate both of them.  We need to update the new PUT request API on our client web page for it to send data. And as we are going to use ajax to send PUT request in our javascript file. And also we need to enable CORS on our API Gateway for letting browsers in client-side to bypass cross-domain resource sharing. you can find detailed steps in the following video  

 

The motive behind writing this post is to demonstrate that we can host as a simple web application as described above with minimal cost. Major cost consuming service is Lambda and S3, Lambda function will be charged for # of times it runs and for how long it runs, it will be charged negligibly as compared to EC2 instance. And as we are having static web pages, the size of the S3 content be will very minimal, Hence overall cost is very less as compared to hosting it traditionally with servers.  

2 thoughts on “Serverless payments solution

Comments are closed.

Comments are closed.