Esempio n. 1
0
 /**
  * 微信支付通知
  *
  */
 public function action_notice_wxpay()
 {
     //获取微信支付服务器提供的数据
     $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
     $result = \handler\common\Tool::xmlToArray($xml);
     //获取商户的支付配置信息
     $trade = \Model_OrderTrade::query()->where('out_trade_no', $result['out_trade_no'])->get_one();
     if (!$trade) {
         die('trade empty');
     }
     //订单交易对象
     $trade->response_msg = json_encode($result);
     $trade->return_trade_no = $result['transaction_id'];
     $trade->real_money = $result['total_fee'] / 100;
     $trade->updated_at = time();
     //订单对象
     $order = $trade->order;
     //支付配置
     $access = \Model_AccessConfig::query()->where('access_type', 'wxpay')->where('seller_id', $order->from_id)->where('enable', 'ENABLE')->get_one();
     //检验签名
     $tmpSign = $result;
     unset($tmpSign['sign']);
     $sign = handler\mp\Tool::getWxPaySign($tmpSign, $access->access_key);
     $params = array('return_code' => 'SUCCESS');
     if ($result['sign'] != $sign) {
         $order->order_status = 'PAYMENT_ERROR';
         $trade->return_status = 'ERROR';
         $params = array('return_code' => 'FAIL', 'return_msg' => '签名失败');
     } else {
         if ($order->order_status == 'WAIT_PAYMENT') {
             $order->paid_fee += $result['total_fee'] / 100;
             $order->pay_at = time();
             if ($order->paid_fee >= $order->original_money) {
                 $order->order_status = 'PAYMENT_SUCCESS';
             }
             $trade->return_status = 'SUCCESS';
             $trade->return_trade_no = $result['transaction_id'];
             $trade->response_msg = json_encode($result);
         }
     }
     if ($order->save() && $order->remark == 'qrcode') {
         \Model_Order::delivery($order->id);
     }
     $trade->save();
     $data = \handler\common\Tool::arrayToXml($params);
     die($data);
 }
Esempio n. 2
0
File: wxapi.php Progetto: wxl2012/wx
 /**
  * 网页授权获取用户基本信息回调处理方法
  *
  * @access  public
  * @return  Response
  */
 public function action_oauth2_callback()
 {
     $params = \Input::get();
     if (!\Input::get('code', false)) {
         \Session::set_flash('msg', ['status' => 'err', 'msg' => '你拒绝授权,系统无法确认您的身份!系统中止!', 'title' => '错误']);
         return $this->show_message();
     }
     $this->account = \Session::get('WXAccount', \Model_WXAccount::find(1));
     $url = handler\mp\Tool::createOauthUrlForOpenid($this->account->app_id, $this->account->app_secret, $params['code']);
     $result = \handler\common\UrlTool::request($url, 'GET', null, true);
     $result = json_decode($result->body);
     if (!isset($result->openid) || !$result->openid) {
         \Session::set_flash('msg', ['status' => 'err', 'msg' => '未获取到OpenId!', 'title' => '错误']);
         return $this->show_message();
     }
     //跳转参数加openid
     $to_url = \Input::get('to_url', '/');
     $addspan = strpos($to_url, '?') !== false ? '&' : '?';
     $to_url = "{$to_url}{$addspan}openid={$result->openid}";
     //获取openid对象
     $wechatOpenID = \Model_WechatOpenid::query()->where(['openid' => $result->openid])->get_one();
     //openid存在,不需要创建
     if ($wechatOpenID) {
         \Response::redirect($to_url);
         return;
     }
     //拉取用户信息
     $url = handler\mp\Tool::createOauthUrlForUserinfo($result->access_token, $result->openid);
     $result = \handler\common\UrlTool::request($url, 'GET', null, true);
     $result = json_decode($result->body);
     if (isset($result->errcode)) {
         \Session::set_flash('msg', ['status' => 'err', 'msg' => $result->errmsg, 'title' => '错误']);
         return $this->show_message();
     }
     //查询微信用户信息是否存在
     $wechat = \Model_Wechat::query()->where(['nickname' => $result->nickname, 'sex' => $result->sex, 'city' => $result->city, 'province' => $result->province, 'country' => $result->country, 'headimgurl' => $result->headimgurl])->get_one();
     //存在则直接赋值微信信息记录
     if ($wechat) {
         $wechatOpenID->wechat_id = $wechatOpenID->id;
         return;
     }
     //创建openid数据及微信信息
     $wechatOpenID = handler\mp\Account::createWechatAccount($result->openid, $this->account);
     if (!$wechatOpenID) {
         \Session::set_flash('msg', ['status' => 'err', 'msg' => '微信信息保存失败! 缺少必要信息,系统终止!', 'title' => '错误']);
         return $this->show_message();
     }
     $wechat = $wechatOpenID->wechat;
     # 保存拉取到的用户信息
     $wechat->nickname = $result->nickname;
     $wechat->sex = $result->sex;
     $wechat->city = $result->city;
     $wechat->province = $result->province;
     $wechat->country = $result->country;
     $wechat->headimgurl = $result->headimgurl;
     $wechat->language = isset($result->language) ? $result->language : '';
     $wechat->subscribe_time = isset($result->subscribe_time) ? $result->subscribe_time : 0;
     $wechat->subscribe = isset($result->subscribe) ? $result->subscribe : 0;
     $wechat->save();
     \Response::redirect($to_url);
 }