Metadata-Version: 2.5
Name: cashela-payin
Version: 0.1.0
Summary: Cashela Pay-In client SDK
Project-URL: Repository, https://github.com/cashela/cashela-payin-python
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# cashela-payin

Python SDK for the Cashela Pay-In API. Same OpenAPI contract and the same HMAC
webhook vectors as the Node and PHP SDKs, so behavior is identical across
languages.

- Python 3.9+
- Zero runtime dependencies (stdlib only: `urllib`, `hmac`, `hashlib`, `json`)
- Import name: `cashela_payin` (package name on PyPI: `cashela-payin`)

## Install

```bash
pip install cashela-payin
```

```python
import cashela_payin
```

## Server-only

**Run this SDK only on your server.** `api_secret` (client auth) and your
webhook signing secret must never reach the browser or any client-side
bundle. If you're calling `CashelaPayIn` or `verify_webhook_signature` from a
Node/Next.js-style app, do it from an API route or server action — never
from client components.

## `CashelaPayIn` client

```python
from cashela_payin import CashelaPayIn, CashelaApiError

client = CashelaPayIn(
    environment="sandbox",  # "dev" | "staging" | "sandbox" | "production"
    api_key="YOUR_API_KEY",
    api_secret="YOUR_API_SECRET",  # server-side only, never in browser code
)

try:
    countries = client.list_countries()

    deposit = client.create_deposit(
        {
            "external_identifier": "order-123",
            # ... rest of the deposit-creation payload
        },
        idempotency_key="order-123",  # only used on create; this POST is never retried by the SDK
    )

    transaction = client.get_transaction(deposit["data"]["reference"])
except CashelaApiError as err:
    print(err.status, str(err), err.raw)
```

Other methods: `list_payment_methods(body)`, `get_exchange_rates(body)`,
`resend_callback(reference)`.

## Verifying webhooks

`verify_webhook_signature` checks the `X-Cashela-Signature` /
`X-Cashela-Timestamp` / `X-Cashela-Nonce` headers against the **raw** request
body and raises `WebhookSignatureError` (with `.reason` one of
`missing-header`, `stale-timestamp`, `bad-signature`) if verification fails.

Always read the body as raw bytes before any JSON parsing — signing is over
the exact bytes Cashela sent, and re-serializing a parsed body will not match.

Example with a stdlib `http.server` handler:

```python
from http.server import BaseHTTPRequestHandler
from cashela_payin import verify_webhook_signature, WebhookSignatureError

WEBHOOK_SECRET = "..."  # server-side only, never in browser code


class WebhookHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        raw_body = self.rfile.read(int(self.headers.get("Content-Length", 0)))  # bytes, unparsed

        try:
            payload = verify_webhook_signature(raw_body, self.headers, WEBHOOK_SECRET)
        except WebhookSignatureError as err:
            self.send_response(400)
            self.end_headers()
            self.wfile.write(f"invalid webhook: {err.reason}".encode())
            return

        # payload is the decoded JSON body; safe to process now.
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"ok")
```

The same pattern applies to any framework (Flask, Django, FastAPI, etc.): get
the raw request body as `bytes` (or `str`) before it's parsed, pass it and
the request headers to `verify_webhook_signature`, and respond `2xx` on
success or `400` on `WebhookSignatureError`.

## Development

```bash
python scripts/sync_vectors.py --check   # verify HMAC test vectors are in sync with the contract repo
python -m unittest discover -s tests -v  # run the test suite
```
