StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
Client.h
1/** Client.h
2 *
3 * The Stripe client for creating customers, checkout sessions, subscriptions,
4 * and billing portal sessions.
5 *
6 * @author Hans de Ruiter
7 *
8 * @license See LICENSE.md for details.
9 */
10
11#pragma once
12
13#include "StripeKit/Async.h"
14#include "StripeKit/Models.h"
15#include "StripeKit/RequestOptions.h"
16
17#include <cstdint>
18#include <exception>
19#include <functional>
20#include <string>
21#include <unordered_map>
22#include <variant>
23
24namespace stripekit {
25
26/**
27 * @addtogroup client
28 * @{
29 */
30
31/**
32 * Describes the customer to create.
33 *
34 * @sa https://docs.stripe.com/api/customers/create
35 */
37 /** Customer email address. Stripe uses it for receipts and invoices. */
38 std::string email;
39
40 /** Customer display name, as it should appear in the Stripe dashboard. */
41 std::string name;
42
43 /**
44 * Free-form key/value pairs to store on the customer.
45 *
46 * Stripe echoes metadata back on related webhook events, so this is a good
47 * place to record the application's own user id.
48 */
49 std::unordered_map<std::string, std::string> metadata;
50};
51
52/**
53 * Describes the Checkout session to create.
54 *
55 * StripeKit creates Checkout sessions for subscriptions (`mode = "subscription"`)
56 * or one-time payments (`mode = "payment"`). Price the item one of two ways:
57 * set `price_id` to reuse a Price already configured in the Stripe dashboard,
58 * or leave it empty and fill in `product_name`, `currency`, `unit_amount`, and
59 * (for subscriptions) `recurring_interval` to define the price inline.
60 *
61 * @sa https://docs.stripe.com/api/checkout/sessions/create
62 */
64 /** Id of an existing Stripe customer to bill. Optional for one-time payments. */
65 std::string customer_id;
66
67 /**
68 * Mode of the Checkout session: "subscription" or "payment".
69 *
70 * Defaults to "subscription". Use "payment" for one-time purchases.
71 */
72 std::string mode = "subscription";
73
74 /** Id of an existing Stripe Price. Leave empty to price the line item inline. */
75 std::string price_id;
76
77 /** Product name, for inline pricing. Ignored when `price_id` is set. */
78 std::string product_name;
79
80 /** ISO currency code for inline pricing, for example "usd". */
81 std::string currency = "usd";
82
83 /**
84 * Amount per unit for inline pricing, in the currency's smallest unit.
85 *
86 * That means cents for USD, so 1499 is $14.99.
87 */
88 std::int64_t unit_amount = 0;
89
90 /**
91 * Billing interval for inline pricing in subscription mode: "day", "week", "month", or "year".
92 *
93 * Ignored when `mode` is "payment".
94 */
95 std::string recurring_interval = "month";
96
97 /**
98 * Where Stripe sends the customer's browser after a successful checkout.
99 *
100 * Reaching this URL is not proof of payment; the customer may close the tab
101 * first, and anyone can visit the URL directly. Grant access from the
102 * `checkout.session.completed` or `invoice.paid` webhook instead.
103 */
104 std::string success_url;
105
106 /** Where Stripe sends the customer's browser if they abandon checkout. */
107 std::string cancel_url;
108
109 /** Number of units of the item to bill for. */
110 std::int64_t quantity = 1;
111
112 /** Free-form key/value pairs to store on the session. */
113 std::unordered_map<std::string, std::string> metadata;
114};
115
116/**
117 * Describes the billing portal session to create.
118 *
119 * @sa https://docs.stripe.com/api/customer_portal/sessions/create
120 */
122 /** Id of the Stripe customer who should manage their billing. */
123 std::string customer_id;
124
125 /** Where Stripe sends the customer when they leave the portal. */
126 std::string return_url;
127};
128
129/**
130 * Result passed to a completion callback: either the value, or the failure.
131 *
132 * Use `std::holds_alternative<std::exception_ptr>()` to check which arrived,
133 * and `std::rethrow_exception()` to inspect a failure.
134 *
135 * @tparam TResult Type produced when the operation succeeds.
136 */
137template <typename TResult>
138using CallbackResult = std::variant<TResult, std::exception_ptr>;
139
140/**
141 * Creates and retrieves Stripe customers.
142 *
143 * Reach an instance through StripeClient::customers(). A Stripe customer is
144 * the object everything else hangs off, so create one per application user and
145 * store the resulting id; do not create a fresh customer per checkout.
146 *
147 * @sa https://docs.stripe.com/api/customers
148 */
150public:
151 /** Completion callback for the callback form of create(). */
152 using CreateCompletion = std::function<void(CallbackResult<Customer>)>;
153
154 /** Completion callback for the callback form of retrieve(). */
155 using RetrieveCompletion = std::function<void(CallbackResult<Customer>)>;
156
157 /**
158 * Destroys the API instance.
159 */
160 virtual ~Customers() = default;
161
162 /**
163 * Creates a customer in Stripe.
164 *
165 * @param request Details of the customer to create.
166 * @param options Per-request settings such as an idempotency key.
167 * @return A task producing the created customer, including its Stripe id.
168 * @throws StripeApiException Stripe rejected the request.
169 */
171 const RequestOptions& options = {}) = 0;
172
173 /**
174 * Creates a customer, reporting the result through a callback.
175 *
176 * Use this in code that is not using coroutines; create_async() is
177 * otherwise preferable. The callback is invoked exactly once, and may run
178 * on a different thread than the caller.
179 *
180 * @param request Details of the customer to create.
181 * @param options Per-request settings such as an idempotency key.
182 * @param completion Receives the created customer or the failure.
183 */
184 virtual void create(const CreateCustomerRequest& request,
185 const RequestOptions& options,
186 CreateCompletion completion) = 0;
187
188 /**
189 * Fetches an existing customer.
190 *
191 * @param customer_id Stripe customer id, of the form "cus_...".
192 * @param options Per-request settings such as a timeout.
193 * @return A task producing the customer.
194 * @throws StripeApiException No such customer, or Stripe rejected the request.
195 */
196 virtual Task<Customer> retrieve_async(std::string customer_id,
197 const RequestOptions& options = {}) = 0;
198
199 /**
200 * Fetches an existing customer, reporting the result through a callback.
201 *
202 * The callback is invoked exactly once, and may run on a different thread
203 * than the caller.
204 *
205 * @param customer_id Stripe customer id, of the form "cus_...".
206 * @param options Per-request settings such as a timeout.
207 * @param completion Receives the customer or the failure.
208 */
209 virtual void retrieve(std::string customer_id,
210 const RequestOptions& options,
211 RetrieveCompletion completion) = 0;
212};
213
214/**
215 * Creates Stripe-hosted Checkout sessions.
216 *
217 * Reach an instance through StripeClient::checkout(). Checkout is a payment
218 * page Stripe hosts: create a session, redirect the customer to
219 * CheckoutSession::url, and Stripe collects the card details.
220 *
221 * @sa https://docs.stripe.com/api/checkout/sessions
222 */
224public:
225 /** Completion callback for the callback form of create(). */
227
228 /**
229 * Destroys the API instance.
230 */
231 virtual ~CheckoutSessions() = default;
232
233 /**
234 * Creates a Checkout session.
235 *
236 * @param request Details of the session to create.
237 * @param options Per-request settings. Set an idempotency key so a retry
238 * cannot start a second subscription.
239 * @return A task producing the session, whose `url` is where the customer
240 * should be sent.
241 * @throws StripeApiException Stripe rejected the request.
242 */
244 const RequestOptions& options = {}) = 0;
245
246 /**
247 * Creates a Checkout session, reporting the result through a callback.
248 *
249 * The callback is invoked exactly once, and may run on a different thread
250 * than the caller.
251 *
252 * @param request Details of the session to create.
253 * @param options Per-request settings such as an idempotency key.
254 * @param completion Receives the created session or the failure.
255 */
256 virtual void create(const CreateCheckoutSessionRequest& request,
257 const RequestOptions& options,
258 CreateCompletion completion) = 0;
259};
260
261/**
262 * Groups the Checkout operations.
263 *
264 * Mirrors Stripe's own grouping, so `checkout().sessions()` matches the
265 * Checkout Sessions section of Stripe's API reference.
266 */
267class Checkout {
268public:
269 /**
270 * Destroys the API instance.
271 */
272 virtual ~Checkout() = default;
273
274 /**
275 * Returns the Checkout session operations.
276 *
277 * @return A reference owned by the client; it stays valid as long as the
278 * client does.
279 */
281};
282
283/**
284 * Retrieves and cancels Stripe subscriptions.
285 *
286 * Reach an instance through StripeClient::subscriptions(). Subscriptions are
287 * created by Checkout rather than here; this interface covers inspecting one
288 * and ending it.
289 *
290 * @sa https://docs.stripe.com/api/subscriptions
291 */
293public:
294 /** Completion callback for the callback form of retrieve(). */
296
297 /** Completion callback for the callback form of cancel(). */
298 using CancelCompletion = std::function<void(CallbackResult<Subscription>)>;
299
300 /**
301 * Destroys the API instance.
302 */
303 virtual ~Subscriptions() = default;
304
305 /**
306 * Fetches a subscription and its current status.
307 *
308 * This reports what Stripe believes right now, which is useful for
309 * reconciliation. For day-to-day access decisions, follow the webhooks
310 * instead of polling.
311 *
312 * @param subscription_id Stripe subscription id, of the form "sub_...".
313 * @param options Per-request settings such as a timeout.
314 * @return A task producing the subscription.
315 * @throws StripeApiException No such subscription, or Stripe rejected the request.
316 */
317 virtual Task<Subscription> retrieve_async(std::string subscription_id,
318 const RequestOptions& options = {}) = 0;
319
320 /**
321 * Fetches a subscription, reporting the result through a callback.
322 *
323 * The callback is invoked exactly once, and may run on a different thread
324 * than the caller.
325 *
326 * @param subscription_id Stripe subscription id, of the form "sub_...".
327 * @param options Per-request settings such as a timeout.
328 * @param completion Receives the subscription or the failure.
329 */
330 virtual void retrieve(std::string subscription_id,
331 const RequestOptions& options,
332 RetrieveCompletion completion) = 0;
333
334 /**
335 * Cancels a subscription immediately.
336 *
337 * Billing stops at once rather than at the end of the paid period. Stripe
338 * sends a `customer.subscription.deleted` webhook, which is where access
339 * should actually be revoked.
340 *
341 * @param subscription_id Stripe subscription id, of the form "sub_...".
342 * @param options Per-request settings such as an idempotency key.
343 * @return A task producing the subscription in its cancelled state.
344 * @throws StripeApiException Stripe rejected the request.
345 */
346 virtual Task<Subscription> cancel_async(std::string subscription_id,
347 const RequestOptions& options = {}) = 0;
348
349 /**
350 * Cancels a subscription, reporting the result through a callback.
351 *
352 * The callback is invoked exactly once, and may run on a different thread
353 * than the caller.
354 *
355 * @param subscription_id Stripe subscription id, of the form "sub_...".
356 * @param options Per-request settings such as an idempotency key.
357 * @param completion Receives the cancelled subscription or the failure.
358 */
359 virtual void cancel(std::string subscription_id,
360 const RequestOptions& options,
361 CancelCompletion completion) = 0;
362};
363
364/**
365 * Creates billing portal sessions.
366 *
367 * Reach an instance through StripeClient::billing_portal(). The billing portal
368 * is a Stripe-hosted page where customers update their card, download
369 * invoices, and cancel — which saves building any of that.
370 *
371 * @sa https://docs.stripe.com/api/customer_portal/sessions
372 */
374public:
375 /** Completion callback for the callback form of create(). */
377
378 /**
379 * Destroys the API instance.
380 */
381 virtual ~BillingPortalSessions() = default;
382
383 /**
384 * Creates a billing portal session.
385 *
386 * Sessions are short-lived, so create one per visit rather than storing
387 * the URL.
388 *
389 * @param request The customer to create the session for, and where to
390 * return them afterwards.
391 * @param options Per-request settings such as a timeout.
392 * @return A task producing the session, whose `url` is where the customer
393 * should be sent.
394 * @throws StripeApiException Stripe rejected the request. A common cause is
395 * the billing portal not yet being configured in the dashboard.
396 */
399 const RequestOptions& options = {}) = 0;
400
401 /**
402 * Creates a billing portal session, reporting the result through a callback.
403 *
404 * The callback is invoked exactly once, and may run on a different thread
405 * than the caller.
406 *
407 * @param request The customer to create the session for, and where to
408 * return them afterwards.
409 * @param options Per-request settings such as a timeout.
410 * @param completion Receives the created session or the failure.
411 */
412 virtual void create(const CreateBillingPortalSessionRequest& request,
413 const RequestOptions& options,
414 CreateCompletion completion) = 0;
415};
416
417/**
418 * Groups the billing portal operations.
419 */
421public:
422 /**
423 * Destroys the API instance.
424 */
425 virtual ~BillingPortal() = default;
426
427 /**
428 * Returns the billing portal session operations.
429 *
430 * @return A reference owned by the client; it stays valid as long as the
431 * client does.
432 */
434};
435
436/**
437 * The entry point for talking to Stripe.
438 *
439 * Operations are grouped the way Stripe's own API reference groups them, so
440 * `checkout().sessions().create_async()` corresponds to Stripe's "Create a
441 * Checkout Session" endpoint.
442 *
443 * Obtain one from an adapter's factory function, such as
444 * stripekit::drogon::make_stripe_client(). One client is enough for the whole
445 * application: it is safe to share across threads and reuses connections to
446 * Stripe, so creating one per request only adds latency.
447 *
448 * @code
449 * auto stripe = stripekit::drogon::make_stripe_client({.api_key = "sk_test_..."});
450 * const auto customer = co_await stripe->customers().create_async({.email = "a@b.com"});
451 * @endcode
452 *
453 * @sa @ref quickstart
454 */
456public:
457 /**
458 * Destroys the client instance.
459 */
460 virtual ~StripeClient() = default;
461
462 /**
463 * Returns the customer operations.
464 *
465 * @return A reference owned by the client; it stays valid as long as the
466 * client does.
467 */
468 virtual Customers& customers() = 0;
469
470 /**
471 * Returns the Checkout operations.
472 *
473 * @return A reference owned by the client; it stays valid as long as the
474 * client does.
475 */
476 virtual Checkout& checkout() = 0;
477
478 /**
479 * Returns the subscription operations.
480 *
481 * @return A reference owned by the client; it stays valid as long as the
482 * client does.
483 */
485
486 /**
487 * Returns the billing portal operations.
488 *
489 * @return A reference owned by the client; it stays valid as long as the
490 * client does.
491 */
493};
494
495/** @} */
496
497} // namespace stripekit
Creates billing portal sessions.
Definition Client.h:373
std::function< void(CallbackResult< BillingPortalSession >)> CreateCompletion
Completion callback for the callback form of create().
Definition Client.h:376
virtual void create(const CreateBillingPortalSessionRequest &request, const RequestOptions &options, CreateCompletion completion)=0
Creates a billing portal session, reporting the result through a callback.
virtual Task< BillingPortalSession > create_async(const CreateBillingPortalSessionRequest &request, const RequestOptions &options={})=0
Creates a billing portal session.
virtual ~BillingPortalSessions()=default
Destroys the API instance.
Groups the billing portal operations.
Definition Client.h:420
virtual BillingPortalSessions & sessions()=0
Returns the billing portal session operations.
virtual ~BillingPortal()=default
Destroys the API instance.
Creates Stripe-hosted Checkout sessions.
Definition Client.h:223
virtual ~CheckoutSessions()=default
Destroys the API instance.
virtual Task< CheckoutSession > create_async(const CreateCheckoutSessionRequest &request, const RequestOptions &options={})=0
Creates a Checkout session.
virtual void create(const CreateCheckoutSessionRequest &request, const RequestOptions &options, CreateCompletion completion)=0
Creates a Checkout session, reporting the result through a callback.
std::function< void(CallbackResult< CheckoutSession >)> CreateCompletion
Completion callback for the callback form of create().
Definition Client.h:226
Groups the Checkout operations.
Definition Client.h:267
virtual ~Checkout()=default
Destroys the API instance.
virtual CheckoutSessions & sessions()=0
Returns the Checkout session operations.
Creates and retrieves Stripe customers.
Definition Client.h:149
virtual void retrieve(std::string customer_id, const RequestOptions &options, RetrieveCompletion completion)=0
Fetches an existing customer, reporting the result through a callback.
std::function< void(CallbackResult< Customer >)> CreateCompletion
Completion callback for the callback form of create().
Definition Client.h:152
std::function< void(CallbackResult< Customer >)> RetrieveCompletion
Completion callback for the callback form of retrieve().
Definition Client.h:155
virtual Task< Customer > retrieve_async(std::string customer_id, const RequestOptions &options={})=0
Fetches an existing customer.
virtual void create(const CreateCustomerRequest &request, const RequestOptions &options, CreateCompletion completion)=0
Creates a customer, reporting the result through a callback.
virtual Task< Customer > create_async(const CreateCustomerRequest &request, const RequestOptions &options={})=0
Creates a customer in Stripe.
virtual ~Customers()=default
Destroys the API instance.
The entry point for talking to Stripe.
Definition Client.h:455
virtual Customers & customers()=0
Returns the customer operations.
virtual Checkout & checkout()=0
Returns the Checkout operations.
virtual ~StripeClient()=default
Destroys the client instance.
virtual Subscriptions & subscriptions()=0
Returns the subscription operations.
virtual BillingPortal & billing_portal()=0
Returns the billing portal operations.
Retrieves and cancels Stripe subscriptions.
Definition Client.h:292
virtual ~Subscriptions()=default
Destroys the API instance.
std::function< void(CallbackResult< Subscription >)> RetrieveCompletion
Completion callback for the callback form of retrieve().
Definition Client.h:295
virtual void cancel(std::string subscription_id, const RequestOptions &options, CancelCompletion completion)=0
Cancels a subscription, reporting the result through a callback.
std::function< void(CallbackResult< Subscription >)> CancelCompletion
Completion callback for the callback form of cancel().
Definition Client.h:298
virtual void retrieve(std::string subscription_id, const RequestOptions &options, RetrieveCompletion completion)=0
Fetches a subscription, reporting the result through a callback.
virtual Task< Subscription > cancel_async(std::string subscription_id, const RequestOptions &options={})=0
Cancels a subscription immediately.
virtual Task< Subscription > retrieve_async(std::string subscription_id, const RequestOptions &options={})=0
Fetches a subscription and its current status.
The result of an asynchronous StripeKit operation.
Definition Async.h:45
std::variant< TResult, std::exception_ptr > CallbackResult
Result passed to a completion callback: either the value, or the failure.
Definition Client.h:138
Async.h.
Definition Async.h:20
Describes the billing portal session to create.
Definition Client.h:121
std::string return_url
Where Stripe sends the customer when they leave the portal.
Definition Client.h:126
std::string customer_id
Id of the Stripe customer who should manage their billing.
Definition Client.h:123
Describes the Checkout session to create.
Definition Client.h:63
std::string currency
ISO currency code for inline pricing, for example "usd".
Definition Client.h:81
std::string recurring_interval
Billing interval for inline pricing in subscription mode: "day", "week", "month", or "year".
Definition Client.h:95
std::string mode
Mode of the Checkout session: "subscription" or "payment".
Definition Client.h:72
std::int64_t unit_amount
Amount per unit for inline pricing, in the currency's smallest unit.
Definition Client.h:88
std::int64_t quantity
Number of units of the item to bill for.
Definition Client.h:110
std::string success_url
Where Stripe sends the customer's browser after a successful checkout.
Definition Client.h:104
std::string cancel_url
Where Stripe sends the customer's browser if they abandon checkout.
Definition Client.h:107
std::unordered_map< std::string, std::string > metadata
Free-form key/value pairs to store on the session.
Definition Client.h:113
std::string price_id
Id of an existing Stripe Price.
Definition Client.h:75
std::string customer_id
Id of an existing Stripe customer to bill.
Definition Client.h:65
std::string product_name
Product name, for inline pricing.
Definition Client.h:78
Describes the customer to create.
Definition Client.h:36
std::string name
Customer display name, as it should appear in the Stripe dashboard.
Definition Client.h:41
std::string email
Customer email address.
Definition Client.h:38
std::unordered_map< std::string, std::string > metadata
Free-form key/value pairs to store on the customer.
Definition Client.h:49
Per-call settings accepted by every client operation.