Skip to content

Maxim-LD/nodejs-payment-service

Repository files navigation

Nodejs Payment Service

This project is a REST API built with Node.js and Express. The API parses and executes financial payment instructions according to a defined syntax and a set of business rules.

The architecture is based on the provided Node.js template, featuring a custom Express server abstraction to ensure consistency and a clear separation of concerns.


Project Structure

The project follows a simplified version of the provided template, focusing only on the necessary components.

  • app.js: The main entry point of the application. It initializes the Express server and dynamically loads all endpoint handlers.

  • core/: Contains the foundational modules for the application.

    • core/express/: A custom abstraction over Express.js that standardizes how routes and responses are handled.
    • core/errors/: A simple utility for creating structured application errors.
  • endpoints/: This directory holds all the API endpoint definitions.

    • payment-instructions.js: Defines the POST /payment-instructions route and links it to the payment service.
  • src/services/: Contains the core business logic.

    • PaymentService.js: The main service file that handles the parsing, validation, execution, and response formatting for all payment instructions.

Core Features

  • Instruction Parser: A robust, non-regex parser that deconstructs string-based payment instructions.
  • Dual Instruction Formats: Supports both DEBIT ... FROM and CREDIT ... TO instruction formats.
  • Comprehensive Validation: Implements all specified business and syntax rules with corresponding error codes.
  • Transaction Execution: Handles both immediate and future-dated (pending) transactions.
  • Structured Responses: Provides consistent JSON responses for successful, pending, and failed transactions as per the assessment specification.

Data Flow Overview

The application follows a clear, sequential data flow for each request:

  1. Client Request: A POST request with the accounts and instruction payload is sent to the /payment-instructions endpoint.
  2. Server (core/express/): The custom Express server receives the request and routes it to the appropriate handler.
  3. Endpoint Handler (endpoints/payment-instructions.js): The handler extracts the accounts and instruction from the request body.
  4. Service (src/services/PaymentService.js): The handler calls the executeTransaction function, which orchestrates the entire process.
    • Parsing: The raw instruction string is parsed into a structured object.
    • Validation: A series of business and syntax rules are validated. If any rule fails, a structured error response is generated.
    • Execution: The transaction is executed (balances are updated) or marked as pending.
    • Formatting: A final response object is formatted according to the assessment specifications.
  5. Response: The endpoint handler receives the final formatted object and sends it back to the client with the appropriate HTTP status code (200 for success/pending, 400 for failure).

How to Run the Application Locally

Follow these steps to run the application locally:

1. Install Dependencies

npm install

2. Register Your Endpoint Folder

Open the app.js file and add your endpoint folder to the ENDPOINT_CONFIGS array:

const ENDPOINT_CONFIGS = [{ path: './endpoints/{your-endpoint-folder-name}/' }];

Replace {your-endpoint-folder-name} with the actual folder name of your endpoint.

3. Start the Server

In your terminal, run the following command:

node app.js

The server should now be running and ready to handle requests at the port 8811 or any port configured in the .env file


API Endpoint

Path: POST /payment-instructions

Request Body:

{
  "accounts": [
    {"id": "a", "balance": 230, "currency": "USD"},
    {"id": "b", "balance": 300, "currency": "USD"}
  ],
  "instruction": "DEBIT 30 USD FROM ACCOUNT a FOR CREDIT TO ACCOUNT b"
}

The service will process the instruction and return a structured JSON response indicating the outcome of the transaction.

About

A REST API for parsing and executing payment instructions, built with Node.js and Express

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors