示例#1
0
 /**
  * 充值完成后台回调函数
  */
 public function sumpay_bg()
 {
     $func = new Lib\Fun();
     $args['resp_code'] = $func->getRequest('resp_code');
     $args['resp_msg'] = $func->getRequest('resp_msg');
     $args['sign_type'] = $func->getRequest('sign_type');
     $args['sign'] = $func->getRequest('sign');
     $args['mer_id'] = $func->getRequest('mer_id');
     $args['order_no'] = $func->getRequest('order_no');
     $args['order_time'] = $func->getRequest('order_time');
     $args['serial_no'] = $func->getRequest('serial_no');
     $args['status'] = $func->getRequest('status');
     // 0 失败  1 成功
     $args['succ_time'] = $func->getRequest('succ_time');
     $args['succ_amt'] = $func->getRequest('succ_amt');
     $args['remark'] = $func->getRequest('remark');
     //通知成功
     if ($args['resp_code'] != '000000') {
         $resp_msg = 'resp_code: ' . $args['resp_code'] . ',resp_msg: ' . $args['resp_msg'];
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     $sumPay = new Lib\SumPay();
     //设置sign -----test
     $args['sign'] = $sumPay->signArrayData($args);
     $str = $sumPay->sortArrayData($args);
     if (!$sumPay->verify($str, $args['sign'])) {
         \Think\Log::record('验签失败!');
         exit('验签失败!');
     }
     //更新充值订单表,及关联的投资|借款交易表
     $PayLog = M("paylog");
     $data = $condition = array();
     if ($args['status'] == 1) {
         $data['f_state'] = 100;
         $data['f_suctime'] = $args['succ_time'];
     } elseif ($args['status'] == 0) {
         $data['f_state'] = -100;
     }
     $condition['f_paycode'] = $args['order_no'];
     $condition['f_money'] = $args['succ_amt'];
     $condition['f_state'] = 0;
     $ret = $PayLog->where($condition)->save($data);
     if (empty($ret)) {
         $resp_msg = '更新充值订单表失败,ret: ' . $ret;
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     $condition = array();
     $condition['f_paycode'] = $args['order_no'];
     $paylog_info = $PayLog->where($condition)->field('f_userid userid,f_paycode paycode,f_tradeid tradeid,f_money money,f_tradetype tradetype,f_state state')->select();
     //给用户加钱入账
     $data = $condition = array();
     $User = M("userinfo");
     $userid = $paylog_info[0]['userid'];
     $condition['f_uid'] = $userid;
     $vo = $User->where($condition)->field('f_usermoney money')->select();
     if (!empty($vo)) {
         $data = array();
         $data['f_usermoney'] = $vo[0]['money'] + $args['succ_amt'];
         $doRecharge = $User->where($condition)->save($data);
         if ($doRecharge !== false) {
             //插入账户流水
             TradeController::insertUserAccountLog(0, $userid, $args['succ_amt']);
         } else {
             $resp_msg = '更新用户账户信息失败,userid=' . $userid . ',usermoney=' . $data['f_usermoney'];
             \Think\Log::record($resp_msg);
             exit('fail');
         }
     } else {
         $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $userid;
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     if (!empty($paylog_info)) {
         $order_status = $paylog_info[0]['state'];
         $tradeid = $paylog_info[0]['tradeid'];
         $tradetype = $paylog_info[0]['tradetype'];
         //投资发起、借给他
         //成功or失败
         if ($order_status != 0) {
             if ($tradetype == 1) {
                 //更新投资交易表
                 $Invest = M("invest");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 200;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_id'] = $tradeid;
                 $condition['f_state'] = 100;
                 $ret = $Invest->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新投资交易表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新投资交易表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
             } elseif ($tradetype == 2) {
                 //更新借款交易表
                 $Loan = M("loan");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 300;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_id'] = $tradeid;
                 $condition['f_state'] = 100;
                 $ret = $Loan->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新借款交易表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新借款交易表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 $Trade_log = M("trade_log");
                 $User = M("userinfo");
                 //给投资用户扣钱、借款用户加钱入账
                 $condition = array();
                 $condition['f_vid'] = $tradeid;
                 $condition['f_type'] = 1;
                 //借款订单
                 $condition['f_state'] = 0;
                 $vo = $Trade_log->where($condition)->field('f_money trademoney,f_inuserid inuserid,f_outuserid outuserid')->select();
                 $invest_userid = $vo[0]['outuserid'];
                 $loan_userid = $vo[0]['inuserid'];
                 $trademoney = $vo[0]['trademoney'];
                 //-money
                 $condition = array();
                 $condition['f_uid'] = $invest_userid;
                 $vo = $User->where($condition)->field('f_usermoney money')->select();
                 if (!empty($vo)) {
                     $data = array();
                     $data['f_usermoney'] = $vo[0]['money'] - $trademoney;
                     $doRecharge = $User->where($condition)->save($data);
                     if ($doRecharge !== false) {
                         //插入账户流水
                         TradeController::insertUserAccountLog(8, $invest_userid, $trademoney);
                     } else {
                         $resp_msg = '更新用户账户信息失败,userid=' . $invest_userid . ',usermoney=' . $data['f_usermoney'];
                         \Think\Log::record($resp_msg);
                         exit('fail');
                     }
                 } else {
                     $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $invest_userid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 //+money
                 $condition = array();
                 $condition['f_uid'] = $loan_userid;
                 $vo = $User->where($condition)->field('f_usermoney money')->select();
                 if (!empty($vo)) {
                     $data = array();
                     $data['f_usermoney'] = $vo[0]['money'] + $trademoney;
                     $doRecharge = $User->where($condition)->save($data);
                     if ($doRecharge !== false) {
                         //插入账户流水
                         TradeController::insertUserAccountLog(9, $loan_userid, $trademoney);
                     } else {
                         $resp_msg = '更新用户账户信息失败,userid=' . $loan_userid . ',usermoney=' . $data['f_usermoney'];
                         \Think\Log::record($resp_msg);
                         exit('fail');
                     }
                 } else {
                     $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $loan_userid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 //更新trade_log表
                 $Trade_log = M("trade_log");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 100;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_vid'] = $tradeid;
                 $condition['f_type'] = 1;
                 $condition['f_state'] = 0;
                 $ret = $Trade_log->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新trade_log表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新trade_log表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
             }
             exit('success');
         }
     } else {
         $resp_msg = 'resp_code: ' . $args['resp_code'] . ',resp_msg: ' . $args['resp_msg'];
         \Think\Log::record($resp_msg);
         exit('fail');
     }
 }