2025-07-28 17:24:10 +08:00

86 lines
2.4 KiB
TypeScript

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<PayItem[]>(`/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<CreateOrder>(`/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<PayOrder>(`/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
}