Skip to content

About

Official Python SDK for EZBILL: 1Bill and Raast QR / Request-to-Pay payments in Pakistan, with webhook verification.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ezbill (Python)

Official Python client for the EZBILL API. Collect payments in Pakistan over Raast: show a dynamic QR that any bank app, JazzCash or Easypaisa can scan, or send a Request-to-Pay to a payer's mobile number or IBAN.

Install

pip install ezbill

Quickstart

from ezbill import EzbillClient, EzbillError, is_paid

client = EzbillClient("ez_test_...")   # ez_test_ = sandbox, ez_live_ = live; same host

# Amounts are integer paisa: Rs 1,500.00 = 150000
qr = client.create_qr(150000, reference="INV-1001", expiry_minutes=30)
print(qr["id"], qr["status"])
print(qr["qr_string"])   # render this string as a QR code

payment = client.get_payment(qr["id"])
if is_paid(payment["status"]):        # PAID or SETTLED
    print("paid")

Request-to-Pay

lookup = client.alias_inquiry("03001234567")          # Raast mobile alias, 11 digits
# or: lookup = client.title_fetch("PK36SCBL0000001123456702")
rtp = client.create_rtp(lookup["rtp_id"], 150000, reference="INV-1002")
client.cancel(rtp["id"])                               # while CREATED or SENT

rtp_id can be used once; reusing it returns 409 rtp_id_used.

Errors

try:
    client.create_qr(150000, reference="INV-1003")
except EzbillError as e:
    print(e.status, e.code, e.retryable, e.correlation_id)
    if e.retryable:      # 503 = Raast / 1LINK outage; retry later
        ...

client.status() (public, no key) tells you whether Raast is up.

Optional: client.create_token() exchanges the key for a 1-hour ezt_ access token, which you can pass to another EzbillClient as its key.

Webhooks

EZBILL sends events to {endpoint base URL}/onego-notification. If you set the base URL in the EZBILL portal to https://your-site.pk/ezbill, listen on /ezbill/onego-notification.

EZBILL POSTs signed events (payment.received, payment.settled, rtp.rejected, rtp.expired). Verify on the raw body:

from flask import Flask, request
from ezbill import verify_webhook, WebhookVerificationError

app = Flask(__name__)
SECRET = "your webhook secret"

@app.post("/ezbill/onego-notification")
def webhook():
    try:
        event = verify_webhook(request.get_data(), request.headers, SECRET)  # tolerance=300 s
    except WebhookVerificationError:
        return "", 400
    key = f'{event["event"]}:{event["correlation_id"]}'   # idempotency key
    if event["event"] in ("payment.received", "payment.settled"):
        mark_order_paid(event["reference"], event["amount_minor"], key)
    return "", 200

Tests

python3 -m unittest discover -s tests -v

Links

License

MIT

About

Official Python SDK for EZBILL: 1Bill and Raast QR / Request-to-Pay payments in Pakistan, with webhook verification.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages