From 303003b550b2868226c1c1c0d635f3bd65ed4abc Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Sun, 28 Sep 2025 14:15:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E4=BB=98=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=20LuffaPayFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/chat18x/payment/LuffaPayFlow.ts | 46 +++++++++++++++++++ .../chat18x/payment/LuffaPayFlow.ts.meta | 9 ++++ .../Scripts/chat18x/payment/PaymentService.ts | 17 +++---- assets/Scripts/chat18x/payment/types.ts | 7 +++ .../chat18x/ui/panels/PurchasePanel.ts | 7 +-- .../Scripts/chat18x/ui/panels/Web3PopPanel.ts | 4 +- 6 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 assets/Scripts/chat18x/payment/LuffaPayFlow.ts create mode 100644 assets/Scripts/chat18x/payment/LuffaPayFlow.ts.meta diff --git a/assets/Scripts/chat18x/payment/LuffaPayFlow.ts b/assets/Scripts/chat18x/payment/LuffaPayFlow.ts new file mode 100644 index 00000000..d3640bc5 --- /dev/null +++ b/assets/Scripts/chat18x/payment/LuffaPayFlow.ts @@ -0,0 +1,46 @@ +/** + * 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 { + // 延时 + await this.delay(this.delayMs); + // 开始轮询 + const data = await this.pollOrder(orderId); + // 刷新订单数据 + const orderData = DataManager.I.getDataById(DataId.Order); + orderData.applyQueryOrder(orderId, data); + // 返回结果 + return data; + } + + async resume(orderId: string, _payUrl: string): Promise { + // 直接轮询(也可以选择先 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)); + } +} diff --git a/assets/Scripts/chat18x/payment/LuffaPayFlow.ts.meta b/assets/Scripts/chat18x/payment/LuffaPayFlow.ts.meta new file mode 100644 index 00000000..e3505113 --- /dev/null +++ b/assets/Scripts/chat18x/payment/LuffaPayFlow.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "2a1d83c2-61b6-493d-b3cb-4928d0f640a2", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/payment/PaymentService.ts b/assets/Scripts/chat18x/payment/PaymentService.ts index 228170d5..8b38989b 100644 --- a/assets/Scripts/chat18x/payment/PaymentService.ts +++ b/assets/Scripts/chat18x/payment/PaymentService.ts @@ -9,29 +9,30 @@ import PaymentPoller from "./PaymentPoller"; import { PaymentFlow } from "./PaymentFlow"; import { CashInrFlow } from "./CashInrFlow"; import { WebUsdFlow } from "./WebUsdFlow"; +import { LuffaPayFlow } from "./LuffaPayFlow"; 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"; import Utils from "../../Main/Common/Utils"; import { InnerMsgCode } from "../../Main/Config/InnerMsgCode"; import { logger } from "db://assets/Scripts/Main/Common/Logger"; +import { PaymentMethod } from './types'; export class PaymentService { private static _I: PaymentService | null = null; static get I() { return this._I ?? (this._I = new PaymentService()); } /** 策略表 */ - private flowMap: Record = {}; + private flowMap: Map; 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); + this.flowMap = new Map(); + 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)); } /** 发起支付:创建订单→打开URL→回到App后开始轮询 */ - async startPayment(req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise { + async startPayment(payMethod: PaymentMethod, req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise { // 下单 logger.log("创建订单的请求数据:", req); const create = await PaymentApi.I.createOrder(req); @@ -49,7 +50,7 @@ export class PaymentService { Utils.sendInnerMsg(InnerMsgCode.PayOrderCreateSucc, { orderId: orderId, payType: req.payType,goodId:req.goodId }); // 分发流程 - const flow = this.flowMap[req.payType]; + const flow = this.flowMap.get(payMethod); return flow?.run(orderId, payUrl, open) ?? null; } diff --git a/assets/Scripts/chat18x/payment/types.ts b/assets/Scripts/chat18x/payment/types.ts index 07ce5318..96e62e55 100644 --- a/assets/Scripts/chat18x/payment/types.ts +++ b/assets/Scripts/chat18x/payment/types.ts @@ -26,3 +26,10 @@ export interface OrderVO { retCode: number; // 失败时的原因码 createdAt: number; } + +// 支付方式 +export enum PaymentMethod { + CashPay = "CashPay", // 现金支付 + Web3 = "Web3", // Web3支付 + Luffa = "Luffa", // Luffa钱包 +} diff --git a/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts b/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts index 0af4bd82..889bdb36 100644 --- a/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts +++ b/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts @@ -16,6 +16,7 @@ import { NavigationManager } from "../../manager/NavigationManager"; import { Web3PopPanel } from "./Web3PopPanel"; import { TipsPanel } from "./TipsPanel"; import { logger } from "db://assets/Scripts/Main/Common/Logger"; +import { PaymentMethod } from 'db://assets/Scripts/chat18x/payment/types'; const { ccclass, property } = _decorator; @@ -194,7 +195,7 @@ export class PurchasePanel extends li_BaseView { const isRechargeId = shopData.isRechargeId(id); if (isRechargeId) { // 需要外部支付进行购买 - this.startPayment(id, this.currentPayType, networkType); + this.startPayment(id, this.currentPayType, networkType, PaymentMethod.CashPay); } //测试用 // Utils.sendInnerMsg(InnerMsgCode.PayOrderCreateSucc, { @@ -287,14 +288,14 @@ export class PurchasePanel extends li_BaseView { } // 创建订单 - async startPayment(goodId: number, payType: number, network: number) { + async startPayment(goodId: number, payType: number, network: number, payMethod: PaymentMethod) { // 请求数据 const reqData = { goodId, payType, network, }; - let res = await PaymentService.I.startPayment(reqData); + let res = await PaymentService.I.startPayment(payMethod, reqData); logger.log("支付完成的响应数据:", res); if (res) { let status = res.status; diff --git a/assets/Scripts/chat18x/ui/panels/Web3PopPanel.ts b/assets/Scripts/chat18x/ui/panels/Web3PopPanel.ts index be0de440..8feeff36 100644 --- a/assets/Scripts/chat18x/ui/panels/Web3PopPanel.ts +++ b/assets/Scripts/chat18x/ui/panels/Web3PopPanel.ts @@ -13,6 +13,7 @@ import { SDKManager } from "../../../Main/Channel/SDKManager"; import LanguageUtils from "../../../Main/Common/LanguageUtils"; import { logger } from "db://assets/Scripts/Main/Common/Logger"; import { PersonalPanel } from "./PersonalPanel"; +import { PaymentMethod } from 'db://assets/Scripts/chat18x/payment/types'; const { ccclass, property } = _decorator; @@ -223,7 +224,8 @@ export class Web3PopPanel extends li_BaseView { this.payPanel.startPayment( this.goodId, proto.cs.EnmPayType.EPT_WEB3_USD, - type + type, + PaymentMethod.Web3 ); this.selection.scale = new Vec3(1, 0, 1);