Implement an Stripe payment flow using NestJs & mongodb
Go to Stripe to create an account
Online payment processing for internet businesses - Stripe
Online payment processing for internet businesses. Stripe is a suite of payment APIs that powers commerce for online…
Navigate to left menu > Developers > API keys, there is a publishable key and secret key, publishable key is used in front end below checkout
Imagine there is an checkout form in web / app, you input credit card, CVC, expired month and year in the form and checkout, In stripe, you need to use those information to get the source id of this payment
Below we use postman with the publishable key to get source id
You can copy your publishable key to the environment variables of postman, and request body are show below as demo, for testing card number, you can base on official document from stripe, https://stripe.com/docs/testing#international-cards
Then you can get the result show below
the “id” which is the source of this payment when call checkout API in our node js
In previous tutorial here, We have created an order, then next, we need to pay for this order, so we build another module call Payment
Since we just want to show the flow, in detail we ignore how to well define status in enum (later other tutorial will mentioned it)
A simple checkout model define as below, since the currency will follow by the order module, so we just need source id and the amount.
Next, We need to define a stripe helper class to help us handle the charge API with stripe.
We will use createCharge() method, which handled convert the amount (*100) before call stripe API.
Next, We create payment checkout API as below
and corresponding service
Then use postman to make a request of checkout API.
id is the return transaction id from stripe.
Go back to stripe console, Navigate > Payment menu item
At the same time, Payment collection also saved transaction information.
The source code is here, hope this help.