1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Webhooks: calls from payment gateways and other services, received
//! safely. For each call Renox:
//!
//! 1. checks it came from the provider ([`Webhook::verify`], usually a
//! signature over the raw body), answering 401 otherwise;
//! 2. stores it in `webhook_calls`, once per provider event id, so the
//! provider's retries of the same event are answered 200 and not
//! processed twice;
//! 3. answers 200 at once and runs [`Webhook::handle`] in a queue worker,
//! retrying on errors; `webhook:failed` lists what failed and
//! `webhook:retry <id>` runs a call again.
//!
//! ```
//! # use renox::prelude::*;
//! # #[derive(serde::Deserialize)] struct Invoice { id: String, status: String, external_id: String }
//! # async fn mark_paid(_: &Db, _: &str) -> Result { Ok(()) }
//! use renox::webhook;
//!
//! struct Xendit;
//!
//! impl Webhook for Xendit {
//! const PROVIDER: &'static str = "xendit";
//!
//! fn verify(req: &WebhookRequest, state: &AppState) -> Result {
//! let token = webhook::secret(state, "XENDIT_CALLBACK_TOKEN")?;
//! webhook::ensure(req.header("x-callback-token").is_some_and(|t| webhook::same(t, &token)))
//! }
//!
//! fn event_id(req: &WebhookRequest) -> Result<String> {
//! let invoice: Invoice = req.json()?;
//! Ok(format!("{}:{}", invoice.id, invoice.status))
//! }
//!
//! async fn handle(call: WebhookCall, ctx: JobContext) -> Result {
//! let invoice: Invoice = call.json()?;
//! mark_paid(&ctx.state.db, &invoice.external_id).await
//! }
//! }
//!
//! // Module::routes: Routes::new().webhook::<Xendit>("/webhooks/xendit")
//! // Module::register: app.webhook::<Xendit>();
//! ```
//!
//! Webhook routes skip CSRF (callers have no session) and keep working in
//! maintenance mode (calls are stored and processed as usual).
pub use *;
pub use SignedRequest;