Skip to main content

Webhook

Struct Webhook 

Source
pub struct Webhook { /* private fields */ }

Implementations§

Source§

impl Webhook

Source

pub fn generate_test_header( payload: &str, secret: &str, timestamp: Option<i64>, ) -> String

Generate a test signature header for webhook testing.

This method generates a properly formatted Stripe signature header that can be used to test webhook signature verification end-to-end. It’s particularly useful for integration tests where you want to verify the entire webhook flow.

§Arguments
  • payload - The webhook payload (JSON string) to generate a signature for
  • secret - The webhook signing secret (e.g., “whsec_test_secret”)
  • timestamp - Optional Unix timestamp to use for the signature. If None, uses current time.
§Returns

A signature header string in the format: “t={timestamp},v1={signature}”

§Examples
use stripe_webhook::Webhook;

let payload = r#"{
    "id": "evt_test",
    "object": "event",
    "api_version": "2017-05-25",
    "created": 1492774577,
    "livemode": false,
    "pending_webhooks": 1,
    "data": {
        "object": {
            "object": "bank_account",
            "country": "us",
            "currency": "usd",
            "id": "ba_test",
            "last4": "6789",
            "status": "verified"
        }
    },
    "type": "account.external_account.created"
}"#;
let secret = "whsec_test_secret";

// Generate a test signature
let signature = Webhook::generate_test_header(payload, secret, None);

// Use it to verify the webhook
let event = Webhook::construct_event(payload, &signature, secret).unwrap();
assert_eq!(event.id.as_str(), "evt_test");
Source

pub fn insecure(payload: &str) -> Result<Event, WebhookError>

Construct an event from a webhook payload, ignoring the secret.

This method is considered insecure and intended for early-stage local testing only. Use construct_event for production instead.

§Errors

This function will return a WebhookError if the payload could not be parsed

Source

pub fn construct_event( payload: &str, sig: &str, secret: &str, ) -> Result<Event, WebhookError>

Construct an event from a webhook payload and signature.

§Errors

This function will return a WebhookError if:

  • the provided signature is invalid
  • the provided secret is invalid
  • the signature timestamp is older than 5 minutes
  • the payload could not be parsed
Source

pub fn construct_event_with_timestamp( payload: &str, sig: &str, secret: &str, timestamp: i64, ) -> Result<Event, WebhookError>

Construct an event from a webhook payload and signature, verifying its signature using the provided timestamp.

This is helpful for replaying requests in tests and should be avoided otherwise in production use.

§Errors

This function will return a WebhookError if:

  • the provided signature is invalid
  • the provided secret is invalid
  • the signature timestamp is older than 5 minutes from the provided timestamp
  • the payload could not be parsed

Trait Implementations§

Source§

impl Debug for Webhook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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