作者 潘浩文

XIAOPAN

@@ -572,36 +572,73 @@ class OrderController extends HomeBaseController{ @@ -572,36 +572,73 @@ class OrderController extends HomeBaseController{
572 * @author 董瑞恩 572 * @author 董瑞恩
573 * @url /portal/order/pay 573 * @url /portal/order/pay
574 * @method GET 574 * @method GET
575 - *  
576 * @param name:order_no type:String require:1 default:无 other: desc:订单号 575 * @param name:order_no type:String require:1 default:无 other: desc:订单号
577 - *  
578 * @return data:返回用于调用支付的参数 576 * @return data:返回用于调用支付的参数
579 */ 577 */
580 - public function pay($order_no){  
581 - $order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();  
582 - $openId=Db::name('users')->where('id',$order['users_id'])->find()['open_id'];  
583 -  
584 - $body='支付';  
585 - $price=$order['price']*100;//订单价格  
586 - $notify_url=url('order/notify','','',true);//回调地址  
587 - $out_trade_no=$order_no.$this->create_noncestr(4);  
588 - $wxPay=new WeixinPay($openId,$out_trade_no,$body,$price,$notify_url);  
589 - $pay=$wxPay->pay();  
590 - if (isset($pay['package'])){  
591 - $data=[  
592 - 'state'=>1,  
593 - 'pay'=>$pay 578 + public function pay(){
  579 + $param=$this->request->param();
  580 + $order = Db::name('order')->where(['order_no' => $param['order_no'], 'state' => 2])->find();
  581 + $user = Db::name('users')->where('id', $order['users_id'])->find();
  582 + $price = $order['price'] * 100;//订单价格
  583 + //微信直接支付
  584 +// if ($param['type']==1) {
  585 +// $body = '支付';
  586 +// $notify_url = url('order/notify', '', '', true);//回调地址
  587 +// $out_trade_no = $param['order_no'] . $this->create_noncestr(4);
  588 +// $wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price, $notify_url);
  589 +// $pay = $wxPay->pay();
  590 +// if (isset($pay['package'])) {
  591 +// $data = [
  592 +// 'state' => 1,
  593 +// 'pay' => $pay
  594 +// ];
  595 +// $this->apiResponse(200, 'success', $data);
  596 +// } else {
  597 +// $data = [
  598 +// 'state' => 0,
  599 +// 'message' => '统一下单失败',
  600 +// 'error' => $pay
  601 +// ];
  602 +// $this->apiResponse(200, 'success', $data);
  603 +// }
  604 +// }
  605 + //余额支付
  606 + if ($user['fee']>$price){
  607 + Db::startTrans();
  608 + try {
  609 + Db::name('users')->where('id', $user['id'])->setDec('fee',$price);
  610 + Db::name('order')->where('id',$order['id'])->update(['state'=>3]);
  611 + Db::name('fee_log')->insert(['user_id'=>$user['id'],'fee'=>$price,'type'=>2,'add_time'=>time()]);
  612 + // 提交事务
  613 + Db::commit();
  614 + } catch (\Exception $e) {
  615 + // 回滚事务
  616 + Db::rollback();
  617 + $this->apiResponse(400,'支付失败');
  618 + }
  619 + $this->apiResponse(201, '余额支付成功');
  620 + }elseif ($user['fee']<$price){
  621 + Db::name('order')->where('id',$order['id'])->update(['group_money'=>($price-$user['fee'])]);
  622 + $body = '支付';
  623 + $notify_url = url('order/group', '', '', true);//回调地址
  624 + $out_trade_no = $param['order_no'] . $this->create_noncestr(4);
  625 + $wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price-$user['fee'], $notify_url);
  626 + $pay = $wxPay->pay();
  627 + if (isset($pay['package'])) {
  628 + $data = [
  629 + 'state' => 1,
  630 + 'pay' => $pay
594 ]; 631 ];
595 - $this->apiResponse(200,'success',$data);  
596 - }else{  
597 - $data=[  
598 - 'state'=>0,  
599 - 'message'=>'统一下单失败', 632 + $this->apiResponse(200, 'success', $data);
  633 + } else {
  634 + $data = [
  635 + 'state' => 0,
  636 + 'message' => '统一下单失败',
600 'error' => $pay 637 'error' => $pay
601 ]; 638 ];
602 - $this->apiResponse(200,'success',$data); 639 + $this->apiResponse(200, 'success', $data);
  640 + }
603 } 641 }
604 -  
605 } 642 }
606 /** 643 /**
607 * @title 充值生成订单 644 * @title 充值生成订单
@@ -656,7 +693,6 @@ class OrderController extends HomeBaseController{ @@ -656,7 +693,6 @@ class OrderController extends HomeBaseController{
656 $user_map['id']=$order['users_id']; 693 $user_map['id']=$order['users_id'];
657 $user=\db('users')->where($user_map)->find(); 694 $user=\db('users')->where($user_map)->find();
658 if ($order['price']>$user['fee']){ 695 if ($order['price']>$user['fee']){
659 -  
660 $this->apiResponse('400','余额不足!'); 696 $this->apiResponse('400','余额不足!');
661 } 697 }
662 $result=\db('users')->where($user_map)->setDec('fee',$order['price']); 698 $result=\db('users')->where($user_map)->setDec('fee',$order['price']);
@@ -705,7 +741,36 @@ class OrderController extends HomeBaseController{ @@ -705,7 +741,36 @@ class OrderController extends HomeBaseController{
705 } 741 }
706 } 742 }
707 743
708 - 744 + //组合支付回调
  745 + public function group(){
  746 + $param = $this->request->param();
  747 + if ($param == null) {
  748 + $param = file_get_contents("php://input");
  749 + if ($param == null) {
  750 + $param = $GLOBALS['HTTP_RAW_POST_DATA'];
  751 + }
  752 + }
  753 + $wxPay=new WeixinPay();
  754 + $data = $wxPay->xmlToArray($param);
  755 + $Sign = $data['sign'];
  756 + //支付成功回调后变更订单状态
  757 + $mySign = $wxPay->getSign($data);
  758 + $order_no =substr($data['out_trade_no'], 0, -4);
  759 + $order=\db('order')->where(['order_no'=>$order_no])->find();
  760 + if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
  761 + try{
  762 + Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
  763 + Db::name('fee_log')->insert(['user_id'=>$order['users_id'],'type'=>2,'fee'=>($order['money']-$order['group_money'])]);
  764 + Db::name('users')->where('id',$order['users_id'])->setDec('fee',($order['money']-$order['group_money']));
  765 + }catch (\Exception $exception){
  766 + $this->apiResponse(301,'error:'.$exception->getMessage());
  767 + }
  768 + return "<xml>
  769 + <return_code><![CDATA[SUCCESS]]></return_code>
  770 + <return_msg><![CDATA[OK]]></return_msg>
  771 + </xml>";
  772 + }
  773 + }
709 774
710 function create_noncestr($length = 4){ 775 function create_noncestr($length = 4){
711 $chars = "0123456789"; 776 $chars = "0123456789";
@@ -166,7 +166,7 @@ class PayController extends HomeBaseController @@ -166,7 +166,7 @@ class PayController extends HomeBaseController
166 */ 166 */
167 public function luckyMoney(){ 167 public function luckyMoney(){
168 //todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用 168 //todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
169 - /* $param=$this->request->param(); 169 + $param=$this->request->param();
170 $signature = $param['s']; 170 $signature = $param['s'];
171 $arithmetic['timeStamp']= $param['t']; 171 $arithmetic['timeStamp']= $param['t'];
172 $arithmetic['randomStr']= $param['r']; 172 $arithmetic['randomStr']= $param['r'];
@@ -174,7 +174,7 @@ class PayController extends HomeBaseController @@ -174,7 +174,7 @@ class PayController extends HomeBaseController
174 $str = arithmetic($arithmetic); 174 $str = arithmetic($arithmetic);
175 if($str != $signature){ 175 if($str != $signature){
176 $this->error('签名验证失败'); 176 $this->error('签名验证失败');
177 - }*/ 177 + }
178 178
179 $app = new Application($this->options); 179 $app = new Application($this->options);
180 $luckyMoney = $app->lucky_money; 180 $luckyMoney = $app->lucky_money;
@@ -270,7 +270,6 @@ class UsersController extends HomeBaseController{ @@ -270,7 +270,6 @@ class UsersController extends HomeBaseController{
270 public function getOrderById2(){ 270 public function getOrderById2(){
271 $users_id=$this->request->param('users_id'); 271 $users_id=$this->request->param('users_id');
272 $order= Db::name('order')->where(['users_id'=>$users_id,'state'=>2,'type'=>'1'])->find(); 272 $order= Db::name('order')->where(['users_id'=>$users_id,'state'=>2,'type'=>'1'])->find();
273 -  
274 if (empty($order)){ 273 if (empty($order)){
275 $this->apiResponse(200,'没有未支付订单'); 274 $this->apiResponse(200,'没有未支付订单');
276 } 275 }