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 402 HTTP response using this gate’s accepted price tags and resource information.

This is the public convenience wrapper around error_into_response, useful in composable flows where the caller handles verification separately from settlement.

Source§

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

Source

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

Handles an incoming request, processing payment if required.

Returns 402 response if payment fails. Otherwise, returns the response from the inner service.

§Errors

This method is infallible (Infallible error type).

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>

Verify 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.

This is the building block for composable payment flows such as deferred settlement for streaming responses.

§Errors

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

Source

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

Handles an incoming request, returning errors as PaygateError.

This is the fallible version of handle_request that returns an actual error instead of converting it into a 402 Payment Required response.

Internally delegates to verify_only for verification and VerifiedPayment::settle for settlement.

§Errors

Returns PaygateError if payment processing 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