@fantastic.dev/better-stripe 0.39.0 → 0.40.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 (53) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -1
  3. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  4. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  5. package/dist/client/costRecovery-invoices.d.ts.map +1 -1
  6. package/dist/client/costRecovery-invoices.js +3 -5
  7. package/dist/client/costRecovery-invoices.js.map +1 -1
  8. package/dist/client/index.d.ts +3 -2
  9. package/dist/client/index.d.ts.map +1 -1
  10. package/dist/client/index.js +2 -2
  11. package/dist/client/index.js.map +1 -1
  12. package/dist/client/paymentSource/client.d.ts +21 -0
  13. package/dist/client/paymentSource/client.d.ts.map +1 -0
  14. package/dist/client/paymentSource/client.js +8 -0
  15. package/dist/client/paymentSource/client.js.map +1 -0
  16. package/dist/client/paymentSource/contracts.d.ts +30 -0
  17. package/dist/client/paymentSource/contracts.d.ts.map +1 -0
  18. package/dist/client/paymentSource/contracts.js +16 -0
  19. package/dist/client/paymentSource/contracts.js.map +1 -0
  20. package/dist/client/paymentSource/economics.d.ts +12 -0
  21. package/dist/client/paymentSource/economics.d.ts.map +1 -0
  22. package/dist/client/paymentSource/economics.js +13 -0
  23. package/dist/client/paymentSource/economics.js.map +1 -0
  24. package/dist/client/paymentSource/funding.d.ts +10 -0
  25. package/dist/client/paymentSource/funding.d.ts.map +1 -0
  26. package/dist/client/paymentSource/funding.js +12 -0
  27. package/dist/client/paymentSource/funding.js.map +1 -0
  28. package/dist/client/paymentSource/history.d.ts +13 -0
  29. package/dist/client/paymentSource/history.d.ts.map +1 -0
  30. package/dist/client/paymentSource/history.js +41 -0
  31. package/dist/client/paymentSource/history.js.map +1 -0
  32. package/dist/client/paymentSource/resolver.d.ts +6 -0
  33. package/dist/client/paymentSource/resolver.d.ts.map +1 -0
  34. package/dist/client/paymentSource/resolver.js +222 -0
  35. package/dist/client/paymentSource/resolver.js.map +1 -0
  36. package/dist/client/settlement/verifiedSale.d.ts.map +1 -1
  37. package/dist/client/settlement/verifiedSale.js +4 -6
  38. package/dist/client/settlement/verifiedSale.js.map +1 -1
  39. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  40. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  41. package/dist/component/earnings/triggers.d.ts +16 -16
  42. package/docs/settlement.md +48 -0
  43. package/package.json +1 -1
  44. package/src/client/costRecovery-invoices.ts +3 -7
  45. package/src/client/index.ts +6 -2
  46. package/src/client/paymentSource/client.ts +13 -0
  47. package/src/client/paymentSource/contracts.ts +33 -0
  48. package/src/client/paymentSource/economics.ts +20 -0
  49. package/src/client/paymentSource/funding.ts +23 -0
  50. package/src/client/paymentSource/history.ts +48 -0
  51. package/src/client/paymentSource/resolver.test.ts +582 -0
  52. package/src/client/paymentSource/resolver.ts +350 -0
  53. package/src/client/settlement/verifiedSale.ts +4 -10
@@ -20,7 +20,7 @@ import type { CreateCheckoutSessionOptions } from "./billing/checkoutOptions.js"
20
20
  export type { CreateCheckoutSessionOptions } from "./billing/checkoutOptions.js";
21
21
  import * as intentsImpl from "./billing/intents.js";
22
22
  export type * from "./billing/intents.js";
23
- import { CatalogClient } from "./products/catalogClient.js";
23
+ import { PaymentSourceClient } from "./paymentSource/client.js";
24
24
  export type {
25
25
  CreateBillingPortalConfigurationOptions,
26
26
  RetrieveBillingPortalConfigurationOptions,
@@ -91,6 +91,10 @@ export type {
91
91
  VerifiedSale,
92
92
  VerifiedSaleOptions,
93
93
  } from "./settlement/verifiedSale.js";
94
+ export type {
95
+ VerifiedPaymentSource,
96
+ VerifiedPaymentSourceOptions,
97
+ } from "./paymentSource/contracts.js";
94
98
  export type { SettleSaleOptions } from "./settlement/settleSale.js";
95
99
  export type { SettlementResult } from "./settlement/execution.js";
96
100
  export type {
@@ -205,7 +209,7 @@ export type BetterStripeComponent = ComponentApi;
205
209
 
206
210
  // BETTER STRIPE CLIENT
207
211
 
208
- export class BetterStripe extends CatalogClient {
212
+ export class BetterStripe extends PaymentSourceClient {
209
213
  readonly stripeApiVersion: SupportedStripeApiVersion;
210
214
  private _apiKey?: string;
211
215
  private _triggers?: SyncTriggers;
@@ -0,0 +1,13 @@
1
+ import { CatalogClient } from "../products/catalogClient.js";
2
+ import type { RunCtx } from "../helpers.js";
3
+ import { resolveVerifiedPaymentSource } from "./resolver.js";
4
+ import type { VerifiedPaymentSourceOptions } from "./contracts.js";
5
+
6
+ export abstract class PaymentSourceClient extends CatalogClient {
7
+ async getVerifiedPaymentSource(
8
+ _ctx: RunCtx,
9
+ opts: VerifiedPaymentSourceOptions,
10
+ ) {
11
+ return resolveVerifiedPaymentSource(this.stripe(), opts);
12
+ }
13
+ }
@@ -0,0 +1,33 @@
1
+ import { z } from "zod";
2
+
3
+ const identifier = z.string().min(1).max(200);
4
+ export const paymentSourceOptions = z
5
+ .object({
6
+ paymentIntentId: identifier,
7
+ customerAccountId: identifier.optional(),
8
+ stripeAccountId: identifier.optional(),
9
+ invoiceId: identifier.optional(),
10
+ subscriptionId: identifier.optional(),
11
+ currency: z.string().regex(/^[a-z]{3}$/),
12
+ gross: z.number().int().min(1).max(Number.MAX_SAFE_INTEGER),
13
+ maxHistoryPages: z.number().int().min(1).max(20).default(5),
14
+ maxHistoryItems: z.number().int().min(1).max(2000).default(500),
15
+ })
16
+ .strict();
17
+ export type VerifiedPaymentSourceOptions = z.input<typeof paymentSourceOptions>;
18
+ /** Provider identities/economics only. The host still authorizes these identities. */
19
+ export type VerifiedPaymentSource = Readonly<{
20
+ paymentIntentId: string;
21
+ chargeId: string;
22
+ invoiceId: string;
23
+ invoicePaymentId: string;
24
+ subscriptionId?: string;
25
+ customerAccountId: string;
26
+ stripeAccountId?: string;
27
+ stripePriceId: string;
28
+ stripeProductId: string;
29
+ gross: number;
30
+ processingFee: number;
31
+ currency: string;
32
+ livemode: boolean;
33
+ }>;
@@ -0,0 +1,20 @@
1
+ /** The existing settlement amount/currency rules, shared without changing its result contract. */
2
+ export function saleAmountFailure(
3
+ payment: { currency: string; amount_received: number },
4
+ charge: { currency: string; amount: number },
5
+ balance: { currency: string; fee: number },
6
+ ): "currency" | "amounts" | null {
7
+ if (
8
+ balance.currency !== payment.currency ||
9
+ charge.currency !== payment.currency
10
+ )
11
+ return "currency";
12
+ if (
13
+ ![payment.amount_received, balance.fee].every(Number.isSafeInteger) ||
14
+ balance.fee < 0 ||
15
+ balance.fee > payment.amount_received ||
16
+ charge.amount !== payment.amount_received
17
+ )
18
+ return "amounts";
19
+ return null;
20
+ }
@@ -0,0 +1,23 @@
1
+ import type Stripe from "stripe";
2
+ export type IntentFunding = Stripe.InvoicePayment & {
3
+ payment: {
4
+ type: "payment_intent";
5
+ payment_intent: string | Stripe.PaymentIntent;
6
+ };
7
+ };
8
+ /** Shared existing funding rule; callers must independently prove scope and complete history. */
9
+ export function singleInvoiceFunding(
10
+ rows: Stripe.InvoicePayment[],
11
+ amountPaid: number,
12
+ ): IntentFunding | null {
13
+ const paid = rows.filter((row) => row.status === "paid");
14
+ const payment = paid[0];
15
+ if (
16
+ paid.length !== 1 ||
17
+ payment?.payment.type !== "payment_intent" ||
18
+ !payment.payment.payment_intent ||
19
+ payment.amount_paid !== amountPaid
20
+ )
21
+ return null;
22
+ return payment as IntentFunding;
23
+ }
@@ -0,0 +1,48 @@
1
+ import type Stripe from "stripe";
2
+
3
+ const incomplete = () => ({
4
+ status: "unknown" as const,
5
+ reason: "history_incomplete" as const,
6
+ retryable: false as const,
7
+ });
8
+ /** Completeness is mandatory; previews, repeated cursors and exhausted budgets are not evidence of uniqueness. */
9
+ export async function completeHistory<T extends { id: string }>(
10
+ fetch: (
11
+ cursor: string | undefined,
12
+ limit: number,
13
+ ) => Promise<Stripe.ApiList<T>>,
14
+ maxPages: number,
15
+ maxItems: number,
16
+ ) {
17
+ const data: T[] = [];
18
+ const seen = new Set<string>();
19
+ let cursor: string | undefined;
20
+ for (let pageIndex = 0; pageIndex < maxPages; pageIndex++) {
21
+ const limit = Math.min(100, maxItems - data.length);
22
+ if (limit < 1) return incomplete();
23
+ let page: Stripe.ApiList<T>;
24
+ try {
25
+ page = await fetch(cursor, limit);
26
+ } catch {
27
+ return incomplete();
28
+ }
29
+ if (
30
+ !page ||
31
+ page.object !== "list" ||
32
+ !Array.isArray(page.data) ||
33
+ page.data.length > limit ||
34
+ typeof page.has_more !== "boolean" ||
35
+ (page.has_more && !page.data.length)
36
+ )
37
+ return incomplete();
38
+ for (const row of page.data) {
39
+ if (!row || typeof row.id !== "string" || !row.id || seen.has(row.id))
40
+ return incomplete();
41
+ seen.add(row.id);
42
+ data.push(row);
43
+ }
44
+ if (!page.has_more) return { status: "complete" as const, data };
45
+ cursor = data[data.length - 1]!.id;
46
+ }
47
+ return incomplete();
48
+ }