Skip to main content

Module paygate

Module paygate 

Source
Expand description

Per-route Axum payment gate middleware.

Provides PaymentGate for creating per-route payment layers that integrate natively with Axum’s .layer() method. Unlike super::server::PaymentGateLayer which uses a global route map with string-based matching, this module lets Axum handle routing while each route independently configures its payment requirements.

§Example

use std::sync::Arc;
use r402::server::X402ResourceServer;
use r402_http::paygate::PaymentGate;
use r402_http::types::{PaymentOption, RouteConfig};
use axum::{Router, routing::get};

let server = Arc::new(X402ResourceServer::new());
let gate = PaymentGate::new(server);

let app = Router::new()
    .route("/weather", get(weather_handler).layer(
        gate.route(RouteConfig::single(PaymentOption {
            scheme: "exact".into(),
            pay_to: "0xRecipient".into(),
            price: serde_json::json!("0.01"),
            network: "eip155:8453".into(),
            max_timeout_seconds: None,
            extra: None,
        }))
        .with_description("Weather forecast data")
    ))
    .route("/premium", get(premium_handler).layer(
        gate.route(RouteConfig::single(PaymentOption {
            scheme: "exact".into(),
            pay_to: "0xRecipient".into(),
            price: serde_json::json!("1.00"),
            network: "eip155:8453".into(),
            max_timeout_seconds: None,
            extra: None,
        }))
        .with_description("Premium content")
        .with_mime_type("application/json")
    ));

Structs§

PaymentGate
Per-route payment gate factory.
PaymentRouteLayer
Per-route Tower Layer that enforces x402 payment requirements.
PaymentRouteService
Per-route Tower Service that enforces x402 payment requirements.