114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
/**
|
|
* 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 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;
|
|
}) {
|
|
return apiFetch<CheckoutCreateResponse>('/api/v1/checkout/create', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
address_id: payload.address_id,
|
|
payment_method: payload.payment_method || 'COD',
|
|
coupon_code: payload.coupon_code,
|
|
}),
|
|
});
|
|
}
|
|
|
|
export async function fetchCustomerAddresses() {
|
|
return apiFetch<CustomerAddress[]>('/api/v1/customer/profile/addresses');
|
|
}
|
|
|
|
export async function createCustomerAddress(address: Omit<CustomerAddress, 'address_id'> & { address_id?: string }) {
|
|
return apiFetch<CustomerAddress>('/api/v1/customer/profile/addresses', {
|
|
method: 'POST',
|
|
body: JSON.stringify(address),
|
|
});
|
|
}
|
|
|
|
export async function initiatePayment(order_id: string) {
|
|
return apiFetch<RazorpayInitiateResponse>('/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),
|
|
});
|
|
}
|