@@ -6,15 +6,16 @@ import Link from "next/link";
6
6
import { useRouter , useSearchParams } from "next/navigation" ;
7
7
import { signIn } from "next-auth/react" ;
8
8
import { usePostHog } from "posthog-js/react" ;
9
- import { useState , useCallback , useEffect } from "react" ;
9
+ import { useState } from "react" ;
10
10
11
11
// Local imports
12
12
import { useAccountContext } from "@/components/Context/Account" ;
13
13
import SpinnerIcon from "@/components/SpinnerIcon" ;
14
14
import CheckMark from "@/components/Symbol/CheckMark" ;
15
- import { ABSOLUTE_URLS , OPENAI_MODEL_O3_MINI , RELATIVE_URLS } from "@/config" ;
15
+ import { ABSOLUTE_URLS , OPENAI_MODEL_O3_MINI } from "@/config" ;
16
16
import { ANTHROPIC_MODEL_CLAUDE_37 } from "@/config/anthropic" ;
17
17
import { DEEPSEEK_MODEL_R1 } from "@/config/deepseek" ;
18
+ import { createPortalOrCheckoutURL } from "@/lib/stripe/createPortalOrCheckoutUrl" ;
18
19
19
20
const pricingButtonStyles = `my-4 sm:my-2 md:my-8 rounded-lg transition-colors duration-200 text-md sm:text-lg xl:text-xl py-2 sm:py-1 md:py-3 w-full shadow-lg hover:shadow-lg font-semibold text-center mx-auto` ;
20
21
@@ -27,9 +28,12 @@ export default function Pricing() {
27
28
jwtToken,
28
29
email,
29
30
selectedIndex,
30
- installations,
31
31
installationsSubscribed,
32
32
userName,
33
+ currentOwnerId,
34
+ currentOwnerType,
35
+ currentOwnerName,
36
+ currentStripeCustomerId,
33
37
} = useAccountContext ( ) ;
34
38
35
39
const router = useRouter ( ) ;
@@ -38,54 +42,33 @@ export default function Pricing() {
38
42
const searchParams = useSearchParams ( ) ;
39
43
const [ billingPeriod , setBillingPeriod ] = useState < string > ( "Yearly" ) ;
40
44
41
- const createPortalOrCheckoutURL = useCallback ( async ( ) => {
42
- let currentIndex = 0 ;
43
- if ( selectedIndex ) currentIndex = selectedIndex ;
44
-
45
- // If user has an installation, create portal or checkout session
46
- if ( installations && installations . length > 0 ) {
47
- const response = await fetch ( "/api/stripe/create-portal-or-checkout-url" , {
48
- method : "POST" ,
49
- body : JSON . stringify ( {
50
- userId : userId ,
51
- jwtToken : jwtToken ,
52
- customerId : installations [ currentIndex ] . stripe_customer_id ,
53
- email : email ,
54
- ownerType : installations [ currentIndex ] . owner_type ,
55
- ownerId : Number ( installations [ currentIndex ] . owner_id ) ,
56
- ownerName : installations [ currentIndex ] . owner_name ,
57
- userName,
58
- billingPeriod : billingPeriod ,
59
- } ) ,
60
- } ) ;
61
-
62
- const res = await response . json ( ) ;
63
- router . push ( res ) ;
64
- } else {
65
- // If not, redirect to installation page
66
- router . push ( RELATIVE_URLS . REDIRECT_TO_INSTALL ) ;
67
- }
68
- } , [ email , jwtToken , router , selectedIndex , userId , installations , billingPeriod , userName ] ) ;
69
-
70
45
// Flow: https://docs.google.com/spreadsheets/d/1AK7VPo_68mL2s3lvsKLy3Rox-QvsT5cngiWf2k0r3Cc/edit#gid=0
71
46
async function handleSubscribe ( ) {
72
47
setIsSubscribeLoading ( true ) ;
73
48
posthog . capture ( "$click" , { $event_type : "subscribe" , $current_url : window . location . href } ) ;
74
49
75
50
try {
76
51
if ( ! userId || ! jwtToken ) {
77
- await signIn ( "github" , { callbackUrl : `/?subscribe` } ) ;
78
- return ;
79
- }
80
-
81
- // Signed in but no installation
82
- if ( ! installations || installations . length === 0 ) {
83
- router . push ( RELATIVE_URLS . REDIRECT_TO_INSTALL ) ;
52
+ await signIn ( "github" , { callbackUrl : "/" } ) ;
84
53
return ;
85
54
}
86
55
87
- // Has at least one installation
88
- await createPortalOrCheckoutURL ( ) ;
56
+ if ( ! currentOwnerId || ! currentOwnerType || ! currentOwnerName ) return ;
57
+ if ( ! currentStripeCustomerId ) return ;
58
+ if ( ! email ) return ;
59
+
60
+ await createPortalOrCheckoutURL ( {
61
+ userId,
62
+ jwtToken,
63
+ customerId : currentStripeCustomerId ,
64
+ email,
65
+ ownerId : currentOwnerId ,
66
+ ownerType : currentOwnerType ,
67
+ ownerName : currentOwnerName ,
68
+ userName,
69
+ billingPeriod,
70
+ router,
71
+ } ) ;
89
72
} catch ( error ) {
90
73
Sentry . captureException ( error ) ;
91
74
console . error ( "Error subscribing" , error ) ;
@@ -94,21 +77,6 @@ export default function Pricing() {
94
77
}
95
78
}
96
79
97
- // If "subscribe" in query parameter create checkout session or portal
98
- useEffect ( ( ) => {
99
- if ( searchParams . has ( "subscribe" ) && installations ) {
100
- createPortalOrCheckoutURL ( ) ;
101
- }
102
- } , [
103
- searchParams ,
104
- installations ,
105
- selectedIndex ,
106
- userId ,
107
- jwtToken ,
108
- router ,
109
- createPortalOrCheckoutURL ,
110
- ] ) ;
111
-
112
80
return (
113
81
< div
114
82
id = "pricing"
0 commit comments