123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace Common\Server;
- /**
- * Class AliAuthorizedServer
- * 支付宝授权服务
- */
- class AliAuthorizedServer
- {
- /**
- * 获取用户信息
- * @author: linch
- * @param $appid
- * @param $rsaPrivateKey
- * @param $alipayrsaPublicKey
- * @param $code
- * @return bool|mixed|SimpleXMLElement
- * @throws Exception
- */
- public function get_user_info($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code) {
- //获取access_token
- //$access_token_msg = $this->get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code);
- $access_token_msg = $this->get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code);
- $msg = array('code' => false, 'user_info' => array(), 'msg' => '');
- if ($access_token_msg['code'] == false) {
- $msg['code'] = false;
- $msg['msg'] = $access_token_msg['msg'];
- } else { //获取用户信息
- $accessToken = $access_token_msg['access_token'];
- Vendor('Pay.Ali2_0.AopClient');
- Vendor('Pay.Ali2_0.AlipayUserInfoShareRequest');
- Vendor('Pay.Ali2_0.SignData');
- $aop = new \AopClient ();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $appid;
- $aop->rsaPrivateKey = $rsaPrivateKey;
- $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA';
- $aop->postCharset = 'UTF-8';
- $aop->format = 'json';
- $request = new \AlipayUserInfoShareRequest ();
- $result = $aop->execute($request, $accessToken);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $result = json_decode(json_encode($result->$responseNode), true);
- if (!empty($result['code']) && $result['code'] = 10000) {
- $msg['code'] = true;
- $msg['user_info'] = $result;
- $msg['msg'] = '获取用户信息成功';
- } else {
- $msg['code'] = false;
- $msg['user_info'] = $result;
- $msg['msg'] = '获取用户信息失败';
- }
- }
- return $msg;
- }
- /**
- * get_access_token
- * @return mixed
- * @author: huangch
- */
- private function get_access_token_bak($code) {
- $nonce_str = $this->createNonceStr();
- $sig = md5($nonce_str.TOKEN_POOL_SALT);
- $url = ALI_TOKEN_POOL_URL.'?sig='.$sig.'&nonce_str='.$nonce_str.'&code='.$code;
- $res = json_decode($this->httpGet($url),true);
- if ($res['code'] == true) {
- return $res['token'];
- } else {
- cplog('get_access_token error:'.var_export($res,true));
- }
- }
- private function createNonceStr($length = 16) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
- }
- return $str;
- }
- /**
- * 获取access_token
- * @author: linch
- * @param $appid
- * @param $rsaPrivateKey
- * @param $alipayrsaPublicKey
- * @param $code
- * @return array
- * @throws Exception
- */
- public function get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code) {
- $msg = array('code' => false, 'access_token' => '', 'msg' => '');
- Vendor('Pay.Ali2_0.AopClient');
- Vendor('Pay.Ali2_0.AlipaySystemOauthTokenRequest');
- Vendor('Pay.Ali2_0.SignData');
- $aop = new \AopClient ();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $appid;
- $aop->rsaPrivateKey = $rsaPrivateKey;
- $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA';
- $aop->postCharset = 'UTF-8';
- $aop->format = 'json';
- $request = new \AlipaySystemOauthTokenRequest ();
- //使用 code 换取
- $request->setGrantType("authorization_code");
- $request->setCode($code);
- $result = $aop->execute($request);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $result = json_decode(json_encode($result->$responseNode), true);
- if (!empty($result) && empty($result['code'])) {
- $msg['code'] = true;
- $msg['access_token'] = $result['access_token'];
- $msg['msg'] = '获取access_token成功';
- } else {
- $msg['code'] = false;
- $msg['access_token'] = '';
- $msg['msg'] = '获取access_token失败';
- }
- return $msg;
- }
- /**
- * 获取access_token或者jsapi_ticket
- * @param $name
- * @return mixed
- */
- private function getConfig($name) {
- $redis = new \Common\Cache\RedisCache();
- return $redis->get($name);
- }
- /**
- * 设置access_token或者jsapi_ticket
- * @param $name
- * @param $value
- * @return mixed
- */
- private function setConfig($name, $value, $time) {
- $redis = new \Common\Cache\RedisCache();
- return $redis->set($name, $value, $time);
- }
- /**
- * curl get 方式
- * @author: linch
- * @param $url
- * @return mixed
- */
- private function httpGet($url) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- //忽略证书
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- }
|