@fantastic.dev/better-stripe 0.37.1 → 0.39.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.
- package/CHANGELOG.md +53 -0
- package/MIGRATION.md +9 -0
- package/README.md +25 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +506 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +253 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +506 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +253 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/core/accountCapabilities.d.ts +20 -0
- package/dist/client/core/accountCapabilities.d.ts.map +1 -0
- package/dist/client/core/accountCapabilities.js +7 -0
- package/dist/client/core/accountCapabilities.js.map +1 -0
- package/dist/client/core/accountConfigurations.d.ts +46 -0
- package/dist/client/core/accountConfigurations.d.ts.map +1 -0
- package/dist/client/core/accountConfigurations.js +99 -0
- package/dist/client/core/accountConfigurations.js.map +1 -0
- package/dist/client/core/accountLinks.d.ts +29 -2
- package/dist/client/core/accountLinks.d.ts.map +1 -1
- package/dist/client/core/accountLinks.js +56 -4
- package/dist/client/core/accountLinks.js.map +1 -1
- package/dist/client/core/accounts.d.ts +2 -36
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +3 -73
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/core/billingPortalClient.d.ts +14 -0
- package/dist/client/core/billingPortalClient.d.ts.map +1 -0
- package/dist/client/core/billingPortalClient.js +18 -0
- package/dist/client/core/billingPortalClient.js.map +1 -0
- package/dist/client/costRecovery-invoices.d.ts +4 -4
- package/dist/client/costRecovery.d.ts +19 -1
- package/dist/client/costRecovery.d.ts.map +1 -1
- package/dist/client/costRecovery.js +4 -0
- package/dist/client/costRecovery.js.map +1 -1
- package/dist/client/financialOutcomes/adapters.d.ts +18 -0
- package/dist/client/financialOutcomes/adapters.d.ts.map +1 -0
- package/dist/client/financialOutcomes/adapters.js +60 -0
- package/dist/client/financialOutcomes/adapters.js.map +1 -0
- package/dist/client/financialOutcomes/contracts.d.ts +42 -0
- package/dist/client/financialOutcomes/contracts.d.ts.map +1 -0
- package/dist/client/financialOutcomes/contracts.js +39 -0
- package/dist/client/financialOutcomes/contracts.js.map +1 -0
- package/dist/client/financialOutcomes/errors.d.ts +6 -0
- package/dist/client/financialOutcomes/errors.d.ts.map +1 -0
- package/dist/client/financialOutcomes/errors.js +22 -0
- package/dist/client/financialOutcomes/errors.js.map +1 -0
- package/dist/client/financialOutcomes/index.d.ts +6 -0
- package/dist/client/financialOutcomes/index.d.ts.map +1 -0
- package/dist/client/financialOutcomes/index.js +4 -0
- package/dist/client/financialOutcomes/index.js.map +1 -0
- package/dist/client/financialOutcomes/retry.d.ts +24 -0
- package/dist/client/financialOutcomes/retry.d.ts.map +1 -0
- package/dist/client/financialOutcomes/retry.js +31 -0
- package/dist/client/financialOutcomes/retry.js.map +1 -0
- package/dist/client/index.d.ts +21 -111
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +11 -85
- package/dist/client/index.js.map +1 -1
- package/dist/client/processingCostDeficit/index.d.ts +27 -0
- package/dist/client/processingCostDeficit/index.d.ts.map +1 -0
- package/dist/client/processingCostDeficit/index.js +54 -0
- package/dist/client/processingCostDeficit/index.js.map +1 -0
- package/dist/client/products/catalog.d.ts +26 -0
- package/dist/client/products/catalog.d.ts.map +1 -0
- package/dist/client/products/catalog.js +140 -0
- package/dist/client/products/catalog.js.map +1 -0
- package/dist/client/products/catalogClient.d.ts +109 -0
- package/dist/client/products/catalogClient.d.ts.map +1 -0
- package/dist/client/products/catalogClient.js +84 -0
- package/dist/client/products/catalogClient.js.map +1 -0
- package/dist/client/products/prices.d.ts +1 -0
- package/dist/client/products/prices.d.ts.map +1 -1
- package/dist/client/products/prices.js +1 -0
- package/dist/client/products/prices.js.map +1 -1
- package/dist/client/publicTypes.d.ts +2 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +284 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +535 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/helpers.d.ts +86 -0
- package/dist/component/balanceFunding/helpers.d.ts.map +1 -0
- package/dist/component/balanceFunding/helpers.js +68 -0
- package/dist/component/balanceFunding/helpers.js.map +1 -0
- package/dist/component/balanceFunding/importHandler.d.ts +13 -0
- package/dist/component/balanceFunding/importHandler.d.ts.map +1 -0
- package/dist/component/balanceFunding/importHandler.js +130 -0
- package/dist/component/balanceFunding/importHandler.js.map +1 -0
- package/dist/component/balanceFunding/lifecycleHandlers.d.ts +85 -0
- package/dist/component/balanceFunding/lifecycleHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/lifecycleHandlers.js +60 -0
- package/dist/component/balanceFunding/lifecycleHandlers.js.map +1 -0
- package/dist/component/balanceFunding/mutationValidators.d.ts +238 -0
- package/dist/component/balanceFunding/mutationValidators.d.ts.map +1 -0
- package/dist/component/balanceFunding/mutationValidators.js +55 -0
- package/dist/component/balanceFunding/mutationValidators.js.map +1 -0
- package/dist/component/balanceFunding/mutations.d.ts +335 -0
- package/dist/component/balanceFunding/mutations.d.ts.map +1 -0
- package/dist/component/balanceFunding/mutations.js +106 -0
- package/dist/component/balanceFunding/mutations.js.map +1 -0
- package/dist/component/balanceFunding/preparationHandlers.d.ts +55 -0
- package/dist/component/balanceFunding/preparationHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/preparationHandlers.js +165 -0
- package/dist/component/balanceFunding/preparationHandlers.js.map +1 -0
- package/dist/component/balanceFunding/queries.d.ts +151 -0
- package/dist/component/balanceFunding/queries.d.ts.map +1 -0
- package/dist/component/balanceFunding/queries.js +180 -0
- package/dist/component/balanceFunding/queries.js.map +1 -0
- package/dist/component/balanceFunding/schema.d.ts +748 -0
- package/dist/component/balanceFunding/schema.d.ts.map +1 -0
- package/dist/component/balanceFunding/schema.js +124 -0
- package/dist/component/balanceFunding/schema.js.map +1 -0
- package/dist/component/balanceFunding/stateHandlers.d.ts +58 -0
- package/dist/component/balanceFunding/stateHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/stateHandlers.js +160 -0
- package/dist/component/balanceFunding/stateHandlers.js.map +1 -0
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +2 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/costRecovery/completion.d.ts +1 -0
- package/dist/component/costRecovery/completion.d.ts.map +1 -1
- package/dist/component/costRecovery/helpers.d.ts +1 -0
- package/dist/component/costRecovery/helpers.d.ts.map +1 -1
- package/dist/component/costRecovery/mutations.d.ts +5 -0
- package/dist/component/costRecovery/mutations.d.ts.map +1 -1
- package/dist/component/costRecovery/preparation.d.ts +50 -0
- package/dist/component/costRecovery/preparation.d.ts.map +1 -0
- package/dist/component/costRecovery/preparation.js +108 -0
- package/dist/component/costRecovery/preparation.js.map +1 -0
- package/dist/component/costRecovery/queries.d.ts +1 -0
- package/dist/component/costRecovery/queries.d.ts.map +1 -1
- package/dist/component/costRecovery/reservations.d.ts +2 -0
- package/dist/component/costRecovery/reservations.d.ts.map +1 -1
- package/dist/component/costRecovery/schema.d.ts +7 -2
- package/dist/component/costRecovery/schema.d.ts.map +1 -1
- package/dist/component/costRecovery/schema.js +1 -0
- package/dist/component/costRecovery/schema.js.map +1 -1
- package/dist/component/durableExecution/runtime.d.ts +3 -0
- package/dist/component/durableExecution/runtime.d.ts.map +1 -0
- package/dist/component/durableExecution/runtime.js +12 -0
- package/dist/component/durableExecution/runtime.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +242 -2
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/products/mutations.d.ts +9 -0
- package/dist/component/products/mutations.d.ts.map +1 -1
- package/dist/component/products/mutations.js +31 -2
- package/dist/component/products/mutations.js.map +1 -1
- package/dist/component/schema.d.ts +253 -1
- package/dist/component/schema.js +4 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/read-contracts.md +33 -0
- package/package.json +4 -2
- package/src/client/core/account-session-client.test.ts +74 -0
- package/src/client/core/accountCapabilities.ts +22 -0
- package/src/client/core/accountConfigurations.test.ts +183 -0
- package/src/client/core/accountConfigurations.ts +161 -0
- package/src/client/core/accountLinks.test.ts +6 -2
- package/src/client/core/accountLinks.ts +122 -5
- package/src/client/core/accounts.ts +8 -115
- package/src/client/core/billingPortal.test.ts +247 -0
- package/src/client/core/billingPortalClient.ts +35 -0
- package/src/client/costRecovery.test.ts +34 -0
- package/src/client/costRecovery.ts +21 -0
- package/src/client/financialOutcomes/adapters.ts +88 -0
- package/src/client/financialOutcomes/contracts.ts +43 -0
- package/src/client/financialOutcomes/errors.ts +29 -0
- package/src/client/financialOutcomes/index.ts +5 -0
- package/src/client/financialOutcomes/outcomes.test.ts +254 -0
- package/src/client/financialOutcomes/retry.ts +52 -0
- package/src/client/index.test.ts +0 -64
- package/src/client/index.ts +35 -185
- package/src/client/processingCostDeficit/README.md +35 -0
- package/src/client/processingCostDeficit/calculator.test.ts +176 -0
- package/src/client/processingCostDeficit/index.ts +91 -0
- package/src/client/products/catalog.test.ts +389 -0
- package/src/client/products/catalog.ts +225 -0
- package/src/client/products/catalogClient.ts +172 -0
- package/src/client/products/prices.test.ts +24 -0
- package/src/client/products/prices.ts +2 -0
- package/src/client/publicTypes.ts +13 -0
- package/src/component/_generated/api.ts +27 -1
- package/src/component/_generated/component.ts +707 -0
- package/src/component/balanceFunding/failure.test.ts +116 -0
- package/src/component/balanceFunding/helpers.ts +102 -0
- package/src/component/balanceFunding/import.test.ts +221 -0
- package/src/component/balanceFunding/importHandler.ts +146 -0
- package/src/component/balanceFunding/lifecycleHandlers.ts +90 -0
- package/src/component/balanceFunding/lookup.test.ts +113 -0
- package/src/component/balanceFunding/mutationValidators.ts +63 -0
- package/src/component/balanceFunding/mutations.ts +136 -0
- package/src/component/balanceFunding/preparation.test.ts +146 -0
- package/src/component/balanceFunding/preparationHandlers.ts +204 -0
- package/src/component/balanceFunding/queries.ts +221 -0
- package/src/component/balanceFunding/requestLookup.test.ts +160 -0
- package/src/component/balanceFunding/schema.ts +169 -0
- package/src/component/balanceFunding/state.test.ts +352 -0
- package/src/component/balanceFunding/stateHandlers.ts +198 -0
- package/src/component/balanceFunding/validation.test.ts +100 -0
- package/src/component/convex.config.ts +2 -0
- package/src/component/costRecovery/preparation.test.ts +355 -0
- package/src/component/costRecovery/preparation.ts +124 -0
- package/src/component/costRecovery/schema.ts +1 -0
- package/src/component/durableExecution/composition.test.ts +568 -0
- package/src/component/durableExecution/runtime.ts +12 -0
- package/src/component/products/mutations.ts +39 -2
- package/src/component/schema.ts +8 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
3
|
+
import { runMutationOrThrow } from "../helpers.js";
|
|
4
|
+
import { throwStripeError } from "../errors.js";
|
|
5
|
+
import { getChargeCurrencyMetadata } from "../charge-currency.js";
|
|
6
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
7
|
+
import type { CreatePriceOptions } from "./prices.js";
|
|
8
|
+
|
|
9
|
+
export type AdoptProductOptions = {
|
|
10
|
+
stripeProductId: string;
|
|
11
|
+
accountId?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Host authorizes catalog scope. Reuse this key only for identical creation parameters. */
|
|
14
|
+
export type EnsurePriceOptions = CreatePriceOptions & {
|
|
15
|
+
lookupKey: string;
|
|
16
|
+
idempotencyKey: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function validateLookupKey(key: string) {
|
|
20
|
+
if (!key.trim() || key.length > 200)
|
|
21
|
+
throwStripeError(
|
|
22
|
+
"INVALID_CONFIGURATION",
|
|
23
|
+
"lookupKey must contain 1–200 characters",
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Provider lookup includes inactive prices so ensure never steals an archived key. */
|
|
28
|
+
export async function getStripePriceByLookupKey(
|
|
29
|
+
stripe: Stripe,
|
|
30
|
+
opts: { lookupKey: string },
|
|
31
|
+
): Promise<Stripe.Price | null> {
|
|
32
|
+
validateLookupKey(opts.lookupKey);
|
|
33
|
+
const page = await stripe.prices.list({
|
|
34
|
+
lookup_keys: [opts.lookupKey],
|
|
35
|
+
limit: 2,
|
|
36
|
+
});
|
|
37
|
+
if (page.has_more || page.data.length > 1)
|
|
38
|
+
throwStripeError("INVALID_CONFIGURATION", "Ambiguous price lookup key");
|
|
39
|
+
return page.data[0] ?? null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Imports provider data; caller must authorize the product and optional owning account. */
|
|
43
|
+
export async function adoptProduct(
|
|
44
|
+
stripe: Stripe,
|
|
45
|
+
component: Component,
|
|
46
|
+
ctx: RunCtx,
|
|
47
|
+
opts: AdoptProductOptions,
|
|
48
|
+
) {
|
|
49
|
+
const product = await stripe.products.retrieve(opts.stripeProductId);
|
|
50
|
+
if ("deleted" in product || product.id !== opts.stripeProductId)
|
|
51
|
+
throwStripeError(
|
|
52
|
+
"PRODUCT_NOT_FOUND",
|
|
53
|
+
"Product is deleted or does not match requested identity",
|
|
54
|
+
);
|
|
55
|
+
const stored = await ctx.runQuery(
|
|
56
|
+
componentRef(component, "products/queries/getProductByStripeId"),
|
|
57
|
+
{ stripeProductId: opts.stripeProductId },
|
|
58
|
+
);
|
|
59
|
+
if (
|
|
60
|
+
stored?.accountId !== undefined &&
|
|
61
|
+
opts.accountId !== undefined &&
|
|
62
|
+
stored.accountId !== opts.accountId
|
|
63
|
+
)
|
|
64
|
+
throwStripeError(
|
|
65
|
+
"INVALID_CONFIGURATION",
|
|
66
|
+
"Product ownership cannot be reassigned by adoption",
|
|
67
|
+
);
|
|
68
|
+
await runMutationOrThrow(
|
|
69
|
+
ctx,
|
|
70
|
+
componentRef(component, "products/mutations/adoptProduct"),
|
|
71
|
+
{
|
|
72
|
+
stripeProductId: product.id,
|
|
73
|
+
accountId: stored?.accountId ?? opts.accountId,
|
|
74
|
+
name: product.name,
|
|
75
|
+
description: product.description ?? undefined,
|
|
76
|
+
active: product.active,
|
|
77
|
+
metadata: product.metadata,
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
const adopted = await ctx.runQuery(
|
|
81
|
+
componentRef(component, "products/queries/getProductByStripeId"),
|
|
82
|
+
{ stripeProductId: product.id },
|
|
83
|
+
);
|
|
84
|
+
if (!adopted)
|
|
85
|
+
throwStripeError(
|
|
86
|
+
"PRODUCT_NOT_FOUND",
|
|
87
|
+
"Product mirror is unavailable after adoption",
|
|
88
|
+
);
|
|
89
|
+
return { productId: adopted._id, stripeProductId: product.id };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function validateOptions(opts: EnsurePriceOptions) {
|
|
93
|
+
validateLookupKey(opts.lookupKey);
|
|
94
|
+
if (
|
|
95
|
+
!opts.idempotencyKey.trim() ||
|
|
96
|
+
opts.idempotencyKey.length > 255 ||
|
|
97
|
+
!opts.stripeProductId.trim()
|
|
98
|
+
)
|
|
99
|
+
throwStripeError(
|
|
100
|
+
"INVALID_CONFIGURATION",
|
|
101
|
+
"A product and caller-scoped idempotencyKey are required",
|
|
102
|
+
);
|
|
103
|
+
if (
|
|
104
|
+
!Number.isSafeInteger(opts.unitAmount) ||
|
|
105
|
+
opts.unitAmount < 0 ||
|
|
106
|
+
!getChargeCurrencyMetadata(opts.currency ?? "usd")
|
|
107
|
+
)
|
|
108
|
+
throwStripeError(
|
|
109
|
+
"INVALID_CONFIGURATION",
|
|
110
|
+
"Invalid price amount or currency",
|
|
111
|
+
);
|
|
112
|
+
if (opts.type === "recurring") {
|
|
113
|
+
const count = opts.intervalCount ?? 1;
|
|
114
|
+
const max = { day: 1095, week: 156, month: 36, year: 3 };
|
|
115
|
+
if (
|
|
116
|
+
!opts.interval ||
|
|
117
|
+
!(opts.interval in max) ||
|
|
118
|
+
!Number.isInteger(count) ||
|
|
119
|
+
count < 1 ||
|
|
120
|
+
count > max[opts.interval]
|
|
121
|
+
)
|
|
122
|
+
throwStripeError(
|
|
123
|
+
"INVALID_CONFIGURATION",
|
|
124
|
+
"Invalid recurring price interval",
|
|
125
|
+
);
|
|
126
|
+
} else if (
|
|
127
|
+
opts.type !== "one_time" ||
|
|
128
|
+
opts.interval !== undefined ||
|
|
129
|
+
opts.intervalCount !== undefined
|
|
130
|
+
)
|
|
131
|
+
throwStripeError(
|
|
132
|
+
"INVALID_CONFIGURATION",
|
|
133
|
+
"One-time prices cannot have recurrence",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function verifyPrice(price: Stripe.Price, opts: EnsurePriceOptions) {
|
|
138
|
+
const productId =
|
|
139
|
+
typeof price.product === "string" ? price.product : price.product.id;
|
|
140
|
+
const recurringMatches =
|
|
141
|
+
opts.type === "recurring"
|
|
142
|
+
? price.recurring?.interval === opts.interval &&
|
|
143
|
+
price.recurring?.interval_count === (opts.intervalCount ?? 1) &&
|
|
144
|
+
price.recurring?.usage_type === "licensed"
|
|
145
|
+
: price.recurring === null;
|
|
146
|
+
if (
|
|
147
|
+
!price.active ||
|
|
148
|
+
price.lookup_key !== opts.lookupKey ||
|
|
149
|
+
productId !== opts.stripeProductId ||
|
|
150
|
+
price.currency !== (opts.currency ?? "usd").toLowerCase() ||
|
|
151
|
+
price.unit_amount !== opts.unitAmount ||
|
|
152
|
+
price.type !== opts.type ||
|
|
153
|
+
price.billing_scheme !== "per_unit" ||
|
|
154
|
+
price.transform_quantity != null ||
|
|
155
|
+
!recurringMatches
|
|
156
|
+
)
|
|
157
|
+
throwStripeError(
|
|
158
|
+
"INVALID_CONFIGURATION",
|
|
159
|
+
"Lookup key refers to an incompatible price; choose a new versioned key",
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Ensures immutable price terms without transferring keys or changing existing subscriptions. */
|
|
164
|
+
export async function ensurePrice(
|
|
165
|
+
stripe: Stripe,
|
|
166
|
+
component: Component,
|
|
167
|
+
ctx: RunCtx,
|
|
168
|
+
opts: EnsurePriceOptions,
|
|
169
|
+
) {
|
|
170
|
+
validateOptions(opts);
|
|
171
|
+
let price = await getStripePriceByLookupKey(stripe, opts);
|
|
172
|
+
if (!price) {
|
|
173
|
+
try {
|
|
174
|
+
price = await stripe.prices.create(
|
|
175
|
+
{
|
|
176
|
+
product: opts.stripeProductId,
|
|
177
|
+
unit_amount: opts.unitAmount,
|
|
178
|
+
currency: (opts.currency ?? "usd").toLowerCase(),
|
|
179
|
+
lookup_key: opts.lookupKey,
|
|
180
|
+
nickname: opts.nickname,
|
|
181
|
+
metadata: opts.metadata,
|
|
182
|
+
...(opts.type === "recurring"
|
|
183
|
+
? {
|
|
184
|
+
recurring: {
|
|
185
|
+
interval: opts.interval!,
|
|
186
|
+
interval_count: opts.intervalCount ?? 1,
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
: {}),
|
|
190
|
+
},
|
|
191
|
+
{ idempotencyKey: opts.idempotencyKey },
|
|
192
|
+
);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
// Reconcile provider races/unknown responses, but never retry a Stripe write here.
|
|
195
|
+
try {
|
|
196
|
+
price = await getStripePriceByLookupKey(stripe, opts);
|
|
197
|
+
} catch {
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
if (!price) throw error;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
verifyPrice(price, opts);
|
|
204
|
+
const product = await adoptProduct(stripe, component, ctx, {
|
|
205
|
+
stripeProductId: opts.stripeProductId,
|
|
206
|
+
});
|
|
207
|
+
await runMutationOrThrow(
|
|
208
|
+
ctx,
|
|
209
|
+
componentRef(component, "products/mutations/upsertPrice"),
|
|
210
|
+
{
|
|
211
|
+
stripePriceId: price.id,
|
|
212
|
+
productId: product.productId,
|
|
213
|
+
stripeProductId: product.stripeProductId,
|
|
214
|
+
nickname: price.nickname ?? undefined,
|
|
215
|
+
unitAmount: price.unit_amount!,
|
|
216
|
+
currency: price.currency,
|
|
217
|
+
active: price.active,
|
|
218
|
+
type: price.type,
|
|
219
|
+
interval: price.recurring?.interval,
|
|
220
|
+
intervalCount: price.recurring?.interval_count,
|
|
221
|
+
metadata: price.metadata,
|
|
222
|
+
},
|
|
223
|
+
);
|
|
224
|
+
return { stripePriceId: price.id };
|
|
225
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { BillingPortalClient } from "../core/billingPortalClient.js";
|
|
3
|
+
import type { RunCtx } from "../helpers.js";
|
|
4
|
+
import type { StripeComponentProduct, StripeComponentPrice } from "../types.js";
|
|
5
|
+
import * as productsImpl from "./products.js";
|
|
6
|
+
import * as pricesImpl from "./prices.js";
|
|
7
|
+
import * as catalog from "./catalog.js";
|
|
8
|
+
|
|
9
|
+
/** Catalog APIs inherited by BetterStripe. Host authorization remains required. */
|
|
10
|
+
export abstract class CatalogClient extends BillingPortalClient {
|
|
11
|
+
abstract get statementDescriptorSuffix(): string | undefined;
|
|
12
|
+
async getStripePriceByLookupKey(
|
|
13
|
+
_ctx: RunCtx,
|
|
14
|
+
opts: { lookupKey: string },
|
|
15
|
+
): Promise<Stripe.Price | null> {
|
|
16
|
+
return catalog.getStripePriceByLookupKey(this.stripe(), opts);
|
|
17
|
+
}
|
|
18
|
+
async adoptProduct(ctx: RunCtx, opts: catalog.AdoptProductOptions) {
|
|
19
|
+
return catalog.adoptProduct(this.stripe(), this.component, ctx, opts);
|
|
20
|
+
}
|
|
21
|
+
async ensurePrice(ctx: RunCtx, opts: catalog.EnsurePriceOptions) {
|
|
22
|
+
return catalog.ensurePrice(this.stripe(), this.component, ctx, opts);
|
|
23
|
+
}
|
|
24
|
+
// PRODUCT METHODS
|
|
25
|
+
|
|
26
|
+
async createProduct(ctx: RunCtx, opts: productsImpl.CreateProductOptions) {
|
|
27
|
+
return productsImpl.createProduct(this.stripe(), this.component, ctx, {
|
|
28
|
+
...opts,
|
|
29
|
+
// Stamp the per-store descriptor at create so it covers the first invoice
|
|
30
|
+
// charge (BTS-67); resolves the store's suffix, else this default.
|
|
31
|
+
defaultStatementDescriptorSuffix: this.statementDescriptorSuffix,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async updateProduct(
|
|
36
|
+
ctx: RunCtx,
|
|
37
|
+
opts: {
|
|
38
|
+
stripeProductId: string;
|
|
39
|
+
name?: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
active?: boolean;
|
|
42
|
+
defaultPrice?: string | null;
|
|
43
|
+
metadata?: Record<string, string>;
|
|
44
|
+
},
|
|
45
|
+
) {
|
|
46
|
+
return productsImpl.updateProduct(this.stripe(), this.component, ctx, {
|
|
47
|
+
...opts,
|
|
48
|
+
defaultStatementDescriptorSuffix: this.statementDescriptorSuffix,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async deactivateProduct(ctx: RunCtx, opts: { stripeProductId: string }) {
|
|
53
|
+
return productsImpl.deactivateProduct(this.stripe(), ctx, opts);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getProduct(
|
|
57
|
+
ctx: RunCtx,
|
|
58
|
+
opts: { productId: string },
|
|
59
|
+
): Promise<StripeComponentProduct | null> {
|
|
60
|
+
return productsImpl.getProduct(this.component, ctx, opts);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async getProductByStripeId(
|
|
64
|
+
ctx: RunCtx,
|
|
65
|
+
opts: { stripeProductId: string },
|
|
66
|
+
): Promise<StripeComponentProduct | null> {
|
|
67
|
+
return productsImpl.getProductByStripeId(this.component, ctx, opts);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async listProducts(
|
|
71
|
+
ctx: RunCtx,
|
|
72
|
+
opts?: { accountId?: string; active?: boolean; limit?: number },
|
|
73
|
+
): Promise<StripeComponentProduct[]> {
|
|
74
|
+
return productsImpl.listProducts(this.component, ctx, opts);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async upsertProduct(
|
|
78
|
+
ctx: RunCtx,
|
|
79
|
+
opts: {
|
|
80
|
+
stripeProductId: string;
|
|
81
|
+
accountId?: string;
|
|
82
|
+
name: string;
|
|
83
|
+
description?: string;
|
|
84
|
+
active: boolean;
|
|
85
|
+
metadata?: Record<string, unknown>;
|
|
86
|
+
},
|
|
87
|
+
) {
|
|
88
|
+
return productsImpl.upsertProduct(this.component, ctx, opts);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async listStripeProducts(
|
|
92
|
+
ctx: RunCtx,
|
|
93
|
+
opts?: { active?: boolean; limit?: number },
|
|
94
|
+
): Promise<Stripe.Product[]> {
|
|
95
|
+
return productsImpl.listStripeProducts(this.stripe(), ctx, opts);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async deleteProduct(
|
|
99
|
+
ctx: RunCtx,
|
|
100
|
+
opts: { stripeProductId: string },
|
|
101
|
+
): Promise<Stripe.Response<Stripe.DeletedProduct>> {
|
|
102
|
+
return productsImpl.deleteProduct(this.stripe(), ctx, opts);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// PRICE METHODS
|
|
106
|
+
|
|
107
|
+
async createPrice(ctx: RunCtx, opts: pricesImpl.CreatePriceOptions) {
|
|
108
|
+
return pricesImpl.createPrice(this.stripe(), this.component, ctx, opts);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async updatePrice(
|
|
112
|
+
ctx: RunCtx,
|
|
113
|
+
opts: {
|
|
114
|
+
stripePriceId: string;
|
|
115
|
+
active?: boolean;
|
|
116
|
+
nickname?: string;
|
|
117
|
+
metadata?: Record<string, string>;
|
|
118
|
+
},
|
|
119
|
+
) {
|
|
120
|
+
return pricesImpl.updatePrice(this.stripe(), ctx, opts);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async deactivatePrice(ctx: RunCtx, opts: { stripePriceId: string }) {
|
|
124
|
+
return pricesImpl.deactivatePrice(this.stripe(), ctx, opts);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async getPrice(
|
|
128
|
+
ctx: RunCtx,
|
|
129
|
+
opts: { priceId: string },
|
|
130
|
+
): Promise<StripeComponentPrice | null> {
|
|
131
|
+
return pricesImpl.getPrice(this.component, ctx, opts);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async getPriceByStripeId(
|
|
135
|
+
ctx: RunCtx,
|
|
136
|
+
opts: { stripePriceId: string },
|
|
137
|
+
): Promise<StripeComponentPrice | null> {
|
|
138
|
+
return pricesImpl.getPriceByStripeId(this.component, ctx, opts);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async listPrices(
|
|
142
|
+
ctx: RunCtx,
|
|
143
|
+
opts?: { productId?: string; active?: boolean; limit?: number },
|
|
144
|
+
): Promise<StripeComponentPrice[]> {
|
|
145
|
+
return pricesImpl.listPrices(this.component, ctx, opts);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async listPricesByProduct(
|
|
149
|
+
ctx: RunCtx,
|
|
150
|
+
opts: { stripeProductId: string },
|
|
151
|
+
): Promise<StripeComponentPrice[]> {
|
|
152
|
+
return pricesImpl.listPricesByProduct(this.component, ctx, opts);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async upsertPrice(
|
|
156
|
+
ctx: RunCtx,
|
|
157
|
+
opts: Parameters<typeof pricesImpl.upsertPrice>[2],
|
|
158
|
+
) {
|
|
159
|
+
return pricesImpl.upsertPrice(this.component, ctx, opts);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async listStripePrices(
|
|
163
|
+
ctx: RunCtx,
|
|
164
|
+
opts?: { productId?: string; active?: boolean; limit?: number },
|
|
165
|
+
): Promise<Stripe.Price[]> {
|
|
166
|
+
return pricesImpl.listStripePrices(this.stripe(), ctx, opts);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async syncAllProducts(ctx: RunCtx) {
|
|
170
|
+
return productsImpl.syncAllProducts(this.stripe(), this.component, ctx);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -73,6 +73,30 @@ function asyncIterable<T>(items: T[]) {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
describe("createPrice", () => {
|
|
76
|
+
it("passes a lookup key to provider creation", async () => {
|
|
77
|
+
const stripe = makeStripe();
|
|
78
|
+
stripe.prices.create.mockResolvedValue({
|
|
79
|
+
id: "price_lookup",
|
|
80
|
+
unit_amount: 100,
|
|
81
|
+
currency: "usd",
|
|
82
|
+
active: true,
|
|
83
|
+
});
|
|
84
|
+
await createPrice(
|
|
85
|
+
asStripe(stripe),
|
|
86
|
+
makeComponent(),
|
|
87
|
+
makeCtx({ _id: "prod_internal" }),
|
|
88
|
+
{
|
|
89
|
+
stripeProductId: "prod_1",
|
|
90
|
+
unitAmount: 100,
|
|
91
|
+
type: "one_time",
|
|
92
|
+
lookupKey: "catalog:v1",
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
expect(stripe.prices.create.mock.calls[0][0]).toMatchObject({
|
|
96
|
+
lookup_key: "catalog:v1",
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
76
100
|
it("builds recurring params, upserts the price, and returns the new id", async () => {
|
|
77
101
|
const stripe = makeStripe();
|
|
78
102
|
stripe.prices.create.mockResolvedValue({
|
|
@@ -18,6 +18,7 @@ export type CreatePriceOptions = {
|
|
|
18
18
|
type: "one_time" | "recurring";
|
|
19
19
|
interval?: "day" | "week" | "month" | "year";
|
|
20
20
|
intervalCount?: number;
|
|
21
|
+
lookupKey?: string;
|
|
21
22
|
nickname?: string;
|
|
22
23
|
metadata?: Record<string, string>;
|
|
23
24
|
idempotencyKey?: string;
|
|
@@ -45,6 +46,7 @@ export async function createPrice(
|
|
|
45
46
|
|
|
46
47
|
const priceParams: Stripe.PriceCreateParams = {
|
|
47
48
|
product: opts.stripeProductId,
|
|
49
|
+
...(opts.lookupKey !== undefined ? { lookup_key: opts.lookupKey } : {}),
|
|
48
50
|
unit_amount: opts.unitAmount,
|
|
49
51
|
currency: opts.currency ?? "usd",
|
|
50
52
|
nickname: opts.nickname ?? undefined,
|
|
@@ -78,3 +78,16 @@ export type {
|
|
|
78
78
|
OperationEvidenceCaptureResult,
|
|
79
79
|
OperationEvidencePage,
|
|
80
80
|
} from "./operationEvidence.js";
|
|
81
|
+
export type {
|
|
82
|
+
ProcessingCostContext,
|
|
83
|
+
ProcessingCostOptions,
|
|
84
|
+
ProcessingCostTransaction,
|
|
85
|
+
ProcessingCostRecord,
|
|
86
|
+
ProcessingCostNotReadyReason,
|
|
87
|
+
} from "./settlement/processingCosts.js";
|
|
88
|
+
export type {
|
|
89
|
+
BalanceFundingCapability,
|
|
90
|
+
BalanceFundingPreparation,
|
|
91
|
+
PrepareBalanceFundingOptions,
|
|
92
|
+
SetBalanceFundingOptions,
|
|
93
|
+
} from "./billing/balanceFunding.js";
|
|
@@ -18,6 +18,14 @@ import type * as affiliatePayouts_recipientGuard from "../affiliatePayouts/recip
|
|
|
18
18
|
import type * as affiliateRecovery_accounting from "../affiliateRecovery/accounting.js";
|
|
19
19
|
import type * as affiliateRecovery_mutations from "../affiliateRecovery/mutations.js";
|
|
20
20
|
import type * as affiliateRecovery_queries from "../affiliateRecovery/queries.js";
|
|
21
|
+
import type * as balanceFunding_helpers from "../balanceFunding/helpers.js";
|
|
22
|
+
import type * as balanceFunding_importHandler from "../balanceFunding/importHandler.js";
|
|
23
|
+
import type * as balanceFunding_lifecycleHandlers from "../balanceFunding/lifecycleHandlers.js";
|
|
24
|
+
import type * as balanceFunding_mutationValidators from "../balanceFunding/mutationValidators.js";
|
|
25
|
+
import type * as balanceFunding_mutations from "../balanceFunding/mutations.js";
|
|
26
|
+
import type * as balanceFunding_preparationHandlers from "../balanceFunding/preparationHandlers.js";
|
|
27
|
+
import type * as balanceFunding_queries from "../balanceFunding/queries.js";
|
|
28
|
+
import type * as balanceFunding_stateHandlers from "../balanceFunding/stateHandlers.js";
|
|
21
29
|
import type * as billing_mutations from "../billing/mutations.js";
|
|
22
30
|
import type * as billing_queries from "../billing/queries.js";
|
|
23
31
|
import type * as billing_validators from "../billing/validators.js";
|
|
@@ -38,11 +46,14 @@ import type * as costRecovery_completion from "../costRecovery/completion.js";
|
|
|
38
46
|
import type * as costRecovery_helpers from "../costRecovery/helpers.js";
|
|
39
47
|
import type * as costRecovery_imports from "../costRecovery/imports.js";
|
|
40
48
|
import type * as costRecovery_mutations from "../costRecovery/mutations.js";
|
|
49
|
+
import type * as costRecovery_preparation from "../costRecovery/preparation.js";
|
|
41
50
|
import type * as costRecovery_queries from "../costRecovery/queries.js";
|
|
42
51
|
import type * as costRecovery_reservations from "../costRecovery/reservations.js";
|
|
52
|
+
import type * as durableExecution_runtime from "../durableExecution/runtime.js";
|
|
43
53
|
import type * as earnings_arithmetic from "../earnings/arithmetic.js";
|
|
44
54
|
import type * as earnings_legacyReads from "../earnings/legacyReads.js";
|
|
45
55
|
import type * as earnings_mutations from "../earnings/mutations.js";
|
|
56
|
+
import type * as earnings_pairedAdjustment from "../earnings/pairedAdjustment.js";
|
|
46
57
|
import type * as earnings_reads from "../earnings/reads.js";
|
|
47
58
|
import type * as earnings_triggers from "../earnings/triggers.js";
|
|
48
59
|
import type * as financialObservations_continuation from "../financialObservations/continuation.js";
|
|
@@ -113,6 +124,7 @@ import type * as refundHistory_commandEvidence from "../refundHistory/commandEvi
|
|
|
113
124
|
import type * as refundHistory_commandEvidenceValidator from "../refundHistory/commandEvidenceValidator.js";
|
|
114
125
|
import type * as refundHistory_commandFields from "../refundHistory/commandFields.js";
|
|
115
126
|
import type * as refundHistory_mutations from "../refundHistory/mutations.js";
|
|
127
|
+
import type * as refundHistory_originalSeller from "../refundHistory/originalSeller.js";
|
|
116
128
|
import type * as refundHistory_queries from "../refundHistory/queries.js";
|
|
117
129
|
import type * as settlement_clawbackSchema from "../settlement/clawbackSchema.js";
|
|
118
130
|
import type * as settlement_clawbacks from "../settlement/clawbacks.js";
|
|
@@ -140,6 +152,14 @@ const fullApi: ApiFromModules<{
|
|
|
140
152
|
"affiliateRecovery/accounting": typeof affiliateRecovery_accounting;
|
|
141
153
|
"affiliateRecovery/mutations": typeof affiliateRecovery_mutations;
|
|
142
154
|
"affiliateRecovery/queries": typeof affiliateRecovery_queries;
|
|
155
|
+
"balanceFunding/helpers": typeof balanceFunding_helpers;
|
|
156
|
+
"balanceFunding/importHandler": typeof balanceFunding_importHandler;
|
|
157
|
+
"balanceFunding/lifecycleHandlers": typeof balanceFunding_lifecycleHandlers;
|
|
158
|
+
"balanceFunding/mutationValidators": typeof balanceFunding_mutationValidators;
|
|
159
|
+
"balanceFunding/mutations": typeof balanceFunding_mutations;
|
|
160
|
+
"balanceFunding/preparationHandlers": typeof balanceFunding_preparationHandlers;
|
|
161
|
+
"balanceFunding/queries": typeof balanceFunding_queries;
|
|
162
|
+
"balanceFunding/stateHandlers": typeof balanceFunding_stateHandlers;
|
|
143
163
|
"billing/mutations": typeof billing_mutations;
|
|
144
164
|
"billing/queries": typeof billing_queries;
|
|
145
165
|
"billing/validators": typeof billing_validators;
|
|
@@ -160,11 +180,14 @@ const fullApi: ApiFromModules<{
|
|
|
160
180
|
"costRecovery/helpers": typeof costRecovery_helpers;
|
|
161
181
|
"costRecovery/imports": typeof costRecovery_imports;
|
|
162
182
|
"costRecovery/mutations": typeof costRecovery_mutations;
|
|
183
|
+
"costRecovery/preparation": typeof costRecovery_preparation;
|
|
163
184
|
"costRecovery/queries": typeof costRecovery_queries;
|
|
164
185
|
"costRecovery/reservations": typeof costRecovery_reservations;
|
|
186
|
+
"durableExecution/runtime": typeof durableExecution_runtime;
|
|
165
187
|
"earnings/arithmetic": typeof earnings_arithmetic;
|
|
166
188
|
"earnings/legacyReads": typeof earnings_legacyReads;
|
|
167
189
|
"earnings/mutations": typeof earnings_mutations;
|
|
190
|
+
"earnings/pairedAdjustment": typeof earnings_pairedAdjustment;
|
|
168
191
|
"earnings/reads": typeof earnings_reads;
|
|
169
192
|
"earnings/triggers": typeof earnings_triggers;
|
|
170
193
|
"financialObservations/continuation": typeof financialObservations_continuation;
|
|
@@ -235,6 +258,7 @@ const fullApi: ApiFromModules<{
|
|
|
235
258
|
"refundHistory/commandEvidenceValidator": typeof refundHistory_commandEvidenceValidator;
|
|
236
259
|
"refundHistory/commandFields": typeof refundHistory_commandFields;
|
|
237
260
|
"refundHistory/mutations": typeof refundHistory_mutations;
|
|
261
|
+
"refundHistory/originalSeller": typeof refundHistory_originalSeller;
|
|
238
262
|
"refundHistory/queries": typeof refundHistory_queries;
|
|
239
263
|
"settlement/clawbackSchema": typeof settlement_clawbackSchema;
|
|
240
264
|
"settlement/clawbacks": typeof settlement_clawbacks;
|
|
@@ -271,4 +295,6 @@ export const internal: FilterApi<
|
|
|
271
295
|
FunctionReference<any, "internal">
|
|
272
296
|
> = anyApi as any;
|
|
273
297
|
|
|
274
|
-
export const components = componentsGeneric() as unknown as {
|
|
298
|
+
export const components = componentsGeneric() as unknown as {
|
|
299
|
+
workflow: import("@convex-dev/workflow/_generated/component.js").ComponentApi<"workflow">;
|
|
300
|
+
};
|