StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
StripeApiClient.h
1/** StripeApiClient.h
2 *
3 * Implementation of the StripeKit client API.
4 *
5 * @author Hans de Ruiter
6 *
7 * @License See LICENSE for details.
8 */
9
10#pragma once
11
12#include "StripeKit/Client.h"
13#include "StripeKit/Errors.h"
14#include "StripeKit/HTTPClient.h"
15
16#include <memory>
17
18namespace stripekit {
19
20struct CallbackLifetime;
21
22/**
23 * @addtogroup adapter_interfaces
24 * @{
25 */
26
27/**
28 * Turns Stripe's responses into StripeKit models.
29 *
30 * StripeKit decodes Stripe's JSON with JsonStripeResponseParser by default.
31 * Implement this only when the application needs different decoding, such as
32 * to reuse a JSON library it already links.
33 */
35public:
36 /** Destroys the parser instance. */
37 virtual ~StripeResponseParser() = default;
38
39 /**
40 * Reads a customer out of a successful response.
41 *
42 * @param response The response body and status from Stripe.
43 * @return The parsed customer.
44 * @throws ConfigurationException The body was not a valid customer object.
45 */
46 virtual Customer parse_customer(const HttpResponse& response) const = 0;
47
48 /**
49 * Reads a Checkout session out of a successful response.
50 *
51 * @param response The response body and status from Stripe.
52 * @return The parsed session.
53 * @throws ConfigurationException The body was not a valid session object.
54 */
55 virtual CheckoutSession parse_checkout_session(const HttpResponse& response) const = 0;
56
57 /**
58 * Reads a subscription out of a successful response.
59 *
60 * @param response The response body and status from Stripe.
61 * @return The parsed subscription.
62 * @throws ConfigurationException The body was not a valid subscription object.
63 */
64 virtual Subscription parse_subscription(const HttpResponse& response) const = 0;
65
66 /**
67 * Reads a billing portal session out of a successful response.
68 *
69 * @param response The response body and status from Stripe.
70 * @return The parsed session.
71 * @throws ConfigurationException The body was not a valid session object.
72 */
74 parse_billing_portal_session(const HttpResponse& response) const = 0;
75
76 /**
77 * Reads the error details out of a failed response.
78 *
79 * Must not throw: it is already handling a failure. Fall back to a
80 * StripeErrorType::Unknown payload when the body cannot be parsed.
81 *
82 * @param response The response body and status from Stripe.
83 * @return The error details, used to build a StripeApiException.
84 */
85 virtual StripeErrorPayload parse_error(const HttpResponse& response) const = 0;
86};
87
88/**
89 * The response parser StripeKit uses by default.
90 *
91 * Decodes Stripe's JSON with JsonCpp. Applications get this automatically and
92 * only need to think about it when supplying a parser of their own.
93 */
95public:
96 /**
97 * Reads a customer out of a successful response.
98 *
99 * @param response The response body and status from Stripe.
100 * @return The parsed customer.
101 * @throws ConfigurationException The body was not valid JSON, or not a customer.
102 */
103 Customer parse_customer(const HttpResponse& response) const override;
104
105 /**
106 * Reads a Checkout session out of a successful response.
107 *
108 * @param response The response body and status from Stripe.
109 * @return The parsed session.
110 * @throws ConfigurationException The body was not valid JSON, or not a session.
111 */
112 CheckoutSession parse_checkout_session(const HttpResponse& response) const override;
113
114 /**
115 * Reads a subscription out of a successful response.
116 *
117 * @param response The response body and status from Stripe.
118 * @return The parsed subscription.
119 * @throws ConfigurationException The body was not valid JSON, or not a subscription.
120 */
121 Subscription parse_subscription(const HttpResponse& response) const override;
122
123 /**
124 * Reads a billing portal session out of a successful response.
125 *
126 * @param response The response body and status from Stripe.
127 * @return The parsed session.
128 * @throws ConfigurationException The body was not valid JSON, or not a session.
129 */
131
132 /**
133 * Reads the error details out of a failed response.
134 *
135 * @param response The response body and status from Stripe.
136 * @return The error details, falling back to StripeErrorType::Unknown when
137 * the body cannot be parsed.
138 */
139 StripeErrorPayload parse_error(const HttpResponse& response) const override;
140};
141
142/**
143 * The StripeClient implementation, built on any HTTPClient.
144 *
145 * This is what turns an adapter's transport into a working Stripe client: it
146 * builds the requests, sends them through the supplied HTTPClient, parses the
147 * responses, and raises StripeApiException on failure. An adapter's factory
148 * function constructs one of these, so applications rarely need to.
149 *
150 * Construct it directly when you need to supply a custom response parser:
151 *
152 * @code
153 * stripekit::drogon::HTTPClient http_client("sk_test_...");
154 * stripekit::StripeApiClient stripe(http_client, custom_parser);
155 * @endcode
156 *
157 * The HTTP client and parser are referenced, not owned, so both must outlive
158 * the client. Safe to use from several threads.
159 */
160class StripeApiClient final : public StripeClient {
161public:
162 /**
163 * Creates a client that decodes responses with JsonCpp.
164 *
165 * @param http_client Transport to send requests through. Must outlive the client.
166 */
167 explicit StripeApiClient(HTTPClient& http_client);
168
169 /**
170 * Creates a client that decodes responses with a parser of your own.
171 *
172 * @param http_client Transport to send requests through. Must outlive the client.
173 * @param response_parser Decoder for Stripe's responses. Must outlive the client.
174 */
175 StripeApiClient(HTTPClient& http_client, const StripeResponseParser& response_parser);
176
177 /** Destroys the client and its operation groups. */
179
180 StripeApiClient(const StripeApiClient&) = delete;
181 StripeApiClient& operator=(const StripeApiClient&) = delete;
182
183 /**
184 * Returns the customer operations.
185 *
186 * @return A reference valid for the lifetime of this client.
187 */
188 Customers& customers() override;
189
190 /**
191 * Returns the Checkout operations.
192 *
193 * @return A reference valid for the lifetime of this client.
194 */
195 Checkout& checkout() override;
196
197 /**
198 * Returns the subscription operations.
199 *
200 * @return A reference valid for the lifetime of this client.
201 */
203
204 /**
205 * Returns the billing portal operations.
206 *
207 * @return A reference valid for the lifetime of this client.
208 */
210
211private:
212 class CustomerOperations final : public Customers {
213 public:
214 explicit CustomerOperations(StripeApiClient& owner);
215 Task<Customer> create_async(const CreateCustomerRequest& request,
216 const RequestOptions& options = {}) override;
217 void create(const CreateCustomerRequest& request,
218 const RequestOptions& options,
219 CreateCompletion completion) override;
220 Task<Customer> retrieve_async(std::string customer_id,
221 const RequestOptions& options = {}) override;
222 void retrieve(std::string customer_id,
223 const RequestOptions& options,
224 RetrieveCompletion completion) override;
225
226 private:
227 StripeApiClient& owner;
228 };
229
230 class CheckoutSessionOperations final : public CheckoutSessions {
231 public:
232 explicit CheckoutSessionOperations(StripeApiClient& owner);
233 Task<CheckoutSession> create_async(const CreateCheckoutSessionRequest& request,
234 const RequestOptions& options = {}) override;
235 void create(const CreateCheckoutSessionRequest& request,
236 const RequestOptions& options,
237 CreateCompletion completion) override;
238
239 private:
240 StripeApiClient& owner;
241 };
242
243 class CheckoutOperations final : public Checkout {
244 public:
245 explicit CheckoutOperations(StripeApiClient& owner);
246 CheckoutSessions& sessions() override;
247
248 private:
249 StripeApiClient& owner;
250 };
251
252 class SubscriptionOperations final : public Subscriptions {
253 public:
254 explicit SubscriptionOperations(StripeApiClient& owner);
255 Task<Subscription> retrieve_async(std::string subscription_id,
256 const RequestOptions& options = {}) override;
257 void retrieve(std::string subscription_id,
258 const RequestOptions& options,
259 RetrieveCompletion completion) override;
260 Task<Subscription> cancel_async(std::string subscription_id,
261 const RequestOptions& options = {}) override;
262 void cancel(std::string subscription_id,
263 const RequestOptions& options,
264 CancelCompletion completion) override;
265
266 private:
267 StripeApiClient& owner;
268 };
269
270 class BillingPortalSessionOperations final : public BillingPortalSessions {
271 public:
272 explicit BillingPortalSessionOperations(StripeApiClient& owner);
273 Task<BillingPortalSession> create_async(const CreateBillingPortalSessionRequest& request,
274 const RequestOptions& options = {}) override;
275 void create(const CreateBillingPortalSessionRequest& request,
276 const RequestOptions& options,
277 CreateCompletion completion) override;
278
279 private:
280 StripeApiClient& owner;
281 };
282
283 class BillingPortalOperations final : public BillingPortal {
284 public:
285 explicit BillingPortalOperations(StripeApiClient& owner);
286 BillingPortalSessions& sessions() override;
287
288 private:
289 StripeApiClient& owner;
290 };
291
292 HTTPClient& http_client;
293 JsonStripeResponseParser default_response_parser;
294 const StripeResponseParser& response_parser;
295 std::shared_ptr<CallbackLifetime> callback_lifetime;
296 CustomerOperations customer_operations;
297 CheckoutSessionOperations checkout_session_operations;
298 CheckoutOperations checkout_operations;
299 SubscriptionOperations subscription_operations;
300 BillingPortalSessionOperations billing_portal_session_operations;
301 BillingPortalOperations billing_portal_operations;
302};
303
304/** @} */
305
306} // namespace stripekit
std::function< void(CallbackResult< BillingPortalSession >)> CreateCompletion
Completion callback for the callback form of create().
Definition Client.h:376
Groups the billing portal operations.
Definition Client.h:420
virtual BillingPortalSessions & sessions()=0
Returns the billing portal session operations.
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 CheckoutSessions & sessions()=0
Returns the Checkout session operations.
Creates and retrieves Stripe customers.
Definition Client.h:149
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
Sends StripeKit's requests to Stripe.
Definition HTTPClient.h:110
The response parser StripeKit uses by default.
Customer parse_customer(const HttpResponse &response) const override
Reads a customer out of a successful response.
BillingPortalSession parse_billing_portal_session(const HttpResponse &response) const override
Reads a billing portal session out of a successful response.
CheckoutSession parse_checkout_session(const HttpResponse &response) const override
Reads a Checkout session out of a successful response.
StripeErrorPayload parse_error(const HttpResponse &response) const override
Reads the error details out of a failed response.
Subscription parse_subscription(const HttpResponse &response) const override
Reads a subscription out of a successful response.
StripeApiClient(HTTPClient &http_client)
Creates a client that decodes responses with JsonCpp.
Subscriptions & subscriptions() override
Returns the subscription operations.
StripeApiClient(HTTPClient &http_client, const StripeResponseParser &response_parser)
Creates a client that decodes responses with a parser of your own.
Customers & customers() override
Returns the customer operations.
BillingPortal & billing_portal() override
Returns the billing portal operations.
Checkout & checkout() override
Returns the Checkout operations.
~StripeApiClient() override
Destroys the client and its operation groups.
The entry point for talking to Stripe.
Definition Client.h:455
Turns Stripe's responses into StripeKit models.
virtual ~StripeResponseParser()=default
Destroys the parser instance.
virtual Customer parse_customer(const HttpResponse &response) const =0
Reads a customer out of a successful response.
virtual CheckoutSession parse_checkout_session(const HttpResponse &response) const =0
Reads a Checkout session out of a successful response.
virtual Subscription parse_subscription(const HttpResponse &response) const =0
Reads a subscription out of a successful response.
virtual BillingPortalSession parse_billing_portal_session(const HttpResponse &response) const =0
Reads a billing portal session out of a successful response.
virtual StripeErrorPayload parse_error(const HttpResponse &response) const =0
Reads the error details out of a failed response.
Retrieves and cancels Stripe subscriptions.
Definition Client.h:292
std::function< void(CallbackResult< Subscription >)> RetrieveCompletion
Completion callback for the callback form of retrieve().
Definition Client.h:295
std::function< void(CallbackResult< Subscription >)> CancelCompletion
Completion callback for the callback form of cancel().
Definition Client.h:298
The result of an asynchronous StripeKit operation.
Definition Async.h:45
Async.h.
Definition Async.h:20
A Stripe billing portal session: a short-lived link to the self-serve billing page.
Definition Models.h:167
A Stripe Checkout session: one attempt by a customer to pay or subscribe.
Definition Models.h:54
Describes the customer to create.
Definition Client.h:36
A Stripe customer: the account everything else is billed against.
Definition Models.h:28
Stripe's answer to a request.
Definition HTTPClient.h:82
Per-call settings accepted by every client operation.
The details Stripe returned about a failure.
Definition Errors.h:46
A Stripe subscription: a customer's recurring agreement to pay.
Definition Models.h:98