WxPay.JsApiPay.php 6.0 KB

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