@@ -3,37 +3,58 @@ import { invariant } from "@/lib/error";
3
3
import { checkMembership } from "@/server/auth" ;
4
4
import { createOrRetrieveCustomer , stripe } from "@/server/stripe" ;
5
5
import { withAuth } from "@/trpc/api/trpc" ;
6
+ import { ZodStripePortalMutationSchema } from "../schema" ;
6
7
7
- export const stripePortalProcedure = withAuth . mutation ( async ( { ctx } ) => {
8
- const { db, session } = ctx ;
9
-
10
- const { url } = await db . $transaction ( async ( tx ) => {
11
- const { companyId } = await checkMembership ( { session, tx } ) ;
12
-
13
- let customer : string ;
14
- try {
15
- customer = await createOrRetrieveCustomer ( {
16
- tx,
17
- companyId,
18
- email : "" ,
19
- } ) ;
20
- } catch ( _error ) {
21
- throw new Error ( "Unable to access customer record." ) ;
22
- }
23
-
24
- invariant ( customer , "Could not get customer." ) ;
25
-
26
- try {
27
- const { url } = await stripe . billingPortal . sessions . create ( {
28
- customer,
29
- return_url : `${ env . NEXT_PUBLIC_BASE_URL } /${ session . user . companyPublicId } /settings/billing` ,
30
- } ) ;
31
-
32
- return { url } ;
33
- } catch ( _error ) {
34
- throw new Error ( "Could not create billing portal." ) ;
35
- }
36
- } ) ;
8
+ export const stripePortalProcedure = withAuth
9
+ . input ( ZodStripePortalMutationSchema )
10
+ . mutation ( async ( { ctx, input } ) => {
11
+ const { db, session } = ctx ;
12
+
13
+ const { url } = await db . $transaction ( async ( tx ) => {
14
+ const { companyId } = await checkMembership ( { session, tx } ) ;
15
+
16
+ let customer : string ;
17
+ try {
18
+ customer = await createOrRetrieveCustomer ( {
19
+ tx,
20
+ companyId,
21
+ email : "" ,
22
+ } ) ;
23
+ } catch ( _error ) {
24
+ throw new Error ( "Unable to access customer record." ) ;
25
+ }
26
+
27
+ invariant ( customer , "Could not get customer." ) ;
28
+ console . log ( { input } ) ;
37
29
38
- return { url } ;
39
- } ) ;
30
+ try {
31
+ const { url } = await stripe . billingPortal . sessions . create ( {
32
+ customer,
33
+ return_url : `${ env . NEXT_PUBLIC_BASE_URL } /${ session . user . companyPublicId } /settings/billing` ,
34
+ flow_data : {
35
+ ...( input . type === "cancel"
36
+ ? {
37
+ type : "subscription_cancel" ,
38
+ subscription_cancel : {
39
+ subscription : input . subscription ,
40
+ } ,
41
+ }
42
+ : {
43
+ type : "subscription_update" ,
44
+ subscription_update : {
45
+ subscription : input . subscription ,
46
+ } ,
47
+ } ) ,
48
+ } ,
49
+ } ) ;
50
+
51
+ return { url } ;
52
+ } catch ( _error ) {
53
+ console . log ( { _error } ) ;
54
+
55
+ throw new Error ( "Could not create billing portal." ) ;
56
+ }
57
+ } ) ;
58
+
59
+ return { url } ;
60
+ } ) ;
0 commit comments