DEV Community

Marzee
Marzee

Posted on

Connecting Stripe with AWS Serverless holy grail: Cognito, Lambda and DynamoDB

Payment gateways are the lifeline of any business, and it doesn't take much to understand why, healthy businesses generate revenue by providing products and/or services to customers, these revenue streams cannot exist without a reliable and secure gateway that abstracts away all the pains of processing payment transactions.

When it comes to payments a couple services spring to mind: Paypal, Stripe, Amazon Pay and Square (honorable mentions to Adyen and Braintree). We chose Stripe because it has a wonderful developer experience, the documentation is very clear and their libraries are easy to use and integrate.

If you've read some of our blogposts in the past you'll know that we make great use of the Serverless model, the "Impulso" project like many others is powered by the AWS Serverless holy grail: Cognito, Lambda and DynamoDB, which map to Authentication, Compute and Storage, respectively, having this model in mind, we needed a payment gateway that could easily integrate with it, Stripe proved to fulfill that role very well.

There are three parts to this problem:

Integrating Stripe into our authentication/authorization system
Syncing latest payment/billing information from Stripe
Providing billing management to users
In Stripe's API the most important resource is the "Customer", we had to fit this into AWS Cognito's lifecycle to create a 1:1 mapping, luckily Cognito has this thing called "Triggers" that allow you to hook custom code (Lambda functions) to different events, this was a lifesaver because it meant that we could create and associate a Stripe "Customer" as soon as a user registered, thus solving part one of the problem which is identity, from now on whenever a user is logged in we can be certain that there is an attribute of that user we can use to identify him within Stripe's system.

Next, we move on to authorization, the second part of the problem, this is one of the most crucial aspects for this platform in specific, as this is a subscription based system, so we need to make sure users don't get access to paid features if their subscriptions expire, and of course, guarantee that they do get access for as long as their subscription is active, I think we can all agree that there's nothing worse in terms of user experience as a buggy system that works well enough to take your money... but not to provide you with the services you paid for, the horror!

Luckily, Stripe has got our back, all we need to do is create a special endpoint on our side, then we let Stripe know of its existence, this is commonly known as a "webhook" (or "web callback"), from that point forward every time some important event happens within Stripe's system we get notified, isn't that great? The event could be a new subscription, a failed subscription renewal, a chargeback, and many more types depending on what you're trying to achieve, the possibilities are endless.

Last but not least, management, after a user registers they most certainly need a portal to manage their payment details and iniciate the billing cycle if they desire to, and let me tell you, we were delighted to find out that not only does Stripe provide a ready to use and customizable hosted UI but also makes it a breeze to setup, and that's how we saved ourselves multiple hours designing and implementing a billing UI, which ultimately made the project cheaper for our client, textbook example of a win-win situation.

In the end we were very happy with the technical solution, it's amazing how much value we added to the "Impulso" project by leveraging Stripe, especially taking into consideration that this integration wasn't part of the original design when we started the project, it was genuinely surprising how straightforward it was to integrate with the existing system.

Blogpost originally posted in Marzee's website: https://marzee.co/blog/connecting-stripe-with-aws

Top comments (0)