@better-auth/stripe 1.2.4-beta.1 → 1.2.4-beta.12
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 +4 -4
- package/dist/client.d.cts +2 -2
- package/dist/client.d.mts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/index.cjs +139 -64
- package/dist/index.d.cts +72 -8
- package/dist/index.d.mts +72 -8
- package/dist/index.d.ts +72 -8
- package/dist/index.mjs +140 -65
- package/package.json +2 -2
- package/src/client.ts +2 -2
- package/src/hooks.ts +3 -3
- package/src/index.ts +177 -73
- package/src/stripe.test.ts +15 -7
- package/src/types.ts +11 -4
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @better-auth/stripe@1.2.4-beta.
|
|
2
|
+
> @better-auth/stripe@1.2.4-beta.12 build /home/runner/work/better-auth/better-auth/packages/stripe
|
|
3
3
|
> unbuild
|
|
4
4
|
|
|
5
5
|
[info] Automatically detected entries: src/index, src/client [esm] [cjs] [dts]
|
|
6
6
|
[info] Building stripe
|
|
7
7
|
[success] Build succeeded for stripe
|
|
8
|
-
[log] dist/index.cjs (total size:
|
|
8
|
+
[log] dist/index.cjs (total size: 33.4 kB, chunk size: 33.4 kB, exports: stripe)
|
|
9
9
|
|
|
10
10
|
[log] dist/client.cjs (total size: 160 B, chunk size: 160 B, exports: stripeClient)
|
|
11
11
|
|
|
12
|
-
[log] dist/index.mjs (total size:
|
|
12
|
+
[log] dist/index.mjs (total size: 33.1 kB, chunk size: 33.1 kB, exports: stripe)
|
|
13
13
|
|
|
14
14
|
[log] dist/client.mjs (total size: 133 B, chunk size: 133 B, exports: stripeClient)
|
|
15
15
|
|
|
16
|
-
Σ Total dist size (byte size):
|
|
16
|
+
Σ Total dist size (byte size): 184 kB
|
|
17
17
|
[log]
|
package/dist/client.d.cts
CHANGED
|
@@ -11,14 +11,14 @@ declare const stripeClient: <O extends {
|
|
|
11
11
|
id: "stripe-client";
|
|
12
12
|
$InferServerPlugin: ReturnType<typeof stripe<O["subscription"] extends true ? {
|
|
13
13
|
stripeClient: any;
|
|
14
|
-
stripeWebhookSecret:
|
|
14
|
+
stripeWebhookSecret: string;
|
|
15
15
|
subscription: {
|
|
16
16
|
enabled: true;
|
|
17
17
|
plans: [];
|
|
18
18
|
};
|
|
19
19
|
} : {
|
|
20
20
|
stripeClient: any;
|
|
21
|
-
stripeWebhookSecret:
|
|
21
|
+
stripeWebhookSecret: string;
|
|
22
22
|
}>>;
|
|
23
23
|
};
|
|
24
24
|
|
package/dist/client.d.mts
CHANGED
|
@@ -11,14 +11,14 @@ declare const stripeClient: <O extends {
|
|
|
11
11
|
id: "stripe-client";
|
|
12
12
|
$InferServerPlugin: ReturnType<typeof stripe<O["subscription"] extends true ? {
|
|
13
13
|
stripeClient: any;
|
|
14
|
-
stripeWebhookSecret:
|
|
14
|
+
stripeWebhookSecret: string;
|
|
15
15
|
subscription: {
|
|
16
16
|
enabled: true;
|
|
17
17
|
plans: [];
|
|
18
18
|
};
|
|
19
19
|
} : {
|
|
20
20
|
stripeClient: any;
|
|
21
|
-
stripeWebhookSecret:
|
|
21
|
+
stripeWebhookSecret: string;
|
|
22
22
|
}>>;
|
|
23
23
|
};
|
|
24
24
|
|
package/dist/client.d.ts
CHANGED
|
@@ -11,14 +11,14 @@ declare const stripeClient: <O extends {
|
|
|
11
11
|
id: "stripe-client";
|
|
12
12
|
$InferServerPlugin: ReturnType<typeof stripe<O["subscription"] extends true ? {
|
|
13
13
|
stripeClient: any;
|
|
14
|
-
stripeWebhookSecret:
|
|
14
|
+
stripeWebhookSecret: string;
|
|
15
15
|
subscription: {
|
|
16
16
|
enabled: true;
|
|
17
17
|
plans: [];
|
|
18
18
|
};
|
|
19
19
|
} : {
|
|
20
20
|
stripeClient: any;
|
|
21
|
-
stripeWebhookSecret:
|
|
21
|
+
stripeWebhookSecret: string;
|
|
22
22
|
}>>;
|
|
23
23
|
};
|
|
24
24
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const betterAuth = require('better-auth');
|
|
3
4
|
const plugins = require('better-auth/plugins');
|
|
4
5
|
const zod = require('zod');
|
|
5
6
|
const api = require('better-auth/api');
|
|
6
7
|
const crypto = require('better-auth/crypto');
|
|
7
|
-
const betterAuth = require('better-auth');
|
|
8
8
|
|
|
9
9
|
async function getPlans(options) {
|
|
10
10
|
return typeof options?.subscription?.plans === "function" ? await options.subscription?.plans() : options.subscription?.plans;
|
|
@@ -95,11 +95,11 @@ async function onSubscriptionUpdated(ctx, options, event) {
|
|
|
95
95
|
const subscriptionUpdated = event.data.object;
|
|
96
96
|
const priceId = subscriptionUpdated.items.data[0].price.id;
|
|
97
97
|
const plan = await getPlanByPriceId(options, priceId);
|
|
98
|
-
const
|
|
98
|
+
const subscriptionId = subscriptionUpdated.metadata?.subscriptionId;
|
|
99
99
|
const customerId = subscriptionUpdated.customer?.toString();
|
|
100
100
|
let subscription = await ctx.context.adapter.findOne({
|
|
101
101
|
model: "subscription",
|
|
102
|
-
where:
|
|
102
|
+
where: subscriptionId ? [{ field: "id", value: subscriptionId }] : [{ field: "stripeSubscriptionId", value: subscriptionUpdated.id }]
|
|
103
103
|
});
|
|
104
104
|
if (!subscription) {
|
|
105
105
|
const subs = await ctx.context.adapter.findMany({
|
|
@@ -313,25 +313,64 @@ const stripe = (options) => {
|
|
|
313
313
|
{
|
|
314
314
|
method: "POST",
|
|
315
315
|
body: zod.z.object({
|
|
316
|
+
/**
|
|
317
|
+
* The name of the plan to subscribe
|
|
318
|
+
*/
|
|
316
319
|
plan: zod.z.string({
|
|
317
320
|
description: "The name of the plan to upgrade to"
|
|
318
321
|
}),
|
|
322
|
+
/**
|
|
323
|
+
* If annual plan should be applied.
|
|
324
|
+
*/
|
|
319
325
|
annual: zod.z.boolean({
|
|
320
326
|
description: "Whether to upgrade to an annual plan"
|
|
321
327
|
}).optional(),
|
|
322
|
-
|
|
328
|
+
/**
|
|
329
|
+
* Reference id of the subscription to upgrade
|
|
330
|
+
* This is used to identify the subscription to upgrade
|
|
331
|
+
* If not provided, the user's id will be used
|
|
332
|
+
*/
|
|
333
|
+
referenceId: zod.z.string({
|
|
334
|
+
description: "Reference id of the subscription to upgrade"
|
|
335
|
+
}).optional(),
|
|
336
|
+
/**
|
|
337
|
+
* This is to allow a specific subscription to be upgrade.
|
|
338
|
+
* If subscription id is provided, and subscription isn't found,
|
|
339
|
+
* it'll throw an error.
|
|
340
|
+
*/
|
|
341
|
+
subscriptionId: zod.z.string({
|
|
342
|
+
description: "The id of the subscription to upgrade"
|
|
343
|
+
}).optional(),
|
|
344
|
+
/**
|
|
345
|
+
* Any additional data you want to store in your database
|
|
346
|
+
* subscriptions
|
|
347
|
+
*/
|
|
323
348
|
metadata: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
349
|
+
/**
|
|
350
|
+
* If a subscription
|
|
351
|
+
*/
|
|
324
352
|
seats: zod.z.number({
|
|
325
353
|
description: "Number of seats to upgrade to (if applicable)"
|
|
326
354
|
}).optional(),
|
|
327
|
-
|
|
355
|
+
/**
|
|
356
|
+
* Success url to redirect back after successful subscription
|
|
357
|
+
*/
|
|
328
358
|
successUrl: zod.z.string({
|
|
329
359
|
description: "callback url to redirect back after successful subscription"
|
|
330
360
|
}).default("/"),
|
|
361
|
+
/**
|
|
362
|
+
* Cancel URL
|
|
363
|
+
*/
|
|
331
364
|
cancelUrl: zod.z.string({
|
|
332
365
|
description: "callback url to redirect back after successful subscription"
|
|
333
366
|
}).default("/"),
|
|
367
|
+
/**
|
|
368
|
+
* Return URL
|
|
369
|
+
*/
|
|
334
370
|
returnUrl: zod.z.string().optional(),
|
|
371
|
+
/**
|
|
372
|
+
* Disable Redirect
|
|
373
|
+
*/
|
|
335
374
|
disableRedirect: zod.z.boolean().default(false)
|
|
336
375
|
}),
|
|
337
376
|
use: [
|
|
@@ -356,7 +395,16 @@ const stripe = (options) => {
|
|
|
356
395
|
message: STRIPE_ERROR_CODES.SUBSCRIPTION_PLAN_NOT_FOUND
|
|
357
396
|
});
|
|
358
397
|
}
|
|
359
|
-
|
|
398
|
+
const subscriptionToUpdate = ctx.body.subscriptionId ? await ctx.context.adapter.findOne({
|
|
399
|
+
model: "subscription",
|
|
400
|
+
where: [{ field: "id", value: ctx.body.subscriptionId }]
|
|
401
|
+
}) : null;
|
|
402
|
+
if (ctx.body.subscriptionId && !subscriptionToUpdate) {
|
|
403
|
+
throw new api.APIError("BAD_REQUEST", {
|
|
404
|
+
message: STRIPE_ERROR_CODES.SUBSCRIPTION_NOT_FOUND
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
let customerId = subscriptionToUpdate?.stripeCustomerId || user.stripeCustomerId;
|
|
360
408
|
if (!customerId) {
|
|
361
409
|
try {
|
|
362
410
|
const stripeCustomer = await client.customers.create(
|
|
@@ -395,8 +443,12 @@ const stripe = (options) => {
|
|
|
395
443
|
const activeSubscription = customerId ? await client.subscriptions.list({
|
|
396
444
|
customer: customerId,
|
|
397
445
|
status: "active"
|
|
398
|
-
}).then(
|
|
399
|
-
|
|
446
|
+
}).then(
|
|
447
|
+
(res) => res.data.find(
|
|
448
|
+
(subscription2) => subscription2.id === ctx.body.subscriptionId
|
|
449
|
+
)
|
|
450
|
+
).catch((e) => null) : null;
|
|
451
|
+
const subscriptions = subscriptionToUpdate ? [subscriptionToUpdate] : await ctx.context.adapter.findMany({
|
|
400
452
|
model: "subscription",
|
|
401
453
|
where: [
|
|
402
454
|
{
|
|
@@ -486,7 +538,7 @@ const stripe = (options) => {
|
|
|
486
538
|
ctx,
|
|
487
539
|
`${ctx.context.baseURL}/subscription/success?callbackURL=${encodeURIComponent(
|
|
488
540
|
ctx.body.successUrl
|
|
489
|
-
)}&
|
|
541
|
+
)}&subscriptionId=${encodeURIComponent(subscription.id)}`
|
|
490
542
|
),
|
|
491
543
|
cancel_url: getUrl(ctx, ctx.body.cancelUrl),
|
|
492
544
|
line_items: [
|
|
@@ -525,10 +577,11 @@ const stripe = (options) => {
|
|
|
525
577
|
"/subscription/cancel/callback",
|
|
526
578
|
{
|
|
527
579
|
method: "GET",
|
|
528
|
-
query: zod.z.record(zod.z.string(), zod.z.any()).optional()
|
|
580
|
+
query: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
581
|
+
use: [api.originCheck((ctx) => ctx.query.callbackURL)]
|
|
529
582
|
},
|
|
530
583
|
async (ctx) => {
|
|
531
|
-
if (!ctx.query || !ctx.query.callbackURL || !ctx.query.
|
|
584
|
+
if (!ctx.query || !ctx.query.callbackURL || !ctx.query.subscriptionId) {
|
|
532
585
|
throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
533
586
|
}
|
|
534
587
|
const session = await api.getSessionFromCtx(
|
|
@@ -538,15 +591,15 @@ const stripe = (options) => {
|
|
|
538
591
|
throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
539
592
|
}
|
|
540
593
|
const { user } = session;
|
|
541
|
-
const { callbackURL,
|
|
594
|
+
const { callbackURL, subscriptionId } = ctx.query;
|
|
542
595
|
if (user?.stripeCustomerId) {
|
|
543
596
|
try {
|
|
544
597
|
const subscription = await ctx.context.adapter.findOne({
|
|
545
598
|
model: "subscription",
|
|
546
599
|
where: [
|
|
547
600
|
{
|
|
548
|
-
field: "
|
|
549
|
-
value:
|
|
601
|
+
field: "id",
|
|
602
|
+
value: subscriptionId
|
|
550
603
|
}
|
|
551
604
|
]
|
|
552
605
|
});
|
|
@@ -569,8 +622,8 @@ const stripe = (options) => {
|
|
|
569
622
|
},
|
|
570
623
|
where: [
|
|
571
624
|
{
|
|
572
|
-
field: "
|
|
573
|
-
value:
|
|
625
|
+
field: "id",
|
|
626
|
+
value: subscription.id
|
|
574
627
|
}
|
|
575
628
|
]
|
|
576
629
|
});
|
|
@@ -597,6 +650,7 @@ const stripe = (options) => {
|
|
|
597
650
|
method: "POST",
|
|
598
651
|
body: zod.z.object({
|
|
599
652
|
referenceId: zod.z.string().optional(),
|
|
653
|
+
subscriptionId: zod.z.string().optional(),
|
|
600
654
|
returnUrl: zod.z.string()
|
|
601
655
|
}),
|
|
602
656
|
use: [
|
|
@@ -607,15 +661,22 @@ const stripe = (options) => {
|
|
|
607
661
|
},
|
|
608
662
|
async (ctx) => {
|
|
609
663
|
const referenceId = ctx.body?.referenceId || ctx.context.session.user.id;
|
|
610
|
-
const subscription = await ctx.context.adapter.findOne({
|
|
664
|
+
const subscription = ctx.body.subscriptionId ? await ctx.context.adapter.findOne({
|
|
611
665
|
model: "subscription",
|
|
612
666
|
where: [
|
|
613
667
|
{
|
|
614
|
-
field: "
|
|
615
|
-
value:
|
|
668
|
+
field: "id",
|
|
669
|
+
value: ctx.body.subscriptionId
|
|
616
670
|
}
|
|
617
671
|
]
|
|
618
|
-
})
|
|
672
|
+
}) : await ctx.context.adapter.findMany({
|
|
673
|
+
model: "subscription",
|
|
674
|
+
where: [{ field: "referenceId", value: referenceId }]
|
|
675
|
+
}).then(
|
|
676
|
+
(subs) => subs.find(
|
|
677
|
+
(sub) => sub.status === "active" || sub.status === "trialing"
|
|
678
|
+
)
|
|
679
|
+
);
|
|
619
680
|
if (!subscription || !subscription.stripeCustomerId) {
|
|
620
681
|
throw ctx.error("BAD_REQUEST", {
|
|
621
682
|
message: STRIPE_ERROR_CODES.SUBSCRIPTION_NOT_FOUND
|
|
@@ -656,7 +717,7 @@ const stripe = (options) => {
|
|
|
656
717
|
ctx,
|
|
657
718
|
`${ctx.context.baseURL}/subscription/cancel/callback?callbackURL=${encodeURIComponent(
|
|
658
719
|
ctx.body?.returnUrl || "/"
|
|
659
|
-
)}&
|
|
720
|
+
)}&subscriptionId=${encodeURIComponent(subscription.id)}`
|
|
660
721
|
),
|
|
661
722
|
flow_data: {
|
|
662
723
|
type: "subscription_cancel",
|
|
@@ -738,10 +799,11 @@ const stripe = (options) => {
|
|
|
738
799
|
"/subscription/success",
|
|
739
800
|
{
|
|
740
801
|
method: "GET",
|
|
741
|
-
query: zod.z.record(zod.z.string(), zod.z.any()).optional()
|
|
802
|
+
query: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
803
|
+
use: [api.originCheck((ctx) => ctx.query.callbackURL)]
|
|
742
804
|
},
|
|
743
805
|
async (ctx) => {
|
|
744
|
-
if (!ctx.query || !ctx.query.callbackURL || !ctx.query.
|
|
806
|
+
if (!ctx.query || !ctx.query.callbackURL || !ctx.query.subscriptionId) {
|
|
745
807
|
throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
746
808
|
}
|
|
747
809
|
const session = await api.getSessionFromCtx(
|
|
@@ -751,38 +813,24 @@ const stripe = (options) => {
|
|
|
751
813
|
throw ctx.redirect(getUrl(ctx, ctx.query?.callbackURL || "/"));
|
|
752
814
|
}
|
|
753
815
|
const { user } = session;
|
|
754
|
-
const { callbackURL,
|
|
755
|
-
const
|
|
816
|
+
const { callbackURL, subscriptionId } = ctx.query;
|
|
817
|
+
const subscription = await ctx.context.adapter.findOne({
|
|
756
818
|
model: "subscription",
|
|
757
819
|
where: [
|
|
758
820
|
{
|
|
759
|
-
field: "
|
|
760
|
-
value:
|
|
821
|
+
field: "id",
|
|
822
|
+
value: subscriptionId
|
|
761
823
|
}
|
|
762
824
|
]
|
|
763
825
|
});
|
|
764
|
-
|
|
765
|
-
(sub) => sub.status === "active" || sub.status === "trialing"
|
|
766
|
-
);
|
|
767
|
-
if (activeSubscription) {
|
|
826
|
+
if (subscription?.status === "active" || subscription?.status === "trialing") {
|
|
768
827
|
return ctx.redirect(getUrl(ctx, callbackURL));
|
|
769
828
|
}
|
|
770
|
-
|
|
829
|
+
const customerId = subscription?.stripeCustomerId || user.stripeCustomerId;
|
|
830
|
+
if (customerId) {
|
|
771
831
|
try {
|
|
772
|
-
const subscription = await ctx.context.adapter.findOne({
|
|
773
|
-
model: "subscription",
|
|
774
|
-
where: [
|
|
775
|
-
{
|
|
776
|
-
field: "referenceId",
|
|
777
|
-
value: reference
|
|
778
|
-
}
|
|
779
|
-
]
|
|
780
|
-
});
|
|
781
|
-
if (!subscription || subscription.status === "active") {
|
|
782
|
-
throw ctx.redirect(getUrl(ctx, callbackURL));
|
|
783
|
-
}
|
|
784
832
|
const stripeSubscription = await client.subscriptions.list({
|
|
785
|
-
customer:
|
|
833
|
+
customer: customerId,
|
|
786
834
|
status: "active"
|
|
787
835
|
}).then((res) => res.data[0]);
|
|
788
836
|
if (stripeSubscription) {
|
|
@@ -790,21 +838,33 @@ const stripe = (options) => {
|
|
|
790
838
|
options,
|
|
791
839
|
stripeSubscription.items.data[0]?.plan.id
|
|
792
840
|
);
|
|
793
|
-
if (plan &&
|
|
841
|
+
if (plan && subscription) {
|
|
794
842
|
await ctx.context.adapter.update({
|
|
795
843
|
model: "subscription",
|
|
796
844
|
update: {
|
|
797
845
|
status: stripeSubscription.status,
|
|
798
846
|
seats: stripeSubscription.items.data[0]?.quantity || 1,
|
|
799
847
|
plan: plan.name.toLowerCase(),
|
|
800
|
-
periodEnd:
|
|
801
|
-
|
|
802
|
-
|
|
848
|
+
periodEnd: new Date(
|
|
849
|
+
stripeSubscription.current_period_end * 1e3
|
|
850
|
+
),
|
|
851
|
+
periodStart: new Date(
|
|
852
|
+
stripeSubscription.current_period_start * 1e3
|
|
853
|
+
),
|
|
854
|
+
stripeSubscriptionId: stripeSubscription.id,
|
|
855
|
+
...stripeSubscription.trial_start && stripeSubscription.trial_end ? {
|
|
856
|
+
trialStart: new Date(
|
|
857
|
+
stripeSubscription.trial_start * 1e3
|
|
858
|
+
),
|
|
859
|
+
trialEnd: new Date(
|
|
860
|
+
stripeSubscription.trial_end * 1e3
|
|
861
|
+
)
|
|
862
|
+
} : {}
|
|
803
863
|
},
|
|
804
864
|
where: [
|
|
805
865
|
{
|
|
806
|
-
field: "
|
|
807
|
-
value:
|
|
866
|
+
field: "id",
|
|
867
|
+
value: subscription.id
|
|
808
868
|
}
|
|
809
869
|
]
|
|
810
870
|
});
|
|
@@ -847,7 +907,11 @@ const stripe = (options) => {
|
|
|
847
907
|
message: "Stripe webhook secret not found"
|
|
848
908
|
});
|
|
849
909
|
}
|
|
850
|
-
event = client.webhooks.
|
|
910
|
+
event = await client.webhooks.constructEventAsync(
|
|
911
|
+
buf,
|
|
912
|
+
sig,
|
|
913
|
+
webhookSecret
|
|
914
|
+
);
|
|
851
915
|
} catch (err) {
|
|
852
916
|
ctx.context.logger.error(`${err.message}`);
|
|
853
917
|
throw new api.APIError("BAD_REQUEST", {
|
|
@@ -900,18 +964,29 @@ const stripe = (options) => {
|
|
|
900
964
|
userId: user.id
|
|
901
965
|
}
|
|
902
966
|
});
|
|
903
|
-
await ctx2.context.adapter.update(
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
967
|
+
const customer = await ctx2.context.adapter.update(
|
|
968
|
+
{
|
|
969
|
+
model: "user",
|
|
970
|
+
update: {
|
|
971
|
+
stripeCustomerId: stripeCustomer.id
|
|
972
|
+
},
|
|
973
|
+
where: [
|
|
974
|
+
{
|
|
975
|
+
field: "id",
|
|
976
|
+
value: user.id
|
|
977
|
+
}
|
|
978
|
+
]
|
|
979
|
+
}
|
|
980
|
+
);
|
|
981
|
+
if (!customer) {
|
|
982
|
+
betterAuth.logger.error("#BETTER_AUTH: Failed to create customer");
|
|
983
|
+
} else {
|
|
984
|
+
await options.onCustomerCreate?.({
|
|
985
|
+
customer,
|
|
986
|
+
stripeCustomer,
|
|
987
|
+
user
|
|
988
|
+
});
|
|
989
|
+
}
|
|
915
990
|
}
|
|
916
991
|
}
|
|
917
992
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -5,7 +5,7 @@ import Stripe from 'stripe';
|
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { APIError } from 'better-auth/api';
|
|
7
7
|
|
|
8
|
-
type
|
|
8
|
+
type StripePlan = {
|
|
9
9
|
/**
|
|
10
10
|
* Monthly price id
|
|
11
11
|
*/
|
|
@@ -24,6 +24,13 @@ type Plan = {
|
|
|
24
24
|
* yearly subscription
|
|
25
25
|
*/
|
|
26
26
|
annualDiscountPriceId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* To use lookup key instead of price id
|
|
29
|
+
*
|
|
30
|
+
* https://docs.stripe.com/products-prices/
|
|
31
|
+
* manage-prices#lookup-keys
|
|
32
|
+
*/
|
|
33
|
+
annualDiscountLookupKey?: string;
|
|
27
34
|
/**
|
|
28
35
|
* Plan name
|
|
29
36
|
*/
|
|
@@ -186,7 +193,7 @@ interface StripeOptions {
|
|
|
186
193
|
/**
|
|
187
194
|
* List of plan
|
|
188
195
|
*/
|
|
189
|
-
plans:
|
|
196
|
+
plans: StripePlan[] | (() => Promise<StripePlan[]>);
|
|
190
197
|
/**
|
|
191
198
|
* Require email verification before a user is allowed to upgrade
|
|
192
199
|
* their subscriptions
|
|
@@ -204,7 +211,7 @@ interface StripeOptions {
|
|
|
204
211
|
event: Stripe.Event;
|
|
205
212
|
stripeSubscription: Stripe.Subscription;
|
|
206
213
|
subscription: Subscription;
|
|
207
|
-
plan:
|
|
214
|
+
plan: StripePlan;
|
|
208
215
|
}, request?: Request) => Promise<void>;
|
|
209
216
|
/**
|
|
210
217
|
* A callback to run after a user is about to cancel their subscription
|
|
@@ -256,7 +263,7 @@ interface StripeOptions {
|
|
|
256
263
|
getCheckoutSessionParams?: (data: {
|
|
257
264
|
user: User & Record<string, any>;
|
|
258
265
|
session: Session & Record<string, any>;
|
|
259
|
-
plan:
|
|
266
|
+
plan: StripePlan;
|
|
260
267
|
subscription: Subscription;
|
|
261
268
|
}, request?: Request) => Promise<{
|
|
262
269
|
params?: Stripe.Checkout.SessionCreateParams;
|
|
@@ -330,8 +337,8 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
330
337
|
metadata?: Record<string, any> | undefined;
|
|
331
338
|
annual?: boolean | undefined;
|
|
332
339
|
referenceId?: string | undefined;
|
|
340
|
+
subscriptionId?: string | undefined;
|
|
333
341
|
seats?: number | undefined;
|
|
334
|
-
uiMode?: "embedded" | "hosted" | undefined;
|
|
335
342
|
successUrl?: string | undefined;
|
|
336
343
|
cancelUrl?: string | undefined;
|
|
337
344
|
returnUrl?: string | undefined;
|
|
@@ -503,25 +510,60 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
503
510
|
options: {
|
|
504
511
|
method: "POST";
|
|
505
512
|
body: z.ZodObject<{
|
|
513
|
+
/**
|
|
514
|
+
* The name of the plan to subscribe
|
|
515
|
+
*/
|
|
506
516
|
plan: z.ZodString;
|
|
517
|
+
/**
|
|
518
|
+
* If annual plan should be applied.
|
|
519
|
+
*/
|
|
507
520
|
annual: z.ZodOptional<z.ZodBoolean>;
|
|
521
|
+
/**
|
|
522
|
+
* Reference id of the subscription to upgrade
|
|
523
|
+
* This is used to identify the subscription to upgrade
|
|
524
|
+
* If not provided, the user's id will be used
|
|
525
|
+
*/
|
|
508
526
|
referenceId: z.ZodOptional<z.ZodString>;
|
|
527
|
+
/**
|
|
528
|
+
* This is to allow a specific subscription to be upgrade.
|
|
529
|
+
* If subscription id is provided, and subscription isn't found,
|
|
530
|
+
* it'll throw an error.
|
|
531
|
+
*/
|
|
532
|
+
subscriptionId: z.ZodOptional<z.ZodString>;
|
|
533
|
+
/**
|
|
534
|
+
* Any additional data you want to store in your database
|
|
535
|
+
* subscriptions
|
|
536
|
+
*/
|
|
509
537
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
538
|
+
/**
|
|
539
|
+
* If a subscription
|
|
540
|
+
*/
|
|
510
541
|
seats: z.ZodOptional<z.ZodNumber>;
|
|
511
|
-
|
|
542
|
+
/**
|
|
543
|
+
* Success url to redirect back after successful subscription
|
|
544
|
+
*/
|
|
512
545
|
successUrl: z.ZodDefault<z.ZodString>;
|
|
546
|
+
/**
|
|
547
|
+
* Cancel URL
|
|
548
|
+
*/
|
|
513
549
|
cancelUrl: z.ZodDefault<z.ZodString>;
|
|
550
|
+
/**
|
|
551
|
+
* Return URL
|
|
552
|
+
*/
|
|
514
553
|
returnUrl: z.ZodOptional<z.ZodString>;
|
|
554
|
+
/**
|
|
555
|
+
* Disable Redirect
|
|
556
|
+
*/
|
|
515
557
|
disableRedirect: z.ZodDefault<z.ZodBoolean>;
|
|
516
558
|
}, "strip", z.ZodTypeAny, {
|
|
517
559
|
plan: string;
|
|
518
|
-
uiMode: "embedded" | "hosted";
|
|
519
560
|
successUrl: string;
|
|
520
561
|
cancelUrl: string;
|
|
521
562
|
disableRedirect: boolean;
|
|
522
563
|
metadata?: Record<string, any> | undefined;
|
|
523
564
|
annual?: boolean | undefined;
|
|
524
565
|
referenceId?: string | undefined;
|
|
566
|
+
subscriptionId?: string | undefined;
|
|
525
567
|
seats?: number | undefined;
|
|
526
568
|
returnUrl?: string | undefined;
|
|
527
569
|
}, {
|
|
@@ -529,8 +571,8 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
529
571
|
metadata?: Record<string, any> | undefined;
|
|
530
572
|
annual?: boolean | undefined;
|
|
531
573
|
referenceId?: string | undefined;
|
|
574
|
+
subscriptionId?: string | undefined;
|
|
532
575
|
seats?: number | undefined;
|
|
533
|
-
uiMode?: "embedded" | "hosted" | undefined;
|
|
534
576
|
successUrl?: string | undefined;
|
|
535
577
|
cancelUrl?: string | undefined;
|
|
536
578
|
returnUrl?: string | undefined;
|
|
@@ -603,6 +645,15 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
603
645
|
options: {
|
|
604
646
|
method: "GET";
|
|
605
647
|
query: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
648
|
+
use: ((inputContext: {
|
|
649
|
+
body?: any;
|
|
650
|
+
query?: Record<string, any> | undefined;
|
|
651
|
+
request?: Request | undefined;
|
|
652
|
+
headers?: Headers | undefined;
|
|
653
|
+
asResponse?: boolean | undefined;
|
|
654
|
+
returnHeaders?: boolean | undefined;
|
|
655
|
+
use?: better_call.Middleware[] | undefined;
|
|
656
|
+
}) => Promise<void>)[];
|
|
606
657
|
} & {
|
|
607
658
|
use: any[];
|
|
608
659
|
};
|
|
@@ -613,6 +664,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
613
664
|
body: {
|
|
614
665
|
returnUrl: string;
|
|
615
666
|
referenceId?: string | undefined;
|
|
667
|
+
subscriptionId?: string | undefined;
|
|
616
668
|
};
|
|
617
669
|
method?: "POST" | undefined;
|
|
618
670
|
query?: Record<string, any> | undefined;
|
|
@@ -641,13 +693,16 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
641
693
|
method: "POST";
|
|
642
694
|
body: z.ZodObject<{
|
|
643
695
|
referenceId: z.ZodOptional<z.ZodString>;
|
|
696
|
+
subscriptionId: z.ZodOptional<z.ZodString>;
|
|
644
697
|
returnUrl: z.ZodString;
|
|
645
698
|
}, "strip", z.ZodTypeAny, {
|
|
646
699
|
returnUrl: string;
|
|
647
700
|
referenceId?: string | undefined;
|
|
701
|
+
subscriptionId?: string | undefined;
|
|
648
702
|
}, {
|
|
649
703
|
returnUrl: string;
|
|
650
704
|
referenceId?: string | undefined;
|
|
705
|
+
subscriptionId?: string | undefined;
|
|
651
706
|
}>;
|
|
652
707
|
use: (((inputContext: {
|
|
653
708
|
body?: any;
|
|
@@ -823,6 +878,15 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
|
|
|
823
878
|
options: {
|
|
824
879
|
method: "GET";
|
|
825
880
|
query: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
881
|
+
use: ((inputContext: {
|
|
882
|
+
body?: any;
|
|
883
|
+
query?: Record<string, any> | undefined;
|
|
884
|
+
request?: Request | undefined;
|
|
885
|
+
headers?: Headers | undefined;
|
|
886
|
+
asResponse?: boolean | undefined;
|
|
887
|
+
returnHeaders?: boolean | undefined;
|
|
888
|
+
use?: better_call.Middleware[] | undefined;
|
|
889
|
+
}) => Promise<void>)[];
|
|
826
890
|
} & {
|
|
827
891
|
use: any[];
|
|
828
892
|
};
|