Esempio n. 1
0
 /**
  * 找他借
  */
 public function toTradeLoan()
 {
     $func = new Lib\Fun();
     $inuserid = $outuserid = $myMoney = 0;
     $sucurl = U('Invest/index');
     $vid = $func->checkint($func->getRequest('vid'), -1);
     if ($vid < 0) {
         $func->showMsg("参数错误");
     } else {
         $userInfo = $this->getUserinfo($this->userid);
         $myMoney = $userInfo['f_usermoney'];
         $Invest = M("invest");
         $condition = array();
         $condition['f_id'] = $vid;
         $vInfo = $Invest->where($condition)->field('f_userid userid,f_money money,f_yield yield,f_horizon horizon,f_danbaomoney danbaomoney,f_state state')->select();
         $loan_userid = $this->userid;
         $invest_userid = intval($vInfo[0]['userid']);
         if ($invest_userid == $loan_userid) {
             $func->showMsg("不能与自己交易");
         }
         if (!empty($vInfo)) {
             $vState = $vInfo[0]['state'];
             $danbaoMoney = $vInfo[0]['danbaomoney'];
             $tradeMoney = $vInfo[0]['money'];
             //已充值成功投资订单才允许找他借
             if ($vState == 200) {
                 //插入交易流水 (暂不用事务处理)
                 $TradeLog = M("trade_log");
                 $data = array();
                 $data['f_type'] = 0;
                 $data['f_inuserid'] = $loan_userid;
                 $data['f_outuserid'] = $invest_userid;
                 $data['f_money'] = $tradeMoney;
                 $data['f_yield'] = $vInfo[0]['yield'];
                 $data['f_horizon'] = $vInfo[0]['horizon'];
                 $data['f_danbaomoney'] = $danbaoMoney;
                 $data['f_vid'] = $vid;
                 $data['f_addtime'] = time();
                 $res = $TradeLog->add($data);
                 if (!empty($res)) {
                     $tradeId = $res;
                     //更新投资表订单状态
                     $data = array();
                     $data['f_state'] = 300;
                     //交易成功
                     $data['f_tradeid'] = $tradeId;
                     $condition['f_id'] = $vid;
                     $condition['f_state'] = 200;
                     $upState = $Invest->where($condition)->save($data);
                     if ($upState !== false) {
                         $User = M("userinfo");
                         //给投资用户扣款
                         $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);
                                 $func->showMsg($resp_msg);
                             }
                         } else {
                             $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $invest_userid;
                             \Think\Log::record($resp_msg);
                             $func->showMsg($resp_msg);
                         }
                         //给贷款用户加款
                         $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);
                                 $func->showMsg($resp_msg);
                             }
                         } else {
                             $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $loan_userid;
                             \Think\Log::record($resp_msg);
                             $func->showMsg($resp_msg);
                         }
                         //更新trade_log表
                         $Trade_log = M("trade_log");
                         $data = array();
                         $data['f_state'] = 100;
                         $condition = array();
                         $condition['f_tid'] = $tradeId;
                         $condition['f_type'] = 0;
                         $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);
                             $func->showMsg($resp_msg);
                         }
                         $func->showMsg("借款成功");
                     } else {
                         $func->showMsg("更新订单状态失败");
                     }
                 } else {
                     $func->showMsg("插入交易流水失败");
                 }
             } else {
                 $func->showMsg("该订单不允许交易");
             }
         } else {
             $func->showMsg("不存在的订单");
         }
     }
 }
Esempio n. 2
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');
     }
 }