支付相关
This commit is contained in:
@@ -10,27 +10,11 @@ import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
||||
import { DataManager, DataId } from "db://assets/Scripts/chat18x/data/DataManager";
|
||||
import { OrderData } from "db://assets/Scripts/chat18x/data/OrderData";
|
||||
|
||||
export const PaymentEvents = {
|
||||
Started: "pay-started", // { orderId }
|
||||
UrlOpened: "pay-url-opened",
|
||||
PollTick: "pay-poll-tick", // { orderId, status }
|
||||
Finished: "pay-finished",
|
||||
};
|
||||
|
||||
export class PaymentService {
|
||||
private static _I: PaymentService | null = null;
|
||||
static get I() { return this._I ?? (this._I = new PaymentService()); }
|
||||
private constructor() {}
|
||||
|
||||
private bus = new EventTarget();
|
||||
|
||||
on(event: string, cb: (...args: any[]) => void, target?: any) {
|
||||
this.bus.on(event, cb, target);
|
||||
}
|
||||
off(event: string, cb: (...args: any[]) => void, target?: any) {
|
||||
this.bus.off(event, cb, target);
|
||||
}
|
||||
|
||||
/** 发起支付:创建订单→打开URL→回到App后开始轮询 */
|
||||
async startPayment(req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise<proto.cs.ICSQueryOrderRes> {
|
||||
// 下单
|
||||
@@ -40,7 +24,6 @@ export class PaymentService {
|
||||
return { status: -1, retCode: -1, balance: 0, vipExpire: 0};
|
||||
}
|
||||
const { orderId, payUrl } = create.data;
|
||||
this.bus.emit(PaymentEvents.Started, { orderId });
|
||||
|
||||
// 保存订单数据
|
||||
const orderData = DataManager.I.getDataById<OrderData>(DataId.Order);
|
||||
@@ -48,7 +31,6 @@ export class PaymentService {
|
||||
|
||||
// 打开支付页
|
||||
await PaymentOpenerFactory.create(open).open(payUrl);
|
||||
this.bus.emit(PaymentEvents.UrlOpened, { orderId });
|
||||
|
||||
// 等待回到前台(系统浏览器策略下)
|
||||
// - 如果是 inapp/minigame,可在 WebView/回跳时立即开始轮询;
|
||||
@@ -62,12 +44,11 @@ export class PaymentService {
|
||||
startIntervalMs: 2000,
|
||||
maxIntervalMs: 10000,
|
||||
jitter: true,
|
||||
}, (d) => this.bus.emit(PaymentEvents.PollTick, { orderId, status: d.status }));
|
||||
}, (d) => console.log());
|
||||
|
||||
// 刷新订单数据
|
||||
orderData.applyQueryOrder(orderId, data);
|
||||
// 返回结果
|
||||
this.bus.emit(PaymentEvents.Finished, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -81,7 +62,6 @@ export class PaymentService {
|
||||
const r = await this.startPollingOnly(o);
|
||||
if (r && r.status !== 1) {
|
||||
orderData.applyQueryOrder(o, r);
|
||||
this.bus.emit(PaymentEvents.Finished, r);
|
||||
}
|
||||
// r === null(轮询超时)时保留在仓库,等待下一次 resume 再查
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user