Go and React helpers for extending the x402 payment protocol
xtended402 is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 7 GitHub stars and 1 forks.
Server-side crypto payments for web apps, built on x402 v2.
x402 v2 is a flexible payment protocol for pay-per-call APIs, content paywalls, and more. xtended402 extends x402's signature-based payment pattern for traditional e-commerce. While x402 was designed for API micropayments, this toolkit makes it work for shopping carts, order processing, and human-driven purchases.
This is a toolbox Use only what you need: context-based pricing helpers, settlement timing control, or request body preservation. Everything works with standard x402 v2 - no wrappers, just helpers.
Why server-side payments matter: Unlike traditional crypto payment solutions that execute transactions client-side (WalletConnect, RainbowKit), x402 enables server-driven payment flows. xtended402 extends this pattern specifically for traditional web-app use cases where you need shopping cart pricing and guaranteed settlement before order processing. This means more robust code, standard REST patterns and no reconciliation headaches.
Standard x402 v2 pattern:
// x402's DynamicPriceFunc only has access to URL and headers
Price: x402http.DynamicPriceFunc(func(ctx, reqCtx) (x402.Price, error) {
// reqCtx only contains: Path, Method, Headers
// NO ACCESS to request body (where your shopping cart data lives!)
})
xtended402 provides:
// Calculate price from POST body in preceding middleware
func validateOrder(c *gin.Context) {
var order Order
json.NewDecoder(c.Request.Body).Decode(&order)
// Server-authoritative pricing from order data
total := calculateTotal(order) // From items, quantities, shipping, etc.
xtended402.SetContextValueGin(c, "x402:price", formatPrice(total))
c.Next()
}
// Payment middleware reads price from context
routes := x402http.RoutesConfig{
"POST /checkout": {
Accepts: []x402http.PaymentOption{{
Price: xtended402.ContextPrice("x402:price"), // Uses context value
}},
},
}
1. Context-Based Dynamic Pricing
2. Configurable Settlement Timing
// Settle BEFORE handler (e-commerce: money confirmed before processing order)
ginmw.WithSettlementTiming("before")
// Or AFTER handler (v2 default: process then settle)
ginmw.WithSettlementTiming("after")
3. Before-Settle Validation Hooks
4. Full x402 v2 Compatibility
Reimplemented Gin middleware with settlement timing control, plus helpers for context-based pricing and request body preservation.
📁 server/go/ - See README for installation and usage
Quick start:
import (
xtended402 "github.com/mvpoyatt/xtended402/server/go"
ginmw "github.com/mvpoyatt/xtended402/server/go/http/gin"
)
r.POST("/checkout",
validateAndPriceOrder, // Calculate price from order body
ginmw.PaymentMiddleware(routes, server,
ginmw.WithSettlementTiming("before"), // Settle before processing
),
fulfillOrder, // Order processing with confirmed payment
)
React component for payment flow integration. One example of a client that uses the enhanced server.
📁 client/react/ - See README for installation and usage
Example:
import { Checkout } from '@xtended402/react';
<Checkout
paymentEndpoint="https://api.example.com/checkout"
orderData={cart} // Component discovers price from server
displayMode="system"
accentColor="#338aea"
/>
1. Client sends request with order data
POST /checkout { items: [...], shipping: {...} }
2. Validation middleware
- Reads request body
- Validates order (inventory, rules, etc.)
- Calculates server-authoritative price
- Sets price in context
- If invalid: abort with 400 (no payment attempted)
3. Payment middleware
- If no X-PAYMENT header: return 402 with price from context
- If X-PAYMENT provided: verify payment
- Call before-settle hook (final validation)
- Settle payment
- If settlement succeeds: continue to handler
- If settlement fails: abort with 402
4. Handler processes order
- Payment already confirmed
- Safe to update database, send emails, etc.
xtended402 is a set of focused additions to x402 v2. Use what you need:
helpers.go, gin_helpers.go): Context-based pricing utilities that work with any x402 v2 setuphttp/gin/middleware.go): Reimplemented Gin middleware with settlement timing controltypes.go): PaymentData wrapper for convenient access to payment info| Feature | x402 v2 | xtended402 |
|---|---|---|
| Multi-chain support | ✅ | ✅ |
| URL/header-based pricing | ✅ | ✅ |
| Request body-based pricing | ❌ | ✅ |
| Settlement timing control | ❌ (after only) | ✅ (before/after) |
| Before-settle hooks | ✅ (server level) | ✅ (+ middleware level) |
| Request body preservation | ❌ | ✅ |
| PaymentData convenience wrapper | ❌ | ✅ |
See package-specific READMEs:
Complete examples available in each package:
Contributions welcome! Please open an issue or PR.
MIT
Built on Coinbase x402 v2. Portions of the code are derived from x402 v2, licensed under Apache License 2.0.