@@ -5,14 +5,38 @@ import { useSearchParams } from 'next/navigation';
55import QueryString from 'query-string' ;
66import '../styles/Subscrip.css' ;
77
8- import { SessionIdDisplay , Last4Display , ProductDetails , TransactionStatus , CheckoutForm } from '@/components/billing' ;
9- import { Product } from '@/components/billing/productDetails' ;
8+ import { SessionIdDisplay , ProductDetails , TransactionStatus , CheckoutForm } from '@/components/billing' ;
9+ import { API_URL } from '@/config' ;
10+
11+ type Product = {
12+ name : string ;
13+ description : string ;
14+ price : number ;
15+ } ;
16+
17+ type SessionDetails = {
18+ customer_name : string ;
19+ customer_email : string ;
20+ status : string ;
21+ amount_total ?: number ;
22+ currency : string ;
23+ payment_status : string ;
24+ invoice : string ;
25+ subscription : string ;
26+ created ?: number ;
27+ expiration ?: number ;
28+ success_url : string ;
29+ cancel_url : string ;
30+ current_transaction_date : string ;
31+ next_transaction_date : string ;
32+ product ?: Product ;
33+ } ;
1034
1135const HomePage : React . FC = ( ) => {
1236 const searchParams = useSearchParams ( ) ;
1337 const [ transactionStatus , setTransactionStatus ] = useState < string | null > ( null ) ;
1438 const [ sessionId , setSessionId ] = useState < string | null > ( null ) ;
15- const [ sessionDetails , setSessionDetails ] = useState < any > ( null ) ;
39+ const [ sessionDetails , setSessionDetails ] = useState < SessionDetails | null > ( null ) ;
1640 const [ loading , setLoading ] = useState < boolean > ( true ) ;
1741
1842 useEffect ( ( ) => {
@@ -34,23 +58,33 @@ const HomePage: React.FC = () => {
3458 }
3559 } , [ searchParams ] ) ;
3660
37- const fetchSessionDetails = async ( sessionId : string ) => {
38- try {
39- const response = await fetch ( `/api/stripe/get-session-details?session_id=${ sessionId } ` ) ;
40- const data = await response . json ( ) ;
41- console . log ( 'Session details:' , data ) ;
42-
43- if ( data . error ) {
44- throw new Error ( data . error ) ;
45- }
46-
47- setSessionDetails ( data ) ;
48- } catch ( error ) {
49- console . error ( 'Error fetching session details:' , error ) ;
50- } finally {
51- setLoading ( false ) ;
61+ async function fetchSessionDetails ( sessionId : string ) {
62+ const response = await fetch ( `${ API_URL } /api/stripe/get-session-details?session_id=${ sessionId } ` ) ;
63+ const data = await response . json ( ) ;
64+
65+ if ( response . ok ) {
66+ setSessionDetails ( {
67+ customer_name : data . customer_name ,
68+ customer_email : data . customer_email ,
69+ status : data . status ,
70+ amount_total : data . amount_total ,
71+ currency : data . currency ,
72+ payment_status : data . payment_status ,
73+ invoice : data . invoice ,
74+ subscription : data . subscription ,
75+ created : data . created ,
76+ expiration : data . expiration ,
77+ success_url : data . success_url ,
78+ cancel_url : data . cancel_url ,
79+ current_transaction_date : data . current_transaction_date ,
80+ next_transaction_date : data . next_transaction_date ,
81+ product : data . product , // Assuming product data is part of the response
82+ } ) ;
83+ setLoading ( false ) ; // Set loading to false once data is fetched
84+ } else {
85+ console . error ( 'Error fetching session details:' , data . error ) ;
5286 }
53- } ;
87+ }
5488
5589 return (
5690 < section className = "relative" >
@@ -64,17 +98,21 @@ const HomePage: React.FC = () => {
6498 < p > < strong > Customer Name:</ strong > { sessionDetails ?. customer_name } </ p >
6599 < p > < strong > Customer Email:</ strong > { sessionDetails ?. customer_email } </ p >
66100 < p > < strong > Status:</ strong > { sessionDetails ?. status } </ p >
67- < p > < strong > Amount Total:</ strong > { sessionDetails ?. amount_total / 100 } { sessionDetails ?. currency . toUpperCase ( ) } </ p >
101+ < p > < strong > Amount Total:</ strong > { sessionDetails ?. amount_total ? sessionDetails . amount_total / 100 : 'N/A' } { sessionDetails ?. currency . toUpperCase ( ) } </ p >
68102 < p > < strong > Payment Status:</ strong > { sessionDetails ?. payment_status } </ p >
69103 < p > < strong > Invoice:</ strong > { sessionDetails ?. invoice } </ p >
70104 < p > < strong > Subscription:</ strong > { sessionDetails ?. subscription } </ p >
71- < p > < strong > Created At:</ strong > { new Date ( sessionDetails ? .created * 1000 ) . toLocaleString ( ) } </ p >
72- < p > < strong > Expires At:</ strong > { new Date ( sessionDetails ? .expiration * 1000 ) . toLocaleString ( ) } </ p >
105+ < p > < strong > Created At:</ strong > { sessionDetails ?. created ? new Date ( sessionDetails . created * 1000 ) . toLocaleString ( ) : 'N/A' } </ p >
106+ < p > < strong > Expires At:</ strong > { sessionDetails ?. expiration ? new Date ( sessionDetails . expiration * 1000 ) . toLocaleString ( ) : 'N/A' } </ p >
73107 < p > < strong > Success URL:</ strong > < a href = { sessionDetails ?. success_url } > { sessionDetails ?. success_url } </ a > </ p >
74108 < p > < strong > Cancel URL:</ strong > < a href = { sessionDetails ?. cancel_url } > { sessionDetails ?. cancel_url } </ a > </ p >
109+ < p > < strong > Current Transaction Date:</ strong > { sessionDetails ?. current_transaction_date } </ p >
110+ < p > < strong > Next Transaction Date:</ strong > { sessionDetails ?. next_transaction_date } </ p >
75111 </ div >
76112
77- < ProductDetails product = { sessionDetails ?. product } />
113+ { sessionDetails ?. product && (
114+ < ProductDetails product = { sessionDetails . product } />
115+ ) }
78116 </ >
79117 ) }
80118 < CheckoutForm />
0 commit comments