47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
/**
|
|||
|
|
* Luffa 支付流程
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
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 LuffaPayFlow implements PaymentFlow {
|
||
|
|
constructor(private delayMs: number = 5000) {}
|
||
|
|
|
||
|
|
async run(orderId: string, _payUrl: string, _open: OpenStrategy): Promise<proto.cs.ICSQueryOrderRes | null> {
|
||
|
|
// 延时
|
||
|
|
await this.delay(this.delayMs);
|
||
|
|
// 开始轮询
|
||
|
|
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> {
|
||
|
|
// 直接轮询(也可以选择先 delay 一下再 poll,看业务需求)
|
||
|
|
return this.pollOrder(orderId);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async pollOrder(orderId: string) {
|
||
|
|
const poller = new PaymentPoller();
|
||
|
|
return poller.poll(orderId, {
|
||
|
|
maxDurationMs: 10 * 60 * 1000,
|
||
|
|
startIntervalMs: 5000,
|
||
|
|
maxIntervalMs: 10000,
|
||
|
|
jitter: true,
|
||
|
|
}, (d) => logger.log());
|
||
|
|
}
|
||
|
|
|
||
|
|
private delay(ms: number) {
|
||
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
|
}
|
||
|
|
}
|