Skip to content

Latest commit

 

History

History
185 lines (156 loc) · 4.22 KB

README.md

File metadata and controls

185 lines (156 loc) · 4.22 KB

Payments-Go

A simple payment simulation program in Go.

The functionality consist of login, payment and logout.
The models consist of users, merchants and transactions.

User Model:

    type User struct {
        Id       int    `json:"id"`
        Name     string `json:"name"`
        Password string `json:"password"`
        Balance  int    `json:"balance"`
    }

Merchant Model:

    type Merchant struct {
        Id       int    `json:"id"`
        Name     string `json:"name"`
        Balance  int    `json:"balance"`
    }

Transaction Model:

    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:

Auth

Login

[POST] /login

Parameters:

username string
password string

Response:

-- set login cookie & redirects to payment.html --


Logout

[GET] /logout

Parameters: none

Response:

-- remove login cookie --


Payment

Pay

[POST] /pay

Parameters:

merchant int
amount int

Response:

    {
        "id": 2,
        "from": 1,
        "to": 3,
        "amount": 40,
        "created_at": "2021-10-29T10:22:50+07:00"
    }

Payment Page

[GET] /payment

Parameters: none

Response:

-- execute payment template, payment.html --


Users

Get users

[GET] /users

Parameters: none

Response:

    [
        {
            "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

[GET] /user/{id}

Parameters:

id int

Response:

    {
        "id": 2,
        "name": "admin",
        "password": "$2b$10$knfJ.4rmOOdY5vDcOQZYeOPOsjO2Mwpl5KnPwN8j.4RIY5hekklSO",
        "balance": 5000
    }

Merchants

Get merchants

[GET] /merchants

Parameters: none

Response:

    [
        {
            "id": 1,
            "name": "Merchant A",
            "balance": 1000
        },
        {
            "id": 2,
            "name": "Merchant B",
            "balance": 500
        },
        {
            "id": 3,
            "name": "Merchant C",
            "balance": 240
        }
    ]

Transactions

Get transactions

[GET] /transactions

Parameters: none

Response:

    [
        {
            "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"
        }
    ]

How to use

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