Skip to content

Production-ready payments module for Python with FastAPI routers, gRPC/Protobuf contracts, SQLAlchemy, JWT auth, and provider adapters.

Notifications You must be signed in to change notification settings

PrashantRatanparikhe/Python4-Payment-Module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Payment Module — High-Quality Implementation

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.

Highlights that demonstrate expertise

  • 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 into models, crud, schemas, and database for persistence
    • Protobuf-first contracts (proto/) defining stable, language-agnostic APIs
  • Strong typing and validation:
    • Pydantic models for request validation and response shaping
    • Marshmallow SQLAlchemy schemas for serialization of ORM entities
    • typing and t.Literal used 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
  • Resilient error handling and clean responses:
    • gRPC exceptions captured and mapped to HTTPException with sanitized messages
    • Provider adapters return structured results and handle null/invalid responses gracefully
  • Scalable, reliable database practices:
    • SQLAlchemy engine with connection pooling and pool_pre_ping
    • Explicit isolation level and scoped_session for thread safety
    • CRUD layer encapsulates queries with filtering, pagination, safe dynamic ordering
  • 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

Tech stack

  • FastAPI (routers), gRPC + Protobuf (service boundary), SQLAlchemy (ORM)
  • Pydantic, Marshmallow (validation/serialization)
  • Authorize.Net SDK, Requests (provider adapters)
  • python-decouple (12-factor env config)

Key directories

  • 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 statuses
  • models/ — Domain DTOs for API boundary
  • sql/ — Persistence layer
    • database.py — engine/session config
    • models/ — ORM entities
    • crud/ — query functions and repositories
    • schemas/ — serialization schemas
  • proto/ — Protobuf definitions for transactions and related entities

Notable implementation details

  • Provider integration (Authorize.Net):
    • Encapsulated in AuthnetService with environment-based configuration (LIVE/TEST)
    • Clear, testable methods (e.g., CreateCustomerProfile, ChargeCreditCard) returning structured dicts
  • gRPC-first API routers:
    • Routers call gRPC stubs; errors are normalized; responses are converted via MessageToDict and cased consistently
  • Authorization voter pattern:
    • TransactionVoter enforces 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

Example surfaces

  • User endpoints: /user with create/update/timezone/password routes
  • Transaction endpoints: /transaction with checkout, profile payment, read, refund, list, settlement, partial refund
  • gRPC TransactionService methods: CheckoutPayment, ProfilePayment, ReadTransaction, RefundTransaction, ReadRefund, CancelRefund, ReadRefundByTransactionId, ListTransactions, ReadSettlementByTransactionId, PartiallyRefundInvoiceItems

Configuration

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 helper layer

Run (overview)

  • 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

Why this codebase stands out

  • 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

About

Production-ready payments module for Python with FastAPI routers, gRPC/Protobuf contracts, SQLAlchemy, JWT auth, and provider adapters.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages