Skip to main content

Unified payment gateway integration for Python

Project description

PayBridge Documentation

Unified payment gateway integration for Python developers.

PayBridge provides a single interface for integrating multiple payment gateways without rewriting payment logic for every provider.


Features

  • Unified payment API
  • Multiple gateway support
  • Easy provider switching
  • Fallback gateway handling
  • Payment verification
  • Refund support
  • Webhook handling
  • Scalable architecture
  • Developer-friendly API
  • FastAPI support

Supported Providers

  • Paystack
  • Flutterwave
  • Stripe
  • More coming soon

Installation

Install PayBridge using pip:

pip install paybridge

Upgrade to the latest version:

pip install --upgrade paybridge

Quick Start

Import PayBridge

from paybridge import PayBridge

Initialize a Provider

Paystack Example

paybridge = PayBridge(
    provider="paystack",
    secret_key="sk_test_xxxxxxxxx"
)

Initialize Payment

response = paybridge.initialize_payment(
    amount=5000,
    email="customer@example.com",
    currency="NGN",
    reference="TXN_001"
)

print(response)

Verify Payment

Always verify payments server-side after payment completion.

verification = paybridge.verify_payment(
    reference="TXN_001"
)

print(verification)

Charge a Customer

response = paybridge.charge(
    amount=10000,
    email="customer@example.com"
)

print(response)

Refund a Payment

refund = paybridge.refund(
    reference="TXN_001",
    amount=5000
)

print(refund)

Webhook Handling

Webhooks allow your application to receive real-time payment events.

Flask Example

from flask import Flask, request

app = Flask(__name__)

@app.route("/webhook", methods=["POST"])
def webhook():
    payload = request.json

    print(payload)

    return {"status": "success"}

Using Multiple Gateways

One of PayBridge’s most powerful features is multi-gateway support.

This allows developers to:

  • Create fallback systems
  • Reduce downtime
  • Route payments dynamically
  • Improve reliability
  • Avoid vendor lock-in

Initialize Multiple Providers

from paybridge import PayBridge

paystack = PayBridge(
    provider="paystack",
    secret_key="PAYSTACK_SECRET_KEY"
)

flutterwave = PayBridge(
    provider="flutterwave",
    secret_key="FLUTTERWAVE_SECRET_KEY"
)

Gateway Fallback Example

If one provider fails, automatically switch to another.

try:
    response = paystack.initialize_payment(
        amount=5000,
        email="customer@example.com"
    )

except Exception:
    response = flutterwave.initialize_payment(
        amount=5000,
        email="customer@example.com"
    )

print(response)

Dynamic Provider Selection

def get_provider(currency):
    if currency == "NGN":
        return paystack

    return flutterwave


provider = get_provider("NGN")

response = provider.initialize_payment(
    amount=10000,
    email="customer@example.com"
)

print(response)

Provider Configuration System

providers = {
    "paystack": PayBridge(
        provider="paystack",
        secret_key="PAYSTACK_SECRET"
    ),

    "flutterwave": PayBridge(
        provider="flutterwave",
        secret_key="FLUTTERWAVE_SECRET"
    )
}

Using a provider dynamically:

selected_provider = providers["paystack"]

response = selected_provider.initialize_payment(
    amount=5000,
    email="customer@example.com"
)

Smart Routing Example

def route_payment(amount):
    if amount > 100000:
        return flutterwave

    return paystack


provider = route_payment(5000)

response = provider.initialize_payment(
    amount=5000,
    email="customer@example.com"
)

Retry Logic Example

providers = [paystack, flutterwave]

for provider in providers:
    try:
        response = provider.initialize_payment(
            amount=5000,
            email="customer@example.com"
        )

        print("Payment initialized successfully")
        break

    except Exception as e:
        print(f"Provider failed: {e}")

FastAPI Integration

This section shows how to integrate PayBridge into a FastAPI application.


Install FastAPI Dependencies

pip install fastapi uvicorn

Basic FastAPI Integration

from fastapi import FastAPI
from paybridge import PayBridge

app = FastAPI()

paybridge = PayBridge(
    provider="paystack",
    secret_key="sk_test_xxxxxxxxx"
)


@app.get("/")
async def home():
    return {"message": "PayBridge API Running"}


@app.post("/initialize-payment")
async def initialize_payment():

    response = paybridge.initialize_payment(
        amount=5000,
        email="customer@example.com",
        currency="NGN",
        reference="TXN_001"
    )

    return response

Run FastAPI Server

uvicorn main:app --reload

FastAPI Verify Payment Example

@app.get("/verify-payment/{reference}")
async def verify_payment(reference: str):

    verification = paybridge.verify_payment(
        reference=reference
    )

    return verification

FastAPI Request Body Example

from pydantic import BaseModel


class PaymentRequest(BaseModel):
    amount: int
    email: str
    currency: str = "NGN"


@app.post("/pay")
async def pay(data: PaymentRequest):

    response = paybridge.initialize_payment(
        amount=data.amount,
        email=data.email,
        currency=data.currency
    )

    return response

FastAPI Multi-Gateway Example

from fastapi import FastAPI
from paybridge import PayBridge

app = FastAPI()

paystack = PayBridge(
    provider="paystack",
    secret_key="PAYSTACK_SECRET"
)

flutterwave = PayBridge(
    provider="flutterwave",
    secret_key="FLUTTERWAVE_SECRET"
)


@app.post("/payment")
async def payment():

    try:
        response = paystack.initialize_payment(
            amount=5000,
            email="customer@example.com"
        )

    except Exception:

        response = flutterwave.initialize_payment(
            amount=5000,
            email="customer@example.com"
        )

    return response

FastAPI Dynamic Routing Example

def get_provider(currency: str):

    if currency == "NGN":
        return paystack

    return flutterwave


@app.post("/dynamic-payment")
async def dynamic_payment(data: PaymentRequest):

    provider = get_provider(data.currency)

    response = provider.initialize_payment(
        amount=data.amount,
        email=data.email,
        currency=data.currency
    )

    return response

FastAPI Webhook Example

from fastapi import Request


@app.post("/webhook")
async def webhook(request: Request):

    payload = await request.json()

    print(payload)

    return {
        "status": "success"
    }

FastAPI Refund Example

@app.post("/refund/{reference}")
async def refund(reference: str):

    response = paybridge.refund(
        reference=reference,
        amount=5000
    )

    return response

Environment Variables

Never hardcode secret keys in production.

Example .env

PAYSTACK_SECRET_KEY=sk_test_xxxxx
FLUTTERWAVE_SECRET_KEY=FLWSECK_TEST_xxxxx

Using python-dotenv

from dotenv import load_dotenv
import os

load_dotenv()

secret_key = os.getenv("PAYSTACK_SECRET_KEY")

Full Integration Example

from paybridge import PayBridge

paybridge = PayBridge(
    provider="paystack",
    secret_key="sk_test_xxxxx"
)

payment = paybridge.initialize_payment(
    amount=5000,
    email="customer@example.com",
    currency="NGN",
    reference="TXN_001"
)

print(payment)

Recommended Architecture

Client Request
      ↓
FastAPI Routes
      ↓
Payment Service
      ↓
Provider Router
      ↓
Paystack / Flutterwave / Stripe

This architecture provides:

  • Scalability
  • Reliability
  • Easier maintenance
  • Better failover handling

Error Handling

Always handle payment failures properly.

try:
    response = paybridge.initialize_payment(
        amount=5000,
        email="customer@example.com"
    )

    print(response)

except Exception as e:
    print(f"Payment failed: {e}")

FastAPI Error Handling

from fastapi import HTTPException


@app.post("/safe-payment")
async def safe_payment():

    try:

        response = paybridge.initialize_payment(
            amount=5000,
            email="customer@example.com"
        )

        return response

    except Exception as e:

        raise HTTPException(
            status_code=400,
            detail=str(e)
        )

Best Practices

  • Always verify payments server-side
  • Use environment variables for secret keys
  • Implement webhook verification
  • Add retry mechanisms
  • Log failed transactions
  • Monitor provider downtime
  • Use fallback gateways
  • Track transaction success rates
  • Separate payment logic into services

Why PayBridge?

PayBridge helps developers:

  • Reduce integration complexity
  • Switch providers easily
  • Build scalable fintech systems faster
  • Avoid vendor lock-in
  • Maintain cleaner codebases

Contributing

Contributions are welcome.

Steps

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to your fork
  5. Open a pull request

License

MIT License


Support

GitHub Repository:

https://github.com/zinsu-moni/paybridge

Issues:

https://github.com/zinsu-moni/paybridge/issues

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

paybridge-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

paybridge-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file paybridge-0.1.0.tar.gz.

File metadata

  • Download URL: paybridge-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for paybridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 84e3986d7053909bcd9256ed8aec70c991cad8f9e2d27c8ee6b5a870e14e990d
MD5 43012f50c87f13b11d23aa4c268655fd
BLAKE2b-256 a000aeb0a9755d7f7dd3abd194fe211339cc9036a7910c2bcf87a78b70c0890d

See more details on using hashes here.

File details

Details for the file paybridge-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: paybridge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for paybridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b1c8aa600ef0b40d0f6ad7368a06b6c20760c4ac7ff6a1b338a75bd1d6c38f4
MD5 03b2a4dd7412f248a4689716d530ad81
BLAKE2b-256 13962d8ff838e083d7506539a5f9ffd322811ce4a20a20e5bec16bb5efa98b8e

See more details on using hashes here.

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