Skip to main content

Reusable Django payment infrastructure with idempotency and webhook support

Project description

payment_infra

A reusable Django package for payment orchestration with clean architecture, idempotency, provider abstraction, and webhook handling.

Features

  • Provider abstraction (paystack, stripe, monnify) through a shared interface.
  • Idempotent payment initialization flow to avoid duplicate charges.
  • Service-layer orchestration (PaymentService, WebhookService) for testable business logic.
  • Django REST API endpoints for charge, verify, and webhook processing.
  • Extensible architecture for adding new providers without changing API/service boundaries.

Package Architecture

payment_infra/
├── api/                        # DRF serializers, views, and URL routes
├── application/                # Use-cases/services + provider/repository interfaces
├── domain/                     # Core entities and domain models
├── infrastructure/
│   ├── idempotency/            # Idempotency persistence and locking
│   ├── providers/              # Gateway adapters (Paystack/Stripe/Monnify)
│   ├── repositories/           # Data access adapters
│   └── tasks/                  # Async task hooks (Celery)
└── migrations/                 # Django migrations

Installation

From package index

pip install payment_infra

For local development

git clone https://github.com/0FFSIDE1/payment_infra.git
cd payment_infra
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Quick Start in a Django Project

1) Add apps

INSTALLED_APPS = [
    # ...
    "rest_framework",
    "payment_infra",
]

2) Include URLs

from django.urls import include, path

urlpatterns = [
    # ...
    path("payments/", include("payment_infra.api.urls")),
]

3) Configure provider settings

Set DJANGO_PAYMENTS_PROVIDER to one of:

  • paystack
  • stripe
  • monnify

Provider-specific settings:

Paystack

  • PAYSTACK_SECRET_KEY
  • PAYSTACK_PUBLIC_KEY
  • PAYSTACK_BASE_URL (optional)
  • PAYSTACK_CALLBACK_URL

Stripe

  • STRIPE_SECRET_KEY
  • STRIPE_PUBLIC_KEY
  • STRIPE_BASE_URL (optional, defaults to https://api.stripe.com/v1)
  • STRIPE_WEBHOOK_SECRET

Monnify

  • MONNIFY_API_KEY
  • MONNIFY_SECRET_KEY
  • MONNIFY_CONTRACT_CODE
  • MONNIFY_PUBLIC_KEY (optional)
  • MONNIFY_BASE_URL (optional)

Common runtime settings:

  • DJANGO_SECRET_KEY
  • REDIS_URL
  • CACHE_BACKEND

4) Run migrations

python manage.py migrate

API Usage

With URLs mounted at /payments/:

  • POST /payments/paystack/charge/
  • GET /payments/paystack/verify/<reference>/
  • POST /payments/webhooks/

Example: initialize charge

curl -X POST http://localhost:8000/payments/paystack/charge/ \
  -H "Content-Type: application/json" \
  -d '{
    "email": "customer@example.com",
    "amount": "1000.00",
    "currency": "NGN",
    "idempotency_key": "trx-20260411103000-abc123",
    "callback_url": "https://example.com/payment/callback"
  }'

Example: verify payment

curl -X GET "http://localhost:8000/payments/paystack/verify/<reference>/"

Programmatic Usage

from decimal import Decimal
from payment_infra.infrastructure.providers.registry import get_payment_service

service = get_payment_service()

result = service.process_payment(
    email="customer@example.com",
    amount=Decimal("1000.00"),
    currency="NGN",
    idempotency_key="trx-20260411103000-abc123",
    metadata={"callback_url": "https://example.com/payment/callback"},
)

Testing

Run all tests:

pytest

Run only non-integration tests:

pytest -m "not integration"

Security Notes

  • Always use HTTPS callback/webhook URLs in production.
  • Keep provider secret keys in environment variables (never commit them).
  • Verify webhook signatures before processing events.
  • Use unique idempotency keys per client request to prevent duplicate processing.

Contributing

  1. Create a feature branch.
  2. Add/modify tests for changes.
  3. Run pytest locally.
  4. Open a pull request.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

payment_infra-1.0.5.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

payment_infra-1.0.5-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file payment_infra-1.0.5.tar.gz.

File metadata

  • Download URL: payment_infra-1.0.5.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for payment_infra-1.0.5.tar.gz
Algorithm Hash digest
SHA256 41193b01d58f3110cdb647825ca42f46d94022fdc3b778ee9873580c8f200ed9
MD5 4c234f59b32ebce0f8180c497eaaf308
BLAKE2b-256 67287ef0534f53ea169eb3793d3ba5fb759a7b8724bb8f82685e1ff68bd9b308

See more details on using hashes here.

Provenance

The following attestation bundles were made for payment_infra-1.0.5.tar.gz:

Publisher: cd.yml on 0FFSIDE1/payment_infra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file payment_infra-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: payment_infra-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for payment_infra-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bcd3687cfd86933ca48cc7e99576518709f9ada6c4bf3ea4b8da80ef92504c61
MD5 92094bd096fe733ab13c64c17c12a0d0
BLAKE2b-256 8401782b144ba2ca5b941cdf69f3cd507adff86d7638dc5fc29ef2bb0fbd111e

See more details on using hashes here.

Provenance

The following attestation bundles were made for payment_infra-1.0.5-py3-none-any.whl:

Publisher: cd.yml on 0FFSIDE1/payment_infra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page