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
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to your fork
- Open a pull request
License
MIT License
Support
GitHub Repository:
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file paybridge-0.1.2.tar.gz.
File metadata
- Download URL: paybridge-0.1.2.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7484a6d34f4ca423ee7049a0dec42d3c5f6baabdebcfd23faebbec9ff510f11
|
|
| MD5 |
88c77092a904f05463bc5ff8ff38b83d
|
|
| BLAKE2b-256 |
a40e2b1568d5d32669db7a3f5cf747f2f5a89caafcee7220214beca7cf8d1c41
|
File details
Details for the file paybridge-0.1.2-py3-none-any.whl.
File metadata
- Download URL: paybridge-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58d6ba5e22d4aebc51af2062c4264268408afa4a01cfaed6e82f3dcc9a990aaa
|
|
| MD5 |
eadf39b4cda7d0ded11eaf4988e1f69d
|
|
| BLAKE2b-256 |
f016ef590181b400d6bcbe40a4122f8526f1fd300245375c566a24e29fbd6d47
|