예제 #1
0
 public function exec($message, $processor)
 {
     if (!empty($processor)) {
         $b = new Bridge(null);
         $platform = $b->getOne($processor['id'], true);
         $body = $this->toRequestXml($message);
         $url = $platform['url'];
         if (!strpos($url, '?') == -1) {
             $url .= '?';
         } else {
             $url .= '&';
         }
         $params = array('timestamp' => TIMESTAMP, 'nonce' => util_random(10, 1));
         $signParams = array($platform['token'], $params['timestamp'], $params['nonce']);
         sort($signParams, SORT_STRING);
         $params['signature'] = sha1(implode($signParams));
         $url .= http_build_query($params, '', '&');
         $ret = Net::httpPost($url, $body);
         if (!empty($ret)) {
             return $this->toPacket($ret);
         }
     }
 }
 public function weixinAction()
 {
     $code = I('get.code');
     if (!empty($code)) {
         Member::loadSettings();
         $setting = C('MS');
         $auth = $setting[Member::OPT_AUTH_WEIXIN];
         if ($auth == '0') {
             exit('request error');
         } else {
             $a = new Account();
             $account = $a->getAccount($auth, Account::ACCOUNT_WEIXIN);
             $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$account['appid']}&secret={$account['secret']}&code={$code}&grant_type=authorization_code";
             $ret = Net::httpGet($url);
             if (!is_error($ret)) {
                 $auth = @json_decode($ret, true);
                 if (is_array($auth) && !empty($auth['openid'])) {
                     $condition = '`platformid`=:platform AND `openid`=:openid';
                     $pars = array();
                     $pars[':platform'] = $account['id'];
                     $pars[':openid'] = $auth['openid'];
                     $fan = $a->table('__MMB_MAPPING_FANS__')->where($condition)->bind($pars)->find();
                     if (empty($fan)) {
                         $platform = Platform::create($account['id']);
                         $info = $platform->fansQueryInfo($auth['openid'], true);
                         $fan = array();
                         $fan['platformid'] = $account['id'];
                         $fan['uid'] = 0;
                         $fan['openid'] = $auth['openid'];
                         if (!is_error($info)) {
                             if (!empty($info['original']['unionid'])) {
                                 $fan['unionid'] = $info['original']['unionid'];
                                 $uid = $a->table('__MMB_MAPPING_FANS__')->field('uid')->where("`unionid`=`{$info['original']['unionid']}`")->find();
                                 if (!empty($uid)) {
                                     $fan['uid'] = $uid;
                                 }
                             }
                             $fan['subscribe'] = $info['original']['subscribe'];
                             $fan['subscribetime'] = $info['original']['subscribe_time'];
                             unset($info['original']);
                             $fan['info'] = serialize($info);
                         }
                         $fan['unsubscribetime'] = 0;
                         $a->table('__MMB_MAPPING_FANS__')->data($fan)->add();
                     }
                     $stateKey = I('get.state');
                     $state = session('auth:forward');
                     session('auth:forward', null);
                     if ($state[0] == $stateKey) {
                         $forward = $state[1];
                     } else {
                         $forward = U('/');
                     }
                     if (stripos($forward, '?') !== false) {
                         $forward .= '&wxref=mp.weixin.qq.com#wechat_redirect';
                     } else {
                         $forward .= '?wxref=mp.weixin.qq.com#wechat_redirect';
                     }
                     if (!empty($fan['uid'])) {
                         //登陆
                         $m = new Member();
                         $member = $m->profile($uid);
                         if (!empty($member)) {
                             $m->login($fan['uid']);
                             redirect($forward);
                         }
                     }
                     if ($setting[Member::OPT_POLICY] == Member::OPT_POLICY_CLASSICAL) {
                         //兼容模式, 创建新用户
                         $this->error('兼容模式暂未支持');
                         redirect($forward);
                     } else {
                         //统一模式, 注册新用户
                         session('fan:weixin', $fan);
                         if (!empty($fan['info'])) {
                             $fan['info'] = unserialize($fan['info']);
                         }
                         $this->assign('fan', $fan);
                         $this->assign('forward', $forward);
                         $this->display('weixin');
                     }
                     return;
                 } else {
                     $this->error('微信授权失败错误信息为: ' . $ret);
                 }
             }
             $this->error('微信授权失败错误信息为: ' . $ret['message']);
         }
     }
     exit('访问错误');
 }
예제 #3
0
 /**
  * @param $log
  * @return js payment object
  */
 public function payWeixin($log)
 {
     self::loadSettings();
     $setting = C('PAY');
     $pay = $setting[self::OPT_WEIXIN];
     $a = new Account();
     $account = $a->getAccount($pay['account']);
     $pay['appid'] = $account['appid'];
     $pay['secret'] = $account['secret'];
     $wOpt = array();
     $m = new Member();
     $fan = $m->fetchFan($log['uid'], $pay['account']);
     $package = array();
     $package['appid'] = $pay['appid'];
     $package['mch_id'] = $pay['mchid'];
     $package['nonce_str'] = util_random(8);
     $package['body'] = $log['title'];
     $package['attach'] = $log['plid'];
     $package['out_trade_no'] = md5($log['plid']);
     $package['total_fee'] = $log['fee'] * 100;
     $package['spbill_create_ip'] = get_client_ip();
     $package['time_start'] = date('YmdHis', TIMESTAMP);
     $package['time_expire'] = date('YmdHis', TIMESTAMP + 600);
     $package['notify_url'] = __HOST__ . U('wander/payment/weixin/t/notify');
     $package['trade_type'] = 'JSAPI';
     $package['openid'] = $fan['openid'];
     ksort($package, SORT_STRING);
     $string1 = '';
     foreach ($package as $key => $v) {
         $string1 .= "{$key}={$v}&";
     }
     $string1 .= "key={$pay['key']}";
     $package['sign'] = strtoupper(md5($string1));
     $dat = util_2xml($package);
     $response = Net::httpPost('https://api.mch.weixin.qq.com/pay/unifiedorder', $dat);
     if (is_error($response)) {
         return $response;
     }
     $xml = '<?xml version="1.0" encoding="utf-8"?>' . $response;
     $dom = new \DOMDocument();
     if (!$dom->loadXML($xml)) {
         return error(-1, 'error response');
     }
     $xpath = new \DOMXPath($dom);
     if ($xpath->evaluate("string(//xml/return_code)") == 'FAIL') {
         return error(-2, $xpath->evaluate("string(//xml/return_msg)"));
     }
     if ($xpath->evaluate("string(//xml/result_code)") == 'FAIL') {
         return error(-3, $xpath->evaluate("string(//xml/err_code_des)"));
     }
     $prepayid = $xpath->evaluate("string(//xml/prepay_id)");
     $wOpt['appId'] = $pay['appid'];
     $wOpt['timeStamp'] = TIMESTAMP;
     $wOpt['nonceStr'] = util_random(8);
     $wOpt['package'] = 'prepay_id=' . $prepayid;
     $wOpt['signType'] = 'MD5';
     ksort($wOpt, SORT_STRING);
     $string = '';
     foreach ($wOpt as $key => $v) {
         $string .= "{$key}={$v}&";
     }
     $string .= "key={$pay['key']}";
     $wOpt['paySign'] = strtoupper(md5($string));
     return $wOpt;
 }
예제 #4
0
 public function fansQueryInfo($uniid, $isPlatform)
 {
     if (!$isPlatform) {
         $openid = '';
     } else {
         $openid = $uniid;
     }
     $token = $this->fetchToken();
     if (is_error($token)) {
         return $token;
     }
     $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
     $response = Net::httpGet($url);
     if (is_error($response)) {
         return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
     }
     $result = @json_decode($response, true);
     if (empty($result)) {
         return error(-2, "接口调用失败, 错误信息: {$response}");
     } elseif (!empty($result['errcode'])) {
         return error(-3, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']}");
     }
     $ret = array();
     $ret['nickname'] = $result['nickname'];
     $ret['gender'] = $result['sex'];
     $ret['residecity'] = $result['city'];
     $ret['resideprovince'] = $result['province'];
     $ret['avatar'] = $result['headimgurl'];
     if (!empty($ret['avatar'])) {
         $ret['avatar'] = rtrim($ret['avatar'], '0');
         $ret['avatar'] .= '132';
     }
     $ret['original'] = $result;
     return $ret;
 }