This is a backend service for managing user accounts and transactions, including recurring payments.
-
User Registration and Authentication:
POST /auth/signup
- Register a new userPOST /auth/signin
- Sign in a userPOST /auth/signout
- Sign out a user
-
Account Management:
GET /api/accounts
- View all user's account details and balancesPOST /api/accounts
- Create new user's accountGET /api/accounts/{id}/history
- View specific account transaction history
-
Transaction:
POST /api/send
- Send funds from one account to anotherPOST /api/withdraw
- Withdraw funds from an account- Note: Transactions (send and withdraw) are processed x seconds after the endpoint is hit. The value of 'x' can be adjusted in the '.env' file.
-
Recurring Payments:
GET /api/recurring-payments
- Retrieve all recurring paymentsPUT /api/recurring-payments{id}
- Set up or update recurring payments
- Node.js with TypeScript
- Fastify for the API server
- Prisma as the ORM
- PostgreSQL for the database
- SuperTokens for authentication
- Swagger for API documentation
- Node.js (v20 or later)
- PostgreSQL (v14 or later)
-
Clone the Repository:
git clone https://github.com/andregit1/payment-system.git cd payment-system
-
Install Dependencies:
npm install
-
Set Up Environment Variables: Create a
.env
file in the project root with the following content:DATABASE_URL="postgresql://root:123123123@localhost:5432/concreteai_dev?schema=public" # for local DATABASE_URL="postgresql://root:123123123@db:5432/concreteai_dev?schema=public" # with Docker
-
Set Up the Database: Run the following command to apply database migrations:
npx prisma migrate dev
-
Start the Server:
npm run dev
The API server should now be running at
http://localhost:3001
.
To seed the database with initial data, add the following to your package.json
:
"prisma": {
"seed": "ts-node-dev prisma/seed.ts"
}
Then, run the seed command:
npx prisma db seed
API documentation is available at http://localhost:3001/documentation
when the server is running.
Demo Video Link: swagger-authorized-demo
-
Open the Swagger UI: Navigate to
http://localhost:3001/documentation
in your browser to access the API documentation. -
Sign Up and Sign In:
- Use the
POST /auth/signup
endpoint to create a new user account. - Use the
POST /auth/signin
endpoint to sign in and obtain an access token from the response.
- Use the
-
Authorize with the Token:
- Copy the
st-access-token
value from the response header of thePOST /auth/signin
request. - Click the Authorize button in the Swagger UI (usually located in the top right corner).
- Paste the token value into the authorization dialog and click Authorize.
- Click Close to exit the authorization dialog.
- Copy the
Now, you can use the Swagger UI to test the endpoints with the provided authorization.
To run the application using Docker:
-
Build the Docker Images:
docker compose build
-
Start the Docker Containers:
docker compose up -d
-
Access the Docker Container:
docker compose exec app sh
-
Stop the Docker Containers:
docker compose down
A cron job is set up to handle recurring payments, currently scheduled to run at midnight every day.
// src/app.ts
// Schedule recurring payments processing
processRecurringPaymentsTask('0 0 * * *'); // Daily at midnight
This job checks for any recurring payments that need to be processed and executes the required transactions accordingly.
Here's an overview of the data relations:
-
User:
- Each
User
has a uniquesupertokensId
(which mimics the SuperTokens user_id for authentication and matches the relationship data table),username
,email
, andpassword
. - A
User
can have multiplePaymentAccounts
.
- Each
-
PaymentAccount:
- Each
PaymentAccount
belongs to aUser
. - It has a unique
accountNumber
,accountType
, andbalance
. - A
PaymentAccount
can have multiple,Transaction
records as the sender or recipient, andRecurringPayment
records as the sender or recipient. - The
PaymentAccount
entity represents accounts in the payload, distinct fromuserId
, as it holds balance information and related data.
- Each
-
Transaction:
- Each
Transaction
involves asenderAccount
andrecipientAccount
. - It records the amount, currency, timestamp, status, and optional address.
- Each
-
RecurringPayment:
- Each
RecurringPayment
involves asenderAccount
andrecipientAccount
. - It records the amount, currency, interval, next payment date, and status.
- Each
- Ensure that PostgreSQL is installed and running on your local machine.
- Verify that the
DATABASE_URL
in your.env
file is correctly configured for your local setup.
- Make sure the PostgreSQL service on your local machine is shut down before running the application in Docker. Docker will manage the PostgreSQL instance, and having two instances running simultaneously can cause conflicts.
- Currency Conversion:
- If transactions involve different currencies, they must be converted to the recipient's currency before processing. Future development will include support for real-time currency conversion to handle such scenarios seamlessly.