Stripe Payment Gateway with Golang (GIN),GORM and Mysql
By: Bikash Dulal
Tags: MySQL, Golang, Payments, Stripe, Payment Gateway

Before we start let discuss about what is payment gateway?

Well,Payment gateway is a technology that merchants accepts the debit card or credit card from customer while purchasing goods from online. This is amazing isn’t is?.

Payment gateway is the essential part for any e-commerce business to be successful and it is a integral part of any e-commerce organization. The world is upside down with corona virus and they are shifting toward the online payment.

There are lots of payment gateway service provider like Stripe ,Paypal, Amazon pay, Payoneer, WePay and many mores. In this tutorial I am going to use Stripe Payment gateway and excited to share my ideas with you.

What is Stripe Payment Gateway?

It is a famous payment gateway that helps in financial transaction between merchants and customers. It accepts 133+ countries currency . It is a powerful payment engine that makes money move easily. Millions of big company like Google, Shopfiy, Salesforce, Uber ,Slack use Stripe payment gateway. This makes you crazy isn’t it?

Now its time to focus on our Work :

I am going to use Golang Gin framework which claims 40X faster than other platform. Gin framework provide faster, robust and great performance. So I decided to use this platform.

Before start I assume you have mysql , code editor and postman install in your device.

Lets start our work:

Go and create a folder for Stripe payment Gateway in your directory.

Now Inside that folder create three folder called bin, pkg and src. I assume you already know about these folder.

Open Folder in your favorite code editor. I am using famous vs code. Now all set to go.

Now Its time to install our packages

Init mod

go mod init

Install gin

go get -u github.com/gin-gonic/gin

Install Stripe

go get github.com/stripe/stripe-go

Install GORM

go get -u github.com/jinzhu/gorm

Once all packages installed lets go inside src and create a folders. We are using MVC pattern so to save information in our mysql lets create a config folder and inside config folder create a mysql.go file . In this file we are config our application with mysql database. Config you database accordingly. I configured as:

Once you configured your database go and create models for the application. Inside models we defined our attributes that are going to save in our database. I created a models and inside model folder create a charge.go file. I created a three models you can create accordingly. We are using gorm which provides attribute like ID created_at, deleted_at and updated_at automatically. GORM is a fantastic library for any golang developer. Using the advantage of gin and gorm I am creating table “charge” automatically.

Once model is created now create a service folder inside src and create a file name charge.go In this section we save models information in our database . In this section we create a function SavePayment which save payment information in our database.

Now Create a controller folder inside src and inside that we need to create a controller.go file which control our api. This is the important section of our application. We need apikey provided by stripe. we will discuss later how to generate secrete key in stripe. we need to pass charge parameter. we provide amount, description of transaction, source of payment and and receipt mail address. It accepts in cents. so we may need to calculate accordingly. I payment is unsuccessful we handle error as shown below.

Now we need to create a router file. it routes our api. For that go and create router folder and inside router folder create route.go file. we user payment function inside our controller and we create a post route method.

All set to go now go and create main.go file inside root folder. main.go is the file that initiate our application.Inside our main.go file

Now go deep inside stripe website. Go to

www.stripe.com and create a account if you dont have.

Once you create a account go to dashboard

Now go to developer >> Api key section of the dashboard. There we can create as many as for different project we need . I am using default standard key .

Now its time to reveal test key token i.e secrete key and add in our project which i mention earlier . My suggestion is user env file to store key.Now all good to go.

Go to code editor and and start our application

Don’t forgot to start your mysql server

go run main.go

Open postman service and hit http://localhost/stripe/payment

Now go to our stripe dashboard and see what happens

BOOM !!! I receive my payment

Did you receive your amount??

Check your mysql database . Mine is below and it works as expected!!

As always all the codes are in git repository

https://github.com/Bikash888/stripe.git

Well, this is the simple tutorial on stripe with golang and you can go and learn more. I hope you enjoy this tutorial.

See you in next article

Thank you !!!