1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
   | <?php namespace Home\Controller;
  use Think\Controller; use Common\Controller\HomeController;
 
 
 
 
 
  class PayController extends HomeController {
      
 
      function __construct() {         parent::__construct();         $pay_scene = 1;          $ModelObj = D('Order');          $bodytags = '产品购买';         $is_test = true;          $this->pay_scene = $pay_scene;         $this->ModelObj = $ModelObj;         $this->bodytags = $bodytags;         $this->is_test = $is_test;     }
      
 
      public function wxpay() {         $ModelObj = $this->ModelObj;                  $order_number = I('order_number') ? I('order_number') : session('order_number');         session('order_number', $order_number);         if (I('order_number')) {                          $this->redirect('wx_getuser');         }
          $order = $ModelObj->where(array('order_number' => $order_number))->find();                  $payData = array(             'out_trade_no' => $order['order_number'],             'attach' => $order_number,              'total_fee' => $this->is_test ? 1 : $order['real_all_money'] * 100,              'body' => $this->bodytags,             'goods_tag' => $this->bodytags,         );         $reData = D('Wxpay', 'Service')->pay($payData);         $this->reData = $reData;         $this->order = $order;         $this->display('wxpay');     }
      
 
      public function wx_getuser() {         $payConfig = D('Wxpay', 'Service')->getConfig();         $appid = $payConfig['APPID'];         $appsecret = $payConfig['APPSECRET'];                  if (I('code') || S('WxData-'.session('uid'))) {             $code = I('code');             if (!S('WxData-'.session('uid'))) {                 $open_id_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';                 $ch = curl_init();                 curl_setopt($ch, CURLOPT_URL, $open_id_url);                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);                 curl_setopt($ch, CURLOPT_HEADER, FALSE);                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);                 $result = curl_exec($ch);                 curl_close($ch);                 $access_token_info = json_decode($result, true);                                  S('WxData-'.session('uid'), $access_token_info, $access_token_info['expires_in'] - 1000);             }             $data = S('WxData-'.session('uid'));                          $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';             $ch = curl_init();             curl_setopt($ch, CURLOPT_URL, $user_info_url);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);             curl_setopt($ch, CURLOPT_HEADER, FALSE);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);             $userinfo = curl_exec($ch);             curl_close($ch);             $userinfoArr = json_decode($userinfo, true);             S('userInfo', $userinfoArr);              $this->redirect('wxpay');          } else {                          $code_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode(C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U(CONTROLLER_NAME . '/' . ACTION_NAME)) . '&response_type=code&scope=snsapi_userinfo&state=state_str#wechat_redirect';             header("Location:" . $code_url);             exit();         }     }
      
 
      public function wxpay_notify_url() {         $ModelObj = $this->ModelObj;         $payServiceObj = D('Wxpay', 'Service');         $payConfig = $payServiceObj->getConfig();         $xml = file_get_contents("php://input");         $array = $payServiceObj->xmlToArray($xml);                  $payServiceObj->setPaylog($array);                  if ($array['result_code'] == 'SUCCESS' && $array['return_code'] == 'SUCCESS' && $array['mch_id'] == $payConfig['MCHID']) {             $orderid = $array['out_trade_no'];                          $re = $ModelObj->setPay($orderid, 2);             $array['pay_scene'] = $this->pay_scene;              $re2 = D('PayWx')->addRecord($array);          }
      }
      
 
      public function wx_resultinfo() {         $ModelObj = $this->ModelObj;         $order_number = I('out_trade_no');         $order = $ModelObj->where(array('order_number' => $order_number))->find();         $this->order = $order;         $this->display('pay_ok');     }
  }
   |