@siglume/direct-request-payment 0.4.19 → 0.4.22
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.
- package/CHANGELOG.md +50 -0
- package/README.md +18 -10
- package/bin/siglume-sdrp.mjs +550 -8
- package/dist/index.cjs +37 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +37 -3
- package/dist/index.js.map +1 -1
- package/docs/announcement-ja.md +17 -3
- package/docs/api-reference.md +60 -13
- package/docs/merchant-quickstart.md +6 -20
- package/docs/metered-statements.md +15 -13
- package/docs/payment-lifecycle.md +12 -9
- package/docs/pricing.md +7 -4
- package/docs/quickstart-10-minutes.md +134 -24
- package/docs/sandbox.md +60 -0
- package/docs/troubleshooting.md +23 -8
- package/examples/express-checkout.ts +37 -13
- package/examples/hosted-checkout-python/app.py +46 -31
- package/examples/hosted-checkout-python/order_store.py +13 -3
- package/examples/hosted-checkout-python/pyproject.toml +1 -1
- package/examples/hosted-checkout-typescript/src/order-store.ts +14 -3
- package/examples/hosted-checkout-typescript/src/server.ts +49 -37
- package/package.json +10 -2
- package/templates/express/README.md +40 -6
- package/templates/express/siglume-order-store.example.ts +22 -6
- package/templates/express/siglume-order-store.sql.ts +585 -0
- package/templates/express/siglume-sdrp-routes.ts +138 -64
- package/templates/fastapi/README.md +22 -3
- package/templates/fastapi/siglume_order_store_example.py +29 -6
- package/templates/fastapi/siglume_order_store_sqlalchemy.py +313 -0
- package/templates/fastapi/siglume_sdrp_routes.py +112 -49
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
-
import
|
|
5
|
-
from typing import Any, Protocol
|
|
4
|
+
from typing import Any, Awaitable, Callable, Literal, Protocol
|
|
6
5
|
|
|
7
6
|
from fastapi import APIRouter, Request
|
|
8
7
|
from fastapi.responses import JSONResponse, Response
|
|
8
|
+
from starlette.concurrency import run_in_threadpool
|
|
9
9
|
from siglume_direct_request_payment import (
|
|
10
10
|
DirectRequestPaymentMerchantClient,
|
|
11
11
|
HostedCheckoutNotAvailableError,
|
|
@@ -15,16 +15,33 @@ from siglume_direct_request_payment import (
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class SiglumeSdrpOrderStore(Protocol):
|
|
18
|
-
async def
|
|
19
|
-
async def mark_checkout_pending(
|
|
20
|
-
|
|
18
|
+
async def begin_checkout_attempt(self, order_id: str, request: Request) -> dict[str, Any] | None: ...
|
|
19
|
+
async def mark_checkout_pending(
|
|
20
|
+
self,
|
|
21
|
+
*,
|
|
22
|
+
order_id: str,
|
|
23
|
+
attempt_id: str,
|
|
24
|
+
stable_nonce: str,
|
|
25
|
+
challenge_hash: str,
|
|
26
|
+
checkout_session_id: str,
|
|
27
|
+
checkout_url: str,
|
|
28
|
+
) -> None: ...
|
|
29
|
+
async def process_webhook_event_once(
|
|
30
|
+
self,
|
|
31
|
+
event_id: str,
|
|
32
|
+
handler: Callable[[], Awaitable[None]],
|
|
33
|
+
) -> Literal["processed", "duplicate"]: ...
|
|
21
34
|
async def find_order_by_challenge_hash(self, challenge_hash: str) -> dict[str, Any] | None: ...
|
|
22
35
|
async def mark_order_paid_once(self, *, order_id: str, requirement_id: str, chain_receipt_id: str) -> None: ...
|
|
23
36
|
async def mark_order_fulfilled_unsettled_once(self, *, order_id: str, requirement_id: str, pricing_band: str) -> None: ...
|
|
24
37
|
async def flag_payment_review(self, data: dict[str, Any]) -> None: ...
|
|
25
38
|
|
|
26
39
|
|
|
27
|
-
def create_siglume_sdrp_router(
|
|
40
|
+
def create_siglume_sdrp_router(
|
|
41
|
+
order_store: SiglumeSdrpOrderStore,
|
|
42
|
+
*,
|
|
43
|
+
allow_metered_payments: bool = False,
|
|
44
|
+
) -> APIRouter:
|
|
28
45
|
router = APIRouter()
|
|
29
46
|
merchant_key = os.environ["SIGLUME_DIRECT_PAYMENT_MERCHANT"]
|
|
30
47
|
shop_origin = os.environ["SHOP_PUBLIC_ORIGIN"]
|
|
@@ -36,27 +53,41 @@ def create_siglume_sdrp_router(order_store: SiglumeSdrpOrderStore) -> APIRouter:
|
|
|
36
53
|
async def start_checkout(request: Request) -> JSONResponse:
|
|
37
54
|
body = await request.json()
|
|
38
55
|
order_id = str(body.get("order_id") or "")
|
|
39
|
-
|
|
40
|
-
if not
|
|
56
|
+
attempt = await order_store.begin_checkout_attempt(order_id, request)
|
|
57
|
+
if not attempt:
|
|
41
58
|
return JSONResponse({"error": "order_not_found"}, status_code=404)
|
|
42
59
|
|
|
60
|
+
if not allow_metered_payments and not _is_standard_checkout_amount(str(attempt["currency"]), int(attempt["amount_minor"])):
|
|
61
|
+
return JSONResponse({"error": "METERED_INTEGRATION_REQUIRED"}, status_code=409)
|
|
62
|
+
|
|
63
|
+
if attempt.get("checkout_url") and attempt.get("checkout_session_id"):
|
|
64
|
+
return JSONResponse({
|
|
65
|
+
"checkout_url": attempt["checkout_url"],
|
|
66
|
+
"session_id": attempt["checkout_session_id"],
|
|
67
|
+
})
|
|
68
|
+
|
|
43
69
|
try:
|
|
44
|
-
session =
|
|
45
|
-
merchant
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
70
|
+
session = await run_in_threadpool(
|
|
71
|
+
lambda: merchant.create_checkout_session(
|
|
72
|
+
merchant=merchant_key,
|
|
73
|
+
amount_minor=int(attempt["amount_minor"]),
|
|
74
|
+
currency=str(attempt["currency"]),
|
|
75
|
+
nonce=str(attempt["stable_nonce"]),
|
|
76
|
+
success_url=f"{shop_origin}/checkout/siglume/success",
|
|
77
|
+
cancel_url=f"{shop_origin}/checkout/siglume/cancel",
|
|
78
|
+
metadata={"order_id": attempt["order_id"], "attempt_id": attempt["attempt_id"]},
|
|
79
|
+
)
|
|
52
80
|
)
|
|
53
81
|
except HostedCheckoutNotAvailableError:
|
|
54
82
|
return JSONResponse({"error": "hosted_checkout_not_enabled"}, status_code=409)
|
|
55
83
|
|
|
56
84
|
await order_store.mark_checkout_pending(
|
|
57
|
-
order_id=str(
|
|
85
|
+
order_id=str(attempt["order_id"]),
|
|
86
|
+
attempt_id=str(attempt["attempt_id"]),
|
|
87
|
+
stable_nonce=str(attempt["stable_nonce"]),
|
|
58
88
|
challenge_hash=session["challenge_hash"],
|
|
59
89
|
checkout_session_id=session["session_id"],
|
|
90
|
+
checkout_url=session["checkout_url"],
|
|
60
91
|
)
|
|
61
92
|
return JSONResponse({"checkout_url": session["checkout_url"], "session_id": session["session_id"]})
|
|
62
93
|
|
|
@@ -68,40 +99,72 @@ def create_siglume_sdrp_router(order_store: SiglumeSdrpOrderStore) -> APIRouter:
|
|
|
68
99
|
request.headers.get("Siglume-Signature", ""),
|
|
69
100
|
)["event"]
|
|
70
101
|
|
|
71
|
-
|
|
72
|
-
|
|
102
|
+
async def handler() -> None:
|
|
103
|
+
await _process_siglume_webhook_event(
|
|
104
|
+
order_store,
|
|
105
|
+
event,
|
|
106
|
+
allow_metered_payments=allow_metered_payments,
|
|
107
|
+
)
|
|
73
108
|
|
|
74
|
-
if event["
|
|
75
|
-
|
|
76
|
-
if confirmation["kind"] == "standard_settled":
|
|
77
|
-
order = await order_store.find_order_by_challenge_hash(confirmation["challenge_hash"])
|
|
78
|
-
if order:
|
|
79
|
-
await order_store.mark_order_paid_once(
|
|
80
|
-
order_id=str(order["id"]),
|
|
81
|
-
requirement_id=confirmation["requirement_id"],
|
|
82
|
-
chain_receipt_id=confirmation["chain_receipt_id"],
|
|
83
|
-
)
|
|
84
|
-
else:
|
|
85
|
-
await order_store.flag_payment_review({
|
|
86
|
-
"reason": "unknown_challenge_hash",
|
|
87
|
-
"requirement_id": confirmation["requirement_id"],
|
|
88
|
-
})
|
|
89
|
-
elif confirmation["kind"] == "metered_usage_accepted":
|
|
90
|
-
order = await order_store.find_order_by_challenge_hash(confirmation["challenge_hash"])
|
|
91
|
-
if order:
|
|
92
|
-
await order_store.mark_order_fulfilled_unsettled_once(
|
|
93
|
-
order_id=str(order["id"]),
|
|
94
|
-
requirement_id=confirmation["requirement_id"],
|
|
95
|
-
pricing_band=confirmation["pricing_band"],
|
|
96
|
-
)
|
|
97
|
-
else:
|
|
98
|
-
await order_store.flag_payment_review({
|
|
99
|
-
"reason": "unknown_metered_challenge_hash",
|
|
100
|
-
"requirement_id": confirmation["requirement_id"],
|
|
101
|
-
})
|
|
102
|
-
else:
|
|
103
|
-
await order_store.flag_payment_review(dict(confirmation))
|
|
109
|
+
if await order_store.process_webhook_event_once(str(event["id"]), handler) == "duplicate":
|
|
110
|
+
return Response(status_code=204)
|
|
104
111
|
|
|
105
112
|
return Response(status_code=204)
|
|
106
113
|
|
|
107
114
|
return router
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async def _process_siglume_webhook_event(
|
|
118
|
+
order_store: SiglumeSdrpOrderStore,
|
|
119
|
+
event: dict[str, Any],
|
|
120
|
+
*,
|
|
121
|
+
allow_metered_payments: bool,
|
|
122
|
+
) -> None:
|
|
123
|
+
if event["type"] != "direct_payment.confirmed":
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
confirmation = classify_direct_payment_confirmation(event)
|
|
127
|
+
if confirmation["kind"] == "standard_settled":
|
|
128
|
+
order = await order_store.find_order_by_challenge_hash(confirmation["challenge_hash"])
|
|
129
|
+
if order:
|
|
130
|
+
await order_store.mark_order_paid_once(
|
|
131
|
+
order_id=str(order["id"]),
|
|
132
|
+
requirement_id=confirmation["requirement_id"],
|
|
133
|
+
chain_receipt_id=confirmation["chain_receipt_id"],
|
|
134
|
+
)
|
|
135
|
+
else:
|
|
136
|
+
await order_store.flag_payment_review({
|
|
137
|
+
"reason": "unknown_challenge_hash",
|
|
138
|
+
"requirement_id": confirmation["requirement_id"],
|
|
139
|
+
})
|
|
140
|
+
elif confirmation["kind"] == "metered_usage_accepted":
|
|
141
|
+
if not allow_metered_payments:
|
|
142
|
+
await order_store.flag_payment_review({
|
|
143
|
+
"reason": "metered_integration_required",
|
|
144
|
+
"requirement_id": confirmation["requirement_id"],
|
|
145
|
+
"pricing_band": confirmation["pricing_band"],
|
|
146
|
+
})
|
|
147
|
+
return
|
|
148
|
+
order = await order_store.find_order_by_challenge_hash(confirmation["challenge_hash"])
|
|
149
|
+
if order:
|
|
150
|
+
await order_store.mark_order_fulfilled_unsettled_once(
|
|
151
|
+
order_id=str(order["id"]),
|
|
152
|
+
requirement_id=confirmation["requirement_id"],
|
|
153
|
+
pricing_band=confirmation["pricing_band"],
|
|
154
|
+
)
|
|
155
|
+
else:
|
|
156
|
+
await order_store.flag_payment_review({
|
|
157
|
+
"reason": "unknown_metered_challenge_hash",
|
|
158
|
+
"requirement_id": confirmation["requirement_id"],
|
|
159
|
+
})
|
|
160
|
+
else:
|
|
161
|
+
await order_store.flag_payment_review(dict(confirmation))
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _is_standard_checkout_amount(currency: str, amount_minor: int) -> bool:
|
|
165
|
+
normalized_currency = currency.upper()
|
|
166
|
+
if normalized_currency == "JPY":
|
|
167
|
+
return amount_minor >= 501
|
|
168
|
+
if normalized_currency == "USD":
|
|
169
|
+
return amount_minor >= 301
|
|
170
|
+
return False
|