@fantastic.dev/better-stripe 0.32.0 → 0.33.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 (51) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +7 -1
  3. package/dist/client/billing/checkoutPages.d.ts +26 -0
  4. package/dist/client/billing/checkoutPages.d.ts.map +1 -0
  5. package/dist/client/billing/checkoutPages.js +111 -0
  6. package/dist/client/billing/checkoutPages.js.map +1 -0
  7. package/dist/client/connect/refundContext.d.ts +6 -0
  8. package/dist/client/connect/refundContext.d.ts.map +1 -0
  9. package/dist/client/connect/refundContext.js +109 -0
  10. package/dist/client/connect/refundContext.js.map +1 -0
  11. package/dist/client/connect/refundContextHistory.d.ts +20 -0
  12. package/dist/client/connect/refundContextHistory.d.ts.map +1 -0
  13. package/dist/client/connect/refundContextHistory.js +55 -0
  14. package/dist/client/connect/refundContextHistory.js.map +1 -0
  15. package/dist/client/connect/refundContextTypes.d.ts +39 -0
  16. package/dist/client/connect/refundContextTypes.d.ts.map +1 -0
  17. package/dist/client/connect/refundContextTypes.js +15 -0
  18. package/dist/client/connect/refundContextTypes.js.map +1 -0
  19. package/dist/client/connect/refundTarget.d.ts +14 -0
  20. package/dist/client/connect/refundTarget.d.ts.map +1 -0
  21. package/dist/client/connect/refundTarget.js +17 -0
  22. package/dist/client/connect/refundTarget.js.map +1 -0
  23. package/dist/client/connect/refunds.d.ts +17 -1
  24. package/dist/client/connect/refunds.d.ts.map +1 -1
  25. package/dist/client/connect/refunds.js +7 -17
  26. package/dist/client/connect/refunds.js.map +1 -1
  27. package/dist/client/index.d.ts +2 -0
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js.map +1 -1
  30. package/dist/client/invoiceReads.d.ts +7 -1
  31. package/dist/client/invoiceReads.d.ts.map +1 -1
  32. package/dist/client/invoiceReads.js +11 -1
  33. package/dist/client/invoiceReads.js.map +1 -1
  34. package/dist/component/payoutRelease/queries.d.ts +2 -2
  35. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  36. package/docs/checkout-session-pages.md +71 -0
  37. package/docs/read-contracts.md +28 -0
  38. package/docs/refund-context.md +84 -0
  39. package/package.json +4 -2
  40. package/src/client/billing/checkoutPages.test.ts +251 -0
  41. package/src/client/billing/checkoutPages.ts +135 -0
  42. package/src/client/connect/refundContext.ts +140 -0
  43. package/src/client/connect/refundContextHistory.ts +84 -0
  44. package/src/client/connect/refundContextTypes.ts +56 -0
  45. package/src/client/connect/refundTarget.ts +39 -0
  46. package/src/client/connect/refunds.test.ts +72 -0
  47. package/src/client/connect/refunds.ts +8 -22
  48. package/src/client/index.ts +9 -0
  49. package/src/client/invoiceReads.ts +17 -1
  50. package/src/client/refund-context-shapes.test.ts +127 -0
  51. package/src/client/refund-context.test.ts +404 -0
package/CHANGELOG.md CHANGED
@@ -26,6 +26,13 @@
26
26
  - Remove internal tracker references from public documentation and add a
27
27
  regression check that prevents their reintroduction.
28
28
 
29
+ ## [0.33.0](https://github.com/FantasticDevHQ/better-stripe/compare/v0.32.0...v0.33.0) (2026-10-16)
30
+
31
+ ### Features
32
+
33
+ - **billing:** expose bounded authorized refund context ([6f4f264](https://github.com/FantasticDevHQ/better-stripe/commit/6f4f264658d48c478165a4e3f3aaa83e6a6cf94b))
34
+ - **billing:** expose bounded buyer Checkout session pages ([f6cb1a6](https://github.com/FantasticDevHQ/better-stripe/commit/f6cb1a64139235ee65f907c3577e4aac75cff96c))
35
+
29
36
  ## [0.32.0](https://github.com/FantasticDevHQ/better-stripe/compare/v0.31.0...v0.32.0) (2026-10-16)
30
37
 
31
38
  ### Features
package/README.md CHANGED
@@ -70,7 +70,7 @@ claim execution are completed.
70
70
 
71
71
  ## Status
72
72
 
73
- **Version**: `0.32.0` <!-- x-release-please-version --> (pre-1.0). API may change between minor versions until 1.0.
73
+ **Version**: `0.33.0` <!-- x-release-please-version --> (pre-1.0). API may change between minor versions until 1.0.
74
74
 
75
75
  Used in production by its authors. Webhook pipeline is covered by unit tests and a live E2E harness (`npm run e2e:webhooks`) that fires real Stripe-signed events and asserts the ledger.
76
76
 
@@ -1754,6 +1754,12 @@ See the [read contract guide](docs/read-contracts.md) for reactive product, pric
1754
1754
  cursors, exact currency summaries, legacy backfill, payout failure details, and
1755
1755
  published-version adoption requirements.
1756
1756
 
1757
+ For buyer purchase-history discovery, use the public invoice and
1758
+ [Checkout-session page readers](docs/checkout-session-pages.md). Checkout pages
1759
+ also reveal open/asynchronous sessions with no invoice. They preserve raw
1760
+ references and require host authorization, continuation checkpoints and
1761
+ historical reconciliation; they do not attest complete history or award credit.
1762
+
1757
1763
  Custom recipient payout minimums are waived during the final day before the verified holding deadline. Provider limits and all financial checks still apply. See [settlement and deadline handling](docs/settlement.md) for worker cadence, retries and expiry limitations.
1758
1764
 
1759
1765
  Holding-expiry recovery remains visible and retryable after schedule restoration. The package preserves unresolved provider balances and payout claims, and prevents reset or custom reactivation from erasing them. See [expiry recovery](docs/settlement.md) for operator handling.
@@ -0,0 +1,26 @@
1
+ import type Stripe from "stripe";
2
+ import { z } from "zod";
3
+ declare const optionsSchema: z.ZodObject<{
4
+ customerAccount: z.ZodString;
5
+ status: z.ZodOptional<z.ZodEnum<{
6
+ complete: "complete";
7
+ expired: "expired";
8
+ open: "open";
9
+ }>>;
10
+ created: z.ZodOptional<z.ZodObject<{
11
+ gte: z.ZodOptional<z.ZodNumber>;
12
+ lte: z.ZodOptional<z.ZodNumber>;
13
+ }, z.core.$strict>>;
14
+ limit: z.ZodDefault<z.ZodNumber>;
15
+ cursor: z.ZodOptional<z.ZodNullable<z.ZodString>>;
16
+ }, z.core.$strict>;
17
+ export type CheckoutSessionPageOptions = z.input<typeof optionsSchema>;
18
+ export type ProviderCheckoutSessionPage = {
19
+ page: Stripe.Checkout.Session[];
20
+ hasMore: boolean;
21
+ continueCursor: string | null;
22
+ };
23
+ /** Provider discovery only. Hosts authorize buyers and reconcile mutable history and gaps. */
24
+ export declare function getCheckoutSessionPageFromStripe(stripe: Stripe, input: CheckoutSessionPageOptions): Promise<ProviderCheckoutSessionPage>;
25
+ export {};
26
+ //# sourceMappingURL=checkoutPages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkoutPages.d.ts","sourceRoot":"","sources":["../../../src/client/billing/checkoutPages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgBxB,QAAA,MAAM,aAAa;;;;;;;;;;;;;kBAKR,CAAC;AAsBZ,MAAM,MAAM,0BAA0B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AACvE,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AA0BF,8FAA8F;AAC9F,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,0BAA0B,GAChC,OAAO,CAAC,2BAA2B,CAAC,CAuDtC"}
@@ -0,0 +1,111 @@
1
+ import { z } from "zod";
2
+ const time = z.number().int().nonnegative().safe();
3
+ const scopeSchema = z
4
+ .object({
5
+ customerAccount: z
6
+ .string()
7
+ .max(128)
8
+ .regex(/^acct_[A-Za-z0-9_]+$/),
9
+ status: z.enum(["open", "complete", "expired"]).optional(),
10
+ created: z
11
+ .object({ gte: time.optional(), lte: time.optional() })
12
+ .strict()
13
+ .optional(),
14
+ })
15
+ .strict();
16
+ const optionsSchema = scopeSchema
17
+ .extend({
18
+ limit: z.number().int().min(1).max(100).default(25),
19
+ cursor: z.string().min(1).max(2048).nullable().optional(),
20
+ })
21
+ .strict();
22
+ const sessionId = z
23
+ .string()
24
+ .max(128)
25
+ .regex(/^cs_[A-Za-z0-9_]+$/);
26
+ const cursorSchema = z
27
+ .object({
28
+ version: z.literal(1),
29
+ scope: scopeSchema,
30
+ after: sessionId,
31
+ })
32
+ .strict();
33
+ const identitySchema = z
34
+ .object({
35
+ id: sessionId,
36
+ object: z.literal("checkout.session"),
37
+ customer_account: z.string(),
38
+ customer: z.null().optional(),
39
+ created: time,
40
+ })
41
+ .passthrough();
42
+ function encodeCursor(scope, after) {
43
+ return btoa(JSON.stringify({ version: 1, scope, after }))
44
+ .replaceAll("+", "-")
45
+ .replaceAll("/", "_")
46
+ .replace(/=+$/, "");
47
+ }
48
+ function decodeCursor(cursor, scope) {
49
+ try {
50
+ if (!/^[A-Za-z0-9_-]+$/.test(cursor))
51
+ throw new Error();
52
+ const parsed = cursorSchema.parse(JSON.parse(atob(cursor.replaceAll("-", "+").replaceAll("_", "/"))));
53
+ if (JSON.stringify(parsed.scope) !== JSON.stringify(scope) ||
54
+ encodeCursor(parsed.scope, parsed.after) !== cursor)
55
+ throw new Error();
56
+ return parsed.after;
57
+ }
58
+ catch {
59
+ throw new Error("Invalid Checkout page cursor or scope");
60
+ }
61
+ }
62
+ /** Provider discovery only. Hosts authorize buyers and reconcile mutable history and gaps. */
63
+ export async function getCheckoutSessionPageFromStripe(stripe, input) {
64
+ const { limit, cursor, ...scope } = optionsSchema.parse(input);
65
+ if (scope.created?.gte !== undefined &&
66
+ scope.created.lte !== undefined &&
67
+ scope.created.gte > scope.created.lte)
68
+ throw new Error("Invalid Checkout page time window");
69
+ if (scope.created &&
70
+ scope.created.gte === undefined &&
71
+ scope.created.lte === undefined)
72
+ delete scope.created;
73
+ const after = cursor ? decodeCursor(cursor, scope) : undefined;
74
+ const response = await stripe.checkout.sessions.list({
75
+ customer_account: scope.customerAccount,
76
+ limit,
77
+ ...(scope.status ? { status: scope.status } : {}),
78
+ ...(scope.created ? { created: scope.created } : {}),
79
+ ...(after ? { starting_after: after } : {}),
80
+ });
81
+ if (!Array.isArray(response.data) ||
82
+ typeof response.has_more !== "boolean" ||
83
+ response.data.length > limit)
84
+ throw new Error("Invalid provider Checkout page");
85
+ const seen = new Set();
86
+ for (const row of response.data) {
87
+ const parsed = identitySchema.safeParse(row);
88
+ if (!parsed.success ||
89
+ parsed.data.customer_account !== scope.customerAccount ||
90
+ (scope.status !== undefined && row.status !== scope.status) ||
91
+ (scope.created?.gte !== undefined &&
92
+ parsed.data.created < scope.created.gte) ||
93
+ (scope.created?.lte !== undefined &&
94
+ parsed.data.created > scope.created.lte))
95
+ throw new Error("Invalid provider Checkout page buyer scope or identity");
96
+ if (seen.has(row.id))
97
+ throw new Error("Duplicate provider Checkout page entry");
98
+ if (row.id === after)
99
+ throw new Error("Provider Checkout page has no advancing continuation");
100
+ seen.add(row.id);
101
+ }
102
+ const last = response.data[response.data.length - 1]?.id;
103
+ if (response.has_more && !last)
104
+ throw new Error("Provider Checkout page has no advancing continuation");
105
+ return {
106
+ page: response.data,
107
+ hasMore: response.has_more,
108
+ continueCursor: response.has_more ? encodeCursor(scope, last) : null,
109
+ };
110
+ }
111
+ //# sourceMappingURL=checkoutPages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkoutPages.js","sourceRoot":"","sources":["../../../src/client/billing/checkoutPages.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC;AACnD,MAAM,WAAW,GAAG,CAAC;KAClB,MAAM,CAAC;IACN,eAAe,EAAE,CAAC;SACf,MAAM,EAAE;SACR,GAAG,CAAC,GAAG,CAAC;SACR,KAAK,CAAC,sBAAsB,CAAC;IAChC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,EAAE;IAC1D,OAAO,EAAE,CAAC;SACP,MAAM,CAAC,EAAE,GAAG,EAAE,IA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;KACtE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type Stripe from "stripe";
2
+ import type { Component, RunCtx } from "../helpers.js";
3
+ import { type RefundContextOptions, type RefundContext } from "./refundContextTypes.js";
4
+ /** Observes one authorized original payment; hosts still own community scope and creation-time revalidation. */
5
+ export declare function getRefundContext(stripe: Stripe, component: Component, ctx: RunCtx, input: RefundContextOptions): Promise<RefundContext>;
6
+ //# sourceMappingURL=refundContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContext.d.ts","sourceRoot":"","sources":["../../../src/client/connect/refundContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAUvD,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAEnB,MAAM,yBAAyB,CAAC;AAiCjC,gHAAgH;AAChH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,EACpB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,aAAa,CAAC,CAoFxB"}
@@ -0,0 +1,109 @@
1
+ import { componentRef } from "../webhooks/helpers.js";
2
+ import { assertRefundAuthorized } from "./refunds.js";
3
+ import { readRefundPayment, readRefundCharge, refundObjectId, } from "./refundTarget.js";
4
+ import { readRefundHistory } from "./refundContextHistory.js";
5
+ import { refundContextOptionsSchema, } from "./refundContextTypes.js";
6
+ const unavailable = (reason) => ({ status: "unavailable", reason });
7
+ function amountsAgree(payment, charge) {
8
+ const amounts = [
9
+ payment.amount,
10
+ payment.amount_received,
11
+ charge.amount,
12
+ charge.amount_captured,
13
+ charge.amount_refunded,
14
+ ];
15
+ return (amounts.every((n) => Number.isSafeInteger(n) && n >= 0) &&
16
+ charge.amount_captured === payment.amount_received &&
17
+ charge.amount_captured <= charge.amount &&
18
+ charge.amount_captured <= payment.amount &&
19
+ charge.amount_refunded <= charge.amount_captured);
20
+ }
21
+ function canonicalCustomer(value) {
22
+ if (value === null)
23
+ return true;
24
+ if (typeof value === "string")
25
+ return /^cus_[A-Za-z0-9_]+$/.test(value);
26
+ return Boolean(value &&
27
+ value.object === "customer" &&
28
+ typeof value.id === "string" &&
29
+ /^cus_[A-Za-z0-9_]+$/.test(value.id));
30
+ }
31
+ /** Observes one authorized original payment; hosts still own community scope and creation-time revalidation. */
32
+ export async function getRefundContext(stripe, component, ctx, input) {
33
+ const opts = refundContextOptionsSchema.parse(input);
34
+ const original = (await assertRefundAuthorized(stripe, component, ctx, opts)) ??
35
+ (await ctx.runQuery(componentRef(component, "connect/queries/getPaymentByStripeId"), { stripePaymentIntentId: opts.stripePaymentIntentId }));
36
+ if (!original)
37
+ return unavailable("payment_unavailable");
38
+ if (original.stripePaymentIntentId !== opts.stripePaymentIntentId)
39
+ return unavailable("payment_scope_mismatch");
40
+ const buyer = original.accountId;
41
+ if (!buyer || !/^acct_[A-Za-z0-9_]+$/.test(buyer))
42
+ return unavailable("buyer_unavailable");
43
+ const payment = await readRefundPayment(stripe, opts);
44
+ if (!payment ||
45
+ payment.object !== "payment_intent" ||
46
+ payment.id !== opts.stripePaymentIntentId)
47
+ return unavailable("payment_scope_mismatch");
48
+ if (!canonicalCustomer(payment.customer))
49
+ return unavailable("buyer_mismatch");
50
+ if (payment.customer_account !== buyer)
51
+ return unavailable("buyer_mismatch");
52
+ if (payment.status !== "succeeded")
53
+ return unavailable("payment_not_succeeded");
54
+ if (!/^[a-z]{3}$/.test(payment.currency) ||
55
+ payment.currency !== original.currency)
56
+ return unavailable("currency_mismatch");
57
+ const chargeId = refundObjectId(payment.latest_charge);
58
+ if (!chargeId || !/^ch_[A-Za-z0-9_]+$/.test(chargeId))
59
+ return unavailable("charge_unavailable");
60
+ const charge = await readRefundCharge(stripe, opts, payment);
61
+ if (!charge ||
62
+ charge.id !== chargeId ||
63
+ refundObjectId(charge.payment_intent) !== payment.id)
64
+ return unavailable("charge_scope_mismatch");
65
+ if (charge.object !== "charge" ||
66
+ charge.status !== "succeeded" ||
67
+ typeof charge.paid !== "boolean" ||
68
+ typeof charge.captured !== "boolean" ||
69
+ typeof charge.disputed !== "boolean" ||
70
+ typeof charge.refunded !== "boolean")
71
+ return unavailable("charge_scope_mismatch");
72
+ if (!canonicalCustomer(charge.customer))
73
+ return unavailable("buyer_mismatch");
74
+ if (refundObjectId(charge.customer) !== refundObjectId(payment.customer))
75
+ return unavailable("buyer_mismatch");
76
+ if (charge.currency !== payment.currency)
77
+ return unavailable("currency_mismatch");
78
+ if (!amountsAgree(payment, charge))
79
+ return unavailable("amount_unavailable");
80
+ if (!charge.paid || !charge.captured || charge.amount_captured === 0)
81
+ return unavailable("charge_not_captured");
82
+ if (charge.disputed)
83
+ return unavailable("charge_disputed");
84
+ const history = await readRefundHistory(stripe, {
85
+ paymentIntentId: payment.id,
86
+ chargeId: charge.id,
87
+ currency: payment.currency,
88
+ capturedAmount: charge.amount_captured,
89
+ maxRefundPages: opts.maxRefundPages,
90
+ stripeAccountId: opts.stripeAccountId,
91
+ });
92
+ if (history.status !== "complete")
93
+ return history;
94
+ if (history.refundedAmount !== charge.amount_refunded ||
95
+ charge.refunded !== (history.refundedAmount === charge.amount_captured))
96
+ return unavailable("refund_counter_mismatch");
97
+ return {
98
+ status: "ready",
99
+ stripePaymentIntentId: payment.id,
100
+ stripeChargeId: charge.id,
101
+ customerAccount: buyer,
102
+ currency: payment.currency,
103
+ capturedAmount: charge.amount_captured,
104
+ refundedAmount: history.refundedAmount,
105
+ remainingRefundableAmount: charge.amount_captured - history.refundedAmount,
106
+ observedAt: Date.now(),
107
+ };
108
+ }
109
+ //# sourceMappingURL=refundContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContext.js","sourceRoot":"","sources":["../../../src/client/connect/refundContext.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAw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AE,OAAO,CAAC,cAAc;QACtC,yBAAyB,EAAE,MAAM,CAAC,eAAe,GAAG,OAAO,CAAC,cAAc;QAC1E,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;KACvB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type Stripe from "stripe";
2
+ import type { RefundContextUnavailableReason } from "./refundContextTypes.js";
3
+ type HistoryResult = {
4
+ status: "complete";
5
+ refundedAmount: number;
6
+ } | {
7
+ status: "unavailable";
8
+ reason: RefundContextUnavailableReason;
9
+ };
10
+ /** All bounded pages must agree with the captured charge; loaded previews cannot establish a remainder. */
11
+ export declare function readRefundHistory(stripe: Stripe, opts: {
12
+ paymentIntentId: string;
13
+ chargeId: string;
14
+ currency: string;
15
+ capturedAmount: number;
16
+ maxRefundPages: number;
17
+ stripeAccountId?: string;
18
+ }): Promise<HistoryResult>;
19
+ export {};
20
+ //# sourceMappingURL=refundContextHistory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContextHistory.d.ts","sourceRoot":"","sources":["../../../src/client/connect/refundContextHistory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AAE9E,KAAK,aAAa,GACd;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,MAAM,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,8BAA8B,CAAA;CAAE,CAAC;AAKtE,2GAA2G;AAC3G,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IACJ,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,GACA,OAAO,CAAC,aAAa,CAAC,CA6DxB"}
@@ -0,0 +1,55 @@
1
+ import { refundObjectId } from "./refundTarget.js";
2
+ const unavailable = (reason) => ({ status: "unavailable", reason });
3
+ /** All bounded pages must agree with the captured charge; loaded previews cannot establish a remainder. */
4
+ export async function readRefundHistory(stripe, opts) {
5
+ let refundedAmount = 0;
6
+ let cursor;
7
+ const seen = new Set();
8
+ for (let count = 0; count < opts.maxRefundPages; count++) {
9
+ const page = await stripe.refunds.list({
10
+ payment_intent: opts.paymentIntentId,
11
+ limit: 100,
12
+ ...(cursor ? { starting_after: cursor } : {}),
13
+ }, opts.stripeAccountId
14
+ ? { stripeAccount: opts.stripeAccountId }
15
+ : undefined);
16
+ if (!page ||
17
+ page.object !== "list" ||
18
+ !Array.isArray(page.data) ||
19
+ page.data.length > 100 ||
20
+ typeof page.has_more !== "boolean" ||
21
+ (page.has_more && !page.data.length))
22
+ return unavailable("refund_history_incomplete");
23
+ for (const refund of page.data) {
24
+ if (!refund ||
25
+ refund.object !== "refund" ||
26
+ typeof refund.id !== "string" ||
27
+ !/^re_[A-Za-z0-9_]+$/.test(refund.id) ||
28
+ seen.has(refund.id))
29
+ return unavailable("refund_history_incomplete");
30
+ seen.add(refund.id);
31
+ if (refundObjectId(refund.charge) !== opts.chargeId ||
32
+ refundObjectId(refund.payment_intent) !== opts.paymentIntentId ||
33
+ refund.currency !== opts.currency ||
34
+ !Number.isSafeInteger(refund.amount) ||
35
+ refund.amount < 0)
36
+ return unavailable("refund_scope_mismatch");
37
+ if (refund.status === "pending" || refund.status === "requires_action")
38
+ return unavailable("refund_pending");
39
+ if (refund.status !== "succeeded" &&
40
+ refund.status !== "failed" &&
41
+ refund.status !== "canceled")
42
+ return unavailable("refund_state_unknown");
43
+ if (refund.status === "succeeded")
44
+ refundedAmount += refund.amount;
45
+ if (!Number.isSafeInteger(refundedAmount) ||
46
+ refundedAmount > opts.capturedAmount)
47
+ return unavailable("refund_counter_mismatch");
48
+ }
49
+ if (!page.has_more)
50
+ return { status: "complete", refundedAmount };
51
+ cursor = page.data[page.data.length - 1].id;
52
+ }
53
+ return unavailable("refund_history_incomplete");
54
+ }
55
+ //# sourceMappingURL=refundContextHistory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContextHistory.js","sourceRoot":"","sources":["../../../src/client/connect/refundContextHistory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAMnD,MAAM,WAAW,GAAG,CAClB,MAAsC,EACvB,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAC;AAExD,2GAA2G;AAC3G,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,MAAc,EACd,IAOC;IAED,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,IAAI,MAA0B,CAAC;IAC/B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,cAAc,EAAE,KAAK,EAAE,EAAE,CAAC;QACzD,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,IAAI,CACpC;YACE,cAAc,EAAE,IAAI,CAAC,eAAe;YACpC,KAAK,EAAE,GAAG;YACV,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC9C,EACD,IAAI,CAAC,eAAe;YAClB,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,eAAe,EAAE;YACzC,CAAC,CAAC,SAAS,CACd,CAAC;QACF,IACE,CAAC,IAAI;YACL,IAAI,CAAC,MAAM,KAAK,MAAM;YACtB,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;YACzB,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,GAAG;YACtB,O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gBAAE,cAAc,IAAI,MAAM,CAAC,MAAM,CAAC;YACnE,IACE,CAAC,MAAM,CAAC,aAAa,CAAC,cAAc,CAAC;gBACrC,cAAc,GAAG,IAAI,CAAC,cAAc;gBAEpC,OAAO,WAAW,CAAC,yBAAyB,CAAC,CAAC;QAClD,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC;QAClE,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC,EAAE,CAAC;IAC/C,CAAC;IACD,OAAO,WAAW,CAAC,2BAA2B,CAAC,CAAC;AAClD,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { z } from "zod";
2
+ import type { RefundActor } from "./refundActor.js";
3
+ /** New observations require an explicit actor; legacy createRefund omission is not inherited. */
4
+ export declare const refundContextOptionsSchema: z.ZodObject<{
5
+ stripePaymentIntentId: z.ZodString;
6
+ actor: z.ZodDiscriminatedUnion<[z.ZodObject<{
7
+ type: z.ZodLiteral<"admin">;
8
+ }, z.core.$strict>, z.ZodObject<{
9
+ type: z.ZodLiteral<"seller">;
10
+ accountId: z.ZodString;
11
+ }, z.core.$strict>], "type">;
12
+ stripeAccountId: z.ZodOptional<z.ZodString>;
13
+ maxRefundPages: z.ZodDefault<z.ZodNumber>;
14
+ }, z.core.$strict>;
15
+ export type RefundContextOptions = {
16
+ stripePaymentIntentId: string;
17
+ actor: RefundActor;
18
+ /** Server-owned provider request scope, distinct from the buyer and merchant accounts. */
19
+ stripeAccountId?: string;
20
+ /** Complete bounded history or unavailable, never a partial refund balance. Default 5, maximum 10. */
21
+ maxRefundPages?: number;
22
+ };
23
+ export type RefundContextUnavailableReason = "payment_unavailable" | "buyer_unavailable" | "payment_scope_mismatch" | "payment_not_succeeded" | "charge_unavailable" | "charge_scope_mismatch" | "buyer_mismatch" | "currency_mismatch" | "amount_unavailable" | "charge_not_captured" | "charge_disputed" | "refund_pending" | "refund_state_unknown" | "refund_history_incomplete" | "refund_scope_mismatch" | "refund_counter_mismatch";
24
+ export type RefundContext = {
25
+ status: "unavailable";
26
+ reason: RefundContextUnavailableReason;
27
+ } | {
28
+ status: "ready";
29
+ stripePaymentIntentId: string;
30
+ stripeChargeId: string;
31
+ customerAccount: string;
32
+ currency: string;
33
+ capturedAmount: number;
34
+ refundedAmount: number;
35
+ remainingRefundableAmount: number;
36
+ /** Milliseconds; an observation, not a reservation or immutable snapshot. */
37
+ observedAt: number;
38
+ };
39
+ //# sourceMappingURL=refundContextTypes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContextTypes.d.ts","sourceRoot":"","sources":["../../../src/client/connect/refundContextTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD,iGAAiG;AACjG,eAAO,MAAM,0BAA0B;;;;;;;;;;kBAU5B,CAAC;AAEZ,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,KAAK,EAAE,WAAW,CAAC;IACnB,0FAA0F;IAC1F,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,sGAAsG;IACtG,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AACF,MAAM,MAAM,8BAA8B,GACtC,qBAAqB,GACrB,mBAAmB,GACnB,wBAAwB,GACxB,uBAAuB,GACvB,oBAAoB,GACpB,uBAAuB,GACvB,gBAAgB,GAChB,mBAAmB,GACnB,oBAAoB,GACpB,qBAAqB,GACrB,iBAAiB,GACjB,gBAAgB,GAChB,sBAAsB,GACtB,2BAA2B,GAC3B,uBAAuB,GACvB,yBAAyB,CAAC;AAC9B,MAAM,MAAM,aAAa,GACrB;IAAE,MAAM,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,8BAA8B,CAAA;CAAE,GACjE;IACE,MAAM,EAAE,OAAO,CAAC;IAChB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,yBAAyB,EAAE,MAAM,CAAC;IAClC,6EAA6E;IAC7E,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { z } from "zod";
2
+ const accountId = z.string().regex(/^acct_[A-Za-z0-9_]+$/);
3
+ /** New observations require an explicit actor; legacy createRefund omission is not inherited. */
4
+ export const refundContextOptionsSchema = z
5
+ .object({
6
+ stripePaymentIntentId: z.string().regex(/^pi_[A-Za-z0-9_]+$/),
7
+ actor: z.discriminatedUnion("type", [
8
+ z.object({ type: z.literal("admin") }).strict(),
9
+ z.object({ type: z.literal("seller"), accountId }).strict(),
10
+ ]),
11
+ stripeAccountId: accountId.optional(),
12
+ maxRefundPages: z.number().int().min(1).max(10).default(5),
13
+ })
14
+ .strict();
15
+ //# sourceMappingURL=refundContextTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundContextTypes.js","sourceRoot":"","sources":["../../../src/client/connect/refundContextTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;AAC3D,iGAAiG;AACjG,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC;KACxC,MAAM,CAAC;IACN,qBAAqB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,oBAAoB,CAAC;IAC7D,KAAK,EAAE,CAAC,CAAC,kBAAkB,CAAC,MAAM,EAAE;QAClC,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE;QAC/C,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KAC5D,CAAC;IACF,eAAe,EAAE,SAAS,CAAC,QAAQ,EAAE;IACrC,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;CAC3D,CAAC;KACD,MAAM,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type Stripe from "stripe";
2
+ export declare function refundObjectId(value: string | {
3
+ id: string;
4
+ } | null | undefined): string | undefined;
5
+ export type RefundTargetOptions = {
6
+ stripePaymentIntentId?: string;
7
+ stripeChargeId?: string;
8
+ stripeAccountId?: string;
9
+ };
10
+ /** Shared target resolution; caller authorization and observation validation remain separate. */
11
+ export declare function readRefundPayment(stripe: Stripe, opts: RefundTargetOptions): Promise<Stripe.Response<Stripe.PaymentIntent> | null>;
12
+ /** Resolve only the selected original charge; does not create refunds or inspect fees. */
13
+ export declare function readRefundCharge(stripe: Stripe, opts: RefundTargetOptions, payment: Stripe.PaymentIntent | null): Promise<Stripe.Response<Stripe.Charge> | null>;
14
+ //# sourceMappingURL=refundTarget.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundTarget.d.ts","sourceRoot":"","sources":["../../../src/client/connect/refundTarget.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,sBAGlD;AACD,MAAM,MAAM,mBAAmB,GAAG;IAChC,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AACF,iGAAiG;AACjG,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,mBAAmB,yDAQ1B;AACD,0FAA0F;AAC1F,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,mBAAmB,EACzB,OAAO,EAAE,MAAM,CAAC,aAAa,GAAG,IAAI,kDAUrC"}
@@ -0,0 +1,17 @@
1
+ export function refundObjectId(value) {
2
+ return typeof value === "string" ? value : value?.id;
3
+ }
4
+ /** Shared target resolution; caller authorization and observation validation remain separate. */
5
+ export async function readRefundPayment(stripe, opts) {
6
+ if (!opts.stripePaymentIntentId)
7
+ return null;
8
+ return stripe.paymentIntents.retrieve(opts.stripePaymentIntentId, undefined, opts.stripeAccountId ? { stripeAccount: opts.stripeAccountId } : undefined);
9
+ }
10
+ /** Resolve only the selected original charge; does not create refunds or inspect fees. */
11
+ export async function readRefundCharge(stripe, opts, payment) {
12
+ const chargeId = opts.stripeChargeId ?? refundObjectId(payment?.latest_charge);
13
+ if (!chargeId)
14
+ return null;
15
+ return stripe.charges.retrieve(chargeId, undefined, opts.stripeAccountId ? { stripeAccount: opts.stripeAccountId } : undefined);
16
+ }
17
+ //# sourceMappingURL=refundTarget.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refundTarget.js","sourceRoot":"","sources":["../../../src/client/connect/refundTarget.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,cAAc,CAC5B,KAAiD;IAEjD,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC;AACvD,CAAC;AAMD,iGAAiG;AACjG,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,MAAc,EACd,IAAyB;IAEzB,IAAI,CAAC,IAAI,CAAC,qBAAqB;QAAE,OAAO,IAAI,CAAC;IAC7C,OAAO,MAAM,CAAC,cAAc,CAAC,QAAQ,CACnC,IAAI,CAAC,qBAAqB,EAC1B,SAAS,EACT,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,SAAS,CAC3E,CAAC;AACJ,CAAC;AACD,0FAA0F;AAC1F,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,MAAc,EACd,IAAyB,EACzB,OAAoC;IAEpC,MAAM,QAAQ,GACZ,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IAChE,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3B,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,CAC5B,QAAQ,EACR,SAAS,EACT,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,SAAS,CAC3E,CAAC;AACJ,CAAC"}
@@ -2,7 +2,7 @@ import type Stripe from "stripe";
2
2
  import type { PaginationOptions, PaginationResult } from "convex/server";
3
3
  import type { RefundStatus } from "../../component/connect/validators.js";
4
4
  import type { Component, RunCtx } from "../helpers.js";
5
- import type { StripeComponentRefund } from "../types.js";
5
+ import type { StripeComponentPayment, StripeComponentRefund } from "../types.js";
6
6
  import { type RefundActor } from "./refundActor.js";
7
7
  export type { RefundActor } from "./refundActor.js";
8
8
  export type RefundListOptions = {
@@ -75,6 +75,22 @@ export declare function createRefund(stripe: Stripe, component: Component, ctx:
75
75
  }): Promise<{
76
76
  stripeRefundId: string;
77
77
  }>;
78
+ /**
79
+ * Enforce refund actor scoping (BTS-35). Resolves the payment the refund
80
+ * targets from the component's `payments` ledger and rejects a seller-initiated
81
+ * refund unless that seller is the sale's merchant — the destination-charge
82
+ * seller or the split sale's `store` leg (the `role` is threaded through from
83
+ * the ledger's `splitRecipients`). Affiliate / `other` split legs are denied.
84
+ * Fails closed: if the target payment can't be resolved (no PaymentIntent, or
85
+ * no ledger row), a seller is denied rather than allowed. No-ops for admin /
86
+ * omitted actors.
87
+ */
88
+ export declare function assertRefundAuthorized(stripe: Stripe, component: Component, ctx: RunCtx, opts: {
89
+ stripePaymentIntentId?: string;
90
+ stripeChargeId?: string;
91
+ stripeAccountId?: string;
92
+ actor?: RefundActor;
93
+ }): Promise<StripeComponentPayment | null>;
78
94
  export declare function getRefundByStripeId(component: Component, ctx: RunCtx, opts: {
79
95
  stripeRefundId: string;
80
96
  }): Promise<StripeComponentRefund | null>;
@@ -1 +1 @@
1
- {"version":3,"file":"refunds.d.ts","sourceRoot":"","sources":["../../../src/client/connect/refunds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AAE1E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,KAAK,EAEV,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,KAAK,WAAW,EAAsB,MAAM,kBAAkB,CAAC;AAExE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,EAAE,OAAO,CAAC,GAAG;IACjE,cAAc,EAAE,iBAA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AE,MAAM,CAAC;IACzB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,GACA,OAAO,CAAC,qBAAqB,EAAE,CAAC,CASlC"}
@@ -1,6 +1,7 @@
1
1
  import { throwStripeError } from "../errors.js";
2
2
  import { componentRef } from "../webhooks/helpers.js";
3
3
  import { isRefundAuthorized } from "./refundActor.js";
4
+ import { readRefundPayment, readRefundCharge } from "./refundTarget.js";
4
5
  /** The host must authenticate/authorize scopes before exposing this reactive reader. */
5
6
  export async function paginateRefunds(component, ctx, opts) {
6
7
  const { stripeAccountId, ...rest } = opts;
@@ -51,9 +52,6 @@ export async function createRefund(stripe, component, ctx, opts) {
51
52
  // actually routed, read from the component's own payments ledger.
52
53
  await assertRefundAuthorized(stripe, component, ctx, opts);
53
54
  try {
54
- const accountRequestOpts = opts.stripeAccountId
55
- ? { stripeAccount: opts.stripeAccountId }
56
- : undefined;
57
55
  const createRequestOpts = opts.stripeAccountId !== undefined || opts.idempotencyKey !== undefined
58
56
  ? {
59
57
  ...(opts.stripeAccountId !== undefined
@@ -64,18 +62,9 @@ export async function createRefund(stripe, component, ctx, opts) {
64
62
  : {}),
65
63
  }
66
64
  : undefined;
67
- // Resolve the charge to gate the marketplace flags on what it can honour.
68
- let chargeId = opts.stripeChargeId;
69
- if (!chargeId && opts.stripePaymentIntentId) {
70
- const pi = await stripe.paymentIntents.retrieve(opts.stripePaymentIntentId, undefined, accountRequestOpts);
71
- chargeId =
72
- typeof pi.latest_charge === "string"
73
- ? pi.latest_charge
74
- : (pi.latest_charge?.id ?? undefined);
75
- }
76
- const charge = chargeId
77
- ? await stripe.charges.retrieve(chargeId, undefined, accountRequestOpts)
78
- : undefined;
65
+ // Preserve the original target and marketplace flags through shared read-only resolution.
66
+ const payment = await readRefundPayment(stripe, opts);
67
+ const charge = await readRefundCharge(stripe, opts, payment);
79
68
  const refundApplicationFee = (opts.refundApplicationFee ?? true) && Boolean(charge?.application_fee);
80
69
  const reverseTransfer = (opts.reverseTransfer ?? true) && Boolean(charge?.transfer);
81
70
  const refund = await stripe.refunds.create({
@@ -105,10 +94,10 @@ export async function createRefund(stripe, component, ctx, opts) {
105
94
  * no ledger row), a seller is denied rather than allowed. No-ops for admin /
106
95
  * omitted actors.
107
96
  */
108
- async function assertRefundAuthorized(stripe, component, ctx, opts) {
97
+ export async function assertRefundAuthorized(stripe, component, ctx, opts) {
109
98
  const { actor } = opts;
110
99
  if (!actor || actor.type === "admin")
111
- return; // unrestricted
100
+ return null; // legacy createRefund remains unrestricted
112
101
  // Resolve the PaymentIntent id that keys the payments ledger. A charge-only
113
102
  // refund resolves through the charge's `payment_intent`.
114
103
  const requestOpts = opts.stripeAccountId
@@ -135,6 +124,7 @@ async function assertRefundAuthorized(stripe, component, ctx, opts) {
135
124
  })) {
136
125
  throwStripeError("REFUND_UNAUTHORIZED", `Refund not authorized: seller ${actor.accountId} may not refund a sale routed to another account`);
137
126
  }
127
+ return payment;
138
128
  }
139
129
  export async function getRefundByStripeId(component, ctx, opts) {
140
130
  return (await ctx.runQuery(componentRef(component, "connect/queries/getRefundByStripeId"), opts));