Skip to main content

Paygate

Struct Paygate 

Source
pub struct Paygate<TFacilitator> { /* private fields */ }
Available on crate feature server only.
Expand description

V2-only payment gate for enforcing x402 payments.

Handles the full payment lifecycle: header extraction, verification, settlement, and 402 response generation using the V2 wire format.

Construct via PaygateBuilder (obtained from Paygate::builder).

To add lifecycle hooks (before/after verify and settle), wrap your facilitator with HookedFacilitator before passing it to the payment gate.

Implementations§

Source§

impl<TFacilitator> Paygate<TFacilitator>

Source

pub const fn builder(facilitator: TFacilitator) -> PaygateBuilder<TFacilitator>

Returns a new builder seeded with the given facilitator.

Source

pub const fn facilitator(&self) -> &TFacilitator

Returns a reference to the underlying facilitator.

Source

pub fn accepts(&self) -> &[PriceTag]

Returns a reference to the accepted price tags.

Source

pub const fn resource(&self) -> &ResourceInfo

Returns a reference to the resource information.

Source

pub fn error_response(&self, err: PaygateError) -> Response

Converts a PaygateError into a proper HTTP response.

Verification errors produce a 402 with the Payment-Required header and a JSON body. Settlement errors produce a 402 with error details.

§Panics

Panics if the payment-required response cannot be serialized to JSON or if the HTTP response builder fails. These indicate a bug.

Source§

impl<TFacilitator> Paygate<TFacilitator>
where TFacilitator: Facilitator + Sync,

Source

pub async fn enrich_accepts(&mut self)

Enriches price tags with facilitator capabilities (e.g., fee payer address).

Source

pub async fn verify_only( &self, headers: &HeaderMap, ) -> Result<VerifiedPayment, PaygateError>

Verifies the payment from request headers without executing the inner service or settling on-chain.

Returns a VerifiedPayment token on success, which the caller can later settle at their discretion.

§Errors

Returns PaygateError::Verification if the payment header is missing, malformed, or rejected by the facilitator.

Source

pub async fn handle_request<ReqBody, ResBody, S: Service<Request<ReqBody>, Response = Response<ResBody>>>( &self, inner: S, req: Request<ReqBody>, ) -> Result<Response, PaygateError>

Handles an incoming request with sequential settlement.

verify → execute → settle → attach header → return

Settlement only runs if the handler returns a success status (not 4xx/5xx).

§Errors

Returns PaygateError if payment verification or settlement fails.

Source§

impl<TFacilitator> Paygate<TFacilitator>
where TFacilitator: Facilitator + Clone + Send + Sync + 'static,

Source

pub async fn handle_request_concurrent<ReqBody, ResBody, S: Service<Request<ReqBody>, Response = Response<ResBody>>>( &self, inner: S, req: Request<ReqBody>, ) -> Result<Response, PaygateError>
where S::Response: IntoResponse, S::Error: IntoResponse, S::Future: Send + 'static, ReqBody: Send + 'static,

Handles an incoming request with concurrent settlement.

verify → (settle ∥ execute) → await settle → attach header → return

Settlement is spawned immediately after verification and runs in parallel with the handler, reducing total latency by one facilitator RTT. On handler error (4xx/5xx), the settlement task is detached.

§Errors

Returns PaygateError if payment verification or settlement fails.

Auto Trait Implementations§

§

impl<TFacilitator> Freeze for Paygate<TFacilitator>
where TFacilitator: Freeze,

§

impl<TFacilitator> !RefUnwindSafe for Paygate<TFacilitator>

§

impl<TFacilitator> Send for Paygate<TFacilitator>
where TFacilitator: Send,

§

impl<TFacilitator> Sync for Paygate<TFacilitator>
where TFacilitator: Sync,

§

impl<TFacilitator> Unpin for Paygate<TFacilitator>
where TFacilitator: Unpin,

§

impl<TFacilitator> UnsafeUnpin for Paygate<TFacilitator>
where TFacilitator: UnsafeUnpin,

§

impl<TFacilitator> !UnwindSafe for Paygate<TFacilitator>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more