varizet 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- varizet-0.2.0/.gitignore +8 -0
- varizet-0.2.0/LICENSE +21 -0
- varizet-0.2.0/PKG-INFO +248 -0
- varizet-0.2.0/README.md +212 -0
- varizet-0.2.0/pyproject.toml +70 -0
- varizet-0.2.0/src/varizet/__init__.py +93 -0
- varizet-0.2.0/src/varizet/_base.py +157 -0
- varizet-0.2.0/src/varizet/_client.py +679 -0
- varizet-0.2.0/src/varizet/_errors.py +123 -0
- varizet-0.2.0/src/varizet/_models.py +503 -0
- varizet-0.2.0/src/varizet/_specs.py +301 -0
- varizet-0.2.0/src/varizet/_version.py +1 -0
- varizet-0.2.0/src/varizet/contrib/__init__.py +2 -0
- varizet-0.2.0/src/varizet/contrib/django.py +98 -0
- varizet-0.2.0/src/varizet/contrib/fastapi.py +57 -0
- varizet-0.2.0/src/varizet/formatting.py +24 -0
- varizet-0.2.0/src/varizet/py.typed +0 -0
- varizet-0.2.0/src/varizet/webhooks.py +166 -0
- varizet-0.2.0/tests/__init__.py +0 -0
- varizet-0.2.0/tests/conftest.py +132 -0
- varizet-0.2.0/tests/test_async.py +112 -0
- varizet-0.2.0/tests/test_client.py +435 -0
- varizet-0.2.0/tests/test_frameworks.py +157 -0
- varizet-0.2.0/tests/test_webhooks.py +117 -0
varizet-0.2.0/.gitignore
ADDED
varizet-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Varizet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
varizet-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: varizet
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Official Python SDK for the Varizet (واریزت) card-to-card payment API: orders, refunds, webhooks, sandbox.
|
|
5
|
+
Project-URL: Documentation, https://varizet.ir/developers
|
|
6
|
+
Project-URL: Source, https://github.com/Varizet/varizet-python
|
|
7
|
+
Author: Varizet
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: card-to-card,iran,payments,varizet,webhooks
|
|
11
|
+
Classifier: Framework :: Django
|
|
12
|
+
Classifier: Framework :: FastAPI
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Requires-Dist: httpx<1,>=0.25
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: anyio>=4; extra == 'dev'
|
|
25
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
26
|
+
Requires-Dist: django>=4.2; extra == 'dev'
|
|
27
|
+
Requires-Dist: fastapi>=0.100; extra == 'dev'
|
|
28
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
31
|
+
Provides-Extra: django
|
|
32
|
+
Requires-Dist: django>=4.2; extra == 'django'
|
|
33
|
+
Provides-Extra: fastapi
|
|
34
|
+
Requires-Dist: fastapi>=0.100; extra == 'fastapi'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# varizet (Python)
|
|
38
|
+
|
|
39
|
+
SDK رسمی پایتون برای API عمومی واریزت: سفارش کارتبهکارت با مبلغ یکتا، وبهوک امضاشده و Sandbox.
|
|
40
|
+
|
|
41
|
+
Official Python SDK for the [Varizet](https://varizet.ir) public API (`/v1`, [developer docs](https://varizet.ir/developers)). Python 3.10+, sync and async
|
|
42
|
+
clients on [httpx](https://www.python-httpx.org/) (the only required dependency), typed models,
|
|
43
|
+
typed errors, webhook verification, and optional Django and FastAPI helpers.
|
|
44
|
+
|
|
45
|
+
**Money is always an `int` number of rial** (never float, never toman). Timestamps are aware UTC
|
|
46
|
+
`datetime` objects.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install varizet # client + webhooks
|
|
50
|
+
pip install 'varizet[django]' # + view decorator
|
|
51
|
+
pip install 'varizet[fastapi]' # + dependency
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from varizet import Varizet
|
|
58
|
+
|
|
59
|
+
client = Varizet("vz_test_…") # or set VARIZET_API_KEY; VARIZET_API_BASE overrides the URL
|
|
60
|
+
|
|
61
|
+
order = client.orders.create(
|
|
62
|
+
amount=8_500_000, # rial
|
|
63
|
+
merchant_order_id="INV-12345",
|
|
64
|
+
customer={"name": "سارا محمدی", "phone": "09121234567"},
|
|
65
|
+
metadata={"cart_id": "c_998"},
|
|
66
|
+
)
|
|
67
|
+
print(order.code, order.unique_amount, order.payment_url) # the customer pays unique_amount
|
|
68
|
+
|
|
69
|
+
order = client.orders.get("VZ-8K2Q") # ord_… or VZ-…
|
|
70
|
+
client.orders.cancel(order.id)
|
|
71
|
+
print(client.me().store.name) # GET /v1/me: store, mode, scopes
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Async, same surface:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from varizet import AsyncVarizet
|
|
78
|
+
|
|
79
|
+
async with AsyncVarizet("vz_test_…") as client:
|
|
80
|
+
order = await client.orders.create(amount=8_500_000)
|
|
81
|
+
async for o in client.orders.list_auto_paging(status="paid"):
|
|
82
|
+
...
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
| Resource | Methods |
|
|
86
|
+
| ------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
87
|
+
| `orders` | `create`, `get`, `list`, `list_auto_paging`, `cancel` |
|
|
88
|
+
| `refunds` | `create` (see the caveat below) |
|
|
89
|
+
| `webhook_endpoints` | `create` (returns the one-time `secret`), `list`, `list_auto_paging`, `get`, `update`, `delete`, `rotate_secret`, `ping` |
|
|
90
|
+
| `events` | `list`, `list_auto_paging`, `resend` |
|
|
91
|
+
| `test` | `simulate_deposit` (test keys only) |
|
|
92
|
+
| client | `me()` |
|
|
93
|
+
|
|
94
|
+
### Idempotency and retries
|
|
95
|
+
|
|
96
|
+
Every `POST` sends an `Idempotency-Key`: a fresh UUID, or yours with `idempotency_key="…"`. Use your
|
|
97
|
+
own (for example `f"order-{invoice.id}"`) when you might call again after a crash: the API replays
|
|
98
|
+
the first answer for 24 h, and a different body with the same key raises `ConflictError`
|
|
99
|
+
(`idempotency_mismatch`). Network errors, timeouts and `500/502/503/504` are retried up to
|
|
100
|
+
`max_retries` (default 2) with backoff, **with the same key**, so a retry never creates a second
|
|
101
|
+
order.
|
|
102
|
+
|
|
103
|
+
### Pagination
|
|
104
|
+
|
|
105
|
+
`list()` returns one `Page` (`data`, `has_more`, `next_cursor`); pass `starting_after=page.next_cursor`
|
|
106
|
+
for the next one, or let `list_auto_paging()` walk every page lazily (`limit` 1–100, newest first).
|
|
107
|
+
Order filters: `status`, `merchant_order_id`, `customer_phone`, `created_gte`, `created_lte`
|
|
108
|
+
(`datetime` or ISO string).
|
|
109
|
+
|
|
110
|
+
### Errors
|
|
111
|
+
|
|
112
|
+
Failed calls raise a subclass of `varizet.VarizetError`. API errors carry the stable English `code`
|
|
113
|
+
and the Persian `message` from the API:
|
|
114
|
+
|
|
115
|
+
| Exception | When |
|
|
116
|
+
| ---------------------------------------- | ----------------------------------------------------- |
|
|
117
|
+
| `InvalidRequestError` | 400 (`amount_too_small`, `invalid_parameter`…) |
|
|
118
|
+
| `AuthenticationError` | 401 (`invalid_api_key`, `key_revoked`) |
|
|
119
|
+
| `PermissionDeniedError` | 403 (`scope_missing`, `testmode_required`…) |
|
|
120
|
+
| `NotFoundError` | 404 (`order_not_found`…) |
|
|
121
|
+
| `ConflictError` | 409 (`idempotency_mismatch`, `order_not_cancelable`…) |
|
|
122
|
+
| `ValidationError` | 422 (`merchant_order_id_duplicate`…) |
|
|
123
|
+
| `RateLimitError` | 429; `retry_after` seconds |
|
|
124
|
+
| `APIServerError` | 5xx; give `request_id` to support |
|
|
125
|
+
| `APIConnectionError` / `APITimeoutError` | no usable answer (network, timeout, malformed body) |
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from varizet import ConflictError
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
client.orders.cancel("VZ-8K2Q")
|
|
132
|
+
except ConflictError as err:
|
|
133
|
+
print(err.code, err.message, err.request_id) # order_not_cancelable, این سفارش قابل لغو نیست.
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Webhooks
|
|
137
|
+
|
|
138
|
+
Varizet signs every delivery: `Varizet-Signature: t=<unix>,v1=<hex hmac_sha256(secret, t + "." + body)>`.
|
|
139
|
+
Verify the **raw body bytes** (never re-serialised JSON); pass the current secret and, while a
|
|
140
|
+
rotation overlaps (24 h), the previous one too. Timestamps older or newer than 300 s are rejected.
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from varizet import WebhookSignatureError, construct_event
|
|
144
|
+
|
|
145
|
+
try:
|
|
146
|
+
event = construct_event(raw_body, headers.get("Varizet-Signature"), ["whsec_current", "whsec_previous"])
|
|
147
|
+
except WebhookSignatureError as err:
|
|
148
|
+
return 400, err.reason # malformed | expired | mismatch | invalid_payload
|
|
149
|
+
if event.type == "order.paid":
|
|
150
|
+
order = event.order() # data.object as a typed Order
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`verify_signature(...)` only checks; `compute_signature` / `build_signature_header` sign (tests).
|
|
154
|
+
The rules are pinned by cross-language test vectors shared with the API and the other SDKs.
|
|
155
|
+
|
|
156
|
+
### Django
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
# settings.py
|
|
160
|
+
VARIZET_WEBHOOK_SECRETS = ["whsec_…"]
|
|
161
|
+
|
|
162
|
+
# views.py
|
|
163
|
+
from django.http import HttpResponse
|
|
164
|
+
from varizet.contrib.django import varizet_webhook
|
|
165
|
+
|
|
166
|
+
@varizet_webhook # or @varizet_webhook(secrets=[…]); sync or async views
|
|
167
|
+
def varizet_hook(request):
|
|
168
|
+
event = request.varizet_event # varizet.WebhookEvent
|
|
169
|
+
...
|
|
170
|
+
return HttpResponse(status=200)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
CSRF-exempt and POST-only; a bad signature answers `400 {"error": "<reason>"}` without calling the view.
|
|
174
|
+
|
|
175
|
+
### FastAPI
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from fastapi import Depends, FastAPI
|
|
179
|
+
from varizet import WebhookEvent
|
|
180
|
+
from varizet.contrib.fastapi import VarizetWebhook
|
|
181
|
+
|
|
182
|
+
app = FastAPI()
|
|
183
|
+
verified = VarizetWebhook(["whsec_…"]) # or a callable returning the secrets
|
|
184
|
+
|
|
185
|
+
@app.post("/webhooks/varizet")
|
|
186
|
+
async def hook(event: WebhookEvent = Depends(verified)):
|
|
187
|
+
return {"received": True}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Rejected requests get `HTTPException(400, {"error": "<reason>"})`.
|
|
191
|
+
|
|
192
|
+
## Test mode
|
|
193
|
+
|
|
194
|
+
With a `vz_test_…` key everything is real except the money: create an order, then pay it through
|
|
195
|
+
the real matcher with a simulated bank SMS (`POST /v1/test/deposits`):
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
order = client.orders.create(amount=8_500_000)
|
|
199
|
+
deposit = client.test.simulate_deposit(amount=order.unique_amount, card_id=order.destination.card_id)
|
|
200
|
+
assert deposit.match_status == "matched"
|
|
201
|
+
assert client.orders.get(order.id).is_paid # order.paid webhook follows
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Live keys get `PermissionDeniedError` (`testmode_required`).
|
|
205
|
+
|
|
206
|
+
## Refunds
|
|
207
|
+
|
|
208
|
+
`client.refunds.create(order_id=…, amount=…, method="card"|"iban", reference=…, reason=…)` records a
|
|
209
|
+
refund the merchant transferred (`POST /v1/refunds`, `refunds:write`; a bookkeeping
|
|
210
|
+
record, the merchant moves the money). It is the same record as «ثبت بازپرداخت» in the panel, and
|
|
211
|
+
both send the `refund.created` webhook whose `data.object` parses with `Refund.from_dict`.
|
|
212
|
+
`client.refunds.get(id)`, `.list(order=…)` and `.list_auto_paging()` read them back
|
|
213
|
+
(`orders:read` or `refunds:write`).
|
|
214
|
+
|
|
215
|
+
## Formatting
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
from varizet import format_rial
|
|
219
|
+
format_rial(8_500_430) # '۸٬۵۰۰٬۴۳۰'
|
|
220
|
+
format_rial(8_500_430, unit=True) # '۸٬۵۰۰٬۴۳۰ ریال'
|
|
221
|
+
format_rial(8_500_430, persian_digits=False) # '8,500,430'
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Development
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
python -m venv .venv && . .venv/bin/activate
|
|
228
|
+
pip install -e '.[dev]'
|
|
229
|
+
pytest # httpx.MockTransport, Django test client, FastAPI TestClient
|
|
230
|
+
ruff check src tests && mypy
|
|
231
|
+
python -m build # dist/varizet-0.1.0-py3-none-any.whl + sdist
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Inside the Varizet monorepo the webhook tests also read the API's shared vectors; elsewhere (this
|
|
235
|
+
repository, an sdist) those cases are skipped.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Contributing and security
|
|
240
|
+
|
|
241
|
+
Issues and pull requests are welcome. This repository is published from Varizet's main repository:
|
|
242
|
+
accepted changes are applied there and appear here with the next release.
|
|
243
|
+
|
|
244
|
+
Please report security vulnerabilities privately to support@varizet.ir, not in public issues.
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
MIT, see [LICENSE](LICENSE).
|
varizet-0.2.0/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# varizet (Python)
|
|
2
|
+
|
|
3
|
+
SDK رسمی پایتون برای API عمومی واریزت: سفارش کارتبهکارت با مبلغ یکتا، وبهوک امضاشده و Sandbox.
|
|
4
|
+
|
|
5
|
+
Official Python SDK for the [Varizet](https://varizet.ir) public API (`/v1`, [developer docs](https://varizet.ir/developers)). Python 3.10+, sync and async
|
|
6
|
+
clients on [httpx](https://www.python-httpx.org/) (the only required dependency), typed models,
|
|
7
|
+
typed errors, webhook verification, and optional Django and FastAPI helpers.
|
|
8
|
+
|
|
9
|
+
**Money is always an `int` number of rial** (never float, never toman). Timestamps are aware UTC
|
|
10
|
+
`datetime` objects.
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install varizet # client + webhooks
|
|
14
|
+
pip install 'varizet[django]' # + view decorator
|
|
15
|
+
pip install 'varizet[fastapi]' # + dependency
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from varizet import Varizet
|
|
22
|
+
|
|
23
|
+
client = Varizet("vz_test_…") # or set VARIZET_API_KEY; VARIZET_API_BASE overrides the URL
|
|
24
|
+
|
|
25
|
+
order = client.orders.create(
|
|
26
|
+
amount=8_500_000, # rial
|
|
27
|
+
merchant_order_id="INV-12345",
|
|
28
|
+
customer={"name": "سارا محمدی", "phone": "09121234567"},
|
|
29
|
+
metadata={"cart_id": "c_998"},
|
|
30
|
+
)
|
|
31
|
+
print(order.code, order.unique_amount, order.payment_url) # the customer pays unique_amount
|
|
32
|
+
|
|
33
|
+
order = client.orders.get("VZ-8K2Q") # ord_… or VZ-…
|
|
34
|
+
client.orders.cancel(order.id)
|
|
35
|
+
print(client.me().store.name) # GET /v1/me: store, mode, scopes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Async, same surface:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from varizet import AsyncVarizet
|
|
42
|
+
|
|
43
|
+
async with AsyncVarizet("vz_test_…") as client:
|
|
44
|
+
order = await client.orders.create(amount=8_500_000)
|
|
45
|
+
async for o in client.orders.list_auto_paging(status="paid"):
|
|
46
|
+
...
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
| Resource | Methods |
|
|
50
|
+
| ------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
51
|
+
| `orders` | `create`, `get`, `list`, `list_auto_paging`, `cancel` |
|
|
52
|
+
| `refunds` | `create` (see the caveat below) |
|
|
53
|
+
| `webhook_endpoints` | `create` (returns the one-time `secret`), `list`, `list_auto_paging`, `get`, `update`, `delete`, `rotate_secret`, `ping` |
|
|
54
|
+
| `events` | `list`, `list_auto_paging`, `resend` |
|
|
55
|
+
| `test` | `simulate_deposit` (test keys only) |
|
|
56
|
+
| client | `me()` |
|
|
57
|
+
|
|
58
|
+
### Idempotency and retries
|
|
59
|
+
|
|
60
|
+
Every `POST` sends an `Idempotency-Key`: a fresh UUID, or yours with `idempotency_key="…"`. Use your
|
|
61
|
+
own (for example `f"order-{invoice.id}"`) when you might call again after a crash: the API replays
|
|
62
|
+
the first answer for 24 h, and a different body with the same key raises `ConflictError`
|
|
63
|
+
(`idempotency_mismatch`). Network errors, timeouts and `500/502/503/504` are retried up to
|
|
64
|
+
`max_retries` (default 2) with backoff, **with the same key**, so a retry never creates a second
|
|
65
|
+
order.
|
|
66
|
+
|
|
67
|
+
### Pagination
|
|
68
|
+
|
|
69
|
+
`list()` returns one `Page` (`data`, `has_more`, `next_cursor`); pass `starting_after=page.next_cursor`
|
|
70
|
+
for the next one, or let `list_auto_paging()` walk every page lazily (`limit` 1–100, newest first).
|
|
71
|
+
Order filters: `status`, `merchant_order_id`, `customer_phone`, `created_gte`, `created_lte`
|
|
72
|
+
(`datetime` or ISO string).
|
|
73
|
+
|
|
74
|
+
### Errors
|
|
75
|
+
|
|
76
|
+
Failed calls raise a subclass of `varizet.VarizetError`. API errors carry the stable English `code`
|
|
77
|
+
and the Persian `message` from the API:
|
|
78
|
+
|
|
79
|
+
| Exception | When |
|
|
80
|
+
| ---------------------------------------- | ----------------------------------------------------- |
|
|
81
|
+
| `InvalidRequestError` | 400 (`amount_too_small`, `invalid_parameter`…) |
|
|
82
|
+
| `AuthenticationError` | 401 (`invalid_api_key`, `key_revoked`) |
|
|
83
|
+
| `PermissionDeniedError` | 403 (`scope_missing`, `testmode_required`…) |
|
|
84
|
+
| `NotFoundError` | 404 (`order_not_found`…) |
|
|
85
|
+
| `ConflictError` | 409 (`idempotency_mismatch`, `order_not_cancelable`…) |
|
|
86
|
+
| `ValidationError` | 422 (`merchant_order_id_duplicate`…) |
|
|
87
|
+
| `RateLimitError` | 429; `retry_after` seconds |
|
|
88
|
+
| `APIServerError` | 5xx; give `request_id` to support |
|
|
89
|
+
| `APIConnectionError` / `APITimeoutError` | no usable answer (network, timeout, malformed body) |
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from varizet import ConflictError
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
client.orders.cancel("VZ-8K2Q")
|
|
96
|
+
except ConflictError as err:
|
|
97
|
+
print(err.code, err.message, err.request_id) # order_not_cancelable, این سفارش قابل لغو نیست.
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Webhooks
|
|
101
|
+
|
|
102
|
+
Varizet signs every delivery: `Varizet-Signature: t=<unix>,v1=<hex hmac_sha256(secret, t + "." + body)>`.
|
|
103
|
+
Verify the **raw body bytes** (never re-serialised JSON); pass the current secret and, while a
|
|
104
|
+
rotation overlaps (24 h), the previous one too. Timestamps older or newer than 300 s are rejected.
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from varizet import WebhookSignatureError, construct_event
|
|
108
|
+
|
|
109
|
+
try:
|
|
110
|
+
event = construct_event(raw_body, headers.get("Varizet-Signature"), ["whsec_current", "whsec_previous"])
|
|
111
|
+
except WebhookSignatureError as err:
|
|
112
|
+
return 400, err.reason # malformed | expired | mismatch | invalid_payload
|
|
113
|
+
if event.type == "order.paid":
|
|
114
|
+
order = event.order() # data.object as a typed Order
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`verify_signature(...)` only checks; `compute_signature` / `build_signature_header` sign (tests).
|
|
118
|
+
The rules are pinned by cross-language test vectors shared with the API and the other SDKs.
|
|
119
|
+
|
|
120
|
+
### Django
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
# settings.py
|
|
124
|
+
VARIZET_WEBHOOK_SECRETS = ["whsec_…"]
|
|
125
|
+
|
|
126
|
+
# views.py
|
|
127
|
+
from django.http import HttpResponse
|
|
128
|
+
from varizet.contrib.django import varizet_webhook
|
|
129
|
+
|
|
130
|
+
@varizet_webhook # or @varizet_webhook(secrets=[…]); sync or async views
|
|
131
|
+
def varizet_hook(request):
|
|
132
|
+
event = request.varizet_event # varizet.WebhookEvent
|
|
133
|
+
...
|
|
134
|
+
return HttpResponse(status=200)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
CSRF-exempt and POST-only; a bad signature answers `400 {"error": "<reason>"}` without calling the view.
|
|
138
|
+
|
|
139
|
+
### FastAPI
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from fastapi import Depends, FastAPI
|
|
143
|
+
from varizet import WebhookEvent
|
|
144
|
+
from varizet.contrib.fastapi import VarizetWebhook
|
|
145
|
+
|
|
146
|
+
app = FastAPI()
|
|
147
|
+
verified = VarizetWebhook(["whsec_…"]) # or a callable returning the secrets
|
|
148
|
+
|
|
149
|
+
@app.post("/webhooks/varizet")
|
|
150
|
+
async def hook(event: WebhookEvent = Depends(verified)):
|
|
151
|
+
return {"received": True}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Rejected requests get `HTTPException(400, {"error": "<reason>"})`.
|
|
155
|
+
|
|
156
|
+
## Test mode
|
|
157
|
+
|
|
158
|
+
With a `vz_test_…` key everything is real except the money: create an order, then pay it through
|
|
159
|
+
the real matcher with a simulated bank SMS (`POST /v1/test/deposits`):
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
order = client.orders.create(amount=8_500_000)
|
|
163
|
+
deposit = client.test.simulate_deposit(amount=order.unique_amount, card_id=order.destination.card_id)
|
|
164
|
+
assert deposit.match_status == "matched"
|
|
165
|
+
assert client.orders.get(order.id).is_paid # order.paid webhook follows
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Live keys get `PermissionDeniedError` (`testmode_required`).
|
|
169
|
+
|
|
170
|
+
## Refunds
|
|
171
|
+
|
|
172
|
+
`client.refunds.create(order_id=…, amount=…, method="card"|"iban", reference=…, reason=…)` records a
|
|
173
|
+
refund the merchant transferred (`POST /v1/refunds`, `refunds:write`; a bookkeeping
|
|
174
|
+
record, the merchant moves the money). It is the same record as «ثبت بازپرداخت» in the panel, and
|
|
175
|
+
both send the `refund.created` webhook whose `data.object` parses with `Refund.from_dict`.
|
|
176
|
+
`client.refunds.get(id)`, `.list(order=…)` and `.list_auto_paging()` read them back
|
|
177
|
+
(`orders:read` or `refunds:write`).
|
|
178
|
+
|
|
179
|
+
## Formatting
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from varizet import format_rial
|
|
183
|
+
format_rial(8_500_430) # '۸٬۵۰۰٬۴۳۰'
|
|
184
|
+
format_rial(8_500_430, unit=True) # '۸٬۵۰۰٬۴۳۰ ریال'
|
|
185
|
+
format_rial(8_500_430, persian_digits=False) # '8,500,430'
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Development
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
python -m venv .venv && . .venv/bin/activate
|
|
192
|
+
pip install -e '.[dev]'
|
|
193
|
+
pytest # httpx.MockTransport, Django test client, FastAPI TestClient
|
|
194
|
+
ruff check src tests && mypy
|
|
195
|
+
python -m build # dist/varizet-0.1.0-py3-none-any.whl + sdist
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Inside the Varizet monorepo the webhook tests also read the API's shared vectors; elsewhere (this
|
|
199
|
+
repository, an sdist) those cases are skipped.
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Contributing and security
|
|
204
|
+
|
|
205
|
+
Issues and pull requests are welcome. This repository is published from Varizet's main repository:
|
|
206
|
+
accepted changes are applied there and appear here with the next release.
|
|
207
|
+
|
|
208
|
+
Please report security vulnerabilities privately to support@varizet.ir, not in public issues.
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT, see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "varizet"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Official Python SDK for the Varizet (واریزت) card-to-card payment API: orders, refunds, webhooks, sandbox."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Varizet" }]
|
|
13
|
+
keywords = ["varizet", "payments", "card-to-card", "iran", "webhooks"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Framework :: Django",
|
|
23
|
+
"Framework :: FastAPI",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = ["httpx>=0.25,<1"]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
django = ["django>=4.2"]
|
|
30
|
+
fastapi = ["fastapi>=0.100"]
|
|
31
|
+
dev = ["pytest>=8", "anyio>=4", "django>=4.2", "fastapi>=0.100", "mypy>=1.10", "ruff>=0.5", "build>=1.2"]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Documentation = "https://varizet.ir/developers"
|
|
35
|
+
Source = "https://github.com/Varizet/varizet-python"
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.wheel]
|
|
38
|
+
packages = ["src/varizet"]
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.sdist]
|
|
41
|
+
include = ["src/varizet", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
42
|
+
|
|
43
|
+
[tool.pytest.ini_options]
|
|
44
|
+
testpaths = ["tests"]
|
|
45
|
+
addopts = "-q"
|
|
46
|
+
filterwarnings = [
|
|
47
|
+
"error::DeprecationWarning:varizet.*",
|
|
48
|
+
"ignore:Using `httpx` with `starlette.testclient` is deprecated",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.mypy]
|
|
52
|
+
strict = true
|
|
53
|
+
python_version = "3.10"
|
|
54
|
+
files = ["src/varizet"]
|
|
55
|
+
|
|
56
|
+
[[tool.mypy.overrides]]
|
|
57
|
+
module = ["django.*"]
|
|
58
|
+
ignore_missing_imports = true
|
|
59
|
+
|
|
60
|
+
[tool.ruff]
|
|
61
|
+
line-length = 110
|
|
62
|
+
target-version = "py310"
|
|
63
|
+
src = ["src", "tests"]
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint]
|
|
66
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF", "N", "S", "PT"]
|
|
67
|
+
ignore = ["S101", "RUF001", "RUF002", "RUF003"]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.lint.per-file-ignores]
|
|
70
|
+
"tests/*" = ["S105", "S106", "PT018", "PT011"]
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Varizet (واریزت) Python SDK: the public ``/v1`` API, webhook verification and sandbox helpers.
|
|
2
|
+
|
|
3
|
+
Money is ``int`` rial everywhere; timestamps are aware UTC ``datetime`` objects.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ._base import API_VERSION, DEFAULT_BASE_URL
|
|
7
|
+
from ._client import AsyncVarizet, Varizet
|
|
8
|
+
from ._errors import (
|
|
9
|
+
APIConnectionError,
|
|
10
|
+
APIError,
|
|
11
|
+
APIServerError,
|
|
12
|
+
APITimeoutError,
|
|
13
|
+
AuthenticationError,
|
|
14
|
+
ConflictError,
|
|
15
|
+
InvalidRequestError,
|
|
16
|
+
NotFoundError,
|
|
17
|
+
PermissionDeniedError,
|
|
18
|
+
RateLimitError,
|
|
19
|
+
ValidationError,
|
|
20
|
+
VarizetError,
|
|
21
|
+
)
|
|
22
|
+
from ._models import (
|
|
23
|
+
ORDER_STATUSES,
|
|
24
|
+
WEBHOOK_EVENT_TYPES,
|
|
25
|
+
ApiKeyIdentity,
|
|
26
|
+
CreatedWebhookEndpoint,
|
|
27
|
+
Customer,
|
|
28
|
+
Deleted,
|
|
29
|
+
Deposit,
|
|
30
|
+
EventResent,
|
|
31
|
+
Order,
|
|
32
|
+
OrderDestination,
|
|
33
|
+
OrderMatch,
|
|
34
|
+
Page,
|
|
35
|
+
Refund,
|
|
36
|
+
RotatedSecret,
|
|
37
|
+
StoreRef,
|
|
38
|
+
WebhookEndpoint,
|
|
39
|
+
WebhookEvent,
|
|
40
|
+
WebhookPingResult,
|
|
41
|
+
)
|
|
42
|
+
from ._version import __version__
|
|
43
|
+
from .formatting import format_rial, to_persian_digits
|
|
44
|
+
from .webhooks import (
|
|
45
|
+
SIGNATURE_HEADER,
|
|
46
|
+
WebhookSignatureError,
|
|
47
|
+
construct_event,
|
|
48
|
+
verify_signature,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
"API_VERSION",
|
|
53
|
+
"DEFAULT_BASE_URL",
|
|
54
|
+
"ORDER_STATUSES",
|
|
55
|
+
"SIGNATURE_HEADER",
|
|
56
|
+
"WEBHOOK_EVENT_TYPES",
|
|
57
|
+
"APIConnectionError",
|
|
58
|
+
"APIError",
|
|
59
|
+
"APIServerError",
|
|
60
|
+
"APITimeoutError",
|
|
61
|
+
"ApiKeyIdentity",
|
|
62
|
+
"AsyncVarizet",
|
|
63
|
+
"AuthenticationError",
|
|
64
|
+
"ConflictError",
|
|
65
|
+
"CreatedWebhookEndpoint",
|
|
66
|
+
"Customer",
|
|
67
|
+
"Deleted",
|
|
68
|
+
"Deposit",
|
|
69
|
+
"EventResent",
|
|
70
|
+
"InvalidRequestError",
|
|
71
|
+
"NotFoundError",
|
|
72
|
+
"Order",
|
|
73
|
+
"OrderDestination",
|
|
74
|
+
"OrderMatch",
|
|
75
|
+
"Page",
|
|
76
|
+
"PermissionDeniedError",
|
|
77
|
+
"RateLimitError",
|
|
78
|
+
"Refund",
|
|
79
|
+
"RotatedSecret",
|
|
80
|
+
"StoreRef",
|
|
81
|
+
"ValidationError",
|
|
82
|
+
"Varizet",
|
|
83
|
+
"VarizetError",
|
|
84
|
+
"WebhookEndpoint",
|
|
85
|
+
"WebhookEvent",
|
|
86
|
+
"WebhookPingResult",
|
|
87
|
+
"WebhookSignatureError",
|
|
88
|
+
"__version__",
|
|
89
|
+
"construct_event",
|
|
90
|
+
"format_rial",
|
|
91
|
+
"to_persian_digits",
|
|
92
|
+
"verify_signature",
|
|
93
|
+
]
|