openpay

command module
v0.0.0-...-5d187b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

OpenPay

Go Logo

OpenPay is the payments, wallet and ledger service shared by every product. Products call it to take payments, keep customer wallets, place orders, refund and pay out; underneath, every movement of money is a balanced double-entry journal in PostgreSQL.

  • Ledger — append-only journals and postings in integer minor units, idempotent on an external id, with invariant checks that run on a schedule.
  • Wallets — per-product and platform wallet types with capabilities (fundable, grantable, transferable, withdrawable), limits, holds and expiry.
  • Payments — provider-agnostic (Razorpay Payment Links, and a mock for development; Cashfree next), webhooks treated as hints, a poller as the safety net, failover and a kill-switch.
  • Orders, refunds, disputes, settlement reconciliation, withdrawals, reports.

The design and its decisions are in plan.md.

Run it locally

docker compose up -d postgres
make setup          # generate protobuf code
make migrate        # apply resources/migrations to the dev database
go run . -env=dev   # HTTP :8085, gRPC :8086, and the background worker

dev.yaml enables the mock provider, whose checkout page at /openpay/v1/mock-provider/checkout/ lets you mark a payment paid. It must never be enabled in production. The API explorer is at http://localhost:8085/openpay/v1/swagger.

Build the image

make docker   # builds the console and the binary inside Docker; image `openpay`

The console is embedded in the binary and served at /payments/, next to the API under /openpay/v1/. Its API and login URLs are fixed at build time and default to production; for another environment:

make docker VITE_API_BASE_URL=https://api.staging.example VITE_LOGIN_URL=https://…/login

Test

make test-integration
# or, with Postgres already up and migrated:
OPENPAY_TEST_POSTGRES=1 go test -race -p 1 ./...

Integration tests use a real Postgres (openpay_test, truncated as they go) because row locks, SKIP LOCKED and constraints under contention are what they test. -p 1 because packages share that database.

A sanity-level load test of the posting engine is opt-in:

OPENPAY_TEST_POSTGRES=1 OPENPAY_LOAD_TEST=1 go test -run TestPostingLoad -v ./internal/wallet/

Integrate a product backend

Ask central ops for a service credential for your product (shown once). From Go, use the client package rather than hand-rolled calls:

import (
    "github.com/gofreego/openpay/api/openpay_v1"
    "github.com/gofreego/openpay/pkg/client"
)

c, err := client.New(client.Config{Target: "openpay.internal:8086", KeyID: keyID, Secret: secret})
defer c.Close()

// Every call that moves money needs an idempotency key. Derive it from the
// action, so a retry is recognised as the same action.
ctx = client.WithIdempotencyKey(ctx, "topup:"+orderID)
err = client.Retry(ctx, 3, func(ctx context.Context) error {
    resp, err = c.CreatePayment(ctx, req)
    return err
})
switch client.Code(err) {
case client.WalletOperationDenied, client.InsufficientBalance:
    // tell the customer
}

From anything else, use HTTP: Authorization: Bearer <key_id>.<secret>, an Idempotency-Key header on mutations, and branch on the code field of error bodies, never on the message. The full surface is in the swagger UI.

A credential belongs to exactly one product. It sees that product's customers' wallets (and platform wallets), never another product's.

Operate

  • On-call runbooks — stuck payments, provider outages, webhook storms, ledger drift, recon breaks, key compromise.
  • Health: GET /healthz (process), GET /readyz (database).
  • Encryption keys for bank account numbers (Service.Encryption) and all provider secrets belong in a secret store, never in a committed file.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
api
openpay_v1
Package openpay_v1 is a reverse proxy.
Package openpay_v1 is a reverse proxy.
cmd
worker
Package worker runs OpenPay's background jobs: the outbox drainer, the idempotency sweeper, the chart-of-accounts bootstrap and the ledger invariant checker, and the hold and wallet expiry sweepers; later the payment status poller and settlement ingest.
Package worker runs OpenPay's background jobs: the outbox drainer, the idempotency sweeper, the chart-of-accounts bootstrap and the ledger invariant checker, and the hold and wallet expiry sweepers; later the payment status poller and settlement ingest.
internal
appcontext
Package appcontext carries the identity of whoever made the current request.
Package appcontext carries the identity of whoever made the current request.
auth
Package auth turns request headers into an authenticated caller.
Package auth turns request headers into an authenticated caller.
health
Package health serves the liveness and readiness probes.
Package health serves the liveness and readiness probes.
ledger
Package ledger holds the chart of accounts: what accounts exist, what they are called, and how they are created.
Package ledger holds the chart of accounts: what accounts exist, what they are called, and how they are created.
middleware
Package middleware populates request context and normalizes errors at the two edges OpenPay serves: the gRPC server and the grpc-gateway HTTP mux.
Package middleware populates request context and normalizes errors at the two edges OpenPay serves: the gRPC server and the grpc-gateway HTTP mux.
opsmetrics
Package opsmetrics publishes the operational gauges that dashboards and alerts are built on: payment success rate per provider, stuck payments, webhook backlog and lag, and hold leakage (plan.md Phase 10).
Package opsmetrics publishes the operational gauges that dashboards and alerts are built on: payment success rate per provider, stuck payments, webhook backlog and lag, and hold leakage (plan.md Phase 10).
order
Package order records purchases and settles them — from wallets, from a card, or both at once (split tender).
Package order records purchases and settles them — from wallets, from a card, or both at once (split tender).
outbox
Package outbox publishes domain events that were recorded alongside the changes that caused them (plan.md D5).
Package outbox publishes domain events that were recorded alongside the changes that caused them (plan.md D5).
payment
Package payment collects money through PSPs and applies it: the state machine, the provider calls, webhook ingestion and the jobs that recover from missed webhooks.
Package payment collects money through PSPs and applies it: the state machine, the provider calls, webhook ingestion and the jobs that recover from missed webhooks.
provider
Package provider is the boundary between OpenPay and the PSPs that actually move money.
Package provider is the boundary between OpenPay and the PSPs that actually move money.
provider/mock
Package mock is a payment provider OpenPay controls completely.
Package mock is a payment provider OpenPay controls completely.
provider/razorpay
Package razorpay is the Razorpay provider.
Package razorpay is the Razorpay provider.
ratelimit
Package ratelimit caps how fast each product backend may call OpenPay.
Package ratelimit caps how fast each product backend may call OpenPay.
recon
Package recon proves that what a PSP says happened matches what our ledger says happened (plan.md phase 8).
Package recon proves that what a PSP says happened matches what our ledger says happened (plan.md phase 8).
telemetry
Package telemetry wires OpenTelemetry: traces and metrics exported over OTLP.
Package telemetry wires OpenTelemetry: traces and metrics exported over OTLP.
testsupport
Package testsupport gives integration tests outside the repository package a real database.
Package testsupport gives integration tests outside the repository package a real database.
wallet
Package wallet moves money in and out of customer wallets.
Package wallet moves money in and out of customer wallets.
withdrawal
Package withdrawal cashes withdrawable wallets out to a customer's bank account or UPI id (plan.md phase 9).
Package withdrawal cashes withdrawable wallets out to a customer's bank account or UPI id (plan.md phase 9).
pkg
apperrors
Package apperrors defines OpenPay's error taxonomy.
Package apperrors defines OpenPay's error taxonomy.
client
Package client is how a product backend talks to OpenPay over gRPC.
Package client is how a product backend talks to OpenPay over gRPC.
fieldcrypt
Package fieldcrypt encrypts individual sensitive fields — bank account numbers — before they reach the database, so a database dump or a backup does not hand over customers' accounts.
Package fieldcrypt encrypts individual sensitive fields — bank account numbers — before they reach the database, so a database dump or a backup does not hand over customers' accounts.
ids
Package ids generates the prefixed, time-sortable identifiers OpenPay exposes over its API.
Package ids generates the prefixed, time-sortable identifiers OpenPay exposes over its API.
money
Package money represents monetary values as integer minor units with an explicit currency.
Package money represents monetary values as integer minor units with an explicit currency.
secrets
Package secrets generates and verifies service credential secrets.
Package secrets generates and verifies service credential secrets.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL