pub struct Paygate<TFacilitator> { /* private fields */ }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>
impl<TFacilitator> Paygate<TFacilitator>
Sourcepub const fn builder(facilitator: TFacilitator) -> PaygateBuilder<TFacilitator>
pub const fn builder(facilitator: TFacilitator) -> PaygateBuilder<TFacilitator>
Returns a new builder seeded with the given facilitator.
Sourcepub const fn facilitator(&self) -> &TFacilitator
pub const fn facilitator(&self) -> &TFacilitator
Returns a reference to the underlying facilitator.
Sourcepub const fn resource(&self) -> &ResourceInfo
pub const fn resource(&self) -> &ResourceInfo
Returns a reference to the resource information.
Sourcepub fn error_response(&self, err: PaygateError) -> Response
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,
impl<TFacilitator> Paygate<TFacilitator>where
TFacilitator: Facilitator + Sync,
Sourcepub async fn handle_request<ReqBody, ResBody, S: Service<Request<ReqBody>, Response = Response<ResBody>>>(
&self,
inner: S,
req: Request<ReqBody>,
) -> Result<Response, Infallible>
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).
Sourcepub async fn enrich_accepts(&mut self)
pub async fn enrich_accepts(&mut self)
Enriches price tags with facilitator capabilities (e.g., fee payer address).
Sourcepub async fn verify_only(
&self,
headers: &HeaderMap,
) -> Result<VerifiedPayment, PaygateError>
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.
Sourcepub async fn handle_request_fallible<ReqBody, ResBody, S: Service<Request<ReqBody>, Response = Response<ResBody>>>(
&self,
inner: S,
req: Request<ReqBody>,
) -> Result<Response, PaygateError>
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.