WxPay.JsApiPay.php 5.7 KB

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