Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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