Files
fc/dev/project/assets/Scripts/mod/payment/payment.mod.js
T

243 lines
7.0 KiB
JavaScript
Raw Normal View History

2026-05-23 22:10:14 +08:00
/******************************************************************
*
* 充值/支付模块
*
******************************************************************/
const BridgeController = require( "bridge.controller" );
const RoleController = require( "role_controller" );
const FFSDK = require( "ff_sdk" );
const TDefine = require( "trace.define" );
const LoginMod = require( "login.mod" );
const CPluginHelp = require("plugin_help");
const TTT = TDefine.TraceType;
const PaymentModel = cc.Class( {
extends: BridgeController,
// 注册协议接受事件
registerProtocals: function() {
this.RegisterProtocal( 16700, this.handle16700 ); //获取充值列表信息
this.RegisterProtocal( 10399, this.handle10399 ); //模拟充值
// SDK支付
if( FFSDK.getInstance().isEnableSDK() ) {
FFSDK.getInstance().regSdkCallBack( "pay", this.onPayResult, this );
}
},
// 从服务器初始化数据
reqBaseFromServer: function( _cb ) {
nx.dt.fnInvoke( _cb, true );
this.reqRechargeList();
nx.bridge.vset( "PayWait", null );
},
// ========================================================
// 价格相关
// ========================================================
// 价格转换
transPrice: function( _price ) {
let info = nx.bridge.vget( "excRates" );
if( nx.dt.objEmpty( info ) ) {
nx.error( "汇率转换失败,配置未找到!" );
return 0;
}
let price = _price * info.mult / info.divi;
return info.round ? Math.round( price ) : Math.ceil( price );
},
// 价格格式化
fmtPrice: function( _price ) {
let info = nx.bridge.vget( "excRates" );
if( nx.dt.objEmpty( info ) ) {
nx.error( "汇率转换失败,配置未找到!" );
return "";
}
return this.transPrice( _price ) + info.name;
},
// ========================================================
// 充值相关
// ========================================================
// 获取充值列表
reqRechargeList: function( _cb ) {
this.SendProtocal( 16700, {}, _cb );
},
// 获取充值列表
handle16700: function( _data ) {
if( !this.isGoodData( _data ) ) {
nx.bridge.vset( "RechargeList", [] );
return;
}
nx.bridge.vset( "RechargeList", _data.list || [] );
},
// ========================================================
// 支付相关
// ========================================================
// 模拟充值
handle10399: function( _data ) { },
// 支付结果统一处理
payDone: function( _ret, _params ) {
// 失败处理
if( !_ret ) {
nx.mbox( _params || "支付失败" );
nx.bridge.vset( "PayWait", null );
nx.dt.fnInvoke( this.cbPay, false, _params );
this.cbPay = null;
return;
}
// 支付成功
nx.bridge.vset( "PayWait", { done: true, msg: "支付成功" } );
nx.bridge.vset( "PayWait", null, false );
nx.dt.fnInvoke( this.cbPay, true, _params );
this.cbPay = null;
},
// 支付
reqPayment: function( _id, _cb ) {
// 支付中
let wait = nx.bridge.vget( "PayWait" );
if( wait && !wait.done ) {
nx.dt.fnInvoke( _cb, false, "支付中" );
return;
}
// 获取商品信息
let product = game.configs.charge_data.data_charge_data[ _id ];
if( !product ) {
nx.mbox( "无效的支付项" );
nx.dt.fnInvoke( _cb, false, "无效支付" );
return;
}
this.cbPay = _cb;
this.product = product;
// 调试模式走GM
if( CC_DEBUG && cc.sys.isBrowser ) {
nx.bridge.vset( "PayWait", { done: false, msg: "支付中" } );
this.SendProtocal( 10399, {
msg: `pay ${ product.id }`
}, ( _ret, _params ) => {
this.payDone( _ret, _params );
} );
return;
}
// 基本信息
let info = nx.bridge.vget( "curAccount" );
let role = require( "role_controller" ).getInstance().getRoleVo();
if( !info || !role ) {
cc.error( `$Payment:支付失败,基本信息无效!` );
this.payDone( false, "基本信息无效" );
return;
}
// SDK支付
if( FFSDK.getInstance().isEnableSDK() ) {
// 埋点
if( nx.mTrace ) {
nx.mTrace.trace( TTT.chargeStart, product.id );
}
nx.bridge.vset( "PayWait", { done: false, msg: "支付中" } );
if( FFSDK.getInstance().mJsonSDKInfo.channelName == "erolabs" ){
if( nx.dt.strEmpty( FFSDK.getInstance().accountinfo.data.user_info.account ) ) {
nx.mbox( "BuyNoAccountTip", [ 'Cancle', 'BuyNoAGoto' ], ( _key, _box )=>{
_box.close();
if( _key == 'BuyNoAGoto' ){
LoginMod.getInstance().reqSDKLogout( ()=>{
FFSDK.getInstance().guestBind();
} );
FFSDK.getInstance().doLogout();
}else{
CPluginHelp.getInstance().sendSdkCallBack( "pay", { } );
}
} )
}
return;
}
FFSDK.getInstance().pay(
product.id,
product.val,
product.name,
"",
info.account,
role.rid,
role.srv_id,
);
return;
}
// 无效支付
this.payDone( false, "无效支付" );
},
// SDK支付回调
onPayResult: function( _data ) {
// 参数无效
if( nx.dt.objEmpty( _data ) ) {
cc.error( `$Payment:支付失败! 空返回!` );
this.payDone( false, "支付失败" );
// 埋点
if( nx.mTrace && this.product ) {
nx.mTrace.trace( TTT.chargeStart, this.product.id, 0 );
}
return;
}
// 支付失败
let res = _data.result || "";
if( res && parseInt( res ) != 0 ) {
cc.error( `$Payment:支付失败! ${ res }, ${ _data.msg || "unknown" }` );
this.payDone( false, _data.msg || "支付失败" );
// 埋点
if( nx.mTrace && this.product ) {
nx.mTrace.trace( TTT.chargeStart, this.product.id, 2 );
}
return;
}
// 支付成功
this.payDone( true, "支付成功" );
// 埋点
if( nx.mTrace && this.product ) {
nx.mTrace.trace( TTT.chargeStart, this.product.id, 1 );
}
},
} );
module.exports = PaymentModel;