import { fetchApi } from "@/lib/server-api-util"; import { CreateOrder, PayOrder } from "@/types/personal-info"; import { PayItem } from "./premium"; // 使用 reduce 方法获取 discount_amount 的 amount 值最大的对象 export const maxDiscountProduct = (products: PayItem[]) => { if (!products || products.length === 0) { return products?.[0]; } return products?.reduce((max, current) => { const maxAmount = parseFloat(max.discount_amount?.amount || '0'); const currentAmount = parseFloat(current.discount_amount?.amount || '0'); return currentAmount > maxAmount ? current : max; }); } // 查看产品项 export const getPAy = async () => { const payInfo = await fetchApi(`/order/product-items?product_type=Membership`) let bestValue = maxDiscountProduct(payInfo) return { bestValue, payInfo } } // 创建订单 export const createOrder = async (id: number, quantity: number) => { const order = await fetchApi(`/order/create`, { method: 'POST', body: JSON.stringify({ items: [{ product_item_id: id, quantity: quantity }] }) }) return order } // 创建支付 export const createPayment = async (order_id: string, payment_method: string) => { const payment = await fetchApi(`/order/pay`, { method: 'POST', body: JSON.stringify({ order_id, payment_method }) }) return payment } // 支付中 export const payProcessing = async (transaction_id: string, third_party_transaction_id: string) => { const payment = await fetchApi(`/order/pay-processing`, { method: 'POST', body: JSON.stringify({ transaction_id, third_party_transaction_id }) }) return payment } // 支付失败 export const payFailure = async (transaction_id: string, reason: string) => { const payment = await fetchApi(`/order/pay-failure`, { method: 'POST', body: JSON.stringify({ transaction_id, reason }) }) return payment } // 支付成功 export const paySuccess = async (transaction_id: string, third_party_transaction_id: string) => { const payment = await fetchApi(`/order/pay-success`, { method: 'POST', body: JSON.stringify({ transaction_id, third_party_transaction_id }) }) return payment } // 判断订单是否过期 /** * 判断指定时间戳是否已过期(即当前时间是否已超过该时间戳) * @param expirationTimestamp - 过期时间戳(单位:毫秒) * @returns boolean - true: 未过期,false: 已过期 */ export const isOrderExpired = async (transactionDate: number) => { // 如果没有提供过期时间,视为无效或未设置,认为“已过期”或状态未知 if (!transactionDate || isNaN(transactionDate)) { return false; } const now = Date.now(); // 当前时间戳(毫秒) return now < transactionDate; }