stableops 0.7.1__py3-none-any.whl
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.
- stableops/__init__.py +83 -0
- stableops/addresses.py +256 -0
- stableops/checkout_sessions.py +158 -0
- stableops/client.py +137 -0
- stableops/errors.py +41 -0
- stableops/http.py +536 -0
- stableops/payment_orders.py +235 -0
- stableops/types.py +225 -0
- stableops/webhooks.py +378 -0
- stableops-0.7.1.dist-info/METADATA +112 -0
- stableops-0.7.1.dist-info/RECORD +13 -0
- stableops-0.7.1.dist-info/WHEEL +4 -0
- stableops-0.7.1.dist-info/licenses/LICENSE +184 -0
stableops/__init__.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""StableOps Python SDK.
|
|
2
|
+
|
|
3
|
+
Official Python SDK for StableOps - Stablecoin payment infrastructure.
|
|
4
|
+
|
|
5
|
+
Example:
|
|
6
|
+
>>> from stableops import StableOps
|
|
7
|
+
>>> client = StableOps(api_key="your_api_key")
|
|
8
|
+
>>> order = client.payment_orders.create(
|
|
9
|
+
... merchant_order_id="order_123",
|
|
10
|
+
... amount="10.00",
|
|
11
|
+
... accepted_assets=[{"chain": "base", "asset": "USDC"}],
|
|
12
|
+
... expires_at="2026-06-20T12:30:00Z",
|
|
13
|
+
... )
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from stableops.client import AsyncStableOps, StableOps
|
|
17
|
+
from stableops.errors import StableOpsError
|
|
18
|
+
from stableops.types import (
|
|
19
|
+
AcceptedAssetInput,
|
|
20
|
+
AmountMode,
|
|
21
|
+
CheckoutSession,
|
|
22
|
+
CheckoutSessionStatus,
|
|
23
|
+
CreateCheckoutSessionInput,
|
|
24
|
+
CreatePaymentOrderInput,
|
|
25
|
+
CreateWebhookEndpointInput,
|
|
26
|
+
PaymentOrder,
|
|
27
|
+
PaymentOrderDetail,
|
|
28
|
+
PaymentOrderInstruction,
|
|
29
|
+
PaymentOrderStatus,
|
|
30
|
+
PaymentOrderTimelineEntry,
|
|
31
|
+
ReplayDeadLetterItem,
|
|
32
|
+
ReplayDeadLettersResult,
|
|
33
|
+
ReplayDeliveryResult,
|
|
34
|
+
UpdateWebhookEndpointInput,
|
|
35
|
+
WebhookDelivery,
|
|
36
|
+
WebhookDeliveryStatus,
|
|
37
|
+
WebhookEndpoint,
|
|
38
|
+
WebhookEventType,
|
|
39
|
+
)
|
|
40
|
+
from stableops.webhooks import (
|
|
41
|
+
DEFAULT_TOLERANCE_SECONDS,
|
|
42
|
+
DELIVERY_ID_HEADER,
|
|
43
|
+
EVENT_ID_HEADER,
|
|
44
|
+
SIGNATURE_HEADER,
|
|
45
|
+
verify_webhook_signature,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
__version__ = "0.7.0"
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
# Client
|
|
52
|
+
"StableOps",
|
|
53
|
+
"AsyncStableOps",
|
|
54
|
+
# Errors
|
|
55
|
+
"StableOpsError",
|
|
56
|
+
# Types
|
|
57
|
+
"AcceptedAssetInput",
|
|
58
|
+
"AmountMode",
|
|
59
|
+
"CheckoutSession",
|
|
60
|
+
"CheckoutSessionStatus",
|
|
61
|
+
"CreateCheckoutSessionInput",
|
|
62
|
+
"CreatePaymentOrderInput",
|
|
63
|
+
"CreateWebhookEndpointInput",
|
|
64
|
+
"PaymentOrder",
|
|
65
|
+
"PaymentOrderDetail",
|
|
66
|
+
"PaymentOrderInstruction",
|
|
67
|
+
"PaymentOrderStatus",
|
|
68
|
+
"PaymentOrderTimelineEntry",
|
|
69
|
+
"ReplayDeadLetterItem",
|
|
70
|
+
"ReplayDeadLettersResult",
|
|
71
|
+
"ReplayDeliveryResult",
|
|
72
|
+
"UpdateWebhookEndpointInput",
|
|
73
|
+
"WebhookDelivery",
|
|
74
|
+
"WebhookDeliveryStatus",
|
|
75
|
+
"WebhookEndpoint",
|
|
76
|
+
"WebhookEventType",
|
|
77
|
+
# Webhooks
|
|
78
|
+
"verify_webhook_signature",
|
|
79
|
+
"SIGNATURE_HEADER",
|
|
80
|
+
"EVENT_ID_HEADER",
|
|
81
|
+
"DELIVERY_ID_HEADER",
|
|
82
|
+
"DEFAULT_TOLERANCE_SECONDS",
|
|
83
|
+
]
|
stableops/addresses.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""Addresses API."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional, Union, cast
|
|
4
|
+
|
|
5
|
+
from stableops.http import AsyncHttpClient, HttpClient
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _Unset:
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
_UNSET = _Unset()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AddressesApi:
|
|
16
|
+
"""Addresses API (synchronous)."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, http: HttpClient) -> None:
|
|
19
|
+
self.http = http
|
|
20
|
+
|
|
21
|
+
def get_pools(self) -> List[Dict[str, Any]]:
|
|
22
|
+
"""Get address pool overview.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
List of address pools grouped by chain
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
>>> pools = client.addresses.get_pools()
|
|
29
|
+
>>> for pool in pools:
|
|
30
|
+
... print(pool["chain"], pool["available"], pool["total"])
|
|
31
|
+
"""
|
|
32
|
+
response = self.http.request(
|
|
33
|
+
method="GET",
|
|
34
|
+
path="/v1/addresses/pools",
|
|
35
|
+
)
|
|
36
|
+
return cast(List[Dict[str, Any]], response["pools"])
|
|
37
|
+
|
|
38
|
+
def import_addresses(
|
|
39
|
+
self,
|
|
40
|
+
chain: str,
|
|
41
|
+
addresses: List[str],
|
|
42
|
+
mode: Optional[str] = None,
|
|
43
|
+
label: Optional[str] = None,
|
|
44
|
+
) -> Dict[str, Any]:
|
|
45
|
+
"""Import receiving addresses.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
chain: Blockchain chain identifier
|
|
49
|
+
addresses: List of addresses to import
|
|
50
|
+
mode: Allocation mode ("single" or "shared", default "single")
|
|
51
|
+
label: Optional label applied to all addresses
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
Import result with imported count and addresses
|
|
55
|
+
|
|
56
|
+
Example:
|
|
57
|
+
>>> result = client.addresses.import_addresses(
|
|
58
|
+
... chain="base",
|
|
59
|
+
... addresses=["0x1234..."],
|
|
60
|
+
... mode="single",
|
|
61
|
+
... )
|
|
62
|
+
"""
|
|
63
|
+
body: Dict[str, Any] = {
|
|
64
|
+
"addresses": [
|
|
65
|
+
{"chain": chain, "address": addr, **({"label": label} if label else {})}
|
|
66
|
+
for addr in addresses
|
|
67
|
+
],
|
|
68
|
+
"mode": mode or "single",
|
|
69
|
+
}
|
|
70
|
+
return cast(Dict[str, Any], self.http.request(
|
|
71
|
+
method="POST",
|
|
72
|
+
path="/v1/addresses/import",
|
|
73
|
+
body=body,
|
|
74
|
+
))
|
|
75
|
+
|
|
76
|
+
def list(
|
|
77
|
+
self,
|
|
78
|
+
chain: Optional[str] = None,
|
|
79
|
+
status: Optional[str] = None,
|
|
80
|
+
limit: Optional[int] = None,
|
|
81
|
+
offset: Optional[int] = None,
|
|
82
|
+
) -> Dict[str, Any]:
|
|
83
|
+
"""List receiving addresses.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
chain: Filter by chain (optional)
|
|
87
|
+
status: Filter by status (optional)
|
|
88
|
+
limit: Page size (optional)
|
|
89
|
+
offset: Pagination offset (optional)
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
Response with items and has_more
|
|
93
|
+
|
|
94
|
+
Example:
|
|
95
|
+
>>> result = client.addresses.list(chain="base", limit=20)
|
|
96
|
+
"""
|
|
97
|
+
query: Dict[str, Any] = {}
|
|
98
|
+
if chain:
|
|
99
|
+
query["chain"] = chain
|
|
100
|
+
if status:
|
|
101
|
+
query["status"] = status
|
|
102
|
+
if limit is not None:
|
|
103
|
+
query["limit"] = limit
|
|
104
|
+
if offset is not None:
|
|
105
|
+
query["offset"] = offset
|
|
106
|
+
|
|
107
|
+
return cast(Dict[str, Any], self.http.request(
|
|
108
|
+
method="GET",
|
|
109
|
+
path="/v1/addresses",
|
|
110
|
+
query=query,
|
|
111
|
+
))
|
|
112
|
+
|
|
113
|
+
def remove(self, address_id: str) -> Dict[str, Any]:
|
|
114
|
+
"""Delete an address.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
address_id: Address ID
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
Success status
|
|
121
|
+
|
|
122
|
+
Example:
|
|
123
|
+
>>> client.addresses.remove("addr_abc123")
|
|
124
|
+
"""
|
|
125
|
+
return cast(Dict[str, Any], self.http.request(
|
|
126
|
+
method="DELETE",
|
|
127
|
+
path=f"/v1/addresses/{address_id}",
|
|
128
|
+
))
|
|
129
|
+
|
|
130
|
+
def update(
|
|
131
|
+
self,
|
|
132
|
+
address_id: str,
|
|
133
|
+
label: Union[str, None, _Unset] = _UNSET,
|
|
134
|
+
mode: Optional[str] = None,
|
|
135
|
+
status: Optional[str] = None,
|
|
136
|
+
) -> Dict[str, Any]:
|
|
137
|
+
"""Update an address.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
address_id: Address ID
|
|
141
|
+
label: New label (optional, set null to clear)
|
|
142
|
+
mode: New mode ("single" or "shared", optional)
|
|
143
|
+
status: New status ("available", "reserved", or "disabled", optional)
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
Updated address
|
|
147
|
+
|
|
148
|
+
Example:
|
|
149
|
+
>>> client.addresses.update("addr_abc123", label="hot wallet 1")
|
|
150
|
+
"""
|
|
151
|
+
body: Dict[str, Any] = {}
|
|
152
|
+
if label is not _UNSET:
|
|
153
|
+
body["label"] = label
|
|
154
|
+
if mode is not None:
|
|
155
|
+
body["mode"] = mode
|
|
156
|
+
if status is not None:
|
|
157
|
+
body["status"] = status
|
|
158
|
+
|
|
159
|
+
return cast(Dict[str, Any], self.http.request(
|
|
160
|
+
method="PATCH",
|
|
161
|
+
path=f"/v1/addresses/{address_id}",
|
|
162
|
+
body=body,
|
|
163
|
+
))
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class AsyncAddressesApi:
|
|
167
|
+
"""Addresses API (asynchronous)."""
|
|
168
|
+
|
|
169
|
+
def __init__(self, http: AsyncHttpClient) -> None:
|
|
170
|
+
self.http = http
|
|
171
|
+
|
|
172
|
+
async def get_pools(self) -> List[Dict[str, Any]]:
|
|
173
|
+
"""Get address pool overview (async)."""
|
|
174
|
+
response = await self.http.request(
|
|
175
|
+
method="GET",
|
|
176
|
+
path="/v1/addresses/pools",
|
|
177
|
+
)
|
|
178
|
+
return cast(List[Dict[str, Any]], response["pools"])
|
|
179
|
+
|
|
180
|
+
async def import_addresses(
|
|
181
|
+
self,
|
|
182
|
+
chain: str,
|
|
183
|
+
addresses: List[str],
|
|
184
|
+
mode: Optional[str] = None,
|
|
185
|
+
label: Optional[str] = None,
|
|
186
|
+
) -> Dict[str, Any]:
|
|
187
|
+
"""Import receiving addresses (async)."""
|
|
188
|
+
body: Dict[str, Any] = {
|
|
189
|
+
"addresses": [
|
|
190
|
+
{"chain": chain, "address": addr, **({"label": label} if label else {})}
|
|
191
|
+
for addr in addresses
|
|
192
|
+
],
|
|
193
|
+
"mode": mode or "single",
|
|
194
|
+
}
|
|
195
|
+
response = await self.http.request(
|
|
196
|
+
method="POST",
|
|
197
|
+
path="/v1/addresses/import",
|
|
198
|
+
body=body,
|
|
199
|
+
)
|
|
200
|
+
return cast(Dict[str, Any], response)
|
|
201
|
+
|
|
202
|
+
async def list(
|
|
203
|
+
self,
|
|
204
|
+
chain: Optional[str] = None,
|
|
205
|
+
status: Optional[str] = None,
|
|
206
|
+
limit: Optional[int] = None,
|
|
207
|
+
offset: Optional[int] = None,
|
|
208
|
+
) -> Dict[str, Any]:
|
|
209
|
+
"""List receiving addresses (async)."""
|
|
210
|
+
query: Dict[str, Any] = {}
|
|
211
|
+
if chain:
|
|
212
|
+
query["chain"] = chain
|
|
213
|
+
if status:
|
|
214
|
+
query["status"] = status
|
|
215
|
+
if limit is not None:
|
|
216
|
+
query["limit"] = limit
|
|
217
|
+
if offset is not None:
|
|
218
|
+
query["offset"] = offset
|
|
219
|
+
|
|
220
|
+
response = await self.http.request(
|
|
221
|
+
method="GET",
|
|
222
|
+
path="/v1/addresses",
|
|
223
|
+
query=query,
|
|
224
|
+
)
|
|
225
|
+
return cast(Dict[str, Any], response)
|
|
226
|
+
|
|
227
|
+
async def remove(self, address_id: str) -> Dict[str, Any]:
|
|
228
|
+
"""Delete an address (async)."""
|
|
229
|
+
response = await self.http.request(
|
|
230
|
+
method="DELETE",
|
|
231
|
+
path=f"/v1/addresses/{address_id}",
|
|
232
|
+
)
|
|
233
|
+
return cast(Dict[str, Any], response)
|
|
234
|
+
|
|
235
|
+
async def update(
|
|
236
|
+
self,
|
|
237
|
+
address_id: str,
|
|
238
|
+
label: Union[str, None, _Unset] = _UNSET,
|
|
239
|
+
mode: Optional[str] = None,
|
|
240
|
+
status: Optional[str] = None,
|
|
241
|
+
) -> Dict[str, Any]:
|
|
242
|
+
"""Update an address (async)."""
|
|
243
|
+
body: Dict[str, Any] = {}
|
|
244
|
+
if label is not _UNSET:
|
|
245
|
+
body["label"] = label
|
|
246
|
+
if mode is not None:
|
|
247
|
+
body["mode"] = mode
|
|
248
|
+
if status is not None:
|
|
249
|
+
body["status"] = status
|
|
250
|
+
|
|
251
|
+
response = await self.http.request(
|
|
252
|
+
method="PATCH",
|
|
253
|
+
path=f"/v1/addresses/{address_id}",
|
|
254
|
+
body=body,
|
|
255
|
+
)
|
|
256
|
+
return cast(Dict[str, Any], response)
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""Checkout Sessions API (hosted WalletConnect checkout)."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
|
|
6
|
+
from stableops.http import AsyncHttpClient, HttpClient
|
|
7
|
+
from stableops.payment_orders import _from_wire as _order_from_wire
|
|
8
|
+
from stableops.types import AmountMode, CheckoutSession
|
|
9
|
+
|
|
10
|
+
DEFAULT_CHECKOUT_BASE_URL = "https://pay.stableops.dev"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _to_create_body(
|
|
14
|
+
merchant_order_id: str,
|
|
15
|
+
amount: str,
|
|
16
|
+
accepted_assets: List[Dict[str, Any]],
|
|
17
|
+
expires_at: str,
|
|
18
|
+
amount_mode: Optional[AmountMode],
|
|
19
|
+
metadata: Optional[Dict[str, Any]],
|
|
20
|
+
title: Optional[str],
|
|
21
|
+
description: Optional[str],
|
|
22
|
+
success_url: Optional[str],
|
|
23
|
+
cancel_url: Optional[str],
|
|
24
|
+
walletconnect_project_id: Optional[str],
|
|
25
|
+
) -> Dict[str, Any]:
|
|
26
|
+
"""组装 checkout session 创建请求体。"""
|
|
27
|
+
body: Dict[str, Any] = {
|
|
28
|
+
"merchant_order_id": merchant_order_id,
|
|
29
|
+
"amount": amount,
|
|
30
|
+
"accepted_assets": accepted_assets,
|
|
31
|
+
"expires_at": expires_at,
|
|
32
|
+
}
|
|
33
|
+
if amount_mode is not None:
|
|
34
|
+
body["amount_mode"] = amount_mode
|
|
35
|
+
if metadata:
|
|
36
|
+
body["metadata"] = metadata
|
|
37
|
+
if title is not None:
|
|
38
|
+
body["title"] = title
|
|
39
|
+
if description is not None:
|
|
40
|
+
body["description"] = description
|
|
41
|
+
if success_url is not None:
|
|
42
|
+
body["success_url"] = success_url
|
|
43
|
+
if cancel_url is not None:
|
|
44
|
+
body["cancel_url"] = cancel_url
|
|
45
|
+
if walletconnect_project_id is not None:
|
|
46
|
+
body["walletconnect_project_id"] = walletconnect_project_id
|
|
47
|
+
return body
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _session_from_wire(wire: Dict[str, Any], checkout_base_url: str) -> Dict[str, Any]:
|
|
51
|
+
"""Convert checkout session wire format to SDK model kwargs."""
|
|
52
|
+
client_secret = wire.get("client_secret")
|
|
53
|
+
url: Optional[str] = None
|
|
54
|
+
if client_secret:
|
|
55
|
+
url = (
|
|
56
|
+
f"{checkout_base_url}/c/{quote(wire['id'], safe='')}"
|
|
57
|
+
f"?client_secret={quote(client_secret, safe='')}"
|
|
58
|
+
)
|
|
59
|
+
return {
|
|
60
|
+
"id": wire["id"],
|
|
61
|
+
"client_secret": client_secret,
|
|
62
|
+
"url": url,
|
|
63
|
+
"status": wire["status"],
|
|
64
|
+
"title": wire.get("title"),
|
|
65
|
+
"description": wire.get("description"),
|
|
66
|
+
"success_url": wire.get("success_url"),
|
|
67
|
+
"cancel_url": wire.get("cancel_url"),
|
|
68
|
+
"walletconnect_project_id": wire.get("walletconnect_project_id"),
|
|
69
|
+
"expires_at": wire.get("expires_at"),
|
|
70
|
+
"created_at": wire["created_at"],
|
|
71
|
+
"payment_order": _order_from_wire(wire["payment_order"]),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class CheckoutSessionsApi:
|
|
76
|
+
"""Checkout Sessions API (synchronous)."""
|
|
77
|
+
|
|
78
|
+
def __init__(self, http: HttpClient, checkout_base_url: Optional[str] = None) -> None:
|
|
79
|
+
"""Initialize Checkout Sessions API."""
|
|
80
|
+
self.http = http
|
|
81
|
+
self.checkout_base_url = (checkout_base_url or DEFAULT_CHECKOUT_BASE_URL).rstrip("/")
|
|
82
|
+
|
|
83
|
+
def create(
|
|
84
|
+
self,
|
|
85
|
+
merchant_order_id: str,
|
|
86
|
+
amount: str,
|
|
87
|
+
accepted_assets: List[Dict[str, Any]],
|
|
88
|
+
expires_at: str,
|
|
89
|
+
amount_mode: Optional[AmountMode] = None,
|
|
90
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
91
|
+
title: Optional[str] = None,
|
|
92
|
+
description: Optional[str] = None,
|
|
93
|
+
success_url: Optional[str] = None,
|
|
94
|
+
cancel_url: Optional[str] = None,
|
|
95
|
+
walletconnect_project_id: Optional[str] = None,
|
|
96
|
+
) -> CheckoutSession:
|
|
97
|
+
"""Create a hosted checkout session.
|
|
98
|
+
|
|
99
|
+
Returns a CheckoutSession whose ``url`` points at the hosted checkout page.
|
|
100
|
+
|
|
101
|
+
Example:
|
|
102
|
+
>>> session = client.checkout_sessions.create(
|
|
103
|
+
... merchant_order_id="order_123",
|
|
104
|
+
... amount="10.00",
|
|
105
|
+
... accepted_assets=[{"chain": "base", "asset": "USDC"}],
|
|
106
|
+
... expires_at="2026-06-20T12:30:00Z",
|
|
107
|
+
... title="Pro plan",
|
|
108
|
+
... success_url="https://shop.example/success",
|
|
109
|
+
... )
|
|
110
|
+
>>> print(session.url)
|
|
111
|
+
"""
|
|
112
|
+
body = _to_create_body(
|
|
113
|
+
merchant_order_id, amount, accepted_assets, expires_at, amount_mode, metadata,
|
|
114
|
+
title, description, success_url, cancel_url, walletconnect_project_id,
|
|
115
|
+
)
|
|
116
|
+
response = self.http.request(
|
|
117
|
+
method="POST",
|
|
118
|
+
path="/v1/checkout-sessions",
|
|
119
|
+
body=body,
|
|
120
|
+
idempotency_key=merchant_order_id,
|
|
121
|
+
)
|
|
122
|
+
return CheckoutSession(**_session_from_wire(response, self.checkout_base_url))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class AsyncCheckoutSessionsApi:
|
|
126
|
+
"""Checkout Sessions API (asynchronous)."""
|
|
127
|
+
|
|
128
|
+
def __init__(self, http: AsyncHttpClient, checkout_base_url: Optional[str] = None) -> None:
|
|
129
|
+
"""Initialize async Checkout Sessions API."""
|
|
130
|
+
self.http = http
|
|
131
|
+
self.checkout_base_url = (checkout_base_url or DEFAULT_CHECKOUT_BASE_URL).rstrip("/")
|
|
132
|
+
|
|
133
|
+
async def create(
|
|
134
|
+
self,
|
|
135
|
+
merchant_order_id: str,
|
|
136
|
+
amount: str,
|
|
137
|
+
accepted_assets: List[Dict[str, Any]],
|
|
138
|
+
expires_at: str,
|
|
139
|
+
amount_mode: Optional[AmountMode] = None,
|
|
140
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
141
|
+
title: Optional[str] = None,
|
|
142
|
+
description: Optional[str] = None,
|
|
143
|
+
success_url: Optional[str] = None,
|
|
144
|
+
cancel_url: Optional[str] = None,
|
|
145
|
+
walletconnect_project_id: Optional[str] = None,
|
|
146
|
+
) -> CheckoutSession:
|
|
147
|
+
"""Create a hosted checkout session (async)."""
|
|
148
|
+
body = _to_create_body(
|
|
149
|
+
merchant_order_id, amount, accepted_assets, expires_at, amount_mode, metadata,
|
|
150
|
+
title, description, success_url, cancel_url, walletconnect_project_id,
|
|
151
|
+
)
|
|
152
|
+
response = await self.http.request(
|
|
153
|
+
method="POST",
|
|
154
|
+
path="/v1/checkout-sessions",
|
|
155
|
+
body=body,
|
|
156
|
+
idempotency_key=merchant_order_id,
|
|
157
|
+
)
|
|
158
|
+
return CheckoutSession(**_session_from_wire(response, self.checkout_base_url))
|
stableops/client.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""StableOps client."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
|
|
5
|
+
from stableops.addresses import AddressesApi, AsyncAddressesApi
|
|
6
|
+
from stableops.checkout_sessions import AsyncCheckoutSessionsApi, CheckoutSessionsApi
|
|
7
|
+
from stableops.http import AsyncHttpClient, DebugOption, HttpClient
|
|
8
|
+
from stableops.payment_orders import AsyncPaymentOrdersApi, PaymentOrdersApi
|
|
9
|
+
from stableops.webhooks import AsyncWebhooksApi, WebhooksApi
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class StableOps:
|
|
13
|
+
"""StableOps synchronous client.
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
>>> from stableops import StableOps
|
|
17
|
+
>>> client = StableOps(
|
|
18
|
+
... api_key="your_api_key",
|
|
19
|
+
... )
|
|
20
|
+
>>> order = client.payment_orders.create(
|
|
21
|
+
... merchant_order_id="order_123",
|
|
22
|
+
... amount="10.00",
|
|
23
|
+
... settlement_asset="USDC",
|
|
24
|
+
... accepted_assets=[{"chain": "base", "asset": "USDC"}],
|
|
25
|
+
... expires_at="2026-06-20T12:30:00Z",
|
|
26
|
+
... )
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
api_key: Optional[str] = None,
|
|
32
|
+
base_url: str = "https://api.stableops.dev",
|
|
33
|
+
timeout: float = 30.0,
|
|
34
|
+
max_retries: int = 2,
|
|
35
|
+
debug: DebugOption = None,
|
|
36
|
+
checkout_base_url: Optional[str] = None,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""Initialize StableOps client.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
api_key: API key for authentication
|
|
42
|
+
base_url: Base API URL (default: "https://api.stableops.dev")
|
|
43
|
+
timeout: Request timeout in seconds (default: 30.0)
|
|
44
|
+
max_retries: Maximum number of retries (default: 2)
|
|
45
|
+
debug: 打印初始化配置 / 请求 / 响应(敏感字段自动脱敏);
|
|
46
|
+
True 走 logging "stableops" logger,传可调用对象可自定义处理。
|
|
47
|
+
checkout_base_url: 自定义收银台域名(默认 https://pay.stableops.dev),
|
|
48
|
+
仅影响 checkout_sessions.create 返回的 url 拼接。
|
|
49
|
+
"""
|
|
50
|
+
self._http = HttpClient(
|
|
51
|
+
api_key=api_key,
|
|
52
|
+
base_url=base_url,
|
|
53
|
+
timeout=timeout,
|
|
54
|
+
max_retries=max_retries,
|
|
55
|
+
debug=debug,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
self.addresses = AddressesApi(self._http)
|
|
59
|
+
self.payment_orders = PaymentOrdersApi(self._http)
|
|
60
|
+
self.checkout_sessions = CheckoutSessionsApi(self._http, checkout_base_url)
|
|
61
|
+
self.webhooks = WebhooksApi(self._http)
|
|
62
|
+
|
|
63
|
+
def close(self) -> None:
|
|
64
|
+
"""Close HTTP client."""
|
|
65
|
+
self._http.close()
|
|
66
|
+
|
|
67
|
+
def __enter__(self) -> "StableOps":
|
|
68
|
+
"""Context manager entry."""
|
|
69
|
+
return self
|
|
70
|
+
|
|
71
|
+
def __exit__(self, *args: Any) -> None:
|
|
72
|
+
"""Context manager exit."""
|
|
73
|
+
self.close()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class AsyncStableOps:
|
|
77
|
+
"""StableOps asynchronous client.
|
|
78
|
+
|
|
79
|
+
Example:
|
|
80
|
+
>>> import asyncio
|
|
81
|
+
>>> from stableops import AsyncStableOps
|
|
82
|
+
>>> async def main():
|
|
83
|
+
... client = AsyncStableOps(api_key="your_api_key")
|
|
84
|
+
... order = await client.payment_orders.create(
|
|
85
|
+
... merchant_order_id="order_123",
|
|
86
|
+
... amount="10.00",
|
|
87
|
+
... settlement_asset="USDC",
|
|
88
|
+
... accepted_assets=[{"chain": "base", "asset": "USDC"}],
|
|
89
|
+
... expires_at="2026-06-20T12:30:00Z",
|
|
90
|
+
... )
|
|
91
|
+
... print(f"Order created: {order.id}")
|
|
92
|
+
>>> asyncio.run(main())
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
api_key: Optional[str] = None,
|
|
98
|
+
base_url: str = "https://api.stableops.dev",
|
|
99
|
+
timeout: float = 30.0,
|
|
100
|
+
max_retries: int = 2,
|
|
101
|
+
debug: DebugOption = None,
|
|
102
|
+
checkout_base_url: Optional[str] = None,
|
|
103
|
+
) -> None:
|
|
104
|
+
"""Initialize async StableOps client.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
api_key: API key for authentication
|
|
108
|
+
base_url: Base API URL (default: "https://api.stableops.dev")
|
|
109
|
+
timeout: Request timeout in seconds (default: 30.0)
|
|
110
|
+
max_retries: Maximum number of retries (default: 2)
|
|
111
|
+
debug: 见 StableOps 同名参数。
|
|
112
|
+
checkout_base_url: 见 StableOps 同名参数。
|
|
113
|
+
"""
|
|
114
|
+
self._http = AsyncHttpClient(
|
|
115
|
+
api_key=api_key,
|
|
116
|
+
base_url=base_url,
|
|
117
|
+
timeout=timeout,
|
|
118
|
+
max_retries=max_retries,
|
|
119
|
+
debug=debug,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
self.addresses = AsyncAddressesApi(self._http)
|
|
123
|
+
self.payment_orders = AsyncPaymentOrdersApi(self._http)
|
|
124
|
+
self.checkout_sessions = AsyncCheckoutSessionsApi(self._http, checkout_base_url)
|
|
125
|
+
self.webhooks = AsyncWebhooksApi(self._http)
|
|
126
|
+
|
|
127
|
+
async def close(self) -> None:
|
|
128
|
+
"""Close HTTP client."""
|
|
129
|
+
await self._http.close()
|
|
130
|
+
|
|
131
|
+
async def __aenter__(self) -> "AsyncStableOps":
|
|
132
|
+
"""Async context manager entry."""
|
|
133
|
+
return self
|
|
134
|
+
|
|
135
|
+
async def __aexit__(self, *args: Any) -> None:
|
|
136
|
+
"""Async context manager exit."""
|
|
137
|
+
await self.close()
|
stableops/errors.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Error types for StableOps SDK."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class StableOpsError(Exception):
|
|
7
|
+
"""Base exception for StableOps SDK errors."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
message: str,
|
|
12
|
+
status: int = 0,
|
|
13
|
+
code: str = "unknown_error",
|
|
14
|
+
details: Optional[Any] = None,
|
|
15
|
+
) -> None:
|
|
16
|
+
"""Initialize StableOpsError.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
message: Error message
|
|
20
|
+
status: HTTP status code (0 for network errors)
|
|
21
|
+
code: Error code
|
|
22
|
+
details: Additional error details
|
|
23
|
+
"""
|
|
24
|
+
super().__init__(message)
|
|
25
|
+
self.message = message
|
|
26
|
+
self.status = status
|
|
27
|
+
self.code = code
|
|
28
|
+
self.details = details
|
|
29
|
+
|
|
30
|
+
def __str__(self) -> str:
|
|
31
|
+
"""String representation."""
|
|
32
|
+
if self.status > 0:
|
|
33
|
+
return f"[{self.status}] {self.code}: {self.message}"
|
|
34
|
+
return f"{self.code}: {self.message}"
|
|
35
|
+
|
|
36
|
+
def __repr__(self) -> str:
|
|
37
|
+
"""Repr representation."""
|
|
38
|
+
return (
|
|
39
|
+
f"StableOpsError(message={self.message!r}, status={self.status}, "
|
|
40
|
+
f"code={self.code!r}, details={self.details!r})"
|
|
41
|
+
)
|