ci: update release ci

This commit is contained in:
sankalp committed 2026-09-03 01:42:22 +05:45
1 parent aa6ff90892
commit ec5bf1d96b
2 files changed
+143 -231

No files matched your search

+2
View File
@@ -13,6 +13,8 @@ jobs:
name: Build and publish to PyPI
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
- name: Checkout
+141 -231
View File
@@ -22,77 +22,57 @@
</a>
</p>
Accept payments in Nepal from your Django app with a single, consistent API.
Accept Nepal payments from Django with **one consistent API** — Khalti, eSewa,
Fonepay (Dynamic QR, Static QR, status, tax refund) and ConnectIPS. No four
HTTP APIs, no four signing schemes, no four callback formats. One
`PaymentManager`, one `PaymentResult`.
We handle **Khalti**, **eSewa**, **Fonepay** (Dynamic QR, Static QR, status
verification, tax refund) and **ConnectIPS**, so you don't have to learn four
different HTTP APIs, four different signing schemes, and four different
callback formats. You get one `PaymentManager`, one `PaymentResult`, done.
The library is deliberately lightweight. No WebSockets, no async event loops,
no background daemons. Just plain HTTPS requests through a pooled
`requests.Session`, which is why it runs happily even on shared hosting.
Lightweight: plain HTTPS through a pooled `requests.Session`. No WebSockets, no
async loops, no daemons — runs on shared hosting.
```bash
pip install django-nepali-payment
# or with uv
uv add django-nepali-payment
# or: uv add django-nepali-payment
```
**Requirements:** Python 3.11+, Django 4.2+ (the library itself is not tied to a
specific Django major). It bundles `requests` for transport and `cryptography`
for ConnectIPS RSA signing.
## What you get
- **One shape for every gateway.** Every call returns a `PaymentResult`, so
reading a success, a failure, or some provider data looks the same whether it
came from Khalti or Fonepay.
- **Signatures that just work.** We reproduced the exact signing from each
provider's reference implementation, so you don't have to debug hex vs base64
yourself:
- eSewa: HMAC-SHA256 (base64)
- Fonepay: HMAC-SHA512 (lowercase hex)
- Khalti: `Authorization: key <secret>` header
- ConnectIPS: SHA256withRSA over your merchant certificate
- **One flag for sandbox vs production.** Flip `PaymentMode` and you're done.
- **Shared-hosting-safe Fonepay monitoring.** Settlement is checked with a
lightweight polling thread rather than a WebSocket, so it works where long
connections are forbidden.
- **Fonepay extras built in.** Tax refunds and Static QR out of the box.
- **Connection reuse.** One `requests.Session` is shared across calls, with
sensible timeouts.
- **Optional retries.** `ApiService` can retry transient failures (connection
drops and HTTP 429/5xx) with exponential backoff — opt in per client.
**Requirements:** Python 3.11+, Django 4.2+, `requests`, `cryptography`
(for ConnectIPS signing).
---
## How it's built
## Highlights
The library has three small layers, all of which you can use directly:
- **One shape for every gateway** — every call returns a `PaymentResult`.
- **Signatures that just work** — reproduced from each provider's reference:
- eSewa — HMAC-SHA256 (base64)
- Fonepay — HMAC-SHA512 (lowercase hex)
- Khalti — `Authorization: key <secret>` header
- ConnectIPS — SHA256withRSA over your merchant certificate
- **Sandbox ↔ production in one flag** — flip `PaymentMode`.
- **Shared-hosting-safe polling** — Fonepay settlement checked with a lightweight
polling thread, not a WebSocket.
- **Fonepay extras built in** — tax refunds and Static QR.
- **Connection reuse** — one shared `requests.Session`, sensible timeouts.
- **Optional retries** — exponential backoff on transient failures; opt in.
- **`PaymentManager`** — the entry point. Pick a gateway, mode and secret key
once, then call `initiate_payment` / `verify_payment` (plus Fonepay tax refund
and static QR) without thinking about which provider you chose.
- **Gateway services** — one per provider (Khalti, eSewa, Fonepay, ConnectIPS).
Every service extends `BasePaymentService`, which stores the things all
gateways share (the HTTP client, the sandbox-vs-production base URL, and a
"turn any error into a failed result" helper). Each service only implements
---
## Architecture
Three small layers, all usable directly:
- **`PaymentManager`** — the entry point. Pick gateway, mode, secret once; call
`initiate_payment` / `verify_payment` (plus Fonepay refund / static QR).
- **Gateway services** — one per provider, extending `BasePaymentService`
(shared HTTP client, sandbox/prod URL, error handling). Each implements only
its own initiate/verify logic and signing.
- **Request/response models** — `PaymentResult` is the universal answer type.
Each gateway also has typed request models. The request models use Python
`dataclass(slots=True)` and serialize themselves with `dataclasses.asdict`,
so they are small in memory and safe to pass straight to a provider.
Under the hood everything is synchronous HTTPS through a pooled
`requests.Session` — no event loops, WebSockets or daemons.
- **Models** — `PaymentResult` is the universal answer type; per-gateway typed
requests are `dataclass(slots=True)` and self-serialize.
---
## Quick start
Create a manager once for your gateway and mode, then reuse it:
```python
from nepali_payment import PaymentManager, PaymentMethod, PaymentMode, PaymentResult
@@ -103,62 +83,54 @@ manager = PaymentManager(
)
```
Every call returns the same `PaymentResult`, with these attributes:
Every call returns a `PaymentResult`:
| Field | What it holds |
| ------------ | ------------------------------------------------ |
| `success` | Did the operation succeed? |
| `message` | Human-readable message (no secrets are logged) |
| `message` | Human-readable message (no secrets logged) |
| `data` | Gateway-specific response object (may be `None`) |
| `status` | Reserved; currently unused (always `200`) |
| `error_code` | Reserved; currently unused (always `None`) |
| `status` | Reserved; unused (always `200`) |
| `error_code` | Reserved; unused (always `None`) |
`PaymentResult` also behaves like a `bool` inside an `if`, and exposes
`raise_for_status()` to turn a failure into a raised `PaymentError` when you'd
rather not handle it inline:
It's truthy like a `bool`, and exposes `raise_for_status()` to raise on failure:
```python
result = manager.initiate_payment(PaymentResult, request).raise_for_status()
print(result.data) # only reached if the payment actually started
```
### Tuning the HTTP client
By default the manager creates its own `ApiService` — a pooled
`requests.Session` with a 15s timeout. You can inject your own to change the
timeout, enable retries, or share one session across several managers:
### Custom HTTP client
```python
from nepali_payment import PaymentManager, PaymentMethod, PaymentMode
from nepali_payment.http import ApiService
api = ApiService(
timeout=30.0, # default request timeout (seconds)
retries=2, # retry transient failures 0..N times (default 0)
retry_backoff=1.0, # initial backoff seconds; doubles each attempt
timeout=30.0, # seconds; default 15
retries=2, # retry transient failures (default 0)
retry_backoff=1.0, # initial backoff; doubles each attempt
)
manager = PaymentManager(
PaymentMethod.KHALTI,
PaymentMode.SANDBOX,
PaymentMethod.KHALTI, PaymentMode.SANDBOX,
secret_key="your-secret-key",
api=api,
)
```
The `ApiService` is shared and reuse-cached, so many calls never open a new
connection. Call `api.close()` (or `manager._api_service().close()`) to release
pooled connections when your app shuts down.
Call `api.close()` (or `manager._api_service().close()`) to release pooled
connections on shutdown.
> Retries are off by default because re-sending a *payment initiation* could
> double-charge. Enable them only for endpoints that are safe to repeat.
> Retries default **off** — re-sending a payment initiation could double-charge.
> Enable only for endpoints safe to repeat.
---
## Initiate a payment
Each gateway has its own request model, but the call is always
`manager.initiate_payment(PaymentResult, request)`.
```python
manager.initiate_payment(PaymentResult, request)
```
### eSewa
@@ -169,17 +141,14 @@ request = EsewaRequest(
amount="100",
total_amount="113", # amount + tax + charges
transaction_uuid="order-123",
product_code="EPAYTEST", # your eSewa product code
product_code="EPAYTEST",
signed_field_names="total_amount,transaction_uuid,product_code",
)
result = manager.initiate_payment(PaymentResult, request)
if result.success:
redirect_url = result.data.payment_url # send the customer here
```
eSewa redirects the customer to a hosted payment form.
### Khalti
```python
@@ -188,15 +157,14 @@ from nepali_payment.models.khalti import PaymentRequest as KhaltiRequest
request = KhaltiRequest(
return_url="https://yoursite.com/callback",
website_url="https://yoursite.com",
amount=1000, # in paisa (NPR 10.00)
amount=1000, # paisa (NPR 10.00)
purchase_order_id="order-123",
purchase_order_name="My Order",
)
result = manager.initiate_payment(PaymentResult, request)
if result.success:
redirect_url = result.data.payment_url # send the customer here
pidx = result.data.pidx # keep for verification later
pidx = result.data.pidx # keep for verification
```
> Khalti amounts are in **paisa** (1 NPR = 100 paisa).
@@ -207,58 +175,48 @@ if result.success:
from nepali_payment.models.fonepay import QrRequest
request = QrRequest(
amount="100",
remarks1="Order 123",
remarks2="Main",
prn="order-123",
merchant_code="NBQM",
username="merchant-user",
password="merchant-password",
amount="100", remarks1="Order 123", remarks2="Main",
prn="order-123", merchant_code="NBQM",
username="merchant-user", password="merchant-password",
)
result = manager.initiate_payment(PaymentResult, request)
if result.success:
qr_data = result.data.qr_message # show this to the customer
qr_data = result.data.qr_message # show to the customer
```
The dynamic QR bakes the amount into the QR. Fonepay settlement happens
asynchronously, so you'll usually pair this with the [status monitor](#fonepay-qr-status-monitoring).
Dynamic QR bakes the amount in. Settlement is async — pair with the
[status monitor](#fonepay-qr-status-monitoring).
---
## Fonepay Static QR
A static QR is a single, fixed merchant QR (think: printed at a point of sale).
The customer types in the amount when they scan, so you don't need an amount up
front. Fetch the QR payload once per station and render it:
One fixed merchant QR; the customer types the amount at scan. Fetch once per
station:
```python
from nepali_payment.models.fonepay import StaticQrRequest
request = StaticQrRequest(
prn="merchant-station",
merchant_code="NBQM",
username="merchant-user",
password="merchant-password",
prn="merchant-station", merchant_code="NBQM",
username="merchant-user", password="merchant-password",
)
result = manager.process_static_qr(PaymentResult, request)
if result.success:
static_qr = result.data.qr_message # render/print this once for this station
static_qr = result.data.qr_message # render/print once for this station
```
> `process_static_qr` is Fonepay-only, just like the tax refund. Calling it on
> another gateway raises `ValidationError`. For settlement checks, use
> `verify_payment` or the `FonepayPaymentMonitor` with the payment's PRN.
> `process_static_qr` is Fonepay-only (like the tax refund). On other gateways
> it raises `ValidationError`. For settlement, use `verify_payment` or the
> `FonepayPaymentMonitor` with the payment's PRN.
---
## ConnectIPS
ConnectIPS is a **form POST** gateway. You render a hidden form and hand the
customer over to ConnectIPS, who redirects them back once the payment is done.
It signs with **SHA256withRSA** using a merchant certificate (a `.pfx`, `.p12`
or `.pem` key), so instead of a secret key the manager takes a config object:
A **form POST** gateway signed with **SHA256withRSA** using a merchant
certificate (`.pfx`, `.p12` or `.pem`). Instead of a secret key it takes a
config object:
```python
import os
@@ -278,31 +236,29 @@ config = ConnectIpsConfig(
manager = PaymentManager(
payment_method=PaymentMethod.CONNECTIPS,
payment_mode=PaymentMode.SANDBOX, # switch to PRODUCTION when ready
secret_key="", # unused for ConnectIPS
payment_mode=PaymentMode.SANDBOX,
secret_key="", # unused for ConnectIPS
config=config,
)
```
Initiate the payment. You get back the hidden form fields to render, plus the
URL to POST them to:
Initiate to get the hidden form fields:
```python
from nepali_payment.models.connectips import PaymentRequest
request = PaymentRequest(
order_id="order-123",
amount=12500, # NPR amount
amount=12500, # NPR
description="Order 123",
)
result = manager.initiate_payment(PaymentResult, request)
if result.success:
form = result.data.form_fields # hidden inputs to render
action = result.data.target_url # the ConnectIPS login form URL
action = result.data.target_url # ConnectIPS login form URL
```
When ConnectIPS redirects back to your callback, verify using the query
parameters it sent (plus the amount you expected):
Verify on the callback with the query params plus the expected amount:
```python
import json
@@ -316,22 +272,20 @@ if result.success:
trans_ref_id = result.data.trans_ref_id
```
> ConnectIPS needs the `cryptography` package, which is installed automatically
> with the library. The certificate is loaded inside the service and never
> logged.
> `cryptography` installs automatically. The cert is loaded inside the service
> and never logged.
---
## Verify a payment
The verification call is also the same across gateways — only the argument
differs:
Same call everywhere — only the argument differs:
```python
# Khalti: pass the pidx
result = manager.verify_payment(PaymentResult, pidx)
# Fonepay: pass a JSON string with prn + merchant credentials
# Fonepay: JSON string with prn + merchant credentials
import json
payload = json.dumps({
"prn": "order-123",
@@ -341,7 +295,7 @@ payload = json.dumps({
})
result = manager.verify_payment(PaymentResult, payload)
# eSewa: pass the base64 response echoed back from the payment form
# eSewa: base64 response echoed back from the payment form
result = manager.verify_payment(PaymentResult, base64_response)
```
@@ -349,37 +303,31 @@ result = manager.verify_payment(PaymentResult, base64_response)
## Errors
There are two kinds of failures, and the library keeps them separate:
Two kinds, kept separate:
**1. Gateway failures return a failed `PaymentResult`** — no exception is
raised, so you can treat rejections, non-2xx responses and network hiccups as
data:
**1. Gateway failures return a failed `PaymentResult`** — no exception, so
rejections, non-2xx responses and network hiccups are data:
```python
result = manager.initiate_payment(PaymentResult, request)
if result.success:
# proceed with checkout
...
... # proceed with checkout
else:
logger.warning("Payment failed: %s", result.message) # no secrets logged
```
If you'd rather stop and handle it with a raised error, chain
`raise_for_status()`:
Prefer raising? Chain `raise_for_status()`:
```python
result = manager.initiate_payment(PaymentResult, request).raise_for_status()
# this line only runs if the payment actually started
```
> eSewa is the one exception to "failures don't raise": it raises `PaymentError`
> instead of returning a failed result, matching how eSewa's own API reports
> errors. Catch it (or catch `PaymentError`) when working with eSewa directly.
> **eSewa is the exception:** it raises `PaymentError` instead of returning a
> failed result, matching eSewa's own API. Catch it when working with eSewa.
**2. Developer-facing mistakes raise typed exceptions.** An empty secret, an
unsupported gateway, a `None`/empty/invalid request — these are bugs in your
call, so they fail loudly:
**2. Developer mistakes raise typed exceptions.** An empty secret, an
unsupported gateway, a `None`/empty/invalid request — these fail loudly:
```python
from nepali_payment.exceptions import (
@@ -388,109 +336,89 @@ from nepali_payment.exceptions import (
)
```
The rule of thumb: if a real payment could fail and you should check `success`,
it comes back as a `PaymentResult`; if it's a mistake in your code, it raises.
Rule of thumb: if a real payment could fail and you should check `success`, it
comes back as a `PaymentResult`; if it's a bug in your call, it raises.
---
## Handling callbacks safely
When Khalti, eSewa or ConnectIPS redirects the customer back to your site, you
verify the payment before marking it complete. A few practical rules:
- **Verify server-side, don't trust the query string.** The `pidx` (Khalti) or
`data` (eSewa) in the URL is only an identifier — always round-trip it through
`manager.verify_payment`, never assume it means "paid". For ConnectIPS, pass
the callback parameters *and* the amount you expected.
- **Reconcile the amount.** Confirm the verified amount matches your order's
amount before marking it paid. Khalti and eSewa report the paid amount in
`result.data.total_amount`, ConnectIPS in `result.data.txn_amount`. The
`examples/` app does exactly this — a settled payment for the wrong amount is
stored as `failed`, never `paid`.
- **Keep the callback handler idempotent.** A provider may redeliver a callback,
or a user may refresh. Guard your handler so re-verifying the same order is a
no-op rather than a duplicate charge or state flip.
- **Check the order belongs to your user.** If callbacks are unauthenticated
(`@csrf_exempt`), binding the order to the current request/session still
matters in real apps before you release goods.
`data` (eSewa) is only an identifier — always round-trip through
`manager.verify_payment`. For ConnectIPS, pass the params *and* the expected
amount.
- **Reconcile the amount** against your order before marking paid — Khalti/eSewa
report it in `result.data.total_amount`, ConnectIPS in
`result.data.txn_amount`. The `examples/` app stores a wrong-amount settlement
as `failed`, never `paid`.
- **Keep the handler idempotent.** Providers may redeliver; a user may refresh.
Re-verifying the same order should be a no-op.
- **Check the order belongs to your user.** If callbacks are `@csrf_exempt`,
still bind the order to the request/session before releasing goods.
---
## Fonepay QR status monitoring
Fonepay's QR flow is asynchronous. The customer scans, pays, and you need to
find out _when_ it settles. Here it's plain **HTTP polling** so it works on shared hosting.
Attach a few handlers and start a poller per payment:
Settlement is async, so poll for it — plain **HTTP polling**, shared-hosting
safe:
```python
from datetime import timedelta
from nepali_payment import FonepayPaymentMonitor, PaymentCredentials
monitor = FonepayPaymentMonitor( # defaults shown; tweak as you like
monitor = FonepayPaymentMonitor(
timeout=timedelta(minutes=15), # overall session lifetime
interval=timedelta(seconds=5), # delay between polls
)
@monitor.on("status") # fires on every poll
@monitor.on("status") # every poll
def on_status(args):
print("status:", args.prn, args.payment_status)
@monitor.on("verified") # payment settled
@monitor.on("verified") # settled
def on_verified(args):
print("verified:", args.prn, args.success)
monitor.stop(args.prn) # done with this one
monitor.stop(args.prn)
@monitor.on("timeout") # no terminal state in time
@monitor.on("timeout")
def on_timeout(args):
print("timeout:", args.prn)
@monitor.on("error") # poller/network error
@monitor.on("error")
def on_error(args):
print("error:", args.prn, args.error_message)
monitor.start(
"order-123",
PaymentCredentials(
secret_key="...",
merchant_code="NBQM",
username="u",
password="p",
sandbox_mode=True,
),
)
monitor.start("order-123", PaymentCredentials(
secret_key="...", merchant_code="NBQM",
username="u", password="p", sandbox_mode=True,
))
```
You can also manage sessions directly:
Session management:
```python
monitor.is_monitoring("order-123") # is a poller active?
monitor.stop("order-123") # stop one poller
monitor.dispose() # stop all and release the session
monitor.stop("order-123") # stop one
monitor.dispose() # stop all, release session
```
> **In Django, don't `sleep()` in the request path.** Start the monitor in a
> background task (Celery), a management command, or your task runner so the
> HTTP response returns immediately.
> background task (Celery), a management command, or a task runner so the HTTP
> response returns immediately.
---
### Why polling, not WebSockets?
## Why polling instead of WebSockets?
Shared hosting usually forbids persistent connections, daemon processes and
async event loops. The polling monitor needs none of those. It just issues
plain HTTPS requests on a background thread through the shared
`requests.Session`. The result is a library that's dependency-light,
synchronous, and deploys cleanly on shared hosting and typical Django setups
alike.
Shared hosting forbids persistent connections, daemons and async loops. The
monitoring thread needs none of these — plain HTTPS on the shared
`requests.Session`. Dependency-light, synchronous, deploys cleanly.
---
## See it in action
There's a complete, runnable Django project in [`examples/`](examples/). It
wires every gateway into real views, an `Order` model, provider callbacks and a
Fonepay background monitor — the way you'd actually use the library for real:
A runnable Django app in [`examples/`](examples/) wires every gateway into real
views, an `Order` model, provider callbacks and a Fonepay background monitor:
```bash
cd examples
@@ -499,51 +427,33 @@ cp .env.sample .env
uv run python manage.py makemigrations payments
uv run python manage.py migrate
uv run python manage.py runserver # open http://127.0.0.1:8000/
uv run python manage.py monitor_fonepay # poll Fonepay settlements in a worker
uv run python manage.py monitor_fonepay # poll settlements in a worker
```
See [`examples/README.md`](examples/README.md) for the full walkthrough. It
defaults to sandbox mode and reads credentials from environment variables, so
you can poke around the UI and the request/response shapes safely before going
live.
Defaults to sandbox; credentials come from env vars. See
[`examples/README.md`](examples/README.md) for the full walkthrough.
---
## Development
We use [uv](https://docs.astral.sh/uv/) for the environment and tooling:
Tooling via [uv](https://docs.astral.sh/uv/):
```bash
# install the package and dev deps in a venv
uv sync --extra dev
# lint (library, tests and the example app)
uv run ruff check nepali_payment tests examples/payments
# format
uv sync --extra dev # venv + dev deps
uv run ruff check nepali_payment tests examples/payments # lint
uv run ruff format --check nepali_payment tests examples/payments
# run all tests (live sandbox tests are skipped without credentials)
uv run pytest
# run tests with a coverage report (the suite sits around 97%)
uv run pytest --cov=nepali_payment
# live sandbox smoke tests (hits real provider endpoints)
NEPALI_PAYMENT_ESEWA_SECRET=... \
NEPALI_PAYMENT_KHALTI_SECRET=... \
NEPALI_PAYMENT_FONEPAY_SECRET=... NEPALI_PAYMENT_FONEPAY_MERCHANT=... \
NEPALI_PAYMENT_FONEPAY_USERNAME=... NEPALI_PAYMENT_FONEPAY_PASSWORD=... \
uv run pytest tests/test_live_api.py -v
uv run pytest # tests (~97% coverage with --cov)
NEPALI_PAYMENT_ESEWA_SECRET=... ... uv run pytest tests/test_live_api.py -v
```
## CI
GitHub Actions workflows live in `.github/workflows/`:
Workflows in `.github/workflows/`:
- **`ci.yml`** lints, formats, and runs a Python × Django test matrix with an
80% coverage gate, plus a package build check, on every push/PR.
- **`live-api.yml`** runs real sandbox smoke tests, either manually or on a
schedule; skipped unless provider credentials are configured as repo secrets.
- **`release.yml`** builds and publishes to PyPI on `django-nepali-payment/v*`
- **`ci.yml`** — lint, format, Python × Django test matrix (80% coverage gate),
package build, on push/PR.
- **`live-api.yml`** — real sandbox smoke tests; manual or scheduled; skipped
unless provider secrets are set.
- **`release.yml`** — builds and publishes to PyPI on `django-nepali-payment/v*`
tags.