Files
18xchat/assets/Scripts/chat18x/payment/CashInrFlow.ts
T
2025-09-21 20:18:30 +08:00

48 lines
1.8 KiB
TypeScript

/**
* 现金支付流程
*
*/
import { PaymentOpenerFactory } from "./PaymentOpener";
import PaymentPoller from "./PaymentPoller";
import type { PaymentFlow } from "./PaymentFlow";
import type proto from "db://assets/Scripts/proto/proto.pb.js";
import { type OpenStrategy } from "./PaymentOpener";
import { DataManager, DataId } from "db://assets/Scripts/chat18x/data/DataManager";
import { OrderData } from "db://assets/Scripts/chat18x/data/OrderData";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
export class CashInrFlow implements PaymentFlow {
constructor(private waitAppResume: (open: OpenStrategy) => Promise<void>) {}
async run(orderId: string, payUrl: string, open: OpenStrategy): Promise<proto.cs.ICSQueryOrderRes | null> {
// 打开支付页
await PaymentOpenerFactory.create(open).open(payUrl);
// 等待回到前台(系统浏览器策略下)
// - 如果是 inapp/minigame,可在 WebView/回跳时立即开始轮询;
// - 这里给一个通用的“等待前台”方法(可用 Cocos 的 onShow/onHide 自行接入)
await this.waitAppResume(open);
// 开始轮询
const data = await this.pollOrder(orderId);
// 刷新订单数据
const orderData = DataManager.I.getDataById<OrderData>(DataId.Order);
orderData.applyQueryOrder(orderId, data);
// 返回结果
return data;
}
async resume(orderId: string, _payUrl: string): Promise<proto.cs.ICSQueryOrderRes | null> {
// 直接恢复轮询
return this.pollOrder(orderId);
}
private async pollOrder(orderId: string) {
const poller = new PaymentPoller();
return poller.poll(orderId, {
maxDurationMs: 2 * 60 * 1000,
startIntervalMs: 2000,
maxIntervalMs: 10000,
jitter: true,
}, (d) => logger.log());
}
}