新增支付流程 LuffaPayFlow

This commit is contained in:
chen wei bo
2025-09-28 14:16:37 +08:00
parent 66ba9dbc67
commit 303003b550
6 changed files with 78 additions and 12 deletions
@@ -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<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));
}
}
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "2a1d83c2-61b6-493d-b3cb-4928d0f640a2",
"files": [],
"subMetas": {},
"userData": {}
}
@@ -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<number, PaymentFlow> = {};
private flowMap: Map<PaymentMethod, 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);
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));
}
/** 发起支付:创建订单→打开URL→回到App后开始轮询 */
async startPayment(req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise<proto.cs.ICSQueryOrderRes> {
async startPayment(payMethod: PaymentMethod, req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise<proto.cs.ICSQueryOrderRes> {
// 下单
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;
}
+7
View File
@@ -26,3 +26,10 @@ export interface OrderVO {
retCode: number; // 失败时的原因码
createdAt: number;
}
// 支付方式
export enum PaymentMethod {
CashPay = "CashPay", // 现金支付
Web3 = "Web3", // Web3支付
Luffa = "Luffa", // Luffa钱包
}
@@ -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;
@@ -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);