-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmerchant_agent.py
More file actions
349 lines (292 loc) · 11.9 KB
/
Copy pathmerchant_agent.py
File metadata and controls
349 lines (292 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""AP2 Merchant Agent - Stateless Protocol Implementation.
This module provides a stateless Merchant Agent for the AP2 protocol.
The agent DOES NOT manage business state - that's your responsibility.
启动顺序:请将 merchant_agent 作为独立服务先运行,再启动 shopper_agent(参考 ap2_complete_flow.py)。
Design Philosophy:
- Stateless: No session management, no database
- Protocol-focused: Only handles signing and verification
- Pure functions: Predictable, testable, composable
"""
import logging
from typing import Any, Optional, Sequence
from anp.ap2 import (
ANPMessage,
CartContents,
CartMandateRequestData,
DisplayItem,
FulfillmentReceipt,
MoneyAmount,
PaymentDetails,
PaymentDetailsTotal,
PaymentMandate,
PaymentMethodData,
PaymentProvider,
PaymentReceipt,
PaymentRequest,
PaymentRequestOptions,
QRCodePaymentData,
ShippingAddress,
)
from anp.ap2.cart_mandate import build_cart_mandate
from anp.ap2.credential_mandate import (
build_fulfillment_receipt,
build_payment_receipt,
)
from anp.ap2.mandate import compute_hash
from anp.ap2.payment_mandate import validate_payment_mandate
logger = logging.getLogger(__name__)
class MerchantAgent:
"""Stateless AP2 Merchant Agent.
This agent provides protocol-level operations for building and verifying
AP2 mandates. It does NOT manage business state (sessions, hashes, etc.).
Design:
- Stateless: No instance variables for business data
- Pure methods: Same input -> same output
- Your responsibility: State management, database, cache
"""
def __init__(
self,
merchant_private_key: str,
merchant_did: str,
merchant_kid: str,
algorithm: str = "RS256",
):
"""Initialize the Merchant Agent.
Args:
merchant_private_key: Merchant's private key for JWS signing
merchant_did: Merchant's DID
merchant_kid: Merchant's key ID for JWS signing
algorithm: JWT algorithm (RS256 or ES256K)
Note:
This agent is STATELESS. It does not store sessions or business data.
You must manage cart_hash, pmt_hash, and other state yourself.
"""
self.merchant_private_key = merchant_private_key
self.merchant_did = merchant_did
self.merchant_kid = merchant_kid
self.algorithm = algorithm
def verify_cart_mandate_request(
self,
request: ANPMessage,
) -> dict[str, Any]:
"""Verify an incoming cart creation request.
Args:
request: ANPMessage containing CartMandateRequestData
Returns:
Dict containing extracted business data
Raises:
ValueError: If request validation fails
TypeError: If message data payload is not CartMandateRequestData
"""
if request.to != self.merchant_did:
raise ValueError(f"Request not for this merchant: {request.to}")
# Parse data dict to CartMandateRequestData
try:
data = CartMandateRequestData.model_validate(request.data)
except Exception as e:
raise TypeError(
f"Invalid message data type: expected CartMandateRequestData, got {e}"
) from e
return {
"cart_mandate_id": data.cart_mandate_id,
"items": [item.model_dump() for item in data.items],
"shipping_address": data.shipping_address.model_dump()
if data.shipping_address
else None,
"client_did": request.from_,
"webhook_url": request.credential_webhook_url,
}
def build_cart_mandate_response(
self,
order_id: str,
items: Sequence[DisplayItem],
total_amount: MoneyAmount,
shopper_did: Optional[str] = None,
payment_method: str = "QR_CODE",
payment_channel: PaymentProvider | str = PaymentProvider.ALIPAY,
qr_url: str = "",
out_trade_no: str = "",
shipping_address: Optional[ShippingAddress] = None,
ttl_seconds: int = 900,
) -> ANPMessage:
"""Build a CartMandate response (stateless).
This method builds a signed CartMandate and wraps it in an ANPMessage.
You must extract and store cart_hash from the contained CartMandate yourself.
Args:
order_id: Order unique identifier
items: List of items with full details
total_amount: Total amount as MoneyAmount model
shopper_did: Shopper's DID
payment_method: Payment method (default: QR_CODE)
payment_channel: PaymentProvider enum or string (default: ALIPAY)
qr_url: QR code URL for payment
out_trade_no: External trade number
shipping_address: Shipping address (optional)
ttl_seconds: JWT Time to live in seconds (default: 900 = 15 minutes)
Returns:
ANPMessage containing the CartMandate
"""
resolved_shopper_did = shopper_did
if not resolved_shopper_did:
raise ValueError("shopper_did or request_from must be provided")
resolved_message_id = f"cart-response-{order_id}"
if not all(isinstance(item, DisplayItem) for item in items):
raise TypeError("All items must be DisplayItem instances")
if not isinstance(total_amount, MoneyAmount):
raise TypeError("total_amount must be a MoneyAmount instance")
logger.info(f"Building CartMandate for order_id={order_id}")
provider = payment_channel
# Ensure provider is PaymentProvider enum
if isinstance(payment_channel, str):
provider = PaymentProvider(payment_channel)
else:
provider = payment_channel
payment_request = PaymentRequest(
method_data=[
PaymentMethodData(
supported_methods=payment_method,
data=QRCodePaymentData(
channel=provider,
qr_url=qr_url,
out_trade_no=out_trade_no,
),
)
],
details=PaymentDetails(
id=order_id,
displayItems=list(items),
total=PaymentDetailsTotal(label="Total", amount=total_amount),
shipping_address=shipping_address,
),
options=PaymentRequestOptions(requestShipping=shipping_address is not None),
)
contents = CartContents(
id=f"cart_{order_id}",
user_signature_required=False,
payment_request=payment_request,
)
cart_mandate_obj = build_cart_mandate(
contents=contents.model_dump(exclude_none=True),
merchant_private_key=self.merchant_private_key,
merchant_did=self.merchant_did,
merchant_kid=self.merchant_kid,
shopper_did=resolved_shopper_did,
algorithm=self.algorithm,
ttl_seconds=ttl_seconds,
)
response = ANPMessage(
messageId=resolved_message_id,
**{"from": self.merchant_did},
to=resolved_shopper_did,
data=cart_mandate_obj.model_dump(exclude_none=True),
)
logger.debug(f"CartMandate built successfully for order_id={order_id}")
return response
def verify_payment_mandate(
self,
request: ANPMessage,
cart_hash: str,
shopper_public_key: str,
) -> dict[str, Any]:
"""Verify an incoming PaymentMandate message (stateless).
This method verifies the signature and hash chain of a PaymentMandate
contained within an ANPMessage. It does NOT store the resulting state.
You must provide the cart_hash from your own storage.
Args:
request: ANPMessage containing the PaymentMandate to verify
cart_hash: The cart_hash you stored earlier
shopper_public_key: Shopper's public key for JWT verification
Returns:
Dict containing decoded JWT payload with pmt_hash computed.
Raises:
ValueError: If signature or hash chain verification fails
TypeError: If the message data payload is not a PaymentMandate
"""
logger.info(f"Verifying ANPMessage with PaymentMandate from {request.from_}")
# Verify ANP message routing
if request.to != self.merchant_did:
raise ValueError(
f"Payment mandate not for this merchant: "
f"expected {self.merchant_did}, got {request.to}"
)
# Parse data dict to PaymentMandate
try:
payment_mandate = PaymentMandate.model_validate(request.data)
except Exception as e:
raise TypeError(
f"Invalid message data type: expected PaymentMandate, got {e}"
) from e
logger.debug(f"Expected cart_hash: {cart_hash[:16]}...")
# Verify payment mandate signature and hash chain
if not validate_payment_mandate(
payment_mandate=payment_mandate,
shopper_public_key=shopper_public_key,
shopper_algorithm=self.algorithm,
expected_merchant_did=self.merchant_did,
expected_cart_hash=cart_hash,
):
raise ValueError("PaymentMandate validation failed")
# Compute pmt_hash for caller
# payment_mandate_contents is already a dict
pmt_hash = compute_hash(payment_mandate.payment_mandate_contents)
logger.info("PaymentMandate verified: pmt_hash=%s...", pmt_hash[:16])
return {"pmt_hash": pmt_hash}
def build_payment_receipt(
self,
payment_receipt_contents: Any,
pmt_hash: str,
shopper_did: str,
ttl_seconds: int = 15552000,
) -> PaymentReceipt:
"""Build a PaymentReceipt credential (stateless).
This method builds and signs a PaymentReceipt but does NOT retrieve stored state.
You must provide the pmt_hash from your own storage.
Args:
payment_receipt_contents: Payment receipt contents
pmt_hash: The pmt_hash you stored earlier
shopper_did: Shopper's DID
ttl_seconds: JWT Time to live in seconds (default: 180 days)
Returns:
PaymentReceipt with merchant authorization
"""
logger.info("Building PaymentReceipt")
return build_payment_receipt(
contents=payment_receipt_contents,
pmt_hash=pmt_hash,
merchant_private_key=self.merchant_private_key,
merchant_did=self.merchant_did,
merchant_kid=self.merchant_kid,
algorithm=self.algorithm,
shopper_did=shopper_did,
ttl_seconds=ttl_seconds,
)
def build_fulfillment_receipt(
self,
fulfillment_receipt_contents: Any,
pmt_hash: str,
shopper_did: str,
ttl_seconds: int = 15552000,
) -> FulfillmentReceipt:
"""Build a FulfillmentReceipt credential (stateless).
This method builds and signs a FulfillmentReceipt but does NOT retrieve stored state.
You must provide the pmt_hash from your own storage.
Args:
fulfillment_receipt_contents: Fulfillment receipt contents
pmt_hash: The pmt_hash you stored earlier
shopper_did: Shopper's DID
ttl_seconds: JWT Time to live in seconds (default: 180 days)
Returns:
FulfillmentReceipt with merchant authorization
"""
logger.info("Building FulfillmentReceipt")
return build_fulfillment_receipt(
contents=fulfillment_receipt_contents,
pmt_hash=pmt_hash,
merchant_private_key=self.merchant_private_key,
merchant_did=self.merchant_did,
merchant_kid=self.merchant_kid,
algorithm=self.algorithm,
shopper_did=shopper_did,
ttl_seconds=ttl_seconds,
)
__all__ = ["MerchantAgent"]