StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
WebhookProcessor.h
1/** WebhookProcessor.h
2 *
3 * Coordinates verified Stripe webhook event handling and de-duplication.
4 *
5 * @author Hans de Ruiter
6 *
7 * @License See LICENSE for details.
8 */
9
10#pragma once
11
12#include "StripeKit/Events.h"
13#include "StripeKit/Logging.h"
14#include "StripeKit/WebhookDedupStore.h"
15#include "StripeKit/Webhooks.h"
16
17#include <chrono>
18#include <string>
19#include <string_view>
20
21namespace stripekit {
22
23/**
24 * @addtogroup webhooks
25 * @{
26 */
27
28/**
29 * Tuning for WebhookProcessor.
30 */
32 /**
33 * How long before a claim left behind by a crashed process is reclaimed.
34 *
35 * Set this comfortably longer than the dispatcher's worst-case runtime.
36 */
38};
39
40/**
41 * Handles incoming webhook deliveries through an EventDispatcher. Webhooks
42 * are HTTP requests sent by Stripe to notify about events.
43 *
44 * Does the same work as stripekit::webhooks::process_event_once() — verify,
45 * de-duplicate, dispatch — but drives a class rather than a coroutine
46 * callback, and does so synchronously. Use it when the application's webhook
47 * handling is already organised as an object; otherwise
48 * stripekit::webhooks::process_event_once() is the simpler route.
49 *
50 * The store, dispatcher, and logger are referenced, not owned, so all three
51 * must outlive the processor. A single processor can serve many requests, and
52 * is safe to use from several threads provided the dispatcher is.
53 */
54class WebhookProcessor final {
55public:
56 /**
57 * Creates a processor.
58 *
59 * @param dedup_store Where claims are recorded. Must outlive the processor.
60 * @param dispatcher Called for each freshly claimed event. Must outlive the
61 * processor.
62 * @param logger Where to report store and dispatch failures. No logging
63 * when null.
64 * @param options Claim timing.
65 */
67 EventDispatcher& dispatcher,
68 Logger* logger = nullptr,
69 WebhookProcessorOptions options = {});
70
71 /**
72 * Verifies, de-duplicates, and dispatches one delivery.
73 *
74 * @param input The raw payload, signature header, secret, and timing rules.
75 * @param delivery_id Optional identifier from the adapter, passed through
76 * to the dispatcher for tracing.
77 * @return Whether to answer Stripe with success, or ask it to retry.
78 * @throws SignatureVerificationException The delivery cannot be trusted.
79 * Answer with HTTP 400 rather than retrying.
80 * @throws ConfigurationException The payload is not a usable Stripe event.
81 */
83 std::string delivery_id = {}) const;
84
85private:
86 WebhookDedupStore& dedup_store;
87 EventDispatcher& dispatcher;
88 Logger* logger;
90};
91
92/** @} */
93
94} // namespace stripekit
Handles verified webhook events, for applications using WebhookProcessor.
Definition Events.h:62
Receives StripeKit's log entries.
Definition Logging.h:74
Remembers which webhook events have been handled.
WebhookProcessor(WebhookDedupStore &dedup_store, EventDispatcher &dispatcher, Logger *logger=nullptr, WebhookProcessorOptions options={})
Creates a processor.
WebhookResponseAction process(const WebhookVerificationInput &input, std::string delivery_id={}) const
Verifies, de-duplicates, and dispatches one delivery.
WebhookResponseAction
What to tell Stripe about a delivery.
constexpr std::chrono::minutes defaultStaleInflightTimeout
How long an unfinished claim is honoured before another process may take it.
Async.h.
Definition Async.h:20
Tuning for WebhookProcessor.
std::chrono::minutes stale_inflight_timeout
How long before a claim left behind by a crashed process is reclaimed.
Everything needed to check a webhook signature.