| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\api\service;
- use app\common\model\ComplexChannelModel;
- use think\Db;
- use think\Env;
- use think\Exception;
- use app\common\model\ComplexPay;
- use app\common\model\PayCpinfo;
- use app\common\model\ComplexMembers;
- use app\common\model\PolychannelGame;
- use app\common\logic\PayCallback;
- class NotifyService
- {
- protected $payInfo; //订单支付信息,子类里面有调用
- // ## TODO:每次新渠道,当前两个必须要添加
- // H5渠道游戏使用渠道逻辑处理
- protected $h5_handle_channel = ['zhaosy'];
- public function notify($channel, $input)
- {
- $channelModel = new ComplexChannelModel();
- try {
- if (!is_numeric($input['gameidqf'])) {
- echo 'no game';
- exit();
- }
- if (!($channel_id = $channelModel->where(['status' => 1, 'mark' => $channel])->value('id'))) {
- echo 'no channel';
- exit();
- }
- $polyChannelGame = [];
- if(isset($input['gameidqf']) && $input['gameidqf']>0){
- if (!$polyChannelGame = (new PolychannelGame())->alias('cpg')->join('cy_game cg', 'cpg.game_id=cg.id')->field('cpg.*, cg.type as game_type')->where(['cpg.channel_id' => $channel_id, 'cpg.game_id' => $input['gameidqf']])->find()) {
- echo 'no game';
- exit();
- }
- }
- $sentence = ucfirst($channel);
- $className = 'app\api\complex\\' . $sentence;
- $model = new $className;
- // H5 渠道游戏,单独处理
- if(in_array($channel, $this->h5_handle_channel) && $polyChannelGame['game_type'] == 3){
- $payResult = $model->paySignH5($input, $polyChannelGame);
- }else{
- $payResult = $model->paySign($input, $polyChannelGame);
- }
- if($payResult !== true){
- // 优化提示判断
- if(!empty($payResult['code']) && $payResult['code'] < 0){
- $msg = "回调处理失败, (c_err:{$payResult['code']})";
- $model->getFail($msg);
- }
- $model->getFail('回调处理失败!');
- }
- // 获取后续系统所需参数
- $dataResult = $model->getData($input);
- if ($dataResult === false) {
- log_message('非法操作!渠道回调参数有误!', 'error', LOG_PATH . 'complexPaylog/' . $channel . '/');
- $model->getFail("回调处理异常!(参数有误)");
- }
- // 优化提示判断
- if(!empty($dataResult['code'])){
- if($dataResult['code'] < 0){
- $dataMsg = "回调处理失败, (c_err:{$dataResult['code']})";
- $model->getFail($dataMsg);
- }
- }
- if (!$dataResult) {
- log_message('签名不一致,orderid=' . $dataResult['orderid'], 'error', LOG_PATH . 'complexPaylog/' . $channel . '/');
- $model->getFail('签名不一致');
- }
- //金额验证
- if (!$this->checkOrder($dataResult['orderid'], $dataResult['amount'])) {
- log_message('订单不存在或订单金额不一致,orderid=' . $dataResult['orderid'], 'error', LOG_PATH . 'complexPaylog/' . $channel . '/');
- $model->getFail('订单不存在或订单金额不一致');
- }
- if ($this->payInfo['status'] == 0) {
- // 启动事务
- Db::startTrans();
- try {
- $this->updateOrder($dataResult['orderid'], $dataResult['sub_orderid']);
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // curlDD('渠道支付回调:' . $e->getMessage(), Env::get('warning_url'));
- // 回滚事务
- Db::rollback();
- $model->getFail('渠道支付回调:' . $e->getMessage());
- }
- } else {
- $model->getSuccess('支付成功');
- }
- // 【产品确认:测试阶段暂时不用通知CP】通知CP,支付成功
- if ((new PayCallback())->callBackToCp($dataResult['orderid'])) {
- $model->getSuccess('支付成功');
- } else {
- $model->getFail('厂商支付回调失败');
- }
- } catch (Exception $e) {
- // dump($e->getMessage().' - '.$e->getFile().':'.$e->getLine());
- log_message("渠道支付回调-{$channel}:" . $e->getMessage(), 'error', LOG_PATH . 'complexPaylog/' . $channel . '/');
- curlDD("渠道支付回调-{$sentence}:" . $e->getMessage().' - '.$e->getFile().':'.$e->getLine() . ' \n input:'.json_encode($input), Env::get('dingtalk.notic_url'));
- echo 'FAIL:ERROR='.$e->getMessage();
- exit();
- }
- }
- /**
- * 检验订单
- *
- * @param $orderid string 订单ID
- * @param $amount string 订单金额
- *
- * @return boolean 验签结果
- */
- protected function checkOrder($orderid, $amount)
- {
- $payModel = new ComplexPay();
- $payInfo = $payModel->field('userid,amount,gameid,channel_id,status')->where(['orderid' => $orderid])->find();
- if (empty($payInfo)) {
- return false;
- // } elseif (floatval($payInfo['amount']) != floatval($amount)) {
- } elseif (bccomp(floatval($payInfo['amount']), floatval($amount), 2) !== 0) {
- return false;
- }
- $this->payInfo = $payInfo;
- return true;
- }
- /**
- * 更新订单信息
- *
- * @param $orderId string 订单号
- * @param $sub_orderid string 下级渠道订单号
- * @param $pay_time int 支付时间
- *
- * @return bool
- */
- protected function updateOrder($orderid, $sub_orderid, $pay_time = NOW_TIMESTAMP)
- {
- $payModel = new ComplexPay();
- $payCpinfoModel = new PayCpinfo;
- $memberModel = new ComplexMembers;
- //消费订单信息
- $payInfo = $payModel->field('id,pay_amount,userid,gameid,channel_id')->where(['orderid' => $orderid])->find();
- if ($payInfo) {
- // 订单表 = 已完成
- $payModel->save(['status' => 1, 'sub_orderid' => $sub_orderid, 'pay_time' => $pay_time], ['orderid' => $orderid]);
- // cp信息 = 支付成功
- $payCpinfoModel->save(['payflag' => 1], ['orderid' => $orderid]);
- //更新nw_complex_members表的total_pay_amount
- $memberModel->where(['id' => $payInfo['userid']])->update(['total_pay_amount' => Db::raw('total_pay_amount+' . $payInfo['pay_amount'])]);
- }
- }
- }
|