pub struct Webhook { /* private fields */ }Implementations§
Source§impl Webhook
impl Webhook
Sourcepub fn generate_test_header(
payload: &str,
secret: &str,
timestamp: Option<i64>,
) -> String
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 forsecret- 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");Sourcepub fn insecure(payload: &str) -> Result<Event, WebhookError>
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
Sourcepub fn construct_event(
payload: &str,
sig: &str,
secret: &str,
) -> Result<Event, WebhookError>
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
Sourcepub fn construct_event_with_timestamp(
payload: &str,
sig: &str,
secret: &str,
timestamp: i64,
) -> Result<Event, WebhookError>
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