"use strict"; cc._RF.push(module, '28a00CfjWRE8axy84j1XmoF', 'payment.mod'); // Scripts/mod/payment/payment.mod.js "use strict"; /****************************************************************** * * 充值/支付模块 * ******************************************************************/ var BridgeController = require("bridge.controller"); var RoleController = require("role_controller"); var FFSDK = require("ff_sdk"); var TDefine = require("trace.define"); var LoginMod = require("login.mod"); var CPluginHelp = require("plugin_help"); var TTT = TDefine.TraceType; var PaymentModel = cc.Class({ "extends": BridgeController, // 注册协议接受事件 registerProtocals: function registerProtocals() { this.RegisterProtocal(16700, this.handle16700); //获取充值列表信息 this.RegisterProtocal(10399, this.handle10399); //模拟充值 // SDK支付 if (FFSDK.getInstance().isEnableSDK()) { FFSDK.getInstance().regSdkCallBack("pay", this.onPayResult, this); } }, // 从服务器初始化数据 reqBaseFromServer: function reqBaseFromServer(_cb) { nx.dt.fnInvoke(_cb, true); this.reqRechargeList(); nx.bridge.vset("PayWait", null); }, // ======================================================== // 价格相关 // ======================================================== // 价格转换 transPrice: function transPrice(_price) { var info = nx.bridge.vget("excRates"); if (nx.dt.objEmpty(info)) { nx.error("汇率转换失败,配置未找到!"); return 0; } var price = _price * info.mult / info.divi; return info.round ? Math.round(price) : Math.ceil(price); }, // 价格格式化 fmtPrice: function fmtPrice(_price) { var info = nx.bridge.vget("excRates"); if (nx.dt.objEmpty(info)) { nx.error("汇率转换失败,配置未找到!"); return ""; } return this.transPrice(_price) + info.name; }, // ======================================================== // 充值相关 // ======================================================== // 获取充值列表 reqRechargeList: function reqRechargeList(_cb) { this.SendProtocal(16700, {}, _cb); }, // 获取充值列表 handle16700: function handle16700(_data) { if (!this.isGoodData(_data)) { nx.bridge.vset("RechargeList", []); return; } nx.bridge.vset("RechargeList", _data.list || []); }, // ======================================================== // 支付相关 // ======================================================== // 模拟充值 handle10399: function handle10399(_data) {}, // 支付结果统一处理 payDone: function payDone(_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 reqPayment(_id, _cb) { var _this = this; // 支付中 var wait = nx.bridge.vget("PayWait"); if (wait && !wait.done) { nx.dt.fnInvoke(_cb, false, "支付中"); return; } // 获取商品信息 var 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 }, function (_ret, _params) { _this.payDone(_ret, _params); }); return; } // 基本信息 var info = nx.bridge.vget("curAccount"); var role = require("role_controller").getInstance().getRoleVo(); if (!info || !role) { cc.error("$Payment:\u652F\u4ED8\u5931\u8D25,\u57FA\u672C\u4FE1\u606F\u65E0\u6548!"); 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'], function (_key, _box) { _box.close(); if (_key == 'BuyNoAGoto') { LoginMod.getInstance().reqSDKLogout(function () { 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 onPayResult(_data) { // 参数无效 if (nx.dt.objEmpty(_data)) { cc.error("$Payment:\u652F\u4ED8\u5931\u8D25! \u7A7A\u8FD4\u56DE!"); this.payDone(false, "支付失败"); // 埋点 if (nx.mTrace && this.product) { nx.mTrace.trace(TTT.chargeStart, this.product.id, 0); } return; } // 支付失败 var res = _data.result || ""; if (res && parseInt(res) != 0) { cc.error("$Payment:\u652F\u4ED8\u5931\u8D25! " + 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; cc._RF.pop();