79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
/**
|
|
* Payment API
|
|
*
|
|
* 付费墙 / 充值相关接口
|
|
*/
|
|
import {
|
|
CreatePaymentOrderRequest,
|
|
CreatePaymentOrderResponse,
|
|
CreatePaymentOrderResponseSchema,
|
|
PaymentOrderStatusResponse,
|
|
PaymentOrderStatusResponseSchema,
|
|
PaymentPlansResponse,
|
|
PaymentPlansResponseSchema,
|
|
TipPaymentPlansResponse,
|
|
TipPaymentPlansResponseSchema,
|
|
} from "@/data/schemas/payment";
|
|
|
|
import { ApiPath } from "./api_path";
|
|
import { httpClient } from "./http_client";
|
|
import { ApiEnvelope, unwrap } from "./response_helper";
|
|
|
|
export class PaymentApi {
|
|
/**
|
|
* 获取套餐列表
|
|
*/
|
|
async getPlans(): Promise<PaymentPlansResponse> {
|
|
const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.paymentPlans);
|
|
return PaymentPlansResponseSchema.parse(
|
|
unwrap(env) as Record<string, unknown>,
|
|
);
|
|
}
|
|
|
|
/** 获取咖啡打赏套餐列表。 */
|
|
async getTipPlans(): Promise<TipPaymentPlansResponse> {
|
|
const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.paymentTipPlans);
|
|
return TipPaymentPlansResponseSchema.parse(
|
|
unwrap(env) as Record<string, unknown>,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 创建支付订单
|
|
*/
|
|
async createOrder(
|
|
body: CreatePaymentOrderRequest,
|
|
): Promise<CreatePaymentOrderResponse> {
|
|
const env = await httpClient<ApiEnvelope<unknown>>(
|
|
ApiPath.paymentCreateOrder,
|
|
{
|
|
method: "POST",
|
|
body,
|
|
},
|
|
);
|
|
return CreatePaymentOrderResponseSchema.parse(
|
|
unwrap(env) as Record<string, unknown>,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 查询支付订单状态
|
|
*/
|
|
async getOrderStatus(orderId: string): Promise<PaymentOrderStatusResponse> {
|
|
const env = await httpClient<ApiEnvelope<unknown>>(
|
|
ApiPath.paymentOrderStatus,
|
|
{
|
|
query: { order_id: orderId },
|
|
},
|
|
);
|
|
return PaymentOrderStatusResponseSchema.parse(
|
|
unwrap(env) as Record<string, unknown>,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 全局单例
|
|
*/
|
|
export const paymentApi = new PaymentApi();
|