feat: Implement x402 payment flow for PHP client - #57
Conversation
This adds the minimal PHP client to handle x402 Payment Required challenges, automate Soroban transaction building via soneso/stellar-php-sdk, sign custom auth entries, and gracefully execute the testnet retries.
|
Reviewed the actual source (X402Client.php, config, examples) — the payment flow itself is solid: correct x402 v2 handshake, correct PAYMENT-SIGNATURE header, correct x402Version: 2. Two things need fixing before this can merge:
Once vendor/ is out and the source-account question is resolved (or verified against a live testnet payment with a resolvable tx hash), happy to take another look. |
|
Reviewed the code, not just the description — three real issues, not merging yet. 1. 2. The transaction source account doesn't match the documented pattern. This client builds the AssembledTransaction with 3. Missing the re-simulate-after-signing step. The code simulates once, signs the auth entries, and sends — but the signature adds bytes to the transaction, so the resource fee computed before signing is too low (measured elsewhere in this project: 24127 vs. the correct 33735). The fix is a second I wasn't able to run PHPUnit in this environment (no |
Closes #51. Reviewed the source, not just the description, and re-implemented the core hash/signature check independently to verify the claims: - Fetched the real live CID (QmSSZdtt3dQ8BqUm62zrKQ85E4BUHYiVfvDgZmHfJsqU1U) myself and confirmed hashMatch=true and the Ed25519 signature verifies against the embedded Stellar key — matches the PR's claim exactly. - Confirmed the tamper fixture actually breaks hash verification (recomputed the hash over the mutated record myself — mismatch, as expected) rather than trusting the reported 8/8 test pass. - Correct recompute-don't-trust pattern (never trusts embedded valid/statement fields), correct SPKI wrapping for raw ed25519 verification, correct domain separation (nirium-audit-v1:<hash>). - Checked-in dist/index.js is the standard, required pattern for JS GitHub Actions (not the vendor/ anti-pattern flagged on #57) — Actions can't npm install at consume time. One gap: no linked Actions run in the PR body, and none exists yet in this repo's run history — looks like the workflow this PR adds is still pending first-time-contributor approval rather than a defect. Merging; the push-triggered run against main will produce a real recorded run right after this lands.
|
@Eras256 This issue shows merged but still not recorded to me, can i know why?? |
|
@ABEEGOLD sorry for the confusion — that's a real mixup, not you missing something. Issue #48 got auto-closed by GitHub on 2026-08-24 because a different contributor's PR (#55, a Go client for issue #46) had a typo in its own description — "closes #48" instead of "closes #46" — and GrantFox's bot then credited that contributor for this issue by mistake, since it never actually did PHP work. I caught that and reopened #48 on 2026-08-26 with the explanation on the issue itself. If GrantFox's own board still shows it as completed/credited to someone else, that's the platform not having picked up the reopen — worth flagging directly on GrantFox too if it's still showing that way on your end. You're still the assigned contributor here and #48 is open. Credit for it goes with whoever's PR actually lands — right now that's this PR, once it's ready. On that: this PR itself hasn't moved since the review on 2026-08-26 — three things are still open before it can merge (quoting my own comment above so it's in one place):
Happy to review again as soon as those land. |
Closes #48
PR Description: Minimal PHP Client & x402 Protocol Implementation
Description
This pull request introduces the first minimal implementation of the Nirium PHP SDK (
packages/php-client), which adds full support for the x402 (Payment Required) protocol flow. It allows developers to seamlessly interact with API endpoints that require x402 payments (e.g., Nirium AI agents) and automate the underlying Soroban transaction logic directly in PHP.Key Features
402 Payment RequiredHTTP responses from agents, extracts the required payment details (exact scheme), and automatically constructs and signs the proper transaction payload to satisfy the challenge.soneso/stellar-php-sdkfor constructing the XDR representation of Soroban token transfers.SOROBAN_CREDENTIALS_ADDRESSsignature, allowing the PHP client to independently sign authorization payloads and hand them off to the agent without assuming the role of the invoker.Dockerfileto handle complex extension compilations (gmp,bcmath) needed by the underlying crypto libraries.GuzzleHttp\Handler\MockHandlerand liveFriendbotfunding integrations to successfully validate testnet simulations.examples/pay.phpdemonstrating how to instantiate the client and fetch an agent URL.Changes Included
packages/php-client/composer.json: Addedsoneso/stellar-php-sdk,guzzlehttp/guzzle, andphpunitdependencies with PSR-4 autoloading.packages/php-client/src/X402Client.php: The main entrypoint for the HTTP client and Soroban integration.packages/php-client/tests/X402ClientTest.php: Complete unit test demonstrating successful flow handling, signature generation, and HTTP retries.packages/php-client/examples/pay.php: Sample script for executing the payment flow against an agent.packages/php-client/Dockerfile: A consistent testing environment.Validation / Testing
simulateTransactionRPC) using real token contracts.PHPUnit 11within the provided Docker container. All assertions passed.