Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.paybridgenp.com/llms.txt

Use this file to discover all available pages before exploring further.

Installation

pip install paybridge-np

Initialization

from paybridge_np import PayBridge

paybridge = PayBridge(
    api_key="sk_sandbox_...",              # sk_sandbox_... or sk_live_...
    base_url="https://api.paybridgenp.com",  # optional, this is the default
    timeout=30.0,                            # optional, seconds. default: 30
    max_retries=2,                           # optional. default: 2
)
You can also use the client as a context manager:
with PayBridge(api_key="sk_live_...") as paybridge:
    session = paybridge.checkout.create({...})
# HTTP client is closed automatically

paybridge.checkout

checkout.create(params)

Creates a checkout session.
session = paybridge.checkout.create({
    "amount": 10000,                              # required - paisa (NPR x 100)
    "returnUrl": "https://yoursite.com/success",  # required
    "cancelUrl": "https://yoursite.com/cart",     # optional
    "provider": "khalti",                         # optional - omit to let customer pick
    "currency": "NPR",                            # optional, default: "NPR"
    "metadata": {"orderId": "ORD-001"},           # optional
})

print(session["checkout_url"])  # redirect customer here
print(session["id"])            # cs_...
print(session["expires_at"])    # ISO timestamp
Parameters:
NameTypeRequiredDescription
amountintYesIn paisa. NPR 100.00 = 10000
returnUrlstrYesRedirect URL after payment
cancelUrlstrNoRedirect URL on cancellation
providerstrNo"esewa", "khalti", "fonepay"
currencystrNoDefault: "NPR"
metadatadictNoPassed through to webhooks and payment records

checkout.expire(id)

Marks a session as expired so it can no longer accept payment. Use this when you mint a fresh session for a logical purchase that already had one outstanding (e.g. a customer requesting a new payment link), so the old URL stops being payable immediately rather than waiting for its 30-minute TTL. Mirrors Stripe’s POST /checkout/sessions/{id}/expire.
paybridge.checkout.expire("cs_6f2jHn2…")
Idempotent: calling on an already-terminal session is a no-op that returns the current row state without error.

paybridge.payments

payments.list(**kwargs)

response = paybridge.payments.list(
    limit=20,   # optional, default 20, max 100
    offset=0,   # optional, default 0
)

print(response["meta"]["total"])

for payment in response["data"]:
    print(payment["id"], payment["amount"], payment["status"])

payments.retrieve(id)

payment = paybridge.payments.retrieve("pay_6f2jHn2...")

print(payment["status"])       # "success" | "failed"
print(payment["amount"])       # paisa
print(payment["provider"])     # "esewa" | "khalti" | ...
print(payment["provider_ref"])  # provider's transaction reference
print(payment["metadata"])     # whatever you passed at checkout

paybridge.webhooks

webhooks.create(**kwargs)

endpoint = paybridge.webhooks.create(
    url="https://yoursite.com/webhooks/paybridge",
    events=["payment.succeeded", "payment.failed"],  # optional - omit for all
)

print(endpoint["signing_secret"])  # save this - shown only once

webhooks.list()

response = paybridge.webhooks.list()
for wh in response["data"]:
    print(wh["id"], wh["url"], wh["enabled"])

webhooks.delete(id)

paybridge.webhooks.delete("wh_...")

WebhooksResource.construct_event(body, signature, secret) (static)

Verifies a webhook signature and returns the parsed event. Use this in your webhook handler.
from paybridge_np.resources.webhooks import WebhooksResource
from paybridge_np import SignatureVerificationError

body = request.get_data(as_text=True)  # raw body string
signature = request.headers.get("X-PayBridge-Signature")

try:
    event = WebhooksResource.construct_event(
        body,
        signature,
        os.environ["PAYBRIDGE_WEBHOOK_SECRET"],
    )
except SignatureVerificationError:
    return {"error": "Invalid signature"}, 400

if event["type"] == "payment.succeeded":
    # event["data"]["id"], event["data"]["amount"], event["data"]["metadata"]
    fulfill_order(event["data"]["metadata"].get("orderId"))
elif event["type"] == "payment.failed":
    notify_order_failed(event["data"]["session_id"])
Raises SignatureVerificationError if:
  • The signature header is missing or malformed
  • The HMAC doesn’t match
  • The timestamp is more than 5 minutes old (replay attack protection)

paybridge.billing

The billing methods provide access to plans, customers, subscriptions, and invoices. Billing API access requires the Premium plan.

Plans

# Create a plan
plan = paybridge.plans.create({
    "name": "Pro Monthly",
    "amount": 99900,         # paisa - NPR 999/month
    "intervalUnit": "month",
    "intervalCount": 1,
    "trialDays": 14,         # optional
})

# List plans
response = paybridge.plans.list()

# Get a plan
plan = paybridge.plans.get("plan_...")

# Update a plan
updated = paybridge.plans.update("plan_...", {"name": "Pro Monthly v2"})

Customers

# Create a customer
customer = paybridge.customers.create({
    "name": "Aarav Sharma",
    "email": "aarav@example.com",
    "externalCustomerId": "user_123",  # optional
})

# List customers
response = paybridge.customers.list()

# Get a customer
customer = paybridge.customers.get("cus_...")

# Update a customer
updated = paybridge.customers.update("cus_...", {"email": "new@example.com"})

# Delete a customer
paybridge.customers.delete("cus_...")

Subscriptions

# Create a subscription
sub = paybridge.subscriptions.create({
    "customerId": "cus_...",
    "planId": "plan_...",
})

# List subscriptions
response = paybridge.subscriptions.list()

# Get a subscription
sub = paybridge.subscriptions.get("sub_...")

# Lifecycle actions
paybridge.subscriptions.pause("sub_...")
paybridge.subscriptions.resume("sub_...")
paybridge.subscriptions.cancel("sub_...")

# Change plan
paybridge.subscriptions.change_plan("sub_...", {"newPlanId": "plan_..."})

Invoices

# List invoices
response = paybridge.invoices.list(
    subscription_id="sub_...",  # optional
)

# Get an invoice
invoice = paybridge.invoices.get("inv_...")

print(invoice["status"])       # "pending" | "paid" | "overdue" | "cancelled"
print(invoice["amountDue"])    # paisa
print(invoice["dueAt"])        # ISO timestamp

Error handling

All SDK methods raise typed exceptions you can catch and inspect:
from paybridge_np import PayBridge, PayBridgeError, AuthenticationError

try:
    paybridge.checkout.create({"amount": 1000, "returnUrl": "..."})
except AuthenticationError:
    # Invalid API key
    print("Check your PAYBRIDGE_API_KEY")
except PayBridgeError as e:
    print(e)              # human-readable message
    print(e.status_code)  # HTTP status
    print(e.code)         # machine-readable code

Exception classes

ClassStatusWhen
AuthenticationError401Invalid or missing API key
InvalidRequestError400Bad request parameters
NotFoundError404Resource not found
RateLimitError429Too many requests
PayBridgeError5xxServer error
SignatureVerificationError-Webhook HMAC mismatch or replay

Type hints

The SDK ships with a py.typed marker and full type annotations. All parameter types are exported as TypedDict classes:
from paybridge_np.types import (
    CreateCheckoutParams,
    CheckoutSession,
    Payment,
    PaymentStatus,
    Provider,
    CreateRefundParams,
    CreatePlanParams,
    CreateCustomerParams,
    CreateSubscriptionParams,
)

Framework examples

# views.py
import json
import os
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from paybridge_np import PayBridge
from paybridge_np.resources.webhooks import WebhooksResource
from paybridge_np.errors import SignatureVerificationError

paybridge = PayBridge(api_key=os.environ["PAYBRIDGE_API_KEY"])

def create_checkout(request):
    data = json.loads(request.body)

    session = paybridge.checkout.create({
        "amount": data["amount"],
        "returnUrl": "https://yoursite.com/success",
        "cancelUrl": "https://yoursite.com/cart",
        "metadata": {"orderId": data["orderId"]},
    })

    return JsonResponse({"checkoutUrl": session["checkout_url"]})

@csrf_exempt
def webhook(request):
    try:
        event = WebhooksResource.construct_event(
            request.body.decode(),
            request.headers.get("X-PayBridge-Signature"),
            os.environ["PAYBRIDGE_WEBHOOK_SECRET"],
        )
    except SignatureVerificationError:
        return JsonResponse({"error": "Invalid signature"}, status=400)

    if event["type"] == "payment.succeeded":
        Order.objects.filter(
            id=event["data"]["metadata"].get("orderId")
        ).update(status="paid")

    return JsonResponse({"received": True})
import os
from flask import Flask, request, jsonify
from paybridge_np import PayBridge
from paybridge_np.resources.webhooks import WebhooksResource
from paybridge_np.errors import SignatureVerificationError

app = Flask(__name__)
paybridge = PayBridge(api_key=os.environ["PAYBRIDGE_API_KEY"])

@app.post("/create-checkout")
def create_checkout():
    data = request.json

    session = paybridge.checkout.create({
        "amount": data["amount"],
        "returnUrl": "https://yoursite.com/success",
        "metadata": {"orderId": data["orderId"]},
    })

    return jsonify(checkoutUrl=session["checkout_url"])

@app.post("/webhooks/paybridge")
def webhook():
    try:
        event = WebhooksResource.construct_event(
            request.get_data(as_text=True),
            request.headers.get("X-PayBridge-Signature"),
            os.environ["PAYBRIDGE_WEBHOOK_SECRET"],
        )
    except SignatureVerificationError:
        return jsonify(error="Invalid signature"), 400

    if event["type"] == "payment.succeeded":
        fulfill_order(event["data"]["metadata"].get("orderId"))

    return jsonify(received=True)
import os
from fastapi import FastAPI, Request, HTTPException
from paybridge_np import PayBridge
from paybridge_np.resources.webhooks import WebhooksResource
from paybridge_np.errors import SignatureVerificationError

app = FastAPI()
paybridge = PayBridge(api_key=os.environ["PAYBRIDGE_API_KEY"])

@app.post("/create-checkout")
async def create_checkout(request: Request):
    data = await request.json()

    session = paybridge.checkout.create({
        "amount": data["amount"],
        "returnUrl": "https://yoursite.com/success",
        "metadata": {"orderId": data["orderId"]},
    })

    return {"checkoutUrl": session["checkout_url"]}

@app.post("/webhooks/paybridge")
async def webhook(request: Request):
    body = (await request.body()).decode()
    signature = request.headers.get("X-PayBridge-Signature")

    try:
        event = WebhooksResource.construct_event(
            body, signature, os.environ["PAYBRIDGE_WEBHOOK_SECRET"],
        )
    except SignatureVerificationError:
        raise HTTPException(status_code=400, detail="Invalid signature")

    if event["type"] == "payment.succeeded":
        await fulfill_order(event["data"]["metadata"].get("orderId"))

    return {"received": True}