@fantastic.dev/better-stripe 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +7 -1
  3. package/dist/client/billing/checkout.d.ts +3 -43
  4. package/dist/client/billing/checkout.d.ts.map +1 -1
  5. package/dist/client/billing/checkout.js +5 -260
  6. package/dist/client/billing/checkout.js.map +1 -1
  7. package/dist/client/billing/checkoutRequest.d.ts +129 -0
  8. package/dist/client/billing/checkoutRequest.d.ts.map +1 -0
  9. package/dist/client/billing/checkoutRequest.js +263 -0
  10. package/dist/client/billing/checkoutRequest.js.map +1 -0
  11. package/dist/client/billing/preparedCheckout.d.ts +22 -0
  12. package/dist/client/billing/preparedCheckout.d.ts.map +1 -0
  13. package/dist/client/billing/preparedCheckout.js +86 -0
  14. package/dist/client/billing/preparedCheckout.js.map +1 -0
  15. package/dist/client/checkoutRequests.d.ts +80 -0
  16. package/dist/client/checkoutRequests.d.ts.map +1 -0
  17. package/dist/client/checkoutRequests.js +28 -0
  18. package/dist/client/checkoutRequests.js.map +1 -0
  19. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +464 -0
  20. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  21. package/dist/client/connect/payoutCollection.d.ts +21 -0
  22. package/dist/client/connect/payoutCollection.d.ts.map +1 -0
  23. package/dist/client/connect/payoutCollection.js +47 -0
  24. package/dist/client/connect/payoutCollection.js.map +1 -0
  25. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +1805 -0
  26. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -0
  27. package/dist/client/connect/payoutExecutionTestHelpers.js +194 -0
  28. package/dist/client/connect/payoutExecutionTestHelpers.js.map +1 -0
  29. package/dist/client/connect/payoutSyncTestHelpers.d.ts +232 -0
  30. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  31. package/dist/client/connect/payoutWorker.d.ts +48 -0
  32. package/dist/client/connect/payoutWorker.d.ts.map +1 -0
  33. package/dist/client/connect/payoutWorker.js +78 -0
  34. package/dist/client/connect/payoutWorker.js.map +1 -0
  35. package/dist/client/index.d.ts +4 -11
  36. package/dist/client/index.d.ts.map +1 -1
  37. package/dist/client/index.js +4 -9
  38. package/dist/client/index.js.map +1 -1
  39. package/dist/client/payoutPolicy.d.ts +67 -0
  40. package/dist/client/payoutPolicy.d.ts.map +1 -1
  41. package/dist/client/payoutPolicy.js +10 -0
  42. package/dist/client/payoutPolicy.js.map +1 -1
  43. package/dist/client/readContracts.d.ts +11 -1
  44. package/dist/client/readContracts.d.ts.map +1 -1
  45. package/dist/client/readContracts.js +10 -0
  46. package/dist/client/readContracts.js.map +1 -1
  47. package/dist/component/_generated/component.d.ts +293 -0
  48. package/dist/component/_generated/component.d.ts.map +1 -1
  49. package/dist/component/core/mutations.d.ts.map +1 -1
  50. package/dist/component/core/mutations.js +2 -0
  51. package/dist/component/core/mutations.js.map +1 -1
  52. package/dist/component/core/resetSafety.d.ts +4 -0
  53. package/dist/component/core/resetSafety.d.ts.map +1 -0
  54. package/dist/component/core/resetSafety.js +54 -0
  55. package/dist/component/core/resetSafety.js.map +1 -0
  56. package/dist/component/earnings/triggers.d.ts +418 -0
  57. package/dist/component/earnings/triggers.d.ts.map +1 -1
  58. package/dist/component/earnings/triggers.js +2 -0
  59. package/dist/component/earnings/triggers.js.map +1 -1
  60. package/dist/component/payoutPolicy/schema.d.ts +2 -0
  61. package/dist/component/payoutPolicy/schema.d.ts.map +1 -1
  62. package/dist/component/payoutPolicy/schema.js +3 -1
  63. package/dist/component/payoutPolicy/schema.js.map +1 -1
  64. package/dist/component/payoutRelease/mutations.js +1 -1
  65. package/dist/component/payoutRelease/schema.d.ts +1 -0
  66. package/dist/component/payoutRelease/schema.d.ts.map +1 -1
  67. package/dist/component/payoutRelease/schema.js +1 -0
  68. package/dist/component/payoutRelease/schema.js.map +1 -1
  69. package/dist/component/payoutSnapshots/localState.d.ts +89 -0
  70. package/dist/component/payoutSnapshots/localState.d.ts.map +1 -0
  71. package/dist/component/payoutSnapshots/localState.js +186 -0
  72. package/dist/component/payoutSnapshots/localState.js.map +1 -0
  73. package/dist/component/payoutSnapshots/mutations.d.ts +108 -0
  74. package/dist/component/payoutSnapshots/mutations.d.ts.map +1 -0
  75. package/dist/component/payoutSnapshots/mutations.js +263 -0
  76. package/dist/component/payoutSnapshots/mutations.js.map +1 -0
  77. package/dist/component/payoutSnapshots/revisions.d.ts +12 -0
  78. package/dist/component/payoutSnapshots/revisions.d.ts.map +1 -0
  79. package/dist/component/payoutSnapshots/revisions.js +173 -0
  80. package/dist/component/payoutSnapshots/revisions.js.map +1 -0
  81. package/dist/component/payoutSnapshots/schema.d.ts +285 -0
  82. package/dist/component/payoutSnapshots/schema.d.ts.map +1 -0
  83. package/dist/component/payoutSnapshots/schema.js +108 -0
  84. package/dist/component/payoutSnapshots/schema.js.map +1 -0
  85. package/dist/component/payoutSnapshots/scope.d.ts +7 -0
  86. package/dist/component/payoutSnapshots/scope.d.ts.map +1 -0
  87. package/dist/component/payoutSnapshots/scope.js +8 -0
  88. package/dist/component/payoutSnapshots/scope.js.map +1 -0
  89. package/dist/component/payoutWorker/helpers.d.ts +30 -0
  90. package/dist/component/payoutWorker/helpers.d.ts.map +1 -0
  91. package/dist/component/payoutWorker/helpers.js +21 -0
  92. package/dist/component/payoutWorker/helpers.js.map +1 -0
  93. package/dist/component/payoutWorker/mutations.d.ts +68 -0
  94. package/dist/component/payoutWorker/mutations.d.ts.map +1 -0
  95. package/dist/component/payoutWorker/mutations.js +161 -0
  96. package/dist/component/payoutWorker/mutations.js.map +1 -0
  97. package/dist/component/payoutWorker/queries.d.ts +123 -0
  98. package/dist/component/payoutWorker/queries.d.ts.map +1 -0
  99. package/dist/component/payoutWorker/queries.js +61 -0
  100. package/dist/component/payoutWorker/queries.js.map +1 -0
  101. package/dist/component/payoutWorker/schema.d.ts +91 -0
  102. package/dist/component/payoutWorker/schema.d.ts.map +1 -0
  103. package/dist/component/payoutWorker/schema.js +27 -0
  104. package/dist/component/payoutWorker/schema.js.map +1 -0
  105. package/dist/component/schema.d.ts +232 -0
  106. package/dist/component/schema.js +10 -0
  107. package/dist/component/schema.js.map +1 -1
  108. package/docs/read-contracts.md +48 -0
  109. package/docs/settlement.md +128 -1
  110. package/package.json +1 -1
  111. package/src/client/billing/checkout.ts +11 -359
  112. package/src/client/billing/checkoutRequest.ts +362 -0
  113. package/src/client/billing/prepared-checkout.test.ts +430 -0
  114. package/src/client/billing/preparedCheckout.ts +143 -0
  115. package/src/client/checkoutRequests.ts +40 -0
  116. package/src/client/connect/payoutCollection.test.ts +74 -0
  117. package/src/client/connect/payoutCollection.ts +82 -0
  118. package/src/client/connect/payoutExecution.test.ts +1 -211
  119. package/src/client/connect/payoutExecutionTestHelpers.ts +213 -0
  120. package/src/client/connect/payoutWorker.test.ts +145 -0
  121. package/src/client/connect/payoutWorker.ts +144 -0
  122. package/src/client/index.ts +10 -21
  123. package/src/client/payoutPolicy.ts +26 -1
  124. package/src/client/readContracts.ts +23 -0
  125. package/src/component/_generated/api.ts +16 -0
  126. package/src/component/_generated/component.ts +361 -0
  127. package/src/component/core/mutations.ts +2 -0
  128. package/src/component/core/resetSafety.ts +67 -0
  129. package/src/component/earnings/triggers.ts +2 -0
  130. package/src/component/payout-reset-safety.test.ts +148 -0
  131. package/src/component/payout-revisions.test.ts +125 -0
  132. package/src/component/payout-snapshots.test.ts +395 -0
  133. package/src/component/payout-worker.test.ts +165 -0
  134. package/src/component/payoutPolicy/schema.ts +3 -1
  135. package/src/component/payoutRelease/mutations.ts +1 -1
  136. package/src/component/payoutRelease/schema.ts +1 -0
  137. package/src/component/payoutSnapshots/localState.ts +238 -0
  138. package/src/component/payoutSnapshots/mutations.ts +275 -0
  139. package/src/component/payoutSnapshots/revisions.ts +247 -0
  140. package/src/component/payoutSnapshots/schema.ts +125 -0
  141. package/src/component/payoutSnapshots/scope.ts +7 -0
  142. package/src/component/payoutWorker/helpers.ts +30 -0
  143. package/src/component/payoutWorker/mutations.ts +181 -0
  144. package/src/component/payoutWorker/queries.ts +64 -0
  145. package/src/component/payoutWorker/schema.ts +26 -0
  146. package/src/component/public.test.ts +2 -2
  147. package/src/component/schema.ts +18 -0
@@ -1,373 +1,25 @@
1
- import { idempotencyOptions } from "../core/idempotency.js";
1
+ import { renderCheckoutSession } from "./checkoutRequest.js";
2
+ import { submitPreparedCheckout } from "./preparedCheckout.js";
2
3
  import type Stripe from "stripe";
3
-
4
- import type { SplitRecipient } from "../../component/lib/fees.js";
5
- import { computeFee, isPercentOnlyFee, validateSplit } from "../core/fees.js";
6
- import { throwStripeError } from "../errors.js";
7
4
  import type { Component, RunCtx } from "../helpers.js";
8
- import { runMutationOrThrow, stripReservedMetadata } from "../helpers.js";
9
- import type { CheckoutSessionCreateParams } from "../stripe-types.js";
10
- import type {
11
- PlatformFeeConfig,
12
- StripeComponentCheckoutSession,
13
- } from "../types.js";
5
+ import { runMutationOrThrow } from "../helpers.js";
6
+ import type { StripeComponentCheckoutSession } from "../types.js";
14
7
  import { componentRef } from "../webhooks/helpers.js";
15
8
 
16
- /**
17
- * Derive the persisted fee/routing fields (BTS-11) from the FINAL Checkout
18
- * Session request, reading the nested `subscription_data`/`payment_intent_data`
19
- * that will actually be sent to Stripe. Deriving from the final request (rather
20
- * than a pre-override draft) keeps the stored row consistent when callers pass
21
- * `sessionOverrides` that replace those nested params.
22
- */
23
- function deriveFeeRow(params: CheckoutSessionCreateParams): {
24
- chargeType?: "destination";
25
- destinationAccountId?: string;
26
- applicationFeePercent?: number;
27
- applicationFeeAmount?: number;
28
- } {
29
- const dest = (d: unknown): string | undefined =>
30
- typeof d === "string"
31
- ? d
32
- : ((d as { id?: string } | null | undefined)?.id ?? undefined);
33
-
34
- if (params.mode === "subscription") {
35
- const destination = dest(
36
- params.subscription_data?.transfer_data?.destination,
37
- );
38
- if (!destination) return {};
39
- const pct = params.subscription_data?.application_fee_percent;
40
- return {
41
- chargeType: "destination",
42
- destinationAccountId: destination,
43
- ...(pct !== undefined ? { applicationFeePercent: pct } : {}),
44
- };
45
- }
46
- if (params.mode === "payment") {
47
- const destination = dest(
48
- params.payment_intent_data?.transfer_data?.destination,
49
- );
50
- if (!destination) return {};
51
- const amt = params.payment_intent_data?.application_fee_amount;
52
- return {
53
- chargeType: "destination",
54
- destinationAccountId: destination,
55
- ...(amt !== undefined ? { applicationFeeAmount: amt } : {}),
56
- };
57
- }
58
- return {};
59
- }
60
-
61
- /**
62
- * Destination and separate charges use platform prices. Catalog accountId is a
63
- * store attribution tag, not Stripe ownership (createProduct uses the platform).
64
- * For tagged products, verify the price in the platform Stripe context before
65
- * rejecting it. Untagged/unknown catalog entries are validated by Checkout.
66
- */
67
- async function assertPlatformPrice(
68
- stripe: Stripe,
69
- component: Component,
70
- ctx: RunCtx,
71
- stripePriceId: string,
72
- ): Promise<void> {
73
- const price = (await ctx.runQuery(
74
- componentRef(component, "products/queries/getPriceByStripeId"),
75
- { stripePriceId },
76
- )) as { stripeProductId?: string } | null;
77
- if (!price?.stripeProductId) return;
78
-
79
- const product = (await ctx.runQuery(
80
- componentRef(component, "products/queries/getProductByStripeId"),
81
- { stripeProductId: price.stripeProductId },
82
- )) as { accountId?: string } | null;
83
-
84
- if (product?.accountId) {
85
- try {
86
- await stripe.prices.retrieve(stripePriceId);
87
- } catch (error) {
88
- // Only absence on the platform means the ownership check failed.
89
- // Auth/network/rate-limit failures must not be misreported as ownership.
90
- if (
91
- !error ||
92
- typeof error !== "object" ||
93
- !("code" in error) ||
94
- error.code !== "resource_missing"
95
- )
96
- throw error;
97
- throwStripeError(
98
- "INVALID_CONFIGURATION",
99
- `Price ${stripePriceId} is unavailable on the platform account; destination charges require a platform-owned price.`,
100
- );
101
- }
102
- }
103
- }
104
-
105
- // =============================================================================
106
- // Checkout methods
107
- // =============================================================================
108
-
109
9
  export async function createCheckoutSession(
110
10
  stripe: Stripe,
111
11
  component: Component,
112
12
  ctx: RunCtx,
113
- opts: {
114
- idempotencyKey?: string;
115
- userId: string;
116
- orgId?: string;
117
- stripePriceId: string;
118
- mode: "payment" | "subscription" | "setup";
119
- uiMode?: "embedded" | "redirect" | "custom" | "form";
120
- quantity?: number;
121
- returnUrl: string;
122
- trialDays?: number;
123
- accountId?: string;
124
- customerEmail?: string;
125
- /**
126
- * The seller/recipient connected account funds are routed to (destination
127
- * charge). Fees only apply when this is set.
128
- */
129
- destinationAccountId?: string;
130
- /**
131
- * Multiple recipients for one sale (store + affiliate[s]). With >1 recipient
132
- * this routes via separate charges & transfers (no `application_fee`); the
133
- * transfers are created by the webhook engine. A single-recipient split is
134
- * treated as a destination charge. Takes precedence over `destinationAccountId`.
135
- */
136
- split?: SplitRecipient[];
137
- /** Resolved platform fee config (the caller resolves override → default). */
138
- feeConfig?: PlatformFeeConfig;
139
- /**
140
- * Charge total in minor units (one-time payment mode). When known, the
141
- * fixed `application_fee_amount` is computed up front; otherwise the fee is
142
- * deferred to the webhook.
143
- */
144
- amount?: number;
145
- /**
146
- * Platform-default statement-descriptor suffix (BTS-32), used when the
147
- * destination account record carries no per-store suffix. The caller
148
- * (BetterStripe) passes its validated configured default.
149
- */
150
- defaultStatementDescriptorSuffix?: string;
151
- metadata?: Record<string, string>;
152
- sessionOverrides?: Record<string, unknown>;
153
- },
13
+ opts: Parameters<typeof renderCheckoutSession>[3],
154
14
  ) {
155
- const uiMode = opts.uiMode ?? "embedded";
156
-
157
- // Setup-mode sessions create no charge, so routing/fees are meaningless and
158
- // would persist an unfunded `chargeType` the webhook engine can't act on.
159
- if (
160
- opts.mode === "setup" &&
161
- (opts.split?.length || opts.destinationAccountId || opts.feeConfig)
162
- ) {
163
- throwStripeError(
164
- "INVALID_CONFIGURATION",
165
- "split/destinationAccountId/fee are not supported for setup-mode Checkout Sessions",
166
- );
167
- }
168
-
169
- // Resolve the routing model. A `split` with >1 recipient uses separate charges
170
- // & transfers (no application_fee; webhook engine creates the transfers); a
171
- // single-recipient split is just a destination charge. `split` wins over
172
- // `destinationAccountId`.
173
- let isSeparate = false;
174
- let destinationAccountId = opts.destinationAccountId;
175
- if (opts.split && opts.split.length > 0) {
176
- validateSplit(opts.split);
177
- if (opts.split.length === 1) {
178
- destinationAccountId = opts.split[0].destinationAccountId;
179
- } else {
180
- isSeparate = true;
181
- }
182
- }
183
- // Metadata markers the webhook split engine reads to create the transfers.
184
- // For subscriptions these live on the subscription and are consumed on each
185
- // invoice.paid; for one-time payments they live on the PaymentIntent.
186
- const separateMeta = isSeparate
187
- ? {
188
- bsChargeType: "separate",
189
- bsSplit: JSON.stringify(opts.split),
190
- ...(opts.feeConfig
191
- ? { bsFeeConfig: JSON.stringify(opts.feeConfig) }
192
- : {}),
193
- }
194
- : {};
195
-
196
- // Strip the reserved `bs*` namespace from caller metadata so it can't forge
197
- // webhook instructions (e.g. trigger split transfers without routing).
198
- const metadata = {
199
- ...stripReservedMetadata(opts.metadata),
200
- userId: opts.userId,
201
- ...(opts.orgId ? { orgId: opts.orgId } : {}),
202
- };
203
-
204
- // Per-store statement descriptor (BTS-32). Destination charges are platform
205
- // charges (platform = merchant of record), so buyer recognition is
206
- // suffix-based: the store's stored suffix, else the configured platform
207
- // default. Resolved from the component account record at session creation.
208
- let statementDescriptorSuffix: string | undefined;
209
- if (destinationAccountId && !isSeparate && opts.mode !== "setup") {
210
- const account = (await ctx.runQuery(
211
- componentRef(component, "core/queries/getAccountByStripeId"),
212
- { stripeAccountId: destinationAccountId },
213
- )) as { statementDescriptor?: string } | null;
214
- statementDescriptorSuffix =
215
- account?.statementDescriptor ?? opts.defaultStatementDescriptorSuffix;
216
- }
217
-
218
- const sessionParams: CheckoutSessionCreateParams = {
219
- mode: opts.mode,
220
- line_items: [{ price: opts.stripePriceId, quantity: opts.quantity ?? 1 }],
221
- metadata,
222
- };
223
-
224
- if (opts.mode === "subscription") {
225
- const subscriptionData: CheckoutSessionCreateParams["subscription_data"] = {
226
- metadata,
227
- };
228
- if (opts.trialDays) {
229
- subscriptionData.trial_period_days = opts.trialDays;
230
- }
231
- if (isSeparate) {
232
- // Separate charges & transfers: NO transfer_data / application_fee. The
233
- // split is carried in metadata for the webhook engine to execute.
234
- subscriptionData.metadata = { ...metadata, ...separateMeta };
235
- } else if (destinationAccountId) {
236
- // Single-recipient destination charge: route funds to the seller and take
237
- // the platform's cut. Percent-only fees map to `application_fee_percent`;
238
- // percent+fixed/tiered fees can't be expressed that way, so flag the
239
- // subscription for per-invoice fee computation (handled by WEBHOOK_FEE).
240
- subscriptionData.transfer_data = { destination: destinationAccountId };
241
- if (opts.feeConfig) {
242
- if (isPercentOnlyFee(opts.feeConfig)) {
243
- subscriptionData.application_fee_percent = opts.feeConfig.percent;
244
- } else {
245
- subscriptionData.metadata = {
246
- ...metadata,
247
- bsFeeMode: "per_invoice",
248
- bsFeeConfig: JSON.stringify(opts.feeConfig),
249
- };
250
- }
251
- }
252
- if (statementDescriptorSuffix) {
253
- // Subscriptions can't carry a suffix directly; the invoice.created
254
- // processor reads this marker and sets it on each invoice.
255
- subscriptionData.metadata = {
256
- ...(subscriptionData.metadata ?? metadata),
257
- bsStatementDescriptor: statementDescriptorSuffix,
258
- };
259
- }
260
- }
261
- sessionParams.subscription_data = subscriptionData;
262
- }
263
-
264
- if (opts.mode === "payment") {
265
- const paymentIntentData: CheckoutSessionCreateParams["payment_intent_data"] =
266
- { metadata };
267
- if (isSeparate) {
268
- paymentIntentData.metadata = { ...metadata, ...separateMeta };
269
- } else if (destinationAccountId) {
270
- // Single-recipient destination charge for a one-time purchase. The platform
271
- // fee is a fixed `application_fee_amount`: compute it when the amount is
272
- // known; otherwise flag the PI with `bsFeeMode=per_charge` and the webhook
273
- // collects it at payment_intent.succeeded via a partial reversal of the
274
- // destination transfer, sized from the amount actually charged (BTS-60).
275
- paymentIntentData.transfer_data = {
276
- destination: destinationAccountId,
277
- };
278
- if (statementDescriptorSuffix) {
279
- paymentIntentData.statement_descriptor_suffix =
280
- statementDescriptorSuffix;
281
- }
282
- if (opts.feeConfig) {
283
- if (opts.amount !== undefined) {
284
- paymentIntentData.application_fee_amount = computeFee(
285
- opts.amount,
286
- opts.feeConfig,
287
- ).feeAmount;
288
- } else {
289
- paymentIntentData.metadata = {
290
- ...metadata,
291
- bsFeeMode: "per_charge",
292
- bsFeeConfig: JSON.stringify(opts.feeConfig),
293
- };
294
- }
295
- }
296
- }
297
- sessionParams.payment_intent_data = paymentIntentData;
298
- }
299
-
300
- if (uiMode === "embedded" || uiMode === "custom" || uiMode === "form") {
301
- sessionParams.ui_mode =
302
- uiMode === "custom"
303
- ? "elements"
304
- : uiMode === "form"
305
- ? "form"
306
- : "embedded_page";
307
- sessionParams.return_url = opts.returnUrl;
308
- } else {
309
- sessionParams.success_url = `${opts.returnUrl}?session_id={CHECKOUT_SESSION_ID}`;
310
- sessionParams.cancel_url = opts.returnUrl;
311
- }
312
-
313
- if (opts.accountId) {
314
- sessionParams.customer_account = opts.accountId;
315
- } else if (opts.customerEmail) {
316
- sessionParams.customer_email = opts.customerEmail;
317
- }
318
-
319
- // Merge overrides, then derive the persisted fee/routing row from the FINAL
320
- // request — sessionOverrides can replace subscription_data/payment_intent_data
321
- // wholesale, so the row must reflect what was actually sent to Stripe.
322
- const finalSessionParams: CheckoutSessionCreateParams = {
323
- ...sessionParams,
324
- ...(opts.sessionOverrides as Partial<CheckoutSessionCreateParams>),
325
- };
326
- // Separate-charge sales persist the split + chargeType directly; single-recipient
327
- // destination charges derive the row from the final transfer_data/fee.
328
- const feeRow = isSeparate
329
- ? { chargeType: "separate" as const, splitRecipients: opts.split }
330
- : deriveFeeRow(finalSessionParams);
331
-
332
- // Both destination and separate charges run on the platform account, so the
333
- // price must be platform-owned. Validate the FINAL request (post-overrides) so
334
- // callers can't inject transfer_data or swap line_items to bypass the check.
335
- const finalPriceId = finalSessionParams.line_items?.[0]?.price;
336
- if (
337
- (isSeparate || feeRow.chargeType === "destination") &&
338
- typeof finalPriceId === "string"
339
- ) {
340
- await assertPlatformPrice(stripe, component, ctx, finalPriceId);
341
- }
342
-
343
- const requestOptions = idempotencyOptions(opts.idempotencyKey);
344
- const session = requestOptions
345
- ? await stripe.checkout.sessions.create(finalSessionParams, requestOptions)
346
- : await stripe.checkout.sessions.create(finalSessionParams);
347
-
348
- await runMutationOrThrow(
15
+ const prepared = await renderCheckoutSession(stripe, component, ctx, opts);
16
+ return submitPreparedCheckout(
17
+ stripe,
18
+ component,
349
19
  ctx,
350
- componentRef(component, "billing/mutations/upsertCheckoutSession"),
351
- {
352
- stripeSessionId: session.id,
353
- userId: opts.userId,
354
- orgId: opts.orgId,
355
- accountId: opts.accountId,
356
- mode: finalSessionParams.mode,
357
- status: (session.status ?? "open") as "open" | "complete" | "expired",
358
- clientSecret: session.client_secret ?? undefined,
359
- url: session.url ?? undefined,
360
- priceId: opts.stripePriceId,
361
- ...feeRow,
362
- metadata,
363
- },
20
+ prepared,
21
+ opts.idempotencyKey,
364
22
  );
365
-
366
- return {
367
- stripeSessionId: session.id,
368
- clientSecret: session.client_secret ?? undefined,
369
- url: session.url ?? undefined,
370
- };
371
23
  }
372
24
 
373
25
  export async function getCheckoutSession(