A modular, production-ready payment module showcasing clean architecture, strong typing, and robust integrations with multiple payment providers (Authorize.Net, Paysafe, Jupiter, etc.). It exposes FastAPI routers backed by gRPC services and a well-structured SQLAlchemy data layer.
- Layered, modular architecture with clear boundaries:
- Routers (
routers/) for HTTP endpoints decoupled from business logic via gRPC stubs - Actions (
actions/) for external provider integrations (e.g., Authorize.Net) - Voter (
voter/) for fine-grained, user-centric authorization decisions - Enumerations (
enumerations/) for type-safe provider and status constants - Domain models (
models/) and validation schemas (sql/schemas/) for data integrity - SQL layer (
sql/) split intomodels,crud,schemas, anddatabasefor persistence - Protobuf-first contracts (
proto/) defining stable, language-agnostic APIs
- Routers (
- Strong typing and validation:
- Pydantic models for request validation and response shaping
- Marshmallow SQLAlchemy schemas for serialization of ORM entities
typingandt.Literalused in routers for compile-time validated query params- Enum-driven design for payment types and statuses to prevent invalid states
- Robust security and authorization:
- JWT-based auth dependency (
JWTBearer) applied consistently in routers - RequestHeaderValidatorInterceptor + voter classes enforcing ownership-based access
- JWT-based auth dependency (
- Resilient error handling and clean responses:
- gRPC exceptions captured and mapped to
HTTPExceptionwith sanitized messages - Provider adapters return structured results and handle null/invalid responses gracefully
- gRPC exceptions captured and mapped to
- Scalable, reliable database practices:
- SQLAlchemy engine with connection pooling and
pool_pre_ping - Explicit isolation level and
scoped_sessionfor thread safety - CRUD layer encapsulates queries with filtering, pagination, safe dynamic ordering
- SQLAlchemy engine with connection pooling and
- Extensibility by design:
- New payment providers are a matter of extending enums and action adapters
- Proto/gRPC services make inter-service contracts explicit and testable
- Maintainability and readability:
- Comprehensive docstrings, consistent naming, and clear module responsibilities
- Consistent camelCase↔snake_case transformations for API/DB boundary handling
- FastAPI (routers), gRPC + Protobuf (service boundary), SQLAlchemy (ORM)
- Pydantic, Marshmallow (validation/serialization)
- Authorize.Net SDK, Requests (provider adapters)
- python-decouple (12-factor env config)
actions/— Provider adapters (e.g.,authnet.py)routers/— HTTP route handlers for Users, Transactions, Webhooks, etc.voter/— Authorization voters (ownership checks, action gating)enumerations/— Enums for payment types and statusesmodels/— Domain DTOs for API boundarysql/— Persistence layerdatabase.py— engine/session configmodels/— ORM entitiescrud/— query functions and repositoriesschemas/— serialization schemas
proto/— Protobuf definitions for transactions and related entities
- Provider integration (Authorize.Net):
- Encapsulated in
AuthnetServicewith environment-based configuration (LIVE/TEST) - Clear, testable methods (e.g.,
CreateCustomerProfile,ChargeCreditCard) returning structured dicts
- Encapsulated in
- gRPC-first API routers:
- Routers call gRPC stubs; errors are normalized; responses are converted via
MessageToDictand cased consistently
- Routers call gRPC stubs; errors are normalized; responses are converted via
- Authorization voter pattern:
TransactionVoterenforces add/edit/view permissions based on the authenticated user
- Transaction querying:
- Rich filtering and sorting; pagination metadata; safe
getattr-based ordering controlled via whitelisted keys
- Rich filtering and sorting; pagination metadata; safe
- User endpoints:
/userwith create/update/timezone/password routes - Transaction endpoints:
/transactionwith checkout, profile payment, read, refund, list, settlement, partial refund - gRPC TransactionService methods:
CheckoutPayment,ProfilePayment,ReadTransaction,RefundTransaction,ReadRefund,CancelRefund,ReadRefundByTransactionId,ListTransactions,ReadSettlementByTransactionId,PartiallyRefundInvoiceItems
Configure via environment variables (12-factor):
- Database:
DB_ENGINE,MYSQL_USER,MYSQL_PASSWORD,MYSQL_URL,MYSQL_PORT,MYSQL_DB - Authorize.Net:
AUTH_ENVIRONMENT,AUTH_TEST_*,AUTH_LIVE_* - Auth/JWT and gRPC channel settings via the
helperlayer
- Ensure Python 3.10+ and dependencies installed
- Provide environment variables for DB and providers
- Start your gRPC backends referenced by the routers
- Serve the FastAPI application that mounts the routers in this module
- Clean separation of concerns and contracts (Routers ⇄ gRPC ⇄ DB/Providers)
- Strong typing, enums, and validation minimize runtime errors
- Security and authorization are first-class, not bolted on
- Extensible architecture makes adding providers and endpoints straightforward
- Production-minded DB setup and defensive error handling