2025-10-29 21:45:14 +01:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import { logger } from '../utils/logger';
|
|
|
|
|
|
2025-10-30 12:36:57 +01:00
|
|
|
// Get API base URL from runtime config (window.env) or build-time config
|
2025-10-30 12:43:16 +01:00
|
|
|
// IMPORTANT: Call this function at REQUEST TIME, not at module load time,
|
|
|
|
|
// to ensure window.env is populated by env-config.js
|
2025-10-30 12:36:57 +01:00
|
|
|
const getApiBaseUrl = (): string => {
|
|
|
|
|
// Check window.env first (runtime config from env.sh)
|
|
|
|
|
if (typeof window !== 'undefined' && window.env?.API_BASE_URL) {
|
|
|
|
|
return window.env.API_BASE_URL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to build-time env or default
|
|
|
|
|
return import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001';
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-29 21:45:14 +01:00
|
|
|
export interface CreateCheckoutSessionParams {
|
|
|
|
|
tons: number;
|
|
|
|
|
portfolioId: number;
|
|
|
|
|
customerEmail?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CheckoutSessionResponse {
|
|
|
|
|
sessionId: string;
|
|
|
|
|
url: string;
|
|
|
|
|
orderId: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface OrderDetails {
|
|
|
|
|
order: {
|
|
|
|
|
id: string;
|
|
|
|
|
tons: number;
|
|
|
|
|
portfolioId: number;
|
|
|
|
|
baseAmount: number;
|
|
|
|
|
processingFee: number;
|
|
|
|
|
totalAmount: number;
|
|
|
|
|
currency: string;
|
|
|
|
|
status: string;
|
|
|
|
|
wrenOrderId: string | null;
|
|
|
|
|
createdAt: string;
|
|
|
|
|
};
|
|
|
|
|
session: {
|
|
|
|
|
paymentStatus: string;
|
|
|
|
|
customerEmail?: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a Stripe checkout session
|
|
|
|
|
* @param params Checkout session parameters
|
|
|
|
|
* @returns Checkout session response with redirect URL
|
|
|
|
|
*/
|
|
|
|
|
export async function createCheckoutSession(
|
|
|
|
|
params: CreateCheckoutSessionParams
|
|
|
|
|
): Promise<CheckoutSessionResponse> {
|
|
|
|
|
try {
|
2025-10-30 12:43:16 +01:00
|
|
|
const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
|
2025-10-29 21:45:14 +01:00
|
|
|
logger.info('Creating checkout session:', params);
|
2025-10-30 12:43:16 +01:00
|
|
|
logger.info('Using API base URL:', apiBaseUrl);
|
2025-10-29 21:45:14 +01:00
|
|
|
|
|
|
|
|
const response = await axios.post<CheckoutSessionResponse>(
|
2025-10-30 12:43:16 +01:00
|
|
|
`${apiBaseUrl}/api/checkout/create-session`,
|
2025-10-29 21:45:14 +01:00
|
|
|
params
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
logger.info('Checkout session created:', response.data.sessionId);
|
|
|
|
|
return response.data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (axios.isAxiosError(error)) {
|
|
|
|
|
logger.error('Checkout session creation failed:', error.response?.data || error.message);
|
|
|
|
|
throw new Error(error.response?.data?.message || 'Failed to create checkout session');
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get order details by session ID
|
|
|
|
|
* @param sessionId Stripe session ID
|
|
|
|
|
* @returns Order details
|
|
|
|
|
*/
|
|
|
|
|
export async function getOrderDetails(sessionId: string): Promise<OrderDetails> {
|
|
|
|
|
try {
|
2025-10-30 12:43:16 +01:00
|
|
|
const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
|
2025-10-29 21:45:14 +01:00
|
|
|
logger.info('Fetching order details for session:', sessionId);
|
|
|
|
|
|
|
|
|
|
const response = await axios.get<OrderDetails>(
|
2025-10-30 12:43:16 +01:00
|
|
|
`${apiBaseUrl}/api/checkout/session/${sessionId}`
|
2025-10-29 21:45:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
logger.info('Order details retrieved:', response.data.order.id);
|
|
|
|
|
return response.data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (axios.isAxiosError(error)) {
|
|
|
|
|
logger.error('Order details retrieval failed:', error.response?.data || error.message);
|
|
|
|
|
throw new Error(error.response?.data?.message || 'Failed to retrieve order details');
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Health check for backend API
|
|
|
|
|
* @returns true if backend is healthy
|
|
|
|
|
*/
|
|
|
|
|
export async function checkBackendHealth(): Promise<boolean> {
|
|
|
|
|
try {
|
2025-10-30 12:43:16 +01:00
|
|
|
const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
|
|
|
|
|
const response = await axios.get(`${apiBaseUrl}/health`);
|
2025-10-29 21:45:14 +01:00
|
|
|
return response.data.status === 'ok';
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('Backend health check failed:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|