支付协议修改
This commit is contained in:
@@ -6,6 +6,9 @@ import { EventTarget, game, Game } from "cc";
|
||||
import { PaymentApi } from "./PaymentApi";
|
||||
import { PaymentOpenerFactory, type OpenStrategy } from "./PaymentOpener";
|
||||
import PaymentPoller from "./PaymentPoller";
|
||||
import { PaymentFlow } from "./PaymentFlow";
|
||||
import { CashInrFlow } from "./CashInrFlow";
|
||||
import { WebUsdFlow } from "./WebUsdFlow";
|
||||
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";
|
||||
@@ -15,7 +18,16 @@ import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
|
||||
export class PaymentService {
|
||||
private static _I: PaymentService | null = null;
|
||||
static get I() { return this._I ?? (this._I = new PaymentService()); }
|
||||
private constructor() {}
|
||||
/** 策略表 */
|
||||
private flowMap: Record<number, PaymentFlow> = {};
|
||||
|
||||
private constructor() {
|
||||
this.flowMap = {};
|
||||
let num1 = proto.cs.EnmPayType.EPT_Cash_INR;
|
||||
this.flowMap[num1] = new CashInrFlow(this.waitAppResumeIfNeeded.bind(this));
|
||||
let num2 = proto.cs.EnmPayType.EPT_WEB3_USD;
|
||||
this.flowMap[num2] = new WebUsdFlow(5000);
|
||||
}
|
||||
|
||||
/** 发起支付:创建订单→打开URL→回到App后开始轮询 */
|
||||
async startPayment(req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise<proto.cs.ICSQueryOrderRes> {
|
||||
@@ -34,27 +46,9 @@ export class PaymentService {
|
||||
// 发出事件,支付订单创建成功
|
||||
Utils.sendInnerMsg(InnerMsgCode.PayOrderCreateSucc, { orderId: orderId });
|
||||
|
||||
// 打开支付页
|
||||
await PaymentOpenerFactory.create(open).open(payUrl);
|
||||
|
||||
// 等待回到前台(系统浏览器策略下)
|
||||
// - 如果是 inapp/minigame,可在 WebView/回跳时立即开始轮询;
|
||||
// - 这里给一个通用的“等待前台”方法(可用 Cocos 的 onShow/onHide 自行接入)
|
||||
await this.waitAppResumeIfNeeded(open);
|
||||
|
||||
// 轮询
|
||||
const poller = new PaymentPoller();
|
||||
const data = await poller.poll(orderId, {
|
||||
maxDurationMs: 2 * 60 * 1000,
|
||||
startIntervalMs: 2000,
|
||||
maxIntervalMs: 10000,
|
||||
jitter: true,
|
||||
}, (d) => console.log());
|
||||
|
||||
// 刷新订单数据
|
||||
orderData.applyQueryOrder(orderId, data);
|
||||
// 返回结果
|
||||
return data;
|
||||
// 分发流程
|
||||
const flow = this.flowMap[req.payType];
|
||||
return flow?.run(orderId, payUrl, open) ?? null;
|
||||
}
|
||||
|
||||
/** App 回到前台后恢复未完成订单的轮询(在 onShow 时调用) */
|
||||
|
||||
Reference in New Issue
Block a user