JsApiPay.class.php.back 5.9 KB

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