示例#1
0
 /**
  * $ret['url']
  */
 public function payAlipay($log)
 {
     $gateway = 'http://wappaygw.alipay.com/service/rest.htm?';
     self::loadSettings();
     $setting = C('PAY');
     $pay = $setting[self::OPT_ALIPAY];
     $set = array();
     $set['service'] = 'alipay.wap.trade.create.direct';
     $set['format'] = 'xml';
     $set['v'] = '2.0';
     $set['partner'] = $pay['partner'];
     $set['req_id'] = $log['plid'];
     $set['sec_id'] = 'MD5';
     $callback = __HOST__ . U('wander/payment/alipay/t/return');
     $notify = __HOST__ . U('wander/payment/alipay/t/nofity');
     $merchant = __HOST__ . U('wander/payment/alipay/t/merchant');
     $expire = 10;
     $set['req_data'] = "<direct_trade_create_req><subject>{$log['title']}</subject><out_trade_no>{$log['plid']}</out_trade_no><total_fee>{$log['fee']}</total_fee><seller_account_name>{$pay['account']}</seller_account_name><call_back_url>{$callback}</call_back_url><notify_url>{$notify}</notify_url><out_user>{$log['uid']}</out_user><merchant_url>{$merchant}</merchant_url><pay_expire>{$expire}</pay_expire></direct_trade_create_req>";
     $prepares = array();
     foreach ($set as $key => $value) {
         if ($key != 'sign') {
             $prepares[] = "{$key}={$value}";
         }
     }
     sort($prepares);
     $string = implode($prepares, '&');
     $string .= $pay['secret'];
     $set['sign'] = md5($string);
     $response = Net::httpGet($gateway . http_build_query($set));
     if (is_error($response)) {
         return $response;
     }
     $ret = array();
     @parse_str($response, $ret);
     foreach ($ret as &$v) {
         $v = str_replace('\\"', '"', $v);
     }
     if (is_array($ret)) {
         if ($ret['res_error']) {
             $dom = new \DOMDocument();
             if (!$dom->loadXML($ret['res_error'])) {
                 return error(-1, '支付宝支付初始化失败');
             }
             $xpath = new \DOMXPath($dom);
             return error(-2, '支付宝支付初始化失败, 详细错误: ' . $xpath->evaluate('string(//err/detail)'));
         }
         if ($ret['partner'] == $set['partner'] && $ret['req_id'] == $set['req_id'] && $ret['sec_id'] == $set['sec_id'] && $ret['service'] == $set['service'] && $ret['v'] == $set['v']) {
             $prepares = array();
             foreach ($ret as $key => $value) {
                 if ($key != 'sign') {
                     $prepares[] = "{$key}={$value}";
                 }
             }
             sort($prepares);
             $string = implode($prepares, '&');
             $string .= $pay['secret'];
             if (md5($string) == $ret['sign']) {
                 $dom = new \DOMDocument();
                 if (!$dom->loadXML($ret['res_data'])) {
                     return error(-3, '支付宝支付初始化失败');
                 }
                 $xpath = new \DOMXPath($dom);
                 $token = $xpath->evaluate('string(//direct_trade_create_res/request_token)');
                 $set = array();
                 $set['service'] = 'alipay.wap.auth.authAndExecute';
                 $set['format'] = 'xml';
                 $set['v'] = '2.0';
                 $set['partner'] = $pay['partner'];
                 $set['sec_id'] = 'MD5';
                 $set['req_data'] = "<auth_and_execute_req><request_token>{$token}</request_token></auth_and_execute_req>";
                 $prepares = array();
                 foreach ($set as $key => $value) {
                     if ($key != 'sign') {
                         $prepares[] = "{$key}={$value}";
                     }
                 }
                 sort($prepares);
                 $string = implode($prepares, '&');
                 $string .= $pay['secret'];
                 $set['sign'] = md5($string);
                 $url = $gateway . http_build_query($set);
                 return array('url' => $url);
             }
         }
     }
     return error(-4, '支付宝支付初始化失败');
 }
 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
 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;
 }