@better-auth/stripe 1.4.17 → 1.4.19
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/.turbo/turbo-build.log +10 -8
- package/dist/client.d.mts +3 -2
- package/dist/client.mjs +2 -1
- package/dist/client.mjs.map +1 -0
- package/dist/{index-BTvn0abC.d.mts → index-BfL0mL4D.d.mts} +3 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +84 -57
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -5
- package/src/client.ts +1 -1
- package/src/error-codes.ts +2 -0
- package/src/hooks.ts +10 -5
- package/src/index.ts +10 -6
- package/src/metadata.ts +94 -0
- package/src/middleware.ts +1 -1
- package/src/routes.ts +144 -155
- package/test/stripe-organization.test.ts +131 -4
- package/test/stripe.test.ts +334 -5
- package/tsdown.config.ts +1 -0
- package/CHANGELOG.md +0 -22
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @better-auth/stripe@1.4.
|
|
2
|
+
> @better-auth/stripe@1.4.19 build /home/runner/work/better-auth/better-auth/packages/stripe
|
|
3
3
|
> tsdown
|
|
4
4
|
|
|
5
5
|
[34mℹ[39m tsdown [2mv0.17.2[22m powered by rolldown [2mv1.0.0-beta.53[22m
|
|
@@ -7,10 +7,12 @@
|
|
|
7
7
|
[34mℹ[39m entry: [34msrc/index.ts, src/client.ts[39m
|
|
8
8
|
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
9
|
[34mℹ[39m Build start
|
|
10
|
-
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [
|
|
11
|
-
[34mℹ[39m [2mdist/[22m[1mclient.mjs[22m [2m
|
|
12
|
-
[34mℹ[39m [2mdist/[
|
|
13
|
-
[34mℹ[39m [2mdist/[
|
|
14
|
-
[34mℹ[39m [2mdist/[22m[
|
|
15
|
-
[34mℹ[39m
|
|
16
|
-
[
|
|
10
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m 59.30 kB[22m [2m│ gzip: 10.90 kB[22m
|
|
11
|
+
[34mℹ[39m [2mdist/[22m[1mclient.mjs[22m [2m 0.30 kB[22m [2m│ gzip: 0.23 kB[22m
|
|
12
|
+
[34mℹ[39m [2mdist/[22mindex.mjs.map [2m125.43 kB[22m [2m│ gzip: 23.81 kB[22m
|
|
13
|
+
[34mℹ[39m [2mdist/[22mclient.mjs.map [2m 1.10 kB[22m [2m│ gzip: 0.53 kB[22m
|
|
14
|
+
[34mℹ[39m [2mdist/[22m[32m[1mclient.d.mts[22m[39m [2m 0.66 kB[22m [2m│ gzip: 0.37 kB[22m
|
|
15
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 0.21 kB[22m [2m│ gzip: 0.14 kB[22m
|
|
16
|
+
[34mℹ[39m [2mdist/[22m[32mindex-BfL0mL4D.d.mts[39m [2m 30.51 kB[22m [2m│ gzip: 5.24 kB[22m
|
|
17
|
+
[34mℹ[39m 7 files, total: 217.51 kB
|
|
18
|
+
[32m✔[39m Build complete in [32m22270ms[39m
|
package/dist/client.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as stripe } from "./index-
|
|
1
|
+
import { n as stripe } from "./index-BfL0mL4D.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/client.d.ts
|
|
4
4
|
declare const stripeClient: <O extends {
|
|
@@ -22,4 +22,5 @@ declare const stripeClient: <O extends {
|
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
24
|
//#endregion
|
|
25
|
-
export { stripeClient };
|
|
25
|
+
export { stripeClient };
|
|
26
|
+
//# sourceMappingURL=client.d.mts.map
|
package/dist/client.mjs
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth/client\";\nimport type { stripe } from \"./index\";\n\nexport const stripeClient = <\n\tO extends {\n\t\tsubscription: boolean;\n\t},\n>(\n\toptions?: O | undefined,\n) => {\n\treturn {\n\t\tid: \"stripe-client\",\n\t\t$InferServerPlugin: {} as ReturnType<\n\t\t\ttypeof stripe<\n\t\t\t\tO[\"subscription\"] extends true\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tstripeClient: any;\n\t\t\t\t\t\t\tstripeWebhookSecret: string;\n\t\t\t\t\t\t\tsubscription: {\n\t\t\t\t\t\t\t\tenabled: true;\n\t\t\t\t\t\t\t\tplans: [];\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\tstripeClient: any;\n\t\t\t\t\t\t\tstripeWebhookSecret: string;\n\t\t\t\t\t\t}\n\t\t\t>\n\t\t>,\n\t\tpathMethods: {\n\t\t\t\"/subscription/billing-portal\": \"POST\",\n\t\t\t\"/subscription/restore\": \"POST\",\n\t\t},\n\t} satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";AAGA,MAAa,gBAKZ,YACI;AACJ,QAAO;EACN,IAAI;EACJ,oBAAoB,EAAE;EAiBtB,aAAa;GACZ,gCAAgC;GAChC,yBAAyB;GACzB;EACD"}
|
|
@@ -936,10 +936,12 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
936
936
|
readonly SUBSCRIPTION_NOT_SCHEDULED_FOR_CANCELLATION: "Subscription is not scheduled for cancellation";
|
|
937
937
|
readonly ORGANIZATION_NOT_FOUND: "Organization not found";
|
|
938
938
|
readonly ORGANIZATION_SUBSCRIPTION_NOT_ENABLED: "Organization subscription is not enabled";
|
|
939
|
+
readonly AUTHORIZE_REFERENCE_REQUIRED: "Organization subscriptions require authorizeReference callback to be configured";
|
|
939
940
|
readonly ORGANIZATION_HAS_ACTIVE_SUBSCRIPTION: "Cannot delete organization with active subscription";
|
|
940
941
|
readonly ORGANIZATION_REFERENCE_ID_REQUIRED: "Reference ID is required. Provide referenceId or set activeOrganizationId in session";
|
|
941
942
|
};
|
|
942
943
|
};
|
|
943
944
|
type StripePlugin<O extends StripeOptions> = ReturnType<typeof stripe<O>>;
|
|
944
945
|
//#endregion
|
|
945
|
-
export { SubscriptionOptions as a, Subscription as i, stripe as n, StripePlan as r, StripePlugin as t };
|
|
946
|
+
export { SubscriptionOptions as a, Subscription as i, stripe as n, StripePlan as r, StripePlugin as t };
|
|
947
|
+
//# sourceMappingURL=index-BfL0mL4D.d.mts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as SubscriptionOptions, i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-
|
|
1
|
+
import { a as SubscriptionOptions, i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-BfL0mL4D.mjs";
|
|
2
2
|
export { StripePlan, StripePlugin, Subscription, SubscriptionOptions, stripe };
|
package/dist/index.mjs
CHANGED
|
@@ -27,10 +27,54 @@ const STRIPE_ERROR_CODES = defineErrorCodes({
|
|
|
27
27
|
SUBSCRIPTION_NOT_SCHEDULED_FOR_CANCELLATION: "Subscription is not scheduled for cancellation",
|
|
28
28
|
ORGANIZATION_NOT_FOUND: "Organization not found",
|
|
29
29
|
ORGANIZATION_SUBSCRIPTION_NOT_ENABLED: "Organization subscription is not enabled",
|
|
30
|
+
AUTHORIZE_REFERENCE_REQUIRED: "Organization subscriptions require authorizeReference callback to be configured",
|
|
30
31
|
ORGANIZATION_HAS_ACTIVE_SUBSCRIPTION: "Cannot delete organization with active subscription",
|
|
31
32
|
ORGANIZATION_REFERENCE_ID_REQUIRED: "Reference ID is required. Provide referenceId or set activeOrganizationId in session"
|
|
32
33
|
});
|
|
33
34
|
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/metadata.ts
|
|
37
|
+
/**
|
|
38
|
+
* Customer metadata - set internal fields and extract typed fields.
|
|
39
|
+
*/
|
|
40
|
+
const customerMetadata = {
|
|
41
|
+
keys: {
|
|
42
|
+
userId: "userId",
|
|
43
|
+
organizationId: "organizationId",
|
|
44
|
+
customerType: "customerType"
|
|
45
|
+
},
|
|
46
|
+
set(internalFields, ...userMetadata) {
|
|
47
|
+
return defu(internalFields, ...userMetadata.filter(Boolean));
|
|
48
|
+
},
|
|
49
|
+
get(metadata) {
|
|
50
|
+
return {
|
|
51
|
+
userId: metadata?.userId,
|
|
52
|
+
organizationId: metadata?.organizationId,
|
|
53
|
+
customerType: metadata?.customerType
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Subscription/Checkout metadata - set internal fields and extract typed fields.
|
|
59
|
+
*/
|
|
60
|
+
const subscriptionMetadata = {
|
|
61
|
+
keys: {
|
|
62
|
+
userId: "userId",
|
|
63
|
+
subscriptionId: "subscriptionId",
|
|
64
|
+
referenceId: "referenceId"
|
|
65
|
+
},
|
|
66
|
+
set(internalFields, ...userMetadata) {
|
|
67
|
+
return defu(internalFields, ...userMetadata.filter(Boolean));
|
|
68
|
+
},
|
|
69
|
+
get(metadata) {
|
|
70
|
+
return {
|
|
71
|
+
userId: metadata?.userId,
|
|
72
|
+
subscriptionId: metadata?.subscriptionId,
|
|
73
|
+
referenceId: metadata?.referenceId
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
34
78
|
//#endregion
|
|
35
79
|
//#region src/utils.ts
|
|
36
80
|
async function getPlans(subscriptionOptions) {
|
|
@@ -120,8 +164,9 @@ async function onCheckoutSessionCompleted(ctx, options, event) {
|
|
|
120
164
|
const priceLookupKey = subscriptionItem.price.lookup_key;
|
|
121
165
|
const plan = await getPlanByPriceInfo(options, priceId, priceLookupKey);
|
|
122
166
|
if (plan) {
|
|
123
|
-
const
|
|
124
|
-
const
|
|
167
|
+
const checkoutMeta = subscriptionMetadata.get(checkoutSession?.metadata);
|
|
168
|
+
const referenceId = checkoutSession?.client_reference_id || checkoutMeta.referenceId;
|
|
169
|
+
const { subscriptionId } = checkoutMeta;
|
|
125
170
|
const seats = subscriptionItem.quantity;
|
|
126
171
|
if (referenceId && subscriptionId) {
|
|
127
172
|
const trial = subscription.trial_start && subscription.trial_end ? {
|
|
@@ -179,7 +224,7 @@ async function onSubscriptionCreated(ctx, options, event) {
|
|
|
179
224
|
ctx.context.logger.warn(`Stripe webhook warning: customer.subscription.created event received without customer ID`);
|
|
180
225
|
return;
|
|
181
226
|
}
|
|
182
|
-
const subscriptionId = subscriptionCreated.metadata
|
|
227
|
+
const { subscriptionId } = subscriptionMetadata.get(subscriptionCreated.metadata);
|
|
183
228
|
const existingSubscription = await ctx.context.adapter.findOne({
|
|
184
229
|
model: "subscription",
|
|
185
230
|
where: subscriptionId ? [{
|
|
@@ -256,7 +301,7 @@ async function onSubscriptionUpdated(ctx, options, event) {
|
|
|
256
301
|
const priceId = subscriptionItem.price.id;
|
|
257
302
|
const priceLookupKey = subscriptionItem.price.lookup_key;
|
|
258
303
|
const plan = await getPlanByPriceInfo(options, priceId, priceLookupKey);
|
|
259
|
-
const subscriptionId = subscriptionUpdated.metadata
|
|
304
|
+
const { subscriptionId } = subscriptionMetadata.get(subscriptionUpdated.metadata);
|
|
260
305
|
const customerId = subscriptionUpdated.customer?.toString();
|
|
261
306
|
let subscription = await ctx.context.adapter.findOne({
|
|
262
307
|
model: "subscription",
|
|
@@ -378,7 +423,7 @@ const referenceMiddleware = (subscriptionOptions, action) => createAuthMiddlewar
|
|
|
378
423
|
if (customerType === "organization") {
|
|
379
424
|
if (!subscriptionOptions.authorizeReference) {
|
|
380
425
|
ctx.context.logger.error(`Organization subscriptions require authorizeReference to be defined in your stripe plugin config.`);
|
|
381
|
-
throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.
|
|
426
|
+
throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.AUTHORIZE_REFERENCE_REQUIRED });
|
|
382
427
|
}
|
|
383
428
|
const referenceId = explicitReferenceId || ctxSession.session.activeOrganizationId;
|
|
384
429
|
if (!referenceId) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.ORGANIZATION_REFERENCE_ID_REQUIRED });
|
|
@@ -493,21 +538,15 @@ const upgradeSubscription = (options) => {
|
|
|
493
538
|
if (!user$1.emailVerified && subscriptionOptions.requireEmailVerification) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED });
|
|
494
539
|
const plan = await getPlanByName(options, ctx.body.plan);
|
|
495
540
|
if (!plan) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.SUBSCRIPTION_PLAN_NOT_FOUND });
|
|
496
|
-
|
|
541
|
+
const subscriptionToUpdate = ctx.body.subscriptionId ? await ctx.context.adapter.findOne({
|
|
497
542
|
model: "subscription",
|
|
498
543
|
where: [{
|
|
499
544
|
field: "stripeSubscriptionId",
|
|
500
545
|
value: ctx.body.subscriptionId
|
|
501
546
|
}]
|
|
502
|
-
}) : referenceId ? await ctx.context.adapter.findOne({
|
|
503
|
-
model: "subscription",
|
|
504
|
-
where: [{
|
|
505
|
-
field: "referenceId",
|
|
506
|
-
value: referenceId
|
|
507
|
-
}]
|
|
508
547
|
}) : null;
|
|
509
|
-
if (ctx.body.subscriptionId && subscriptionToUpdate && subscriptionToUpdate.referenceId !== referenceId) subscriptionToUpdate = null;
|
|
510
548
|
if (ctx.body.subscriptionId && !subscriptionToUpdate) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.SUBSCRIPTION_NOT_FOUND });
|
|
549
|
+
if (ctx.body.subscriptionId && subscriptionToUpdate && subscriptionToUpdate.referenceId !== referenceId) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.SUBSCRIPTION_NOT_FOUND });
|
|
511
550
|
let customerId;
|
|
512
551
|
if (customerType === "organization") {
|
|
513
552
|
customerId = subscriptionToUpdate?.stripeCustomerId;
|
|
@@ -523,7 +562,7 @@ const upgradeSubscription = (options) => {
|
|
|
523
562
|
customerId = org.stripeCustomerId;
|
|
524
563
|
if (!customerId) try {
|
|
525
564
|
let stripeCustomer = (await client.customers.search({
|
|
526
|
-
query: `metadata["organizationId"]:"${org.id}"`,
|
|
565
|
+
query: `metadata["${customerMetadata.keys.organizationId}"]:"${org.id}"`,
|
|
527
566
|
limit: 1
|
|
528
567
|
})).data[0];
|
|
529
568
|
if (!stripeCustomer) {
|
|
@@ -531,11 +570,10 @@ const upgradeSubscription = (options) => {
|
|
|
531
570
|
if (options.organization?.getCustomerCreateParams) extraCreateParams = await options.organization.getCustomerCreateParams(org, ctx);
|
|
532
571
|
const customerParams = defu({
|
|
533
572
|
name: org.name,
|
|
534
|
-
metadata: {
|
|
535
|
-
...ctx.body.metadata,
|
|
573
|
+
metadata: customerMetadata.set({
|
|
536
574
|
organizationId: org.id,
|
|
537
575
|
customerType: "organization"
|
|
538
|
-
}
|
|
576
|
+
}, ctx.body.metadata)
|
|
539
577
|
}, extraCreateParams);
|
|
540
578
|
stripeCustomer = await client.customers.create(customerParams);
|
|
541
579
|
await options.organization?.onCustomerCreate?.({
|
|
@@ -564,17 +602,16 @@ const upgradeSubscription = (options) => {
|
|
|
564
602
|
customerId = subscriptionToUpdate?.stripeCustomerId || user$1.stripeCustomerId;
|
|
565
603
|
if (!customerId) try {
|
|
566
604
|
let stripeCustomer = (await client.customers.search({
|
|
567
|
-
query: `email:"${escapeStripeSearchValue(user$1.email)}" AND -metadata["customerType"]:"organization"`,
|
|
605
|
+
query: `email:"${escapeStripeSearchValue(user$1.email)}" AND -metadata["${customerMetadata.keys.customerType}"]:"organization"`,
|
|
568
606
|
limit: 1
|
|
569
607
|
})).data[0];
|
|
570
608
|
if (!stripeCustomer) stripeCustomer = await client.customers.create({
|
|
571
609
|
email: user$1.email,
|
|
572
610
|
name: user$1.name,
|
|
573
|
-
metadata: {
|
|
574
|
-
...ctx.body.metadata,
|
|
611
|
+
metadata: customerMetadata.set({
|
|
575
612
|
userId: user$1.id,
|
|
576
613
|
customerType: "user"
|
|
577
|
-
}
|
|
614
|
+
}, ctx.body.metadata)
|
|
578
615
|
});
|
|
579
616
|
await ctx.context.adapter.update({
|
|
580
617
|
model: "user",
|
|
@@ -603,8 +640,18 @@ const upgradeSubscription = (options) => {
|
|
|
603
640
|
if (activeOrTrialingSubscription?.stripeSubscriptionId) return sub.id === activeOrTrialingSubscription.stripeSubscriptionId;
|
|
604
641
|
return false;
|
|
605
642
|
});
|
|
643
|
+
const stripeSubscriptionPriceId = activeSubscription?.items.data[0]?.price.id;
|
|
606
644
|
const incompleteSubscription = subscriptions$1.find((sub) => sub.status === "incomplete");
|
|
607
|
-
|
|
645
|
+
const priceId = ctx.body.annual ? plan.annualDiscountPriceId : plan.priceId;
|
|
646
|
+
const lookupKey = ctx.body.annual ? plan.annualDiscountLookupKey : plan.lookupKey;
|
|
647
|
+
const resolvedPriceId = lookupKey ? await resolvePriceIdFromLookupKey(client, lookupKey) : void 0;
|
|
648
|
+
const priceIdToUse = priceId || resolvedPriceId;
|
|
649
|
+
if (!priceIdToUse) throw ctx.error("BAD_REQUEST", { message: "Price ID not found for the selected plan" });
|
|
650
|
+
const isSamePlan = activeOrTrialingSubscription?.plan === ctx.body.plan;
|
|
651
|
+
const isSameSeats = activeOrTrialingSubscription?.seats === (ctx.body.seats || 1);
|
|
652
|
+
const isSamePriceId = stripeSubscriptionPriceId === priceIdToUse;
|
|
653
|
+
const isSubscriptionStillValid = !activeOrTrialingSubscription?.periodEnd || activeOrTrialingSubscription.periodEnd > /* @__PURE__ */ new Date();
|
|
654
|
+
if (activeOrTrialingSubscription?.status === "active" && isSamePlan && isSameSeats && isSamePriceId && isSubscriptionStillValid) throw new APIError$1("BAD_REQUEST", { message: STRIPE_ERROR_CODES.ALREADY_SUBSCRIBED_PLAN });
|
|
608
655
|
if (activeSubscription && customerId) {
|
|
609
656
|
let dbSubscription = await ctx.context.adapter.findOne({
|
|
610
657
|
model: "subscription",
|
|
@@ -627,15 +674,6 @@ const upgradeSubscription = (options) => {
|
|
|
627
674
|
});
|
|
628
675
|
dbSubscription = activeOrTrialingSubscription;
|
|
629
676
|
}
|
|
630
|
-
let priceIdToUse$1 = void 0;
|
|
631
|
-
if (ctx.body.annual) {
|
|
632
|
-
priceIdToUse$1 = plan.annualDiscountPriceId;
|
|
633
|
-
if (!priceIdToUse$1 && plan.annualDiscountLookupKey) priceIdToUse$1 = await resolvePriceIdFromLookupKey(client, plan.annualDiscountLookupKey);
|
|
634
|
-
} else {
|
|
635
|
-
priceIdToUse$1 = plan.priceId;
|
|
636
|
-
if (!priceIdToUse$1 && plan.lookupKey) priceIdToUse$1 = await resolvePriceIdFromLookupKey(client, plan.lookupKey);
|
|
637
|
-
}
|
|
638
|
-
if (!priceIdToUse$1) throw ctx.error("BAD_REQUEST", { message: "Price ID not found for the selected plan" });
|
|
639
677
|
const { url } = await client.billingPortal.sessions.create({
|
|
640
678
|
customer: customerId,
|
|
641
679
|
return_url: getUrl(ctx, ctx.body.returnUrl || "/"),
|
|
@@ -650,7 +688,7 @@ const upgradeSubscription = (options) => {
|
|
|
650
688
|
items: [{
|
|
651
689
|
id: activeSubscription.items.data[0]?.id,
|
|
652
690
|
quantity: ctx.body.seats || 1,
|
|
653
|
-
price: priceIdToUse
|
|
691
|
+
price: priceIdToUse
|
|
654
692
|
}]
|
|
655
693
|
}
|
|
656
694
|
}
|
|
@@ -707,14 +745,6 @@ const upgradeSubscription = (options) => {
|
|
|
707
745
|
})).some((s) => {
|
|
708
746
|
return !!(s.trialStart || s.trialEnd) || s.status === "trialing";
|
|
709
747
|
}) && plan.freeTrial ? { trial_period_days: plan.freeTrial.days } : void 0;
|
|
710
|
-
let priceIdToUse = void 0;
|
|
711
|
-
if (ctx.body.annual) {
|
|
712
|
-
priceIdToUse = plan.annualDiscountPriceId;
|
|
713
|
-
if (!priceIdToUse && plan.annualDiscountLookupKey) priceIdToUse = await resolvePriceIdFromLookupKey(client, plan.annualDiscountLookupKey);
|
|
714
|
-
} else {
|
|
715
|
-
priceIdToUse = plan.priceId;
|
|
716
|
-
if (!priceIdToUse && plan.lookupKey) priceIdToUse = await resolvePriceIdFromLookupKey(client, plan.lookupKey);
|
|
717
|
-
}
|
|
718
748
|
const checkoutSession = await client.checkout.sessions.create({
|
|
719
749
|
...customerId ? {
|
|
720
750
|
customer: customerId,
|
|
@@ -732,24 +762,20 @@ const upgradeSubscription = (options) => {
|
|
|
732
762
|
}],
|
|
733
763
|
subscription_data: {
|
|
734
764
|
...freeTrial,
|
|
735
|
-
metadata: {
|
|
736
|
-
...ctx.body.metadata,
|
|
737
|
-
...params?.params?.subscription_data?.metadata,
|
|
765
|
+
metadata: subscriptionMetadata.set({
|
|
738
766
|
userId: user$1.id,
|
|
739
767
|
subscriptionId: subscription.id,
|
|
740
768
|
referenceId
|
|
741
|
-
}
|
|
769
|
+
}, ctx.body.metadata, params?.params?.subscription_data?.metadata)
|
|
742
770
|
},
|
|
743
771
|
mode: "subscription",
|
|
744
772
|
client_reference_id: referenceId,
|
|
745
773
|
...params?.params,
|
|
746
|
-
metadata: {
|
|
747
|
-
...ctx.body.metadata,
|
|
748
|
-
...params?.params?.metadata,
|
|
774
|
+
metadata: subscriptionMetadata.set({
|
|
749
775
|
userId: user$1.id,
|
|
750
776
|
subscriptionId: subscription.id,
|
|
751
777
|
referenceId
|
|
752
|
-
}
|
|
778
|
+
}, ctx.body.metadata, params?.params?.metadata)
|
|
753
779
|
}, params?.options).catch(async (e) => {
|
|
754
780
|
throw ctx.error("BAD_REQUEST", {
|
|
755
781
|
message: e.message,
|
|
@@ -773,11 +799,9 @@ const cancelSubscriptionCallback = (options) => {
|
|
|
773
799
|
use: [originCheck((ctx) => ctx.query.callbackURL)]
|
|
774
800
|
}, async (ctx) => {
|
|
775
801
|
if (!ctx.query || !ctx.query.callbackURL || !ctx.query.subscriptionId) throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
776
|
-
|
|
777
|
-
if (!session) throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
778
|
-
const { user: user$1 } = session;
|
|
802
|
+
if (!await getSessionFromCtx(ctx)) throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
779
803
|
const { callbackURL, subscriptionId } = ctx.query;
|
|
780
|
-
|
|
804
|
+
try {
|
|
781
805
|
const subscription = await ctx.context.adapter.findOne({
|
|
782
806
|
model: "subscription",
|
|
783
807
|
where: [{
|
|
@@ -786,8 +810,10 @@ const cancelSubscriptionCallback = (options) => {
|
|
|
786
810
|
}]
|
|
787
811
|
});
|
|
788
812
|
if (!subscription || subscription.status === "canceled" || isPendingCancel(subscription)) throw ctx.redirect(getUrl(ctx, callbackURL));
|
|
813
|
+
const customerId = subscription.stripeCustomerId;
|
|
814
|
+
if (!customerId) throw ctx.redirect(getUrl(ctx, callbackURL));
|
|
789
815
|
const currentSubscription = (await client.subscriptions.list({
|
|
790
|
-
customer:
|
|
816
|
+
customer: customerId,
|
|
791
817
|
status: "active"
|
|
792
818
|
})).data.find((sub) => sub.id === subscription.stripeSubscriptionId);
|
|
793
819
|
if (currentSubscription && isStripePendingCancel(currentSubscription) && !isPendingCancel(subscription)) {
|
|
@@ -1400,7 +1426,7 @@ const stripe = (options) => {
|
|
|
1400
1426
|
if (!ctx$1 || !options.createCustomerOnSignUp || user$1.stripeCustomerId) return;
|
|
1401
1427
|
try {
|
|
1402
1428
|
let stripeCustomer = (await client.customers.search({
|
|
1403
|
-
query: `email:"${escapeStripeSearchValue(user$1.email)}" AND -metadata["customerType"]:"organization"`,
|
|
1429
|
+
query: `email:"${escapeStripeSearchValue(user$1.email)}" AND -metadata["${customerMetadata.keys.customerType}"]:"organization"`,
|
|
1404
1430
|
limit: 1
|
|
1405
1431
|
})).data[0];
|
|
1406
1432
|
if (stripeCustomer) {
|
|
@@ -1420,10 +1446,10 @@ const stripe = (options) => {
|
|
|
1420
1446
|
const params = defu({
|
|
1421
1447
|
email: user$1.email,
|
|
1422
1448
|
name: user$1.name,
|
|
1423
|
-
metadata: {
|
|
1449
|
+
metadata: customerMetadata.set({
|
|
1424
1450
|
userId: user$1.id,
|
|
1425
1451
|
customerType: "user"
|
|
1426
|
-
}
|
|
1452
|
+
}, extraCreateParams?.metadata)
|
|
1427
1453
|
}, extraCreateParams);
|
|
1428
1454
|
stripeCustomer = await client.customers.create(params);
|
|
1429
1455
|
await ctx$1.context.internalAdapter.updateUser(user$1.id, { stripeCustomerId: stripeCustomer.id });
|
|
@@ -1464,4 +1490,5 @@ const stripe = (options) => {
|
|
|
1464
1490
|
};
|
|
1465
1491
|
|
|
1466
1492
|
//#endregion
|
|
1467
|
-
export { stripe };
|
|
1493
|
+
export { stripe };
|
|
1494
|
+
//# sourceMappingURL=index.mjs.map
|