Skip to main content

Official Python SDK for the PayBridgeNP payment gateway - accept eSewa, Khalti, and Fonepay through a single API

Project description

paybridge-np

Official Python SDK for the PayBridgeNP payment gateway. Accept eSewa, Khalti, and Fonepay through a single API.

Installation

pip install paybridge-np

Quick start

from paybridge_np import PayBridgeNP

client = PayBridgeNP(api_key="sk_live_...")  # from dashboard.paybridgenp.com

# Create a checkout session
session = client.checkout.create({
    "amount": 250000,        # NPR 2,500 in paisa
    "currency": "NPR",
    "returnUrl": "https://mystore.com/success",
    "cancelUrl": "https://mystore.com/cart",
    "metadata": {"orderId": "ORD-7842"},
    "customer": {
        "name": "Ram Shrestha",
        "email": "ram@example.com",
        "phone": "9841000000",
    },
})

# Redirect customer to hosted checkout
# session["checkout_url"] => https://checkout.paybridgenp.com/checkout/cs_xxx

# Expire a previously-created session so its URL stops being payable
# (use when you mint a fresh session for the same purchase).
client.checkout.expire("cs_xxx")

# Retrieve or list checkout sessions (read-only). The read shape uses
# camelCase keys (customerName, expiresAt, ...).
session = client.checkout.retrieve("cs_xxx")
sessions = client.checkout.list(limit=20, status="success")

Payments

# List payments
result = client.payments.list(limit=20)
payments = result["data"]

# Get a single payment
payment = client.payments.retrieve("pay_xxx")

Payment links

Reusable hosted payment pages. Responses use camelCase keys.

# Create
link = client.payment_links.create({"title": "Donation", "amount": 50000})

# List, retrieve (with view/conversion stats), update, cancel, or delete
links = client.payment_links.list(active=True)
detail = client.payment_links.retrieve(link["id"])
client.payment_links.update(link["id"], {"active": False})
client.payment_links.cancel(link["id"])  # deactivate, keep for records
client.payment_links.delete(link["id"])  # only if never used

Direct-QR (Fonepay)

Premium feature -- mint a Fonepay QR server-side and embed it in your own UI, skipping the hosted checkout page. Subscribe to events_url (SSE) for qr.scanned / qr.paid / qr.expired.

qr = client.qr.fonepay({
    "amount": 10000,  # paisa
    "customer": {"name": "Aarav Sharma", "email": "aarav@example.com"},
})
# qr["qr_image"] (PNG data URL), qr["qr_message"], qr["events_url"], qr["expires_at"]

# The QR display window is ~3 min. Refresh it for the SAME session -- same id,
# events_url, and webhook -- without spawning a new session. Lifetime unchanged.
fresh = client.qr.refresh(qr["id"])

Refunds

refund = client.refunds.create({
    "paymentId": "pay_xxx",
    "amount": 100000,  # NPR 1,000 in paisa
    "reason": "customer_request",
})

Webhooks

# Register an endpoint
endpoint = client.webhooks.create(
    url="https://mystore.com/webhooks/paybridge",
    events=["payment.succeeded", "payment.failed"],
)

# Verify a webhook signature (no client instance needed)
from paybridge_np.resources.webhooks import WebhooksResource

event = WebhooksResource.construct_event(
    body=raw_body,
    signature=request.headers["X-PayBridgeNP-Signature"],
    secret="whsec_...",
)

Billing (Subscriptions)

# Create a plan
plan = client.plans.create({
    "name": "Pro Monthly",
    "amount": 99900,
    "intervalUnit": "month",
})

# Create a customer
customer = client.customers.create({
    "name": "Sita Gurung",
    "email": "sita@example.com",
})

# Subscribe
subscription = client.subscriptions.create({
    "customerId": customer["id"],
    "planId": plan["id"],
})

# List invoices
invoices = client.invoices.list(customer_id=customer["id"])

Sandbox mode

Use a test-mode API key (sk_test_...) to test without real money. Mode is determined server-side by the key prefix (sk_test_ vs sk_live_) - there is nothing to configure in the SDK.

Error handling

from paybridge_np import PayBridgeError, AuthenticationError

try:
    session = client.checkout.create({...})
except AuthenticationError:
    print("Invalid API key")
except PayBridgeError as e:
    print(e, e.status_code)

Context manager

with PayBridgeNP(api_key="sk_live_...") as client:
    session = client.checkout.create({...})
# HTTP client is closed automatically

Documentation

License

MIT

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_np-3.2.1.tar.gz (18.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_np-3.2.1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file paybridge_np-3.2.1.tar.gz.

File metadata

  • Download URL: paybridge_np-3.2.1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for paybridge_np-3.2.1.tar.gz
Algorithm Hash digest
SHA256 6b9b60269178e3b5c44f94085cc9c9cac247c36bd4447a1564f021620dd99715
MD5 5abc41990ba2906b1ba74a051f12d276
BLAKE2b-256 62f2b6867be1a57d18a1ec603f64d1b3fc454417c955aa2320eb644bd7fab6fa

See more details on using hashes here.

File details

Details for the file paybridge_np-3.2.1-py3-none-any.whl.

File metadata

  • Download URL: paybridge_np-3.2.1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for paybridge_np-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e624cd963dfd353548bf1edf2667e378b5a92fa39a2e7470c555012e377aab59
MD5 d13a14f1ac719890351c7bb8e23c89b4
BLAKE2b-256 9e5d0c04524cef9261d161c1d786bd30253155d2c0df24d9038f2c4d6d61018a

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