接入Luffa钱包
This commit is contained in:
@@ -9,20 +9,61 @@ 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";
|
||||
import { SDKManager } from "db://assets/Scripts/Main/Channel/SDKManager";
|
||||
|
||||
export class LuffaPayFlow implements PaymentFlow {
|
||||
constructor(private delayMs: number = 5000) {}
|
||||
constructor(private delayMs: number = 10000) {}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
async resume(orderId: string, _payUrl: string): Promise<proto.cs.ICSQueryOrderRes | null> {
|
||||
@@ -33,8 +74,8 @@ export class LuffaPayFlow implements PaymentFlow {
|
||||
private async pollOrder(orderId: string) {
|
||||
const poller = new PaymentPoller();
|
||||
return poller.poll(orderId, {
|
||||
maxDurationMs: 10 * 60 * 1000,
|
||||
startIntervalMs: 5000,
|
||||
maxDurationMs: 2 * 60 * 1000,
|
||||
startIntervalMs: 3000,
|
||||
maxIntervalMs: 10000,
|
||||
jitter: true,
|
||||
}, (d) => logger.log());
|
||||
|
||||
@@ -28,7 +28,7 @@ export class PaymentService {
|
||||
this.flowMap = new Map<PaymentMethod, PaymentFlow>();
|
||||
this.flowMap.set(PaymentMethod.CashPay, new CashInrFlow(this.waitAppResumeIfNeeded.bind(this)));
|
||||
this.flowMap.set(PaymentMethod.Web3, new WebUsdFlow(5000));
|
||||
this.flowMap.set(PaymentMethod.Luffa, new LuffaPayFlow(5000));
|
||||
this.flowMap.set(PaymentMethod.Luffa, new LuffaPayFlow(10000));
|
||||
}
|
||||
|
||||
/** 发起支付:创建订单→打开URL→回到App后开始轮询 */
|
||||
|
||||
Reference in New Issue
Block a user