AliAuthorizedServer.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Common\Server;
  3. /**
  4. * Class AliAuthorizedServer
  5. * 支付宝授权服务
  6. */
  7. class AliAuthorizedServer
  8. {
  9. /**
  10. * 获取用户信息
  11. * @author: linch
  12. * @param $appid
  13. * @param $rsaPrivateKey
  14. * @param $alipayrsaPublicKey
  15. * @param $code
  16. * @return bool|mixed|SimpleXMLElement
  17. * @throws Exception
  18. */
  19. public function get_user_info($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code) {
  20. //获取access_token
  21. //$access_token_msg = $this->get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code);
  22. $access_token_msg = $this->get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code);
  23. $msg = array('code' => false, 'user_info' => array(), 'msg' => '');
  24. if ($access_token_msg['code'] == false) {
  25. $msg['code'] = false;
  26. $msg['msg'] = $access_token_msg['msg'];
  27. } else { //获取用户信息
  28. $accessToken = $access_token_msg['access_token'];
  29. Vendor('Pay.Ali2_0.AopClient');
  30. Vendor('Pay.Ali2_0.AlipayUserInfoShareRequest');
  31. Vendor('Pay.Ali2_0.SignData');
  32. $aop = new \AopClient ();
  33. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  34. $aop->appId = $appid;
  35. $aop->rsaPrivateKey = $rsaPrivateKey;
  36. $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
  37. $aop->apiVersion = '1.0';
  38. $aop->signType = 'RSA';
  39. $aop->postCharset = 'UTF-8';
  40. $aop->format = 'json';
  41. $request = new \AlipayUserInfoShareRequest ();
  42. $result = $aop->execute($request, $accessToken);
  43. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  44. $result = json_decode(json_encode($result->$responseNode), true);
  45. if (!empty($result['code']) && $result['code'] = 10000) {
  46. $msg['code'] = true;
  47. $msg['user_info'] = $result;
  48. $msg['msg'] = '获取用户信息成功';
  49. } else {
  50. $msg['code'] = false;
  51. $msg['user_info'] = $result;
  52. $msg['msg'] = '获取用户信息失败';
  53. }
  54. }
  55. return $msg;
  56. }
  57. /**
  58. * get_access_token
  59. * @return mixed
  60. * @author: huangch
  61. */
  62. private function get_access_token_bak($code) {
  63. $nonce_str = $this->createNonceStr();
  64. $sig = md5($nonce_str.TOKEN_POOL_SALT);
  65. $url = ALI_TOKEN_POOL_URL.'?sig='.$sig.'&nonce_str='.$nonce_str.'&code='.$code;
  66. $res = json_decode($this->httpGet($url),true);
  67. if ($res['code'] == true) {
  68. return $res['token'];
  69. } else {
  70. cplog('get_access_token error:'.var_export($res,true));
  71. }
  72. }
  73. private function createNonceStr($length = 16) {
  74. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  75. $str = "";
  76. for ($i = 0; $i < $length; $i++) {
  77. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  78. }
  79. return $str;
  80. }
  81. /**
  82. * 获取access_token
  83. * @author: linch
  84. * @param $appid
  85. * @param $rsaPrivateKey
  86. * @param $alipayrsaPublicKey
  87. * @param $code
  88. * @return array
  89. * @throws Exception
  90. */
  91. public function get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code) {
  92. $msg = array('code' => false, 'access_token' => '', 'msg' => '');
  93. Vendor('Pay.Ali2_0.AopClient');
  94. Vendor('Pay.Ali2_0.AlipaySystemOauthTokenRequest');
  95. Vendor('Pay.Ali2_0.SignData');
  96. $aop = new \AopClient ();
  97. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  98. $aop->appId = $appid;
  99. $aop->rsaPrivateKey = $rsaPrivateKey;
  100. $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
  101. $aop->apiVersion = '1.0';
  102. $aop->signType = 'RSA';
  103. $aop->postCharset = 'UTF-8';
  104. $aop->format = 'json';
  105. $request = new \AlipaySystemOauthTokenRequest ();
  106. //使用 code 换取
  107. $request->setGrantType("authorization_code");
  108. $request->setCode($code);
  109. $result = $aop->execute($request);
  110. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  111. $result = json_decode(json_encode($result->$responseNode), true);
  112. if (!empty($result) && empty($result['code'])) {
  113. $msg['code'] = true;
  114. $msg['access_token'] = $result['access_token'];
  115. $msg['msg'] = '获取access_token成功';
  116. } else {
  117. $msg['code'] = false;
  118. $msg['access_token'] = '';
  119. $msg['msg'] = '获取access_token失败';
  120. }
  121. return $msg;
  122. }
  123. /**
  124. * 获取access_token或者jsapi_ticket
  125. * @param $name
  126. * @return mixed
  127. */
  128. private function getConfig($name) {
  129. $redis = new \Common\Cache\RedisCache();
  130. return $redis->get($name);
  131. }
  132. /**
  133. * 设置access_token或者jsapi_ticket
  134. * @param $name
  135. * @param $value
  136. * @return mixed
  137. */
  138. private function setConfig($name, $value, $time) {
  139. $redis = new \Common\Cache\RedisCache();
  140. return $redis->set($name, $value, $time);
  141. }
  142. /**
  143. * curl get 方式
  144. * @author: linch
  145. * @param $url
  146. * @return mixed
  147. */
  148. private function httpGet($url) {
  149. $curl = curl_init();
  150. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  151. //忽略证书
  152. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  153. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  154. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  155. curl_setopt($curl, CURLOPT_URL, $url);
  156. $res = curl_exec($curl);
  157. curl_close($curl);
  158. return $res;
  159. }
  160. }