A simple payment simulation program in Go.
The functionality consist of login, payment and logout.
The models consist of users, merchants and transactions.
type User struct {
Id int `json:"id"`
Name string `json:"name"`
Password string `json:"password"`
Balance int `json:"balance"`
}
type Merchant struct {
Id int `json:"id"`
Name string `json:"name"`
Balance int `json:"balance"`
}
type Transaction struct {
Id int `json:"id"`
From int `json:"from"`
To int `json:"to"`
Amount int `json:"amount"`
CreatedAt string `json:"created_at"`
}
The APIs are as follows:
[POST] /login
username string
password string
-- set login cookie & redirects to payment.html --
[GET] /logout
-- remove login cookie --
[POST] /pay
merchant int
amount int
{
"id": 2,
"from": 1,
"to": 3,
"amount": 40,
"created_at": "2021-10-29T10:22:50+07:00"
}
[GET] /payment
-- execute payment template, payment.html --
[GET] /users
[
{
"id": 1,
"name": "kervin",
"password": "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO",
"balance": 90
},
{
"id": 2,
"name": "admin",
"password": "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO",
"balance": 5000
}
]
[GET] /user/{id}
id int
{
"id": 2,
"name": "admin",
"password": "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO",
"balance": 5000
}
[GET] /merchants
[
{
"id": 1,
"name": "Merchant A",
"balance": 1000
},
{
"id": 2,
"name": "Merchant B",
"balance": 500
},
{
"id": 3,
"name": "Merchant C",
"balance": 240
}
]
[GET] /transactions
[
{
"id": 1,
"from": 1,
"to": 1,
"amount": 1000,
"created_at": "2021-10-21 00:00:00"
},
{
"id": 2,
"from": 0,
"to": 3,
"amount": 40,
"created_at": "2021-10-29T10:22:50+07:00"
}
]
Run the command: go run main.go to run the program.
You will be prompted to login before you can access the main page.
Use this credential:
username: admin
password: pass123
You will be redirected to the payments page. Here you can select which merchant you want to transfer money to, choices are Merchant A, B and C.
After you click pay, balance from user admin will be deducted and balance of the designated merchant will increase.
You can open http://127.0.0.1:10000/transactions to see the list of transactions.
Core routes:
http://127.0.0.1:10000/login
http://127.0.0.1:10000/payment
http://127.0.0.1:10000/transactions
http://127.0.0.1:10000/users
http://127.0.0.1:10000/merchants
http://127.0.0.1:10000/logout