Commit 69eaddb
fix(x402): coerce dict payload + serialize Pydantic SettleResponse (1.3.3) (#10)
## Summary
Two sibling bugs in `process_x402_settle`'s verify → settle path, both
surfaced once #9 (1.3.2) cleared the facilitator-wiring layer.
### 1) Dict `payload` not coerced to typed `PaymentPayload`
`verify_x402_request` returns `payload` as a plain dict (the result of
`json.loads(base64.b64decode(X-Payment))`), but x402 2.9's
`verify_payment` / `settle_payment` call `payload.get_scheme()` and
other Pydantic-model methods on it. Without coercion, prod store crashed
at the verify leg with:
```
AttributeError("'dict' object has no attribute 'get_scheme'")
```
New `_coerce_payment_payload()` routes by `x402Version` field — `1` →
`PaymentPayloadV1` (flat top-level shape); anything else →
`PaymentPayload` (v2 nested under `accepted`). Falls back to the
original input on any failure so caller-typed instances pass through
unchanged. Both verify **and** settle legs now receive the coerced
payload (so the post-verify settle leg also sees a typed model).
### 2) Pydantic `SettleResponse` not JSON-serializable for the response
header
x402 2.9's `settle_payment` returns a `SettleResponse` Pydantic model.
The helper was doing plain `json.dumps(settle_result)` to base64-encode
the X-Payment-Response header — that raises `TypeError("Object of type
SettleResponse is not JSON serializable")`. Caught by the surrounding
`except Exception`, the helper would mark the settle as `settle_failed`
**after the on-chain settle had already succeeded** — payment taken,
order shows as failed.
New `_settle_result_to_json_bytes()` uses
`model_dump_json(by_alias=True)` for Pydantic models (so emitted keys
match the wire shape — `errorReason` / `errorMessage` camelCase) and
falls through to `json.dumps` for plain dicts (older x402 / test stubs).
## Verification
Live integration test against the real Coinbase CDP facilitator (with
prod CDP creds, mainnet) confirmed:
1. Dict → typed `PaymentPayload` happens (`get_scheme()` resolves on the
model).
2. `verify_payment` is reached with the typed model.
3. CDP receives the request, validates through schema, returns a
structured 400 (rejected the synth signature) — **no `AttributeError`
anywhere**.
The CDP rejection at the schema layer (not the type layer) is the
strongest signal short of an actual settle. Real `agentscore-pay`-signed
payloads conform to the x402V2 schema and will pass.
## Tests
- New: `coerces_dict_payload_v2_to_typed_payment_payload` — asserts both
verify + settle legs see the typed `PaymentPayload` and `get_scheme()`
resolves.
- New: `passes_typed_payment_payload_unchanged` — caller-typed instance
flows through.
- New: `serializes_pydantic_settle_result_to_payment_response_header` —
Pydantic `SettleResponse` round-trips to base64'd JSON with
`errorReason` (camelCase wire key).
- New: `serializes_dict_settle_result_for_legacy_stubs` — plain-dict
settle results still serialize.
- Full suite: **723 passed, 3 skipped, 95.30% coverage**.
## Test plan
- [ ] CI green.
- [ ] Tag `v1.3.3`, push tag → trigger PyPI publish.
- [ ] Bump `core/store/uv.lock` to 1.3.3 + fix store's `tx_hash`
extraction (companion bug — store reads `settle_obj.get("transaction")`
only when isinstance dict; with 1.3.3 the inner `settle_result` will be
a Pydantic model, so the dict branch is skipped and tx_hash silently
stays `None`).
- [ ] Deploy store prod.
- [ ] T4 base mainnet smoke against agents.agentscore.sh — expect `200 +
order` with non-null `tx_hash`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent dcb8041 commit 69eaddb
4 files changed
Lines changed: 265 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
220 | 220 | | |
221 | 221 | | |
222 | 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 | + | |
223 | 252 | | |
224 | 253 | | |
225 | 254 | | |
226 | 255 | | |
| 256 | + | |
227 | 257 | | |
228 | 258 | | |
229 | 259 | | |
| |||
267 | 297 | | |
268 | 298 | | |
269 | 299 | | |
270 | | - | |
| 300 | + | |
271 | 301 | | |
272 | 302 | | |
273 | 303 | | |
| |||
288 | 318 | | |
289 | 319 | | |
290 | 320 | | |
291 | | - | |
| 321 | + | |
292 | 322 | | |
293 | 323 | | |
294 | | - | |
295 | | - | |
| 324 | + | |
296 | 325 | | |
297 | 326 | | |
298 | 327 | | |
| |||
301 | 330 | | |
302 | 331 | | |
303 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
537 | 751 | | |
538 | 752 | | |
539 | 753 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments