Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .air.toml → .air.app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -buildvcs=false -o ./tmp/main ./cmd/app"
bin = "./tmp/app"
cmd = "go build -buildvcs=false -o ./tmp/app ./cmd/app"
delay = 1000
entrypoint = ["./tmp/main"]
entrypoint = ["./tmp/app"]
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
Expand Down
58 changes: 58 additions & 0 deletions .air.worker.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#:schema https://json.schemastore.org/any.json

env_files = []
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/worker"
cmd = "go build -buildvcs=false -o ./tmp/worker ./cmd/worker"
delay = 1000
entrypoint = ["./tmp/worker"]
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
ignore_dangerous_root_dir = false
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
silent = false
time = false

[misc]
clean_on_exit = false

[proxy]
app_port = 0
app_start_timeout = 0
enabled = false
proxy_port = 0

[screen]
clear_on_rebuild = false
keep_scroll = true
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
.git
/docker/storage
/storage/*.sqlite
/storage/*.db
/storage/*.db
tmp
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
!.env.example
tmp

compose.override.yaml
compose.override.yaml
project_structure.md
coverage.txt
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUN if [ "$APP_ENV" != "local" ]; then \
go build -o cmd/app/main ./cmd/app; \
fi

ENTRYPOINT ["/bin/sh", "start.sh"]
ENTRYPOINT ["/bin/sh", "scripts/start.app.sh"]
File renamed without changes.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Ledger

A Payment Gateway service between a fictional e-commerce platform (FicMart) and a fictional Bank.

## Overview

Ledger is a work-in-progress Go service that sits between FicMart and a bank integration layer. It is designed to accept payment intents, communicate with the bank, manage payment state, and handle retries and terminal failures.

## Repository Scope

This repository currently includes:

- core payment intent domain logic
- bank integration abstractions
- payment event tracking
- idempotency support
- worker and queue components
- configuration, database, and notifier plumbing

## Status

This project is not complete yet. The repository is a minimal starting point for development and documentation will expand as the implementation matures.

## Getting Started

The repository contains Docker and Compose files, plus Go entry points under `cmd/`.

If you want to explore the code, the main areas are:

- `cmd/` – application, CLI, and worker entry points
- `internal/domain/` – payment gateway domain logic
- `internal/jobs/` – background processing and queue workers
- `internal/platform/` – configuration, database, middleware, notifier, and rendering support
80 changes: 46 additions & 34 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
package main

import (
"context"
"ledger/app/config"
"ledger/app/middleware"
"ledger/app/storage"
"ledger/internal/bank"
"ledger/internal/idempotency"
"ledger/internal/paymentevent"
"ledger/internal/paymentintent"
"ledger/pkg/uow"
"ledger/internal/domain/bank"
"ledger/internal/domain/idempotency"
"ledger/internal/domain/paymentevent"
"ledger/internal/domain/paymentintent"
"ledger/internal/jobs/queue"
"ledger/internal/platform/config"
"ledger/internal/platform/database"
"ledger/internal/platform/middleware"
"ledger/internal/platform/notifier"
"log"
"net/http"
"time"

"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/hibiken/asynq"
)

func main() {
if err := config.LoadEnv(); err != nil {
log.Fatal(err)
}

pgsql, err := storage.InitPostgres()
pgsql, err := database.InitPostgres()
if err != nil {
log.Fatal(err)
}

redis, err := storage.InitRedis()
redis, err := database.InitRedis()
if err != nil {
log.Fatal(err)
}
Expand All @@ -37,58 +38,69 @@ func main() {
Timeout: 10 * time.Second,
}

appMiddleware := middleware.NewMiddleware()

ficmartBankRepo := bank.NewFicMartBankRepo(client)

idempotencyRepo := idempotency.NewRedisRepo(redis)
idempotencyService := idempotency.NewService(idempotencyRepo)

paymentEventRepo := paymentevent.NewPostgresRepo(pgsql)

uow := uow.NewPgsqlUoW(pgsql)
uow := paymentintent.NewPgsqlUoW(pgsql)

notifier := notifier.NewRedisNotifier(redis)

asynqClient := asynq.NewClient(asynq.RedisClientOpt{
Addr: config.Env.Redis.Addr,
Password: config.Env.Redis.Password,
DB: 1,
})

queueClient := queue.NewClient(asynqClient)

paymentIntentRepo := paymentintent.NewPostgresRepo(pgsql)
paymentIntentService := paymentintent.NewService(
paymentIntentRepo,
paymentEventRepo,
ficmartBankRepo,

&paymentIntentTxAdapter{u: uow},
uow,
idempotencyService,
queueClient,
notifier,
)
paymentIntentHandler := paymentintent.NewHandler(paymentIntentService)

router := chi.NewRouter()

router.Use(chiMiddleware.Logger, chiMiddleware.Recoverer)

appMiddleware := middleware.NewMiddleware()
router.Use(appMiddleware.EnsureIndempotencyKey)
router.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("OK"))
if err != nil {
log.Printf("Error writing health check response: %s", err)
}
})

router.Post("/payment/intent", paymentIntentHandler.Create)
router.Post("/payment/intent/capture", paymentIntentHandler.Capture)
router.Post("/payment/intent/refund", paymentIntentHandler.Refund)
router.Post("/payment/intent/cancel", paymentIntentHandler.Cancel)
router.Get("/payment/intent", paymentIntentHandler.Get)

router.Group(func(r chi.Router) {
r.Use(appMiddleware.EnsureIndempotencyKey)

r.Post("/payment/intent", paymentIntentHandler.Create)
r.Post("/payment/intent/capture", paymentIntentHandler.Capture)
r.Post("/payment/intent/refund", paymentIntentHandler.Refund)
r.Post("/payment/intent/cancel", paymentIntentHandler.Cancel)
})

s := &http.Server{
Addr: ":" + config.Env.App.Port,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
WriteTimeout: 40 * time.Second,
MaxHeaderBytes: 1 << 20,
}

log.Fatal(s.ListenAndServe())
}

type paymentIntentTxAdapter struct {
u *uow.PgsqlUoW
}

func (a *paymentIntentTxAdapter) RunInTx(ctx context.Context, fn func(paymentintent.Repos) error) error {
return a.u.RunInTx(ctx, func(r uow.Repos) error {
return fn(paymentintent.Repos{
PaymentIntent: r.PaymentIntent,
PaymentEvent: r.PaymentEvent,
})
})
}
7 changes: 4 additions & 3 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"context"
"ledger/app/config"
"ledger/app/storage"
"ledger/internal/platform/config"
"ledger/internal/platform/database"
"log"
"time"

Expand All @@ -23,6 +23,7 @@ var migrations = []cmdforge.Migration{
amount BIGINT NOT NULL,
currency CHAR(3) NOT NULL,
status TEXT NOT NULL,
current_operation TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
Expand Down Expand Up @@ -69,7 +70,7 @@ func main() {
log.Fatal(err)
}

pgsql, err := storage.InitPostgres()
pgsql, err := database.InitPostgres()
if err != nil {
log.Fatal(err)
}
Expand Down
73 changes: 73 additions & 0 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"ledger/internal/domain/bank"
"ledger/internal/domain/paymentintent"
"ledger/internal/jobs/tasks"
"ledger/internal/jobs/worker"
"ledger/internal/platform/config"
"ledger/internal/platform/database"
"ledger/internal/platform/notifier"
"log"
"net/http"
"time"

"github.com/hibiken/asynq"
)

func main() {
if err := config.LoadEnv(); err != nil {
log.Fatal(err)
}

pgsql, err := database.InitPostgres()
if err != nil {
log.Fatal(err)
}

redis, err := database.InitRedis()
if err != nil {
log.Fatal(err)
}

srv := asynq.NewServer(
asynq.RedisClientOpt{
Addr: config.Env.Redis.Addr,
Password: config.Env.Redis.Password,
DB: 1,
},

asynq.Config{
// Specify how many concurrent workers to use
Concurrency: 10,
// Optionally specify multiple queues with different priority.
Queues: map[string]int{
"critical": 6,
"default": 3,
"low": 1,
},
// See the godoc for other configuration options
},
)

client := &http.Client{
Timeout: 10 * time.Second,
}

bankRepo := bank.NewFicMartBankRepo(client)
uow := paymentintent.NewPgsqlUoW(pgsql)
notifier := notifier.NewRedisNotifier(redis)

taskHandler := worker.NewPaymentWorker(bankRepo, uow, notifier)

// mux maps a type to a handler
mux := asynq.NewServeMux()
mux.HandleFunc(tasks.TypeCreatePayment, taskHandler.HandleCreate)
mux.HandleFunc(tasks.TypeCapturePayment, taskHandler.HandleCapture)
// mux.HandleFunc(tasks.TypeRefundPayment, taskHandler.HandleRefund)
// mux.HandleFunc(tasks.TypeCancelPayment, taskHandler.HandleCancel)

if err := srv.Run(mux); err != nil {
log.Fatalf("could not run server: %v", err)
}
}
9 changes: 9 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ services:
working_dir: /var/www
ports:
- "${APP_PORT}:${APP_PORT}"
worker:
image: ledger-app
container_name: ledger-worker
restart: unless-stopped
env_file:
- .env
entrypoint: ["/bin/sh", "-c", "exec sh scripts/start.worker.sh"]
depends_on:
- redis
extra_hosts:
- "host.docker.internal:host-gateway"
db:
Expand Down
Loading
Loading