@fantastic.dev/better-stripe 0.41.0 → 0.43.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 (209) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +1 -1
  3. package/dist/client/balanceFundingWorkflow/client.d.ts +172 -0
  4. package/dist/client/balanceFundingWorkflow/client.d.ts.map +1 -0
  5. package/dist/client/balanceFundingWorkflow/client.js +289 -0
  6. package/dist/client/balanceFundingWorkflow/client.js.map +1 -0
  7. package/dist/client/billing/balanceFunding.d.ts +5 -0
  8. package/dist/client/billing/balanceFunding.d.ts.map +1 -1
  9. package/dist/client/billing/balanceFunding.js +30 -0
  10. package/dist/client/billing/balanceFunding.js.map +1 -1
  11. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +1388 -2
  12. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  13. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +710 -17
  14. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  15. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +1420 -34
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  17. package/dist/client/connect/payoutSyncTestHelpers.d.ts +694 -1
  18. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  19. package/dist/client/costRecovery-invoices.d.ts +3 -2
  20. package/dist/client/costRecovery-invoices.d.ts.map +1 -1
  21. package/dist/client/costRecovery-invoices.js +10 -5
  22. package/dist/client/costRecovery-invoices.js.map +1 -1
  23. package/dist/client/costRecovery.d.ts +4 -2
  24. package/dist/client/costRecovery.d.ts.map +1 -1
  25. package/dist/client/costRecovery.js +2 -2
  26. package/dist/client/costRecovery.js.map +1 -1
  27. package/dist/client/index.d.ts +3 -0
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js +4 -0
  30. package/dist/client/index.js.map +1 -1
  31. package/dist/client/processingCostReconciliation/client.d.ts +14 -0
  32. package/dist/client/processingCostReconciliation/client.d.ts.map +1 -0
  33. package/dist/client/processingCostReconciliation/client.js +35 -0
  34. package/dist/client/processingCostReconciliation/client.js.map +1 -0
  35. package/dist/client/processingCostReconciliation/contracts.d.ts +172 -0
  36. package/dist/client/processingCostReconciliation/contracts.d.ts.map +1 -0
  37. package/dist/client/processingCostReconciliation/contracts.js +55 -0
  38. package/dist/client/processingCostReconciliation/contracts.js.map +1 -0
  39. package/dist/client/processingCostReconciliation/invoiceFunding.d.ts +15 -0
  40. package/dist/client/processingCostReconciliation/invoiceFunding.d.ts.map +1 -0
  41. package/dist/client/processingCostReconciliation/invoiceFunding.js +195 -0
  42. package/dist/client/processingCostReconciliation/invoiceFunding.js.map +1 -0
  43. package/dist/client/processingCostReconciliation/provider.d.ts +9 -0
  44. package/dist/client/processingCostReconciliation/provider.d.ts.map +1 -0
  45. package/dist/client/processingCostReconciliation/provider.js +148 -0
  46. package/dist/client/processingCostReconciliation/provider.js.map +1 -0
  47. package/dist/client/settlement/processingCostHistory.d.ts +12 -0
  48. package/dist/client/settlement/processingCostHistory.d.ts.map +1 -0
  49. package/dist/client/settlement/processingCostHistory.js +20 -0
  50. package/dist/client/settlement/processingCostHistory.js.map +1 -0
  51. package/dist/client/settlement/processingCosts.d.ts +3 -0
  52. package/dist/client/settlement/processingCosts.d.ts.map +1 -1
  53. package/dist/client/settlement/processingCosts.js +27 -7
  54. package/dist/client/settlement/processingCosts.js.map +1 -1
  55. package/dist/component/_generated/api.d.ts +36 -0
  56. package/dist/component/_generated/api.d.ts.map +1 -1
  57. package/dist/component/_generated/api.js.map +1 -1
  58. package/dist/component/_generated/component.d.ts +988 -0
  59. package/dist/component/_generated/component.d.ts.map +1 -1
  60. package/dist/component/balanceFunding/helpers.d.ts +42 -0
  61. package/dist/component/balanceFunding/helpers.d.ts.map +1 -1
  62. package/dist/component/balanceFunding/internal/actions.d.ts +39 -0
  63. package/dist/component/balanceFunding/internal/actions.d.ts.map +1 -0
  64. package/dist/component/balanceFunding/internal/actions.js +80 -0
  65. package/dist/component/balanceFunding/internal/actions.js.map +1 -0
  66. package/dist/component/balanceFunding/internal/mutations.d.ts +55 -0
  67. package/dist/component/balanceFunding/internal/mutations.d.ts.map +1 -0
  68. package/dist/component/balanceFunding/internal/mutations.js +54 -0
  69. package/dist/component/balanceFunding/internal/mutations.js.map +1 -0
  70. package/dist/component/balanceFunding/internal/queries.d.ts +116 -0
  71. package/dist/component/balanceFunding/internal/queries.d.ts.map +1 -0
  72. package/dist/component/balanceFunding/internal/queries.js +35 -0
  73. package/dist/component/balanceFunding/internal/queries.js.map +1 -0
  74. package/dist/component/balanceFunding/internal/workflow.d.ts +10 -0
  75. package/dist/component/balanceFunding/internal/workflow.d.ts.map +1 -0
  76. package/dist/component/balanceFunding/internal/workflow.js +31 -0
  77. package/dist/component/balanceFunding/internal/workflow.js.map +1 -0
  78. package/dist/component/balanceFunding/lifecycleHandlers.d.ts +126 -0
  79. package/dist/component/balanceFunding/lifecycleHandlers.d.ts.map +1 -1
  80. package/dist/component/balanceFunding/mutations.d.ts +419 -16
  81. package/dist/component/balanceFunding/mutations.d.ts.map +1 -1
  82. package/dist/component/balanceFunding/mutations.js +24 -0
  83. package/dist/component/balanceFunding/mutations.js.map +1 -1
  84. package/dist/component/balanceFunding/preparationHandlers.d.ts +42 -0
  85. package/dist/component/balanceFunding/preparationHandlers.d.ts.map +1 -1
  86. package/dist/component/balanceFunding/queries.d.ts +78 -0
  87. package/dist/component/balanceFunding/queries.d.ts.map +1 -1
  88. package/dist/component/balanceFunding/queries.js +35 -0
  89. package/dist/component/balanceFunding/queries.js.map +1 -1
  90. package/dist/component/balanceFunding/receipt.d.ts +5 -0
  91. package/dist/component/balanceFunding/receipt.d.ts.map +1 -0
  92. package/dist/component/balanceFunding/receipt.js +37 -0
  93. package/dist/component/balanceFunding/receipt.js.map +1 -0
  94. package/dist/component/balanceFunding/schema.d.ts +506 -2
  95. package/dist/component/balanceFunding/schema.d.ts.map +1 -1
  96. package/dist/component/balanceFunding/schema.js +2 -0
  97. package/dist/component/balanceFunding/schema.js.map +1 -1
  98. package/dist/component/balanceFunding/stateHandlers.d.ts +42 -0
  99. package/dist/component/balanceFunding/stateHandlers.d.ts.map +1 -1
  100. package/dist/component/balanceFunding/workflowControl.d.ts +19 -0
  101. package/dist/component/balanceFunding/workflowControl.d.ts.map +1 -0
  102. package/dist/component/balanceFunding/workflowControl.js +149 -0
  103. package/dist/component/balanceFunding/workflowControl.js.map +1 -0
  104. package/dist/component/balanceFunding/workflowSchema.d.ts +356 -0
  105. package/dist/component/balanceFunding/workflowSchema.d.ts.map +1 -0
  106. package/dist/component/balanceFunding/workflowSchema.js +48 -0
  107. package/dist/component/balanceFunding/workflowSchema.js.map +1 -0
  108. package/dist/component/balanceFunding/workflowState.d.ts +157 -0
  109. package/dist/component/balanceFunding/workflowState.d.ts.map +1 -0
  110. package/dist/component/balanceFunding/workflowState.js +194 -0
  111. package/dist/component/balanceFunding/workflowState.js.map +1 -0
  112. package/dist/component/balanceFunding/workflowValidators.d.ts +306 -0
  113. package/dist/component/balanceFunding/workflowValidators.d.ts.map +1 -0
  114. package/dist/component/balanceFunding/workflowValidators.js +27 -0
  115. package/dist/component/balanceFunding/workflowValidators.js.map +1 -0
  116. package/dist/component/earnings/triggers.d.ts +388 -2
  117. package/dist/component/earnings/triggers.d.ts.map +1 -1
  118. package/dist/component/payoutRelease/queries.d.ts +2 -2
  119. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  120. package/dist/component/processingCostReconciliation/adoption.d.ts +29 -0
  121. package/dist/component/processingCostReconciliation/adoption.d.ts.map +1 -0
  122. package/dist/component/processingCostReconciliation/adoption.js +58 -0
  123. package/dist/component/processingCostReconciliation/adoption.js.map +1 -0
  124. package/dist/component/processingCostReconciliation/bridge.d.ts +45 -0
  125. package/dist/component/processingCostReconciliation/bridge.d.ts.map +1 -0
  126. package/dist/component/processingCostReconciliation/bridge.js +41 -0
  127. package/dist/component/processingCostReconciliation/bridge.js.map +1 -0
  128. package/dist/component/processingCostReconciliation/helpers.d.ts +327 -0
  129. package/dist/component/processingCostReconciliation/helpers.d.ts.map +1 -0
  130. package/dist/component/processingCostReconciliation/helpers.js +39 -0
  131. package/dist/component/processingCostReconciliation/helpers.js.map +1 -0
  132. package/dist/component/processingCostReconciliation/mutations.d.ts +303 -0
  133. package/dist/component/processingCostReconciliation/mutations.d.ts.map +1 -0
  134. package/dist/component/processingCostReconciliation/mutations.js +121 -0
  135. package/dist/component/processingCostReconciliation/mutations.js.map +1 -0
  136. package/dist/component/processingCostReconciliation/queries.d.ts +422 -0
  137. package/dist/component/processingCostReconciliation/queries.d.ts.map +1 -0
  138. package/dist/component/processingCostReconciliation/queries.js +19 -0
  139. package/dist/component/processingCostReconciliation/queries.js.map +1 -0
  140. package/dist/component/processingCostReconciliation/rollback.d.ts +21 -0
  141. package/dist/component/processingCostReconciliation/rollback.d.ts.map +1 -0
  142. package/dist/component/processingCostReconciliation/rollback.js +54 -0
  143. package/dist/component/processingCostReconciliation/rollback.js.map +1 -0
  144. package/dist/component/processingCostReconciliation/schema.d.ts +2460 -0
  145. package/dist/component/processingCostReconciliation/schema.d.ts.map +1 -0
  146. package/dist/component/processingCostReconciliation/schema.js +137 -0
  147. package/dist/component/processingCostReconciliation/schema.js.map +1 -0
  148. package/dist/component/processingCostReconciliation/steps.d.ts +306 -0
  149. package/dist/component/processingCostReconciliation/steps.d.ts.map +1 -0
  150. package/dist/component/processingCostReconciliation/steps.js +149 -0
  151. package/dist/component/processingCostReconciliation/steps.js.map +1 -0
  152. package/dist/component/processingCostReconciliation/validation.d.ts +13 -0
  153. package/dist/component/processingCostReconciliation/validation.d.ts.map +1 -0
  154. package/dist/component/processingCostReconciliation/validation.js +61 -0
  155. package/dist/component/processingCostReconciliation/validation.js.map +1 -0
  156. package/dist/component/processingCostReconciliation/workflow.d.ts +105 -0
  157. package/dist/component/processingCostReconciliation/workflow.d.ts.map +1 -0
  158. package/dist/component/processingCostReconciliation/workflow.js +33 -0
  159. package/dist/component/processingCostReconciliation/workflow.js.map +1 -0
  160. package/dist/component/schema.d.ts +694 -1
  161. package/dist/component/schema.js +2 -0
  162. package/dist/component/schema.js.map +1 -1
  163. package/package.json +1 -1
  164. package/src/client/balanceFundingWorkflow/capture.test.ts +144 -0
  165. package/src/client/balanceFundingWorkflow/client.ts +419 -0
  166. package/src/client/billing/balanceFunding.ts +50 -0
  167. package/src/client/costRecovery-invoices.ts +19 -4
  168. package/src/client/costRecovery.ts +4 -2
  169. package/src/client/index.ts +15 -0
  170. package/src/client/processingCostReconciliation/client.ts +75 -0
  171. package/src/client/processingCostReconciliation/contracts.ts +176 -0
  172. package/src/client/processingCostReconciliation/invoiceFunding.ts +270 -0
  173. package/src/client/processingCostReconciliation/invoiceProvider.test.ts +353 -0
  174. package/src/client/processingCostReconciliation/provider.test.ts +324 -0
  175. package/src/client/processingCostReconciliation/provider.ts +178 -0
  176. package/src/client/settlement/processingCostHistory.ts +31 -0
  177. package/src/client/settlement/processingCosts.ts +50 -15
  178. package/src/component/_generated/api.ts +36 -0
  179. package/src/component/_generated/component.ts +1094 -0
  180. package/src/component/balanceFunding/internal/actions.ts +107 -0
  181. package/src/component/balanceFunding/internal/mutations.ts +66 -0
  182. package/src/component/balanceFunding/internal/queries.ts +34 -0
  183. package/src/component/balanceFunding/internal/workflow.ts +49 -0
  184. package/src/component/balanceFunding/mutations.ts +34 -0
  185. package/src/component/balanceFunding/queries.ts +38 -0
  186. package/src/component/balanceFunding/receipt.ts +48 -0
  187. package/src/component/balanceFunding/schema.ts +2 -0
  188. package/src/component/balanceFunding/scopeLookup.test.ts +94 -0
  189. package/src/component/balanceFunding/workflow.fixtures.ts +366 -0
  190. package/src/component/balanceFunding/workflow.test.ts +552 -0
  191. package/src/component/balanceFunding/workflowControl.ts +198 -0
  192. package/src/component/balanceFunding/workflowSchema.ts +78 -0
  193. package/src/component/balanceFunding/workflowState.ts +285 -0
  194. package/src/component/balanceFunding/workflowValidators.ts +40 -0
  195. package/src/component/processingCostReconciliation/adoption.test.ts +206 -0
  196. package/src/component/processingCostReconciliation/adoption.ts +77 -0
  197. package/src/component/processingCostReconciliation/bridge.ts +50 -0
  198. package/src/component/processingCostReconciliation/helpers.ts +55 -0
  199. package/src/component/processingCostReconciliation/invoice.test.ts +152 -0
  200. package/src/component/processingCostReconciliation/mutations.ts +148 -0
  201. package/src/component/processingCostReconciliation/queries.ts +18 -0
  202. package/src/component/processingCostReconciliation/rollback.ts +72 -0
  203. package/src/component/processingCostReconciliation/schema.ts +191 -0
  204. package/src/component/processingCostReconciliation/state.test.ts +407 -0
  205. package/src/component/processingCostReconciliation/steps.ts +168 -0
  206. package/src/component/processingCostReconciliation/validation.ts +79 -0
  207. package/src/component/processingCostReconciliation/workflow.test.ts +329 -0
  208. package/src/component/processingCostReconciliation/workflow.ts +50 -0
  209. package/src/component/schema.ts +2 -0
@@ -0,0 +1,178 @@
1
+ import { readCollectedInvoiceFunding } from "./invoiceFunding.js";
2
+ import Stripe from "stripe";
3
+ import type { Component, RunCtx } from "../helpers.js";
4
+ import { componentRef } from "../webhooks/helpers.js";
5
+ import { resolveVerifiedPaymentSource } from "../paymentSource/resolver.js";
6
+ import { getProcessingCostContext } from "../settlement/processingCosts.js";
7
+ import { ProcessingCostHistoryIncomplete } from "../settlement/processingCostHistory.js";
8
+ import { processingCostOutcome } from "../financialOutcomes/adapters.js";
9
+ import { costObservationSchema, processingCostRef } from "./contracts.js";
10
+ import type {
11
+ ProcessingCostProviderRequest,
12
+ ProcessingCostProviderReceipt,
13
+ ProcessingCostSnapshot,
14
+ } from "./contracts.js";
15
+ export type ProcessingCostProviderOptions = {
16
+ stripeSecretKey: string;
17
+ httpClient?: NonNullable<
18
+ ConstructorParameters<typeof Stripe>[1]
19
+ >["httpClient"];
20
+ };
21
+ export async function observeProcessingCostProvider(
22
+ component: Component,
23
+ ctx: RunCtx,
24
+ request: ProcessingCostProviderRequest,
25
+ options: ProcessingCostProviderOptions,
26
+ ): Promise<ProcessingCostProviderReceipt> {
27
+ const echo = {
28
+ ...processingCostRef(request),
29
+ providerApiVersion: request.providerApiVersion,
30
+ definitionVersion: request.definitionVersion,
31
+ };
32
+ const unknown = {
33
+ ...echo,
34
+ outcome: {
35
+ status: "unknown",
36
+ reason: "provider_outcome_unknown",
37
+ retryable: false,
38
+ },
39
+ } as const;
40
+ try {
41
+ const op = (await ctx.runQuery(
42
+ componentRef(component, "processingCostReconciliation/queries/snapshot"),
43
+ processingCostRef(request),
44
+ )) as ProcessingCostSnapshot;
45
+ if (
46
+ op.providerApiVersion !== request.providerApiVersion ||
47
+ op.definitionVersion !== request.definitionVersion ||
48
+ request.definitionVersion !== 1
49
+ )
50
+ return {
51
+ ...echo,
52
+ outcome: {
53
+ status: "conflict",
54
+ reason: "identity_changed",
55
+ retryable: false,
56
+ },
57
+ };
58
+ const stripe = new Stripe(options.stripeSecretKey, {
59
+ apiVersion: request.providerApiVersion as Stripe.LatestApiVersion,
60
+ maxNetworkRetries: 0,
61
+ ...(options.httpClient ? { httpClient: options.httpClient } : {}),
62
+ });
63
+ if (op.source.kind === "invoice_recovery") {
64
+ const funded = await readCollectedInvoiceFunding(
65
+ stripe,
66
+ component,
67
+ ctx,
68
+ op,
69
+ );
70
+ if (funded.status !== "ready") return { ...echo, outcome: funded };
71
+ const { cost, provenance } = funded.value;
72
+ const observed = costObservationSchema.safeParse({
73
+ paymentIntentId: cost.paymentIntentId,
74
+ sourceChargeId: cost.chargeId,
75
+ currency: cost.currency,
76
+ gross: cost.gross,
77
+ originalProcessingFee: cost.processingFee,
78
+ processingFee: Math.max(
79
+ 0,
80
+ cost.processingFee + cost.refundFeeAdjustment,
81
+ ),
82
+ disputeFees: Math.max(0, cost.disputeFeeAdjustment),
83
+ principalReturned: cost.principalReturned,
84
+ provenance,
85
+ });
86
+ return observed.success
87
+ ? { ...echo, outcome: { status: "ready", value: observed.data } }
88
+ : unknown;
89
+ }
90
+ const input = op.source;
91
+ const source = await resolveVerifiedPaymentSource(stripe, {
92
+ paymentIntentId: input.paymentIntentId,
93
+ gross: input.gross,
94
+ currency: op.currency,
95
+ customerAccountId: input.customerAccountId,
96
+ ...(input.invoiceId ? { invoiceId: input.invoiceId } : {}),
97
+ ...(input.subscriptionId ? { subscriptionId: input.subscriptionId } : {}),
98
+ ...(input.stripeAccountId
99
+ ? { stripeAccountId: input.stripeAccountId }
100
+ : {}),
101
+ maxHistoryPages: 5,
102
+ maxHistoryItems: 500,
103
+ });
104
+ if (source.status !== "ready") return { ...echo, outcome: source };
105
+ if (
106
+ (input.stripePriceId !== undefined &&
107
+ input.stripePriceId !== source.value.stripePriceId) ||
108
+ (input.stripeProductId !== undefined &&
109
+ input.stripeProductId !== source.value.stripeProductId)
110
+ )
111
+ return {
112
+ ...echo,
113
+ outcome: {
114
+ status: "conflict",
115
+ reason: "identity_changed",
116
+ retryable: false,
117
+ },
118
+ };
119
+ const cost = await getProcessingCostContext(stripe, {
120
+ paymentIntentId: input.paymentIntentId,
121
+ ...(input.stripeAccountId
122
+ ? { stripeAccountId: input.stripeAccountId }
123
+ : {}),
124
+ historyBounds: { maxPages: 5, maxItems: 500 },
125
+ });
126
+ if (cost.status !== "ready")
127
+ return { ...echo, outcome: processingCostOutcome(cost) };
128
+ if (
129
+ cost.chargeId !== source.value.chargeId ||
130
+ cost.paymentIntentId !== source.value.paymentIntentId ||
131
+ cost.currency !== source.value.currency ||
132
+ cost.gross !== source.value.gross ||
133
+ cost.processingFee !== source.value.processingFee
134
+ )
135
+ return {
136
+ ...echo,
137
+ outcome: {
138
+ status: "conflict",
139
+ reason: "identity_changed",
140
+ retryable: false,
141
+ },
142
+ };
143
+ const observed = costObservationSchema.safeParse({
144
+ paymentIntentId: cost.paymentIntentId,
145
+ sourceChargeId: cost.chargeId,
146
+ currency: cost.currency,
147
+ gross: cost.gross,
148
+ originalProcessingFee: cost.processingFee,
149
+ processingFee: Math.max(0, cost.processingFee + cost.refundFeeAdjustment),
150
+ disputeFees: Math.max(0, cost.disputeFeeAdjustment),
151
+ principalReturned: cost.principalReturned,
152
+ provenance: {
153
+ invoiceId: source.value.invoiceId,
154
+ invoicePaymentId: source.value.invoicePaymentId,
155
+ customerAccountId: source.value.customerAccountId,
156
+ ...(source.value.subscriptionId
157
+ ? { subscriptionId: source.value.subscriptionId }
158
+ : {}),
159
+ stripePriceId: source.value.stripePriceId,
160
+ stripeProductId: source.value.stripeProductId,
161
+ },
162
+ });
163
+ return observed.success
164
+ ? { ...echo, outcome: { status: "ready", value: observed.data } }
165
+ : unknown;
166
+ } catch (error) {
167
+ return error instanceof ProcessingCostHistoryIncomplete
168
+ ? {
169
+ ...echo,
170
+ outcome: {
171
+ status: "unknown",
172
+ reason: "history_incomplete",
173
+ retryable: false,
174
+ },
175
+ }
176
+ : unknown;
177
+ }
178
+ }
@@ -0,0 +1,31 @@
1
+ import type Stripe from "stripe";
2
+ import { completeHistory } from "../paymentSource/history.js";
3
+ export type ProcessingCostHistoryBounds = {
4
+ maxPages: number;
5
+ maxItems: number;
6
+ };
7
+ export class ProcessingCostHistoryIncomplete extends Error {
8
+ constructor() {
9
+ super("Processing-cost history is incomplete.");
10
+ }
11
+ }
12
+ export async function boundedCostHistory<T extends { id: string }>(
13
+ fetch: (
14
+ cursor: string | undefined,
15
+ limit: number,
16
+ ) => Promise<Stripe.ApiList<T>>,
17
+ bounds: ProcessingCostHistoryBounds,
18
+ ): Promise<T[]> {
19
+ if (
20
+ !Number.isSafeInteger(bounds.maxPages) ||
21
+ bounds.maxPages < 1 ||
22
+ bounds.maxPages > 20 ||
23
+ !Number.isSafeInteger(bounds.maxItems) ||
24
+ bounds.maxItems < 1 ||
25
+ bounds.maxItems > 2000
26
+ )
27
+ throw new RangeError("Invalid processing-cost history bounds.");
28
+ const result = await completeHistory(fetch, bounds.maxPages, bounds.maxItems);
29
+ if (result.status !== "complete") throw new ProcessingCostHistoryIncomplete();
30
+ return result.data;
31
+ }
@@ -1,9 +1,15 @@
1
+ import {
2
+ boundedCostHistory,
3
+ type ProcessingCostHistoryBounds,
4
+ } from "./processingCostHistory.js";
1
5
  import type Stripe from "stripe";
2
6
 
3
7
  export type ProcessingCostOptions = {
4
8
  /** Host authorizes the payment and optional connected-account context. */
5
9
  paymentIntentId: string;
6
10
  stripeAccountId?: string;
11
+ /** Optional complete-history budgets. Existing callers retain their pagination behavior. */
12
+ historyBounds?: ProcessingCostHistoryBounds;
7
13
  };
8
14
  export type ProcessingCostTransaction = {
9
15
  id: string;
@@ -114,6 +120,14 @@ export async function getProcessingCostContext(
114
120
  typeof value === "string"
115
121
  ? await stripe.balanceTransactions.retrieve(value, {}, requestOptions)
116
122
  : value;
123
+ if (
124
+ opts.historyBounds &&
125
+ (tx.object !== "balance_transaction" ||
126
+ !tx.id ||
127
+ id(tx.source) !== sourceId ||
128
+ (typeof value === "string" && tx.id !== value))
129
+ )
130
+ throw new Error("Processing-cost transaction identity does not match.");
117
131
  if (tx.currency !== currency) throw new NotReady("currency_mismatch");
118
132
  [tx.amount, tx.fee, tx.net].forEach(integer);
119
133
  if (integer(tx.amount - tx.fee) !== tx.net)
@@ -162,17 +176,26 @@ export async function getProcessingCostContext(
162
176
  if (gross < 0 || original.amount !== gross)
163
177
  throw new Error("Payment and processed charge amounts do not reconcile.");
164
178
  const refunds = new Map<string, ProcessingCostRecord>();
165
- for await (const refund of stripe.refunds.list(
166
- {
167
- payment_intent: payment.id,
168
- limit: 100,
169
- expand: [
170
- "data.balance_transaction",
171
- "data.failure_balance_transaction",
172
- ],
173
- },
174
- requestOptions,
175
- )) {
179
+ const refundParams = {
180
+ payment_intent: payment.id,
181
+ limit: 100,
182
+ expand: ["data.balance_transaction", "data.failure_balance_transaction"],
183
+ };
184
+ const refundRows = opts.historyBounds
185
+ ? await boundedCostHistory(
186
+ (cursor, limit) =>
187
+ stripe.refunds.list(
188
+ {
189
+ ...refundParams,
190
+ limit,
191
+ ...(cursor ? { starting_after: cursor } : {}),
192
+ },
193
+ requestOptions,
194
+ ),
195
+ opts.historyBounds,
196
+ )
197
+ : stripe.refunds.list(refundParams, requestOptions);
198
+ for await (const refund of refundRows) {
176
199
  if (
177
200
  id(refund.charge) !== charge.id ||
178
201
  (refund.payment_intent && id(refund.payment_intent) !== payment.id)
@@ -213,10 +236,22 @@ export async function getProcessingCostContext(
213
236
  });
214
237
  }
215
238
  const disputes = new Map<string, ProcessingCostRecord>();
216
- for await (const dispute of stripe.disputes.list(
217
- { payment_intent: payment.id, limit: 100 },
218
- requestOptions,
219
- )) {
239
+ const disputeParams = { payment_intent: payment.id, limit: 100 };
240
+ const disputeRows = opts.historyBounds
241
+ ? await boundedCostHistory(
242
+ (cursor, limit) =>
243
+ stripe.disputes.list(
244
+ {
245
+ ...disputeParams,
246
+ limit,
247
+ ...(cursor ? { starting_after: cursor } : {}),
248
+ },
249
+ requestOptions,
250
+ ),
251
+ opts.historyBounds,
252
+ )
253
+ : stripe.disputes.list(disputeParams, requestOptions);
254
+ for await (const dispute of disputeRows) {
220
255
  if (
221
256
  id(dispute.charge) !== charge.id ||
222
257
  (dispute.payment_intent && id(dispute.payment_intent) !== payment.id)
@@ -20,12 +20,21 @@ import type * as affiliateRecovery_mutations from "../affiliateRecovery/mutation
20
20
  import type * as affiliateRecovery_queries from "../affiliateRecovery/queries.js";
21
21
  import type * as balanceFunding_helpers from "../balanceFunding/helpers.js";
22
22
  import type * as balanceFunding_importHandler from "../balanceFunding/importHandler.js";
23
+ import type * as balanceFunding_internal_actions from "../balanceFunding/internal/actions.js";
24
+ import type * as balanceFunding_internal_mutations from "../balanceFunding/internal/mutations.js";
25
+ import type * as balanceFunding_internal_queries from "../balanceFunding/internal/queries.js";
26
+ import type * as balanceFunding_internal_workflow from "../balanceFunding/internal/workflow.js";
23
27
  import type * as balanceFunding_lifecycleHandlers from "../balanceFunding/lifecycleHandlers.js";
24
28
  import type * as balanceFunding_mutationValidators from "../balanceFunding/mutationValidators.js";
25
29
  import type * as balanceFunding_mutations from "../balanceFunding/mutations.js";
26
30
  import type * as balanceFunding_preparationHandlers from "../balanceFunding/preparationHandlers.js";
27
31
  import type * as balanceFunding_queries from "../balanceFunding/queries.js";
32
+ import type * as balanceFunding_receipt from "../balanceFunding/receipt.js";
28
33
  import type * as balanceFunding_stateHandlers from "../balanceFunding/stateHandlers.js";
34
+ import type * as balanceFunding_workflowControl from "../balanceFunding/workflowControl.js";
35
+ import type * as balanceFunding_workflowSchema from "../balanceFunding/workflowSchema.js";
36
+ import type * as balanceFunding_workflowState from "../balanceFunding/workflowState.js";
37
+ import type * as balanceFunding_workflowValidators from "../balanceFunding/workflowValidators.js";
29
38
  import type * as billing_mutations from "../billing/mutations.js";
30
39
  import type * as billing_queries from "../billing/queries.js";
31
40
  import type * as billing_validators from "../billing/validators.js";
@@ -116,6 +125,15 @@ import type * as payoutSync_validators from "../payoutSync/validators.js";
116
125
  import type * as payoutWorker_helpers from "../payoutWorker/helpers.js";
117
126
  import type * as payoutWorker_mutations from "../payoutWorker/mutations.js";
118
127
  import type * as payoutWorker_queries from "../payoutWorker/queries.js";
128
+ import type * as processingCostReconciliation_adoption from "../processingCostReconciliation/adoption.js";
129
+ import type * as processingCostReconciliation_bridge from "../processingCostReconciliation/bridge.js";
130
+ import type * as processingCostReconciliation_helpers from "../processingCostReconciliation/helpers.js";
131
+ import type * as processingCostReconciliation_mutations from "../processingCostReconciliation/mutations.js";
132
+ import type * as processingCostReconciliation_queries from "../processingCostReconciliation/queries.js";
133
+ import type * as processingCostReconciliation_rollback from "../processingCostReconciliation/rollback.js";
134
+ import type * as processingCostReconciliation_steps from "../processingCostReconciliation/steps.js";
135
+ import type * as processingCostReconciliation_validation from "../processingCostReconciliation/validation.js";
136
+ import type * as processingCostReconciliation_workflow from "../processingCostReconciliation/workflow.js";
119
137
  import type * as products_mutations from "../products/mutations.js";
120
138
  import type * as products_queries from "../products/queries.js";
121
139
  import type * as products_validators from "../products/validators.js";
@@ -160,12 +178,21 @@ const fullApi: ApiFromModules<{
160
178
  "affiliateRecovery/queries": typeof affiliateRecovery_queries;
161
179
  "balanceFunding/helpers": typeof balanceFunding_helpers;
162
180
  "balanceFunding/importHandler": typeof balanceFunding_importHandler;
181
+ "balanceFunding/internal/actions": typeof balanceFunding_internal_actions;
182
+ "balanceFunding/internal/mutations": typeof balanceFunding_internal_mutations;
183
+ "balanceFunding/internal/queries": typeof balanceFunding_internal_queries;
184
+ "balanceFunding/internal/workflow": typeof balanceFunding_internal_workflow;
163
185
  "balanceFunding/lifecycleHandlers": typeof balanceFunding_lifecycleHandlers;
164
186
  "balanceFunding/mutationValidators": typeof balanceFunding_mutationValidators;
165
187
  "balanceFunding/mutations": typeof balanceFunding_mutations;
166
188
  "balanceFunding/preparationHandlers": typeof balanceFunding_preparationHandlers;
167
189
  "balanceFunding/queries": typeof balanceFunding_queries;
190
+ "balanceFunding/receipt": typeof balanceFunding_receipt;
168
191
  "balanceFunding/stateHandlers": typeof balanceFunding_stateHandlers;
192
+ "balanceFunding/workflowControl": typeof balanceFunding_workflowControl;
193
+ "balanceFunding/workflowSchema": typeof balanceFunding_workflowSchema;
194
+ "balanceFunding/workflowState": typeof balanceFunding_workflowState;
195
+ "balanceFunding/workflowValidators": typeof balanceFunding_workflowValidators;
169
196
  "billing/mutations": typeof billing_mutations;
170
197
  "billing/queries": typeof billing_queries;
171
198
  "billing/validators": typeof billing_validators;
@@ -256,6 +283,15 @@ const fullApi: ApiFromModules<{
256
283
  "payoutWorker/helpers": typeof payoutWorker_helpers;
257
284
  "payoutWorker/mutations": typeof payoutWorker_mutations;
258
285
  "payoutWorker/queries": typeof payoutWorker_queries;
286
+ "processingCostReconciliation/adoption": typeof processingCostReconciliation_adoption;
287
+ "processingCostReconciliation/bridge": typeof processingCostReconciliation_bridge;
288
+ "processingCostReconciliation/helpers": typeof processingCostReconciliation_helpers;
289
+ "processingCostReconciliation/mutations": typeof processingCostReconciliation_mutations;
290
+ "processingCostReconciliation/queries": typeof processingCostReconciliation_queries;
291
+ "processingCostReconciliation/rollback": typeof processingCostReconciliation_rollback;
292
+ "processingCostReconciliation/steps": typeof processingCostReconciliation_steps;
293
+ "processingCostReconciliation/validation": typeof processingCostReconciliation_validation;
294
+ "processingCostReconciliation/workflow": typeof processingCostReconciliation_workflow;
259
295
  "products/mutations": typeof products_mutations;
260
296
  "products/queries": typeof products_queries;
261
297
  "products/validators": typeof products_validators;