2025-09-28 14:15:54 +08:00
|
|
|
/**
|
|
|
|
|
* 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";
|
2025-09-28 23:04:16 +08:00
|
|
|
import { SDKManager } from "db://assets/Scripts/Main/Channel/SDKManager";
|
2025-09-28 14:15:54 +08:00
|
|
|
|
|
|
|
|
export class LuffaPayFlow implements PaymentFlow {
|
2025-09-28 23:04:16 +08:00
|
|
|
constructor(private delayMs: number = 10000) {}
|
2025-09-28 14:15:54 +08:00
|
|
|
|
|
|
|
|
async run(orderId: string, _payUrl: string, _open: OpenStrategy): Promise<proto.cs.ICSQueryOrderRes | null> {
|
2025-09-28 23:04:16 +08:00
|
|
|
try {
|
|
|
|
|
// 连接钱包(只需一次,如果已连过可以跳过)
|
|
|
|
|
const address = SDKManager.I.getWalletAddress();
|
|
|
|
|
if (!address) {
|
|
|
|
|
logger.log("钱包未连接,开始连接 Luffa 钱包...");
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
|
SDKManager.I.connectWallet((addr) => {
|
|
|
|
|
if (addr) {
|
|
|
|
|
logger.log("已连接 Luffa 钱包:", addr);
|
|
|
|
|
resolve();
|
|
|
|
|
} else {
|
|
|
|
|
logger.error("Luffa 钱包连接失败");
|
|
|
|
|
reject(new Error("Luffa 钱包连接失败"));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造交易参数
|
|
|
|
|
const rawTx = {
|
|
|
|
|
toAddress: "Hm3g4xXM4JHt5EwFUcvVZ8RthR4KWUnR51fKHCbnKkiZ", // TODO
|
|
|
|
|
amount: 1 // TODO
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 签名交易
|
|
|
|
|
const signed = await SDKManager.I.signTransaction(rawTx);
|
|
|
|
|
logger.log("Luffa 签名结果:", signed);
|
|
|
|
|
|
|
|
|
|
// 发送交易
|
|
|
|
|
const sent = await SDKManager.I.sendTransaction(signed.signedData);
|
|
|
|
|
logger.log("Luffa 交易已提交,hash:", sent.hash);
|
|
|
|
|
|
|
|
|
|
// 等待一会再开始轮询
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error("Luffa Payment process exception:", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-09-28 14:15:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, {
|
2025-09-28 23:04:16 +08:00
|
|
|
maxDurationMs: 2 * 60 * 1000,
|
|
|
|
|
startIntervalMs: 3000,
|
2025-09-28 14:15:54 +08:00
|
|
|
maxIntervalMs: 10000,
|
|
|
|
|
jitter: true,
|
|
|
|
|
}, (d) => logger.log());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private delay(ms: number) {
|
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
|
}
|
|
|
|
|
}
|