| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- /**
- * 聚合渠道统一下单接口
- *
- */
- namespace app\api\controller\v1;
- use app\api\controller\Api;
- use app\common\library\RsaSign;
- use app\common\model\ComplexMembers;
- use app\common\model\Game;
- use app\common\model\ComplexChannel;
- use app\common\model\Cpurl;
- use app\common\model\ComplexPay as PayModel;
- use app\common\model\PayCpinfo;
- use app\common\logic\Complex;
- use app\common\model\PolychannelGame;
- use think\Db;
- use think\Config;
- use think\Env;
- use think\Exception;
- use think\Model;
- class ComplexPay extends Api
- {
- protected $nowTime;
- // 需要进行单独处理的渠道标识
- protected $_neekCheckChannel = ['sanjiu', 'ledaoios', 'dangle', 'kuaishou', 'quchen', 'simo', 'gyln', 'kshy', 'weilian'];
- public function _initialize()
- {
- parent::_initialize();
- $this->nowTime = NOW_TIMESTAMP;
- }
- /**
- * 支付处理
- */
- public function index()
- {
- $gameid = $this->input('gameid'); //游戏ID
- $member_id = $this->input('userid'); //用户ID
- $serverid = $this->input('serverid'); //服务器ID
- $amount = $this->input('amount'); //充值金额(单位为分)
- $roleid = $this->input('roleid'); //角色ID
- $attach = $this->input('attach'); // CP方的扩展参数
- $imeil = $this->input('imeil'); //手机imeil码
- $rolename = $this->input('rolename', ''); //角色名
- $rolelevel = $this->input('rolelevel', ''); //角色角色等级
- $channel_mark = $this->input('channel_mark'); //渠道标识
- $orderid = 'CM' . makeOrderid();
- $memberModel = new ComplexMembers;
- $cpurlModel = new Cpurl;
- $gameModel = new Game;
- $payModel = new PayModel;
- $payCpinfoModel = new PayCpinfo;
- $channelModel = new ComplexChannel;
- $complexLogic = new Complex;
- $checkResult = $this->validate($this->input, [
- ['gameid', 'require|integer', '游戏id不能为空|游戏ID必须为整型'],
- ['userid', 'require|integer', '用户ID不能为空|用户ID必须为整型'],
- ['serverid', 'require', '游戏服务器id不能为空'],
- ['amount', 'require|gt:0', '充值金额不能为空|充值金额必须大于0'],
- ['roleid', 'require', '角色id不能为空'],
- ['attach', 'require', '游戏合作方订单参数不能为空'],
- ['channel_mark', 'require', '渠道标识不能为空']
- ]);
- if (true !== $checkResult) {
- $this->jsonResult('', 0, $checkResult);
- }
- if (!($channel_id = $channelModel->where(['status' => 1, 'mark' => $channel_mark])->value('id'))) {
- $this->jsonResult('', 0, '渠道信息不存在');
- } elseif ($payModel->where(['attach' => $attach, 'gameid' => $gameid])->find()) {
- $this->jsonResult('', 0, '游戏合作方订单参数不能重复');
- }
- if (!$polyChannelGame = (new PolychannelGame())->where(['channel_id' => $channel_id, 'game_id' => $gameid])->find()) {
- $this->jsonResult('', 0, '渠道游戏不存在');
- }
- $memberInfo = $memberModel->field('mg_username,complex_id,complex_username,extparam')->where(['id' => $member_id])->find();
- if (empty($memberInfo)) {
- $this->jsonResult('', 0, '用户不存在');
- }
- //当前游戏的渠道是否禁止消费(非归属渠道)
- if ($complexLogic->isFrozen($channel_id, $gameid, 'consume')) {
- $this->jsonResult('', 0, '您所在渠道已经被禁止在此游戏消费');
- }
- //充值回调地址
- $cpurl = $cpurlModel->where(['appid' => $this->appInfo['id']])->value('url');
- if (empty($cpurl)) {
- $this->jsonResult('', 0, '没有回调地址,请通知我方配置');
- }
- //游戏信息
- $gameInfo = $gameModel->field('order_recheck,nickname')->where(['id' => $gameid])->find();
- if(empty($gameInfo)){
- $this->jsonResult('', 0, '游戏id参数错误');
- }
- $payData['orderid'] = $orderid;
- $payData['amount'] = $amount;
- $payData['userid'] = $member_id;
- $payData['username'] = $memberInfo['mg_username'];
- $payData['roleid'] = $roleid;
- $payData['rolename'] = $rolename;
- $payData['rolelevel'] = $rolelevel;
- $payData['serverid'] = $serverid;
- $payData['gameid'] = $gameid;
- $payData['status'] = 0;
- $payData['ip'] = request()->ip();
- $payData['imei'] = $imeil;
- $payData['create_time'] = $this->nowTime;
- $payData['channel_id'] = $channel_id;
- $payData['attach'] = $attach;
- $payData['pay_amount'] = $amount;
- $payData['discount'] = 1.00;
- // ## 折扣 - 实付金额 ##
- $proportion = Db::table('cy_game_pay_discount')->where(['game_id' => $gameid])->value('proportion');
- if($proportion > 0 && $proportion < 1){
- // 先计算折扣后的应付总额
- $payData['pay_amount'] = round(bcmul((string)$amount, (string)$proportion, 3), 2);
- $payData['discount'] = $proportion;
- }
- // 启动事务
- Db::startTrans();
- try{
- $result = []; //返回值
- $payModel->insert($payData);
- $str = "orderid=".urlencode($orderid)."&username=".urlencode($memberInfo['mg_username'])."&gameid=".$gameid."&roleid=".urlencode($roleid)."&serverid=".urlencode($serverid)."&paytype=".urlencode('jhzf')."&amount=".$amount."&paytime=".$this->nowTime."&attach=".urlencode($attach);
- $param = $str."&appkey=".urlencode($this->appInfo['appkey']);
- $md5params = md5($param);
- $params = $str . "&sign=".urlencode($md5params);
- $paycpData['orderid'] = $orderid;
- $paycpData['fcallbackurl'] = $cpurl;
- $paycpData['params'] = $params;
- $paycpData['create_time'] = $this->nowTime;
- $paycpData['payflag'] = 0;
- //写入cy_paycpinfo表数据
- $payCpinfoModel->insert($paycpData);
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->jsonResult('', 0, '订单生成失败' . $e->getMessage());
- }
- $result['username'] = $memberInfo['complex_username']; //聚合子渠道的玩家用户名
- $result['orderid'] = (Config::get('self_complex_channel_mark') == $channel_mark) ? 'xkhyn###' . $orderid : $orderid;
- $result['extend'] = ['notify_url' => ''];
- // 判断是否有做登录验证的渠道
- if (in_array($channel_mark, $this->_neekCheckChannel)) {
- try {
- $sentence = ucfirst($channel_mark);
- $className = 'app\api\complex\\' . $sentence;
- $model = new $className;
- $channelData = $this->input;
- $channelData['pay_info'] = $payData;
- $channelData['member_info'] = $memberInfo->toArray();
- $channelData['game_info'] = $gameInfo->toArray();
- $channelData['channel_info'] = $polyChannelGame->toArray();
- $channelData['notify_url'] = API_DOMAINS."/v1.complex_pay_notify/notify/channelqf/{$channel_mark}/gameidqf/{$channelData['gameid']}";
- $channelData['extparam'] = [];
- if(!empty($memberInfo['extparam'])){
- $channelData['extparam'] = json_decode($memberInfo['extparam'],true);
- }
- $resultData = $model->getOrder($channelData);
- if ($resultData['code'] != 200) {
- curlDD("渠道-" . $sentence . '-下单异常_创建订单失败:' . $resultData['msg'], Env::get('dingtalk.notic_url'));
- if($resultData['code'] == -200){
- $this->jsonResult('', 0, '订单创建失败:'.$resultData['msg']);
- }
- $this->jsonResult('', 0, "订单创建失败({$resultData['code']}={$resultData['msg']})");
- }
- $result['extend'] = $resultData['param']??"{}";
- } catch (Exception $e) {
- // dump($e->getMessage(), $e->getFile(), $e->getLine());
- curlDD("渠道-" . $sentence . '-下单异常:' . $e->getMessage().' - '.$e->getFile().":".$e->getLine(), Env::get('dingtalk.notic_url'));
- $this->jsonResult('', 0, '订单创建异常:' . $e->getMessage());
- }
- }
- $this->jsonResult($result, 1, '订单生成成功');
- }
- }
|