Metadata-Version: 2.5
Name: aba-khqr
Version: 0.1.0
Summary: Python client for ABA PayWay QR payments and transaction status.
Project-URL: Homepage, https://developer.payway.com.kh/
Project-URL: Documentation, https://developer.payway.com.kh/qr-api-14530840e0
Author: aba-khqr contributors
License: MIT
License-File: LICENSE
Keywords: aba,cambodia,khqr,payments,payway
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial
Requires-Python: >=3.9
Requires-Dist: httpx>=0.27
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: respx>=0.21; extra == 'dev'
Description-Content-Type: text/markdown

# aba-khqr

Python client for [ABA PayWay](https://developer.payway.com.kh/) **QR API** and **Check transaction** API.

The library builds the HMAC-SHA512 request hash, Base64-encodes fields PayWay expects, and talks to sandbox or production checkout hosts.

| API | Method | Path |
| --- | --- | --- |
| [QR API](https://developer.payway.com.kh/qr-api-14530840e0) | `POST` | `/api/payment-gateway/v1/payments/generate-qr` |
| [Check transaction](https://developer.payway.com.kh/check-transaction-14530826e0) | `POST` | `/api/payment-gateway/v1/payments/check-transaction-2` |

Hosts:

- Sandbox: `https://checkout-sandbox.payway.com.kh`
- Production: `https://checkout.payway.com.kh`

## Install

```bash
pip install aba-khqr
```

The install/distribution name is `aba-khqr`; the Python import namespace remains `aba_payway`.

Requires Python 3.9+ and an ABA PayWay merchant id plus API key.

## Quick start

```python
from aba_payway import PayWayClient, Currency, PaymentOption, Environment

client = PayWayClient(
    merchant_id="your_merchant_id",
    api_key="your_api_key",
    environment=Environment.SANDBOX,  # or Environment.PRODUCTION
)

qr = client.generate_qr(
    amount=1.00,
    currency=Currency.USD,
    payment_option=PaymentOption.ABAPAY_KHQR,
    first_name="Sok",
    last_name="Dara",
    email="dara@example.com",
    phone="012345678",
    items=[{"name": "Coffee", "quantity": 1, "price": 1.00}],
    callback_url="https://yoursite.com/payway/callback",
    return_params={"order_id": "1001"},
    lifetime=30,  # minutes
)

print(qr.tran_id)
print(qr.qr_string)          # KHQR payload for your own renderer
print(qr.abapay_deeplink)    # open ABA Mobile
qr.save_qr_image("khqr.png") # PayWay-rendered PNG

status = client.check_transaction(qr.tran_id)
print(status.data.payment_status if status.data else status.status)
client.close()
```

`tran_id` is generated for you when omitted (max 20 characters). Keep your own copy; PayWay rejects duplicates.

### KHR vs USD

- USD is sent as a 2-decimal string (`1.00`). Minimum `0.01`.
- KHR is sent as whole riel (`4000`). Minimum `100`.
- `abapay_khqr` works for both currencies. WeChat and Alipay QR are USD-only.

### Confirm payments

PayWay POSTs to your `callback_url` after payment. Treat the callback as a hint and always confirm with `check_transaction` (last 7 days only).

```python
callback = client.parse_callback(request_json)
confirmed = client.check_transaction(callback.tran_id)
if confirmed.approved:
    fulfill_order(callback.tran_id)
```

See `examples/handle_callback.py`.

## Hashing

PayWay signs requests as `Base64(HMAC-SHA512(api_key, concatenated fields))`.

QR field order:

`req_time + merchant_id + tran_id + amount + items + first_name + last_name + email + phone + purchase_type + payment_option + callback_url + return_deeplink + currency + custom_fields + return_params + payout + lifetime + qr_image_template`

Check transaction field order:

`req_time + merchant_id + tran_id`

Missing optional QR fields are hashed as empty strings, matching the PHP samples in the official docs.

## Development

```bash
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest
```

Sandbox credentials come from ABA after you register a PayWay sandbox merchant. Do not commit API keys.

## License

MIT
