pub struct Webhook { /* private fields */ }
Implementations
sourceimpl Webhook
impl Webhook
sourcepub fn construct_event(
payload: &str,
sig: &str,
secret: &str
) -> Result<WebhookEvent, WebhookError>
pub fn construct_event(
payload: &str,
sig: &str,
secret: &str
) -> Result<WebhookEvent, WebhookError>
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
Examples found in repository
examples/webhook-actix.rs (line 38)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
pub fn handle_webhook(req: HttpRequest, payload: web::Bytes) -> Result<(), WebhookError> {
let payload_str = std::str::from_utf8(payload.borrow()).unwrap();
let stripe_signature = get_header_value(&req, "Stripe-Signature").unwrap_or_default();
if let Ok(event) = Webhook::construct_event(payload_str, stripe_signature, "whsec_xxxxx") {
match event.event_type {
EventType::AccountUpdated => {
if let EventObject::Account(account) = event.data.object {
handle_account_updated(account)?;
}
}
EventType::CheckoutSessionCompleted => {
if let EventObject::CheckoutSession(session) = event.data.object {
handle_checkout_session(session)?;
}
}
_ => {
println!("Unknown event encountered in webhook: {:?}", event.event_type);
}
}
} else {
println!("Failed to construct webhook event, ensure your webhook secret is correct.");
}
Ok(())
}
Auto Trait Implementations
impl RefUnwindSafe for Webhook
impl Send for Webhook
impl Sync for Webhook
impl Unpin for Webhook
impl UnwindSafe for Webhook
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more