49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
/**
|
|
* 和服务器交互的API接口
|
|
*
|
|
*/
|
|
import { ApiClient } from "../network/client/ApiClient";
|
|
import type { Endpoint } from "../network/client/endpoints";
|
|
import type { ApiResponse } from "../network/client/types";
|
|
import { OrderStatus, CreateOrderReq, CreateOrderData, QueryOrderData } from "./types";
|
|
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
|
|
|
export class PaymentApi {
|
|
private static _I: PaymentApi | null = null;
|
|
static get I() { return this._I ?? (this._I = new PaymentApi()); }
|
|
private constructor(private api = ApiClient.I) {}
|
|
|
|
// 下单
|
|
createOrder(req: CreateOrderReq): Promise<ApiResponse<CreateOrderData>> {
|
|
let epData: Endpoint<CreateOrderReq, { orderId: string; payUrl: string; expireAt?: number; }> = {
|
|
path: "pay/create",
|
|
method: "POST",
|
|
codec: "json",
|
|
needsAuth: true,
|
|
};
|
|
return this.api.call(epData, req);
|
|
}
|
|
|
|
// 查询订单
|
|
queryOrder(orderId: string): Promise<ApiResponse<QueryOrderData>> {
|
|
let epData: Endpoint<{ orderId: string }, { orderId: string; status: OrderStatus; paidAt?: number; failureReason?: string; }> = {
|
|
path: "pay/query",
|
|
method: "POST",
|
|
codec: "json",
|
|
needsAuth: true,
|
|
};
|
|
return this.api.call(epData, { orderId });
|
|
}
|
|
|
|
// 取消订单
|
|
cancelOrder(orderId: string): Promise<ApiResponse<{ ok: boolean }>> {
|
|
let epData: Endpoint<{ orderId: string }, { ok: boolean; }> = {
|
|
path: "pay/cancel",
|
|
method: "POST",
|
|
codec: "json",
|
|
needsAuth: true,
|
|
};
|
|
return this.api.call(epData, { orderId });
|
|
}
|
|
}
|