You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

209 regels
5.8 KiB

  1. <?php
  2. require_once "../WxPay/lib/WxPay.Api.php";
  3. /**
  4. *
  5. * JSAPI支付实现类
  6. * 该类实现了从微信公众平台获取code、通过code获取openid和access_token、
  7. * 生成jsapi支付js接口所需的参数、生成获取共享收货地址所需的参数
  8. *
  9. * 该类是微信支付提供的样例程序,商户可根据自己的需求修改,或者使用lib中的api自行开发
  10. *
  11. * @author widy
  12. *
  13. */
  14. class JsApiPay
  15. {
  16. /**
  17. *
  18. * 网页授权接口微信服务器返回的数据,返回样例如下
  19. * {
  20. * "access_token":"ACCESS_TOKEN",
  21. * "expires_in":7200,
  22. * "refresh_token":"REFRESH_TOKEN",
  23. * "openid":"OPENID",
  24. * "scope":"SCOPE",
  25. * "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
  26. * }
  27. * 其中access_token可用于获取共享收货地址
  28. * openid是微信支付jsapi支付接口必须的参数
  29. * @var array
  30. */
  31. public $data = null;
  32. public $curl_timeout=3;
  33. /**
  34. *
  35. * 通过跳转获取用户的openid,跳转流程如下:
  36. * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize
  37. * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code
  38. *
  39. * @return 用户的openid
  40. */
  41. public function GetOpenid($state='')
  42. {
  43. //通过code获得openid
  44. if (!isset($_GET['code'])){
  45. //触发微信返回code码
  46. $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
  47. $url = $this->__CreateOauthUrlForCode($baseUrl,$state);
  48. file_put_contents('./code', date("Y-m-d H:i:s")." ".$url.PHP_EOL,FILE_APPEND);
  49. Header("Location: $url");
  50. exit();
  51. } else {
  52. //获取code码,以获取openid
  53. $code = $_GET['code'];
  54. $openid = $this->getOpenidFromMp($code);
  55. return $openid;
  56. }
  57. }
  58. /**
  59. *
  60. * 获取jsapi支付的参数
  61. * @param array $UnifiedOrderResult 统一支付接口返回的数据
  62. * @throws WxPayException
  63. *
  64. * @return json数据,可直接填入js函数作为参数
  65. */
  66. public function GetJsApiParameters($UnifiedOrderResult)
  67. {
  68. if(!array_key_exists("appid", $UnifiedOrderResult)
  69. || !array_key_exists("prepay_id", $UnifiedOrderResult)
  70. || $UnifiedOrderResult['prepay_id'] == "")
  71. {
  72. throw new WxPayException("参数错误");
  73. }
  74. $jsapi = new WxPayJsApiPay();
  75. $jsapi->SetAppid($UnifiedOrderResult["appid"]);
  76. $timeStamp = time();
  77. $jsapi->SetTimeStamp("$timeStamp");
  78. $jsapi->SetNonceStr(WxPayApi::getNonceStr());
  79. $jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
  80. $jsapi->SetSignType("MD5");
  81. $jsapi->SetPaySign($jsapi->MakeSign());
  82. $parameters = json_encode($jsapi->GetValues());
  83. return $parameters;
  84. }
  85. /**
  86. *
  87. * 通过code从工作平台获取openid机器access_token
  88. * @param string $code 微信跳转回来带上的code
  89. *
  90. * @return openid
  91. */
  92. public function GetOpenidFromMp($code)
  93. {
  94. $url = $this->__CreateOauthUrlForOpenid($code);
  95. //初始化curl
  96. $ch = curl_init();
  97. //设置超时
  98. curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
  101. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
  102. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  104. if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
  105. && WxPayConfig::CURL_PROXY_PORT != 0){
  106. curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
  107. curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
  108. }
  109. //运行curl,结果以jason形式返回
  110. $res = curl_exec($ch);
  111. curl_close($ch);
  112. //取出openid
  113. $data = json_decode($res,true);
  114. $this->data = $data;
  115. $openid = $data['openid'];
  116. return $openid;
  117. }
  118. /**
  119. *
  120. * 拼接签名字符串
  121. * @param array $urlObj
  122. *
  123. * @return 返回已经拼接好的字符串
  124. */
  125. private function ToUrlParams($urlObj)
  126. {
  127. $buff = "";
  128. foreach ($urlObj as $k => $v)
  129. {
  130. if($k != "sign"){
  131. $buff .= $k . "=" . $v . "&";
  132. }
  133. }
  134. $buff = trim($buff, "&");
  135. return $buff;
  136. }
  137. /**
  138. *
  139. * 获取地址js参数
  140. *
  141. * @return 获取共享收货地址js函数需要的参数,json格式可以直接做参数使用
  142. */
  143. public function GetEditAddressParameters()
  144. {
  145. $getData = $this->data;
  146. $data = array();
  147. $data["appid"] = WxPayConfig::APPID;
  148. $data["url"] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  149. $time = time();
  150. $data["timestamp"] = "$time";
  151. $data["noncestr"] = "1234568";
  152. $data["accesstoken"] = $getData["access_token"];
  153. ksort($data);
  154. $params = $this->ToUrlParams($data);
  155. $addrSign = sha1($params);
  156. $afterData = array(
  157. "addrSign" => $addrSign,
  158. "signType" => "sha1",
  159. "scope" => "jsapi_address",
  160. "appId" => WxPayConfig::APPID,
  161. "timeStamp" => $data["timestamp"],
  162. "nonceStr" => $data["noncestr"]
  163. );
  164. $parameters = json_encode($afterData);
  165. return $parameters;
  166. }
  167. /**
  168. *
  169. * 构造获取code的url连接
  170. * @param string $redirectUrl 微信服务器回跳的url,需要url编码
  171. *
  172. * @return 返回构造好的url
  173. */
  174. private function __CreateOauthUrlForCode($redirectUrl,$state='aa')
  175. {
  176. $urlObj["appid"] = WxPayConfig::APPID;
  177. $urlObj["redirect_uri"] = "$redirectUrl";
  178. $urlObj["response_type"] = "code";
  179. $urlObj["scope"] = "snsapi_base";
  180. $urlObj["state"] = $state."#wechat_redirect";
  181. $bizString = $this->ToUrlParams($urlObj);
  182. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  183. }
  184. /**
  185. *
  186. * 构造获取open和access_toke的url地址
  187. * @param string $code,微信跳转带回的code
  188. *
  189. * @return 请求的url
  190. */
  191. private function __CreateOauthUrlForOpenid($code)
  192. {
  193. $urlObj["appid"] = WxPayConfig::APPID;
  194. $urlObj["secret"] = WxPayConfig::APPSECRET;
  195. $urlObj["code"] = $code;
  196. $urlObj["grant_type"] = "authorization_code";
  197. $bizString = $this->ToUrlParams($urlObj);
  198. return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  199. }
  200. }