@fantastic.dev/better-stripe 0.37.0 → 0.38.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 +64 -158
- package/README.md +5 -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 +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 +20 -111
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +10 -85
- package/dist/client/index.js.map +1 -1
- 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/component.d.ts +8 -0
- package/dist/component/_generated/component.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/docs/read-contracts.md +33 -0
- package/package.json +1 -1
- 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/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 +34 -185
- 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 +4 -0
- package/src/component/_generated/component.ts +14 -0
- package/src/component/products/mutations.ts +39 -2
|
@@ -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";
|
|
@@ -43,6 +43,7 @@ import type * as costRecovery_reservations from "../costRecovery/reservations.js
|
|
|
43
43
|
import type * as earnings_arithmetic from "../earnings/arithmetic.js";
|
|
44
44
|
import type * as earnings_legacyReads from "../earnings/legacyReads.js";
|
|
45
45
|
import type * as earnings_mutations from "../earnings/mutations.js";
|
|
46
|
+
import type * as earnings_pairedAdjustment from "../earnings/pairedAdjustment.js";
|
|
46
47
|
import type * as earnings_reads from "../earnings/reads.js";
|
|
47
48
|
import type * as earnings_triggers from "../earnings/triggers.js";
|
|
48
49
|
import type * as financialObservations_continuation from "../financialObservations/continuation.js";
|
|
@@ -113,6 +114,7 @@ import type * as refundHistory_commandEvidence from "../refundHistory/commandEvi
|
|
|
113
114
|
import type * as refundHistory_commandEvidenceValidator from "../refundHistory/commandEvidenceValidator.js";
|
|
114
115
|
import type * as refundHistory_commandFields from "../refundHistory/commandFields.js";
|
|
115
116
|
import type * as refundHistory_mutations from "../refundHistory/mutations.js";
|
|
117
|
+
import type * as refundHistory_originalSeller from "../refundHistory/originalSeller.js";
|
|
116
118
|
import type * as refundHistory_queries from "../refundHistory/queries.js";
|
|
117
119
|
import type * as settlement_clawbackSchema from "../settlement/clawbackSchema.js";
|
|
118
120
|
import type * as settlement_clawbacks from "../settlement/clawbacks.js";
|
|
@@ -165,6 +167,7 @@ const fullApi: ApiFromModules<{
|
|
|
165
167
|
"earnings/arithmetic": typeof earnings_arithmetic;
|
|
166
168
|
"earnings/legacyReads": typeof earnings_legacyReads;
|
|
167
169
|
"earnings/mutations": typeof earnings_mutations;
|
|
170
|
+
"earnings/pairedAdjustment": typeof earnings_pairedAdjustment;
|
|
168
171
|
"earnings/reads": typeof earnings_reads;
|
|
169
172
|
"earnings/triggers": typeof earnings_triggers;
|
|
170
173
|
"financialObservations/continuation": typeof financialObservations_continuation;
|
|
@@ -235,6 +238,7 @@ const fullApi: ApiFromModules<{
|
|
|
235
238
|
"refundHistory/commandEvidenceValidator": typeof refundHistory_commandEvidenceValidator;
|
|
236
239
|
"refundHistory/commandFields": typeof refundHistory_commandFields;
|
|
237
240
|
"refundHistory/mutations": typeof refundHistory_mutations;
|
|
241
|
+
"refundHistory/originalSeller": typeof refundHistory_originalSeller;
|
|
238
242
|
"refundHistory/queries": typeof refundHistory_queries;
|
|
239
243
|
"settlement/clawbackSchema": typeof settlement_clawbackSchema;
|
|
240
244
|
"settlement/clawbacks": typeof settlement_clawbacks;
|
|
@@ -5473,6 +5473,20 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
5473
5473
|
};
|
|
5474
5474
|
products: {
|
|
5475
5475
|
mutations: {
|
|
5476
|
+
adoptProduct: FunctionReference<
|
|
5477
|
+
"mutation",
|
|
5478
|
+
"internal",
|
|
5479
|
+
{
|
|
5480
|
+
accountId?: string;
|
|
5481
|
+
active: boolean;
|
|
5482
|
+
description?: string;
|
|
5483
|
+
metadata?: any;
|
|
5484
|
+
name: string;
|
|
5485
|
+
stripeProductId: string;
|
|
5486
|
+
},
|
|
5487
|
+
null,
|
|
5488
|
+
Name
|
|
5489
|
+
>;
|
|
5476
5490
|
upsertPrice: FunctionReference<
|
|
5477
5491
|
"mutation",
|
|
5478
5492
|
"internal",
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
2
|
|
|
3
3
|
import { mutation } from "../_generated/server";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
priceIntervalValidator,
|
|
6
|
+
priceTypeValidator,
|
|
7
|
+
productFields,
|
|
8
|
+
} from "./validators";
|
|
5
9
|
|
|
6
10
|
// =============================================================================
|
|
7
11
|
// PRODUCT MUTATIONS
|
|
@@ -35,6 +39,39 @@ export const upsertProduct = mutation({
|
|
|
35
39
|
},
|
|
36
40
|
});
|
|
37
41
|
|
|
42
|
+
/** Atomically import provider fields without reassigning a claimed owner. */
|
|
43
|
+
export const adoptProduct = mutation({
|
|
44
|
+
args: productFields,
|
|
45
|
+
returns: v.null(),
|
|
46
|
+
handler: async (ctx, args) => {
|
|
47
|
+
const existing = await ctx.db
|
|
48
|
+
.query("products")
|
|
49
|
+
.withIndex("by_stripe_product_id", (q) =>
|
|
50
|
+
q.eq("stripeProductId", args.stripeProductId),
|
|
51
|
+
)
|
|
52
|
+
.first();
|
|
53
|
+
if (
|
|
54
|
+
existing?.accountId !== undefined &&
|
|
55
|
+
args.accountId !== undefined &&
|
|
56
|
+
existing.accountId !== args.accountId
|
|
57
|
+
) {
|
|
58
|
+
throw new ConvexError({
|
|
59
|
+
code: "INVALID_CONFIGURATION",
|
|
60
|
+
message: "Product ownership cannot be reassigned by adoption",
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (existing) {
|
|
64
|
+
await ctx.db.patch("products", existing._id, {
|
|
65
|
+
...args,
|
|
66
|
+
accountId: existing.accountId ?? args.accountId,
|
|
67
|
+
});
|
|
68
|
+
} else {
|
|
69
|
+
await ctx.db.insert("products", args);
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
38
75
|
// =============================================================================
|
|
39
76
|
// PRICE MUTATIONS
|
|
40
77
|
// =============================================================================
|