/** * Cart + checkout API helpers for authenticated storefront checkout sync & Razorpay payment. */ import { apiFetch } from './client'; export interface SyncCartItem { variant_id: string; qty: number; } export interface CheckoutCreateResponse { order_id: string; order_no: string; total_amount: number; tax_amount?: number; shipping_cost?: number; final_amount: number; status: string; payment_status: string; payment_method?: string; invoice_id?: string | null; invoice_no?: string | null; } export interface CustomerAddress { address_id: string; address_type: string; full_name: string; phone: string; street_address: string; city: string; state: string; pincode: string; is_default: boolean; } export interface RazorpayInitiateResponse { order_id: string; rzp_order_id: string; rzp_key_id: string; amount: number; currency: string; security_token: string; zero_amount?: boolean; status?: string; invoice_id?: string | null; invoice_no?: string | null; } export interface AvailableCoupon { coupon_id: string; code: string; title: string; description?: string | null; discount_type: 'PERCENTAGE' | 'FIXED' | 'FREE_SHIPPING'; discount_value: number; max_discount_cap?: number | null; min_order_amount: number; first_order_only: boolean; valid_to?: string | null; is_eligible: boolean; ineligibility_reason?: string | null; } export async function syncCartItems(items: SyncCartItem[]) { return apiFetch<{ message: string; cart_id: string }>('/api/v1/cart/items', { method: 'PUT', body: JSON.stringify({ items }), }); } export async function createCheckoutOrder(payload: { address_id: string; payment_method?: string; coupon_code?: string; use_wallet_credits?: boolean; }) { return apiFetch('/api/v1/checkout/create', { method: 'POST', body: JSON.stringify({ address_id: payload.address_id, payment_method: payload.payment_method || 'RAZORPAY', coupon_code: payload.coupon_code, use_wallet_credits: Boolean(payload.use_wallet_credits), }), }); } export async function fetchCustomerAddresses() { return apiFetch('/api/v1/customer/profile/addresses'); } export async function fetchAvailableCoupons() { return apiFetch('/api/v1/storefront/coupons'); } export async function createCustomerAddress(address: Omit & { address_id?: string }) { return apiFetch('/api/v1/customer/profile/addresses', { method: 'POST', body: JSON.stringify(address), }); } export async function initiatePayment(order_id: string) { return apiFetch('/api/v1/payment/initiate', { method: 'POST', body: JSON.stringify({ order_id }), }); } export async function cancelPayment(order_id: string, reason?: string) { return apiFetch<{ status: string; message: string }>('/api/v1/payment/cancel', { method: 'POST', body: JSON.stringify({ order_id, reason }), }); } export async function verifyPayment(payload: { order_id: string; razorpay_order_id: string; razorpay_payment_id: string; razorpay_signature: string; }) { return apiFetch<{ status: string; message: string; order_id: string; order_no: string; invoice_id?: string | null; invoice_no?: string | null; }>('/api/v1/payment/verify', { method: 'POST', body: JSON.stringify(payload), }); }