Python SDK
Official PonponPay SDK for Python. Compatible with Python 3.8+ and works with Django, Flask, FastAPI, and other frameworks.
Installation
pip install ponponpayBasic Usage
Initialize Client
import os
from ponponpay import PonponPay
client = PonponPay(
api_key=os.environ['PONPONPAY_API_KEY'],
# Optional configurations
base_url='https://api.ponponpay.com/api/v1/pay/sdk',
timeout=30
)Create Order
order = client.create_order(
currency='USDT',
network='tron',
amount=100.00,
mch_order_id='ORDER_123456', # Optional
notify_url='https://your-site.com/webhook',
redirect_url='https://your-site.com/success'
)
print(order.trade_id) # PonponPay transaction ID
print(order.payment_url) # Redirect user to this URL
print(order.address) # Receiving wallet address
print(order.actual_amount) # Actual payment amountQuery Order
# Query by PonponPay trade ID
order = client.query_order('PP202412110001')
# Query by your merchant order ID
order = client.query_order_by_mch_id('ORDER_123456')
print(order.status) # 'pending', 'paid', 'expired', 'cancelled'
print(order.tx_hash) # Blockchain transaction hash (if paid)Cancel Order
# Cancel by trade ID
client.cancel_order('PP202412110001')
# Cancel by merchant order ID
client.cancel_order_by_mch_id('ORDER_123456')Get Payment Methods
methods = client.get_payment_methods()
for method in methods:
print(f"{method.currency} on {method.network}")Webhook Verification
# Generic example
signature = request.headers.get('X-PonponPay-Signature')
payload = request.body
if client.verify_webhook(payload, signature):
data = client.parse_webhook(payload)
if data.status == 'paid':
# Update your order status
print(f"Payment received: {data.trade_id}")Django Integration
Settings
# settings.py
PONPONPAY_API_KEY = os.environ.get('PONPONPAY_API_KEY')Views
# views.py
from django.shortcuts import redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from ponponpay import PonponPay
client = PonponPay(api_key=settings.PONPONPAY_API_KEY)
def create_payment(request):
order = client.create_order(
currency=request.POST.get('currency', 'USDT'),
network=request.POST.get('network', 'tron'),
amount=float(request.POST.get('amount', 100)),
notify_url=request.build_absolute_uri('/webhook/'),
redirect_url=request.build_absolute_uri('/success/')
)
return redirect(order.payment_url)
@csrf_exempt
def webhook(request):
signature = request.headers.get('X-PonponPay-Signature')
payload = request.body.decode('utf-8')
if client.verify_webhook(payload, signature):
data = client.parse_webhook(payload)
if data.status == 'paid':
# Update order status in database
pass
return HttpResponse('OK')
def success(request):
return render(request, 'payment/success.html')URLs
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('payment/create/', views.create_payment, name='create_payment'),
path('webhook/', views.webhook, name='webhook'),
path('success/', views.success, name='success'),
]Flask Integration
from flask import Flask, request, redirect, url_for
from ponponpay import PonponPay
import os
app = Flask(__name__)
client = PonponPay(api_key=os.environ['PONPONPAY_API_KEY'])
@app.route('/payment/create', methods=['POST'])
def create_payment():
order = client.create_order(
currency=request.form.get('currency', 'USDT'),
network=request.form.get('network', 'tron'),
amount=float(request.form.get('amount', 100)),
notify_url=url_for('webhook', _external=True),
redirect_url=url_for('success', _external=True)
)
return redirect(order.payment_url)
@app.route('/webhook', methods=['POST'])
def webhook():
signature = request.headers.get('X-PonponPay-Signature')
payload = request.data.decode('utf-8')
if client.verify_webhook(payload, signature):
data = client.parse_webhook(payload)
if data.status == 'paid':
# Update order status
pass
return 'OK'
@app.route('/success')
def success():
return 'Payment successful!'
if __name__ == '__main__':
app.run()FastAPI Integration
from fastapi import FastAPI, Request, Header
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
from ponponpay import PonponPay
import os
app = FastAPI()
client = PonponPay(api_key=os.environ['PONPONPAY_API_KEY'])
class PaymentRequest(BaseModel):
currency: str = 'USDT'
network: str = 'tron'
amount: float
@app.post('/payment/create')
async def create_payment(payment: PaymentRequest):
order = client.create_order(
currency=payment.currency,
network=payment.network,
amount=payment.amount,
notify_url='https://your-site.com/webhook',
redirect_url='https://your-site.com/success'
)
return RedirectResponse(url=order.payment_url, status_code=303)
@app.post('/webhook')
async def webhook(
request: Request,
x_ponponpay_signature: str = Header(None)
):
payload = await request.body()
payload_str = payload.decode('utf-8')
if client.verify_webhook(payload_str, x_ponponpay_signature):
data = client.parse_webhook(payload_str)
if data.status == 'paid':
# Update order status
pass
return {'status': 'OK'}
@app.get('/success')
async def success():
return {'message': 'Payment successful!'}Async Support
from ponponpay import AsyncPonponPay
import asyncio
async def main():
client = AsyncPonponPay(api_key=os.environ['PONPONPAY_API_KEY'])
order = await client.create_order(
currency='USDT',
network='tron',
amount=100.00
)
print(order.payment_url)
await client.close()
asyncio.run(main())Error Handling
from ponponpay.exceptions import (
PonponPayError,
AuthenticationError,
ValidationError,
NetworkError
)
try:
order = client.create_order(
currency='USDT',
network='tron',
amount=100.00
)
except AuthenticationError as e:
# Invalid API Key
print(f'Authentication failed: {e}')
except ValidationError as e:
# Invalid parameters
print(f'Validation error: {e}')
except NetworkError as e:
# Network connection issues
print(f'Network error: {e}')
except PonponPayError as e:
# Other API errors
print(f'Error {e.code}: {e.message}')Type Hints
from ponponpay import PonponPay
from ponponpay.models import (
OrderResult,
OrderDetail,
PaymentMethod,
WebhookPayload
)
def process_order(order: OrderResult) -> None:
print(order.trade_id)
print(order.payment_url)