101 lines
2.6 KiB
TypeScript
101 lines
2.6 KiB
TypeScript
|
|
import axios from 'axios';
|
||
|
|
import { logger } from '../utils/logger';
|
||
|
|
|
||
|
|
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001';
|
||
|
|
|
||
|
|
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 {
|
||
|
|
logger.info('Creating checkout session:', params);
|
||
|
|
|
||
|
|
const response = await axios.post<CheckoutSessionResponse>(
|
||
|
|
`${API_BASE_URL}/api/checkout/create-session`,
|
||
|
|
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 {
|
||
|
|
logger.info('Fetching order details for session:', sessionId);
|
||
|
|
|
||
|
|
const response = await axios.get<OrderDetails>(
|
||
|
|
`${API_BASE_URL}/api/checkout/session/${sessionId}`
|
||
|
|
);
|
||
|
|
|
||
|
|
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 {
|
||
|
|
const response = await axios.get(`${API_BASE_URL}/health`);
|
||
|
|
return response.data.status === 'ok';
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Backend health check failed:', error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|