WxPayPubHelper.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. <?php
  2. /**
  3. * 微信支付帮助库
  4. * ====================================================
  5. * 接口分三种类型:
  6. * 【请求型接口】--Wxpay_client_
  7. * 统一支付接口类--UnifiedOrder
  8. * 订单查询接口--OrderQuery
  9. * 退款申请接口--Refund
  10. * 退款查询接口--RefundQuery
  11. * 对账单接口--DownloadBill
  12. * 短链接转换接口--ShortUrl
  13. * 【响应型接口】--Wxpay_server_
  14. * 通用通知接口--Notify
  15. * Native支付——请求商家获取商品信息接口--NativeCall
  16. * 【其他】
  17. * 静态链接二维码--NativeLink
  18. * JSAPI支付--JsApi
  19. * =====================================================
  20. * 【CommonUtil】常用工具:
  21. * trimString(),设置参数时需要用到的字符处理函数
  22. * createNoncestr(),产生随机字符串,不长于32位
  23. * formatBizQueryParaMap(),格式化参数,签名过程需要用到
  24. * getSign(),生成签名
  25. * arrayToXml(),array转xml
  26. * xmlToArray(),xml转 array
  27. * postXmlCurl(),以post方式提交xml到对应的接口url
  28. * postXmlSSLCurl(),使用证书,以post方式提交xml到对应的接口url
  29. */
  30. namespace app\index\service\wechat;
  31. use think\Config;
  32. use think\Log;
  33. require_once('SDKRuntimeException.php');
  34. /**
  35. * 所有接口的基类
  36. */
  37. class Common_util_pub
  38. {
  39. function __construct() {
  40. }
  41. function trimString($value)
  42. {
  43. $ret = null;
  44. if (null != $value)
  45. {
  46. $ret = $value;
  47. if (strlen($ret) == 0)
  48. {
  49. $ret = null;
  50. }
  51. }
  52. return $ret;
  53. }
  54. /**
  55. * 作用:产生随机字符串,不长于32位
  56. */
  57. public function createNoncestr( $length = 32 )
  58. {
  59. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  60. $str ="";
  61. for ( $i = 0; $i < $length; $i++ ) {
  62. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  63. }
  64. return $str;
  65. }
  66. /**
  67. * 作用:格式化参数,签名过程需要使用
  68. */
  69. function formatBizQueryParaMap($paraMap, $urlencode)
  70. {
  71. $buff = "";
  72. ksort($paraMap);
  73. foreach ($paraMap as $k => $v)
  74. {
  75. if($urlencode)
  76. {
  77. $v = urlencode($v);
  78. }
  79. $buff .= $k . "=" . $v . "&";
  80. }
  81. $reqPar = '';
  82. if (strlen($buff) > 0)
  83. {
  84. $reqPar = substr($buff, 0, strlen($buff)-1);
  85. }
  86. return $reqPar;
  87. }
  88. /**
  89. * 作用:生成签名
  90. */
  91. public function getSign($Obj)
  92. {
  93. if(!Config::get('WECHAT_PAY_KEY')){
  94. throw new \Exception('商户key不存在!');
  95. }
  96. foreach ((array)$Obj as $k => $v)
  97. {
  98. $Parameters[$k] = $v;
  99. }
  100. //签名步骤一:按字典序排序参数
  101. ksort($Parameters);
  102. $String = $this->formatBizQueryParaMap($Parameters, false);
  103. //echo '【string1】'.$String.'</br>';
  104. //签名步骤二:在string后加入KEY
  105. $String = $String."&key=".Config::get('WECHAT_PAY_KEY');
  106. //签名步骤三:MD5加密
  107. $String = md5($String);
  108. //echo "【string3】 ".$String."</br>";
  109. //签名步骤四:所有字符转为大写
  110. $result_ = strtoupper($String);
  111. //echo "【result】 ".$result_."</br>";
  112. return $result_;
  113. }
  114. /**
  115. * 作用:array转xml
  116. */
  117. function arrayToXml($arr){
  118. $xml = "<xml>";
  119. foreach ($arr as $key=>$val)
  120. {
  121. //if (is_numeric($val)){
  122. $xml.="<".$key.">".$val."</".$key.">";
  123. //}
  124. // else{
  125. // $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  126. // }
  127. }
  128. $xml.="</xml>";
  129. return $xml;
  130. }
  131. /**
  132. * 作用:将xml转为array
  133. */
  134. public function xmlToArray($xml)
  135. {
  136. //将XML转为array
  137. $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  138. return $array_data;
  139. }
  140. /**
  141. * 作用:以post方式提交xml到对应的接口url
  142. */
  143. public function postXmlCurl($xml,$url,$second=30)
  144. {
  145. //初始化curl
  146. $ch = curl_init();
  147. //设置超时
  148. //curl_setopt($ch, CURLOP_TIMEOUT, $second);
  149. //这里设置代理,如果有的话
  150. //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  151. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  152. curl_setopt($ch,CURLOPT_URL, $url);
  153. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  154. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  155. //设置header
  156. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  157. //要求结果为字符串且输出到屏幕上
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  159. //post提交方式
  160. curl_setopt($ch, CURLOPT_POST, TRUE);
  161. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  162. //运行curl
  163. $data = curl_exec($ch);
  164. //curl_close($ch);
  165. //返回结果
  166. if($data)
  167. {
  168. curl_close($ch);
  169. return $data;
  170. }
  171. else
  172. {
  173. $error = curl_errno($ch);
  174. //echo "curl出错,错误码:$error"."<br>";
  175. echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
  176. curl_close($ch);
  177. die("pay error($error)");
  178. return false;
  179. }
  180. }
  181. /**
  182. * 作用:使用证书,以post方式提交xml到对应的接口url
  183. */
  184. function postXmlSSLCurl($xml,$url,$second=30)
  185. {
  186. $ch = curl_init();
  187. //超时时间
  188. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  189. //这里设置代理,如果有的话
  190. //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  191. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  192. curl_setopt($ch,CURLOPT_URL, $url);
  193. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  194. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  195. //设置header
  196. curl_setopt($ch,CURLOPT_HEADER,FALSE);
  197. //要求结果为字符串且输出到屏幕上
  198. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  199. //设置证书
  200. //使用证书:cert 与 key 分别属于两个.pem文件
  201. //默认格式为PEM,可以注释
  202. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  203. curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::$SSLCERT_PATH);
  204. //默认格式为PEM,可以注释
  205. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  206. curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::$SSLKEY_PATH);
  207. //post提交方式
  208. curl_setopt($ch,CURLOPT_POST, true);
  209. curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
  210. $data = curl_exec($ch);
  211. //返回结果
  212. if($data){
  213. curl_close($ch);
  214. return $data;
  215. }
  216. else {
  217. $error = curl_errno($ch);
  218. echo "curl出错,错误码:$error"."<br>";
  219. echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
  220. curl_close($ch);
  221. return false;
  222. }
  223. }
  224. /**
  225. * 作用:打印数组
  226. */
  227. function printErr($wording='',$err='')
  228. {
  229. print_r('<pre>');
  230. echo $wording."</br>";
  231. var_dump($err);
  232. print_r('</pre>');
  233. }
  234. }
  235. /**
  236. * 请求型接口的基类
  237. */
  238. class Wxpay_client_pub extends Common_util_pub
  239. {
  240. public $parameters;//请求参数,类型为关联数组
  241. public $requestXml;
  242. public $response;//微信返回的响应
  243. public $result;//返回参数,类型为关联数组
  244. var $url;//接口链接
  245. var $curl_timeout;//curl超时时间
  246. /**
  247. * 作用:设置请求参数
  248. */
  249. function setParameter($parameter, $parameterValue)
  250. {
  251. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  252. }
  253. /**
  254. * 作用:设置标配的请求参数,生成签名,生成接口参数xml
  255. */
  256. function createXml()
  257. {
  258. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  259. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  260. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  261. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  262. return $this->arrayToXml($this->parameters);
  263. }
  264. /**
  265. * 作用:post请求xml
  266. */
  267. function postXml()
  268. {
  269. $xml = $this->createXml();
  270. var_dump($xml);;exit;
  271. $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
  272. return $this->response;
  273. }
  274. /**
  275. * 作用:使用证书post请求xml
  276. */
  277. function postXmlSSL()
  278. {
  279. $xml = $this->createXml();
  280. $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
  281. return $this->response;
  282. }
  283. /**
  284. * 作用:获取结果,默认不使用证书
  285. */
  286. function getResult()
  287. {
  288. $this->postXml();
  289. $this->result = $this->xmlToArray($this->response);
  290. return $this->result;
  291. }
  292. }
  293. /**
  294. * 统一支付接口类
  295. */
  296. class UnifiedOrder_pub extends Wxpay_client_pub
  297. {
  298. function __construct()
  299. {
  300. //设置接口链接
  301. $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  302. //设置curl超时时间
  303. $this->curl_timeout = 3;
  304. }
  305. /**
  306. * 生成接口参数xml
  307. */
  308. function createXml()
  309. {
  310. try{
  311. //检测必填参数
  312. if($this->parameters["out_trade_no"] == null)
  313. {
  314. throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no!"."<br>");
  315. }elseif($this->parameters["body"] == null){
  316. throw new SDKRuntimeException("缺少统一支付接口必填参数body!"."<br>");
  317. }elseif ($this->parameters["total_fee"] == null ) {
  318. throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee!"."<br>");
  319. }elseif ($this->parameters["notify_url"] == null) {
  320. throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url!"."<br>");
  321. }elseif ($this->parameters["trade_type"] == null) {
  322. throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type!"."<br>");
  323. }elseif ($this->parameters["trade_type"] == "JSAPI" &&
  324. empty($this->parameters["openid"]) ){
  325. //throw new SDKRuntimeException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."<br>");
  326. }
  327. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  328. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  329. if(!empty(Config::get('WECHAT_SUB_MCHID'))){
  330. $this->parameters["sub_mch_id"] = Config::get('WECHAT_SUB_MCHID');//商户号
  331. }
  332. if(empty($this->parameters["openid"])){
  333. unset($this->parameters["openid"]);
  334. }
  335. if(!empty(Config::get('WECHAT_SUB_APPID'))){
  336. $this->parameters["sub_appid"] = Config::get('WECHAT_SUB_APPID');//商户号
  337. }
  338. if(empty(Config::get('WECHAT_SUB_APPID')) && !empty($this->parameters["sub_openid"])){
  339. $this->parameters["openid"] = $this->parameters["sub_openid"];
  340. unset($this->parameters["sub_openid"]);
  341. }
  342. $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
  343. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  344. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  345. $this->requestXml = $this->arrayToXml($this->parameters);
  346. return $this->requestXml;
  347. }catch (SDKRuntimeException $e){
  348. die($e->errorMessage());
  349. }
  350. }
  351. /**
  352. * 获取prepay_id
  353. */
  354. function getPrepayId()
  355. {
  356. $this->postXml();
  357. $this->result = $this->xmlToArray($this->response);
  358. if(!isset($this->result["prepay_id"])) {
  359. \app\common\service\HelperService::returnJson([
  360. 'data'=>$this->result,
  361. 'msg'=>'prepay_id失败',
  362. 'code'=>200,
  363. 'para'=>$this->requestXml
  364. ]);
  365. }
  366. $prepay_id = $this->result["prepay_id"];
  367. return $prepay_id;
  368. }
  369. }
  370. /**
  371. * 订单查询接口
  372. */
  373. class OrderQuery_pub extends Wxpay_client_pub
  374. {
  375. function __construct()
  376. {
  377. //设置接口链接
  378. $this->url = "https://api.mch.weixin.qq.com/pay/orderquery";
  379. //设置curl超时时间
  380. $this->curl_timeout = 3;
  381. }
  382. /**
  383. * 生成接口参数xml
  384. */
  385. function createXml()
  386. {
  387. try
  388. {
  389. //检测必填参数
  390. if($this->parameters["out_trade_no"] == null &&
  391. $this->parameters["transaction_id"] == null)
  392. {
  393. throw new SDKRuntimeException("订单查询接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  394. }
  395. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  396. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  397. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  398. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  399. return $this->arrayToXml($this->parameters);
  400. }catch (SDKRuntimeException $e)
  401. {
  402. die($e->errorMessage());
  403. }
  404. }
  405. }
  406. /**
  407. * 退款申请接口
  408. */
  409. class Refund_pub extends Wxpay_client_pub
  410. {
  411. function __construct() {
  412. //设置接口链接
  413. $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  414. //设置curl超时时间
  415. $this->curl_timeout = 3;
  416. }
  417. /**
  418. * 生成接口参数xml
  419. */
  420. function createXml()
  421. {
  422. try
  423. {
  424. //检测必填参数
  425. if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  426. throw new SDKRuntimeException("退款申请接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  427. }elseif($this->parameters["out_refund_no"] == null){
  428. throw new SDKRuntimeException("退款申请接口中,缺少必填参数out_refund_no!"."<br>");
  429. }elseif($this->parameters["total_fee"] == null){
  430. throw new SDKRuntimeException("退款申请接口中,缺少必填参数total_fee!"."<br>");
  431. }elseif($this->parameters["refund_fee"] == null){
  432. throw new SDKRuntimeException("退款申请接口中,缺少必填参数refund_fee!"."<br>");
  433. }elseif($this->parameters["op_user_id"] == null){
  434. throw new SDKRuntimeException("退款申请接口中,缺少必填参数op_user_id!"."<br>");
  435. }
  436. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  437. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  438. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  439. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  440. return $this->arrayToXml($this->parameters);
  441. }catch (SDKRuntimeException $e)
  442. {
  443. die($e->errorMessage());
  444. }
  445. }
  446. /**
  447. * 作用:获取结果,使用证书通信
  448. */
  449. function getResult()
  450. {
  451. $this->postXmlSSL();
  452. $this->result = $this->xmlToArray($this->response);
  453. return $this->result;
  454. }
  455. }
  456. /**
  457. * 提交刷卡支付
  458. */
  459. class MicroPay_pub extends Wxpay_client_pub
  460. {
  461. function __construct() {
  462. //设置接口链接
  463. $this->url = "https://api.mch.weixin.qq.com/pay/micropay";
  464. //设置curl超时时间
  465. $this->curl_timeout = 3;
  466. }
  467. /**
  468. * 生成接口参数xml
  469. */
  470. function createXml()
  471. {
  472. try
  473. {
  474. //检测必填参数
  475. if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  476. throw new SDKRuntimeException("付款码接口中,out_trade_no至少填一个!"."<br>");
  477. }elseif($this->parameters["spbill_create_ip"] == null){
  478. throw new SDKRuntimeException("付款码接口中,缺少必填参数spbill_create_ip!"."<br>");
  479. }elseif($this->parameters["total_fee"] == null){
  480. throw new SDKRuntimeException("付款码接口中,缺少必填参数total_fee!"."<br>");
  481. }elseif($this->parameters["auth_code"] == null){
  482. throw new SDKRuntimeException("付款码接口中,缺少必填参数auth_code!"."<br>");
  483. }
  484. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  485. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  486. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  487. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  488. return $this->arrayToXml($this->parameters);
  489. }catch (SDKRuntimeException $e){
  490. die($e->errorMessage());
  491. }
  492. }
  493. /**
  494. * 作用:获取结果,使用证书通信
  495. */
  496. function getResult()
  497. {
  498. $this->postXmlSSL();
  499. $this->result = $this->xmlToArray($this->response);
  500. return $this->result;
  501. }
  502. }
  503. /**
  504. * 退款查询接口
  505. */
  506. class RefundQuery_pub extends Wxpay_client_pub
  507. {
  508. function __construct() {
  509. //设置接口链接
  510. $this->url = "https://api.mch.weixin.qq.com/pay/refundquery";
  511. //设置curl超时时间
  512. $this->curl_timeout = 5;
  513. }
  514. /**
  515. * 生成接口参数xml
  516. */
  517. function createXml()
  518. {
  519. try
  520. {
  521. if($this->parameters["out_refund_no"] == null &&
  522. $this->parameters["out_trade_no"] == null &&
  523. $this->parameters["transaction_id"] == null &&
  524. $this->parameters["refund_id "] == null)
  525. {
  526. throw new SDKRuntimeException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"."<br>");
  527. }
  528. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  529. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  530. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  531. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  532. return $this->arrayToXml($this->parameters);
  533. }catch (SDKRuntimeException $e)
  534. {
  535. die($e->errorMessage());
  536. }
  537. }
  538. /**
  539. * 作用:获取结果,使用证书通信
  540. */
  541. function getResult()
  542. {
  543. $this->postXmlSSL();
  544. $this->result = $this->xmlToArray($this->response);
  545. return $this->result;
  546. }
  547. }
  548. /**
  549. * 对账单接口
  550. */
  551. class DownloadBill_pub extends Wxpay_client_pub
  552. {
  553. function __construct()
  554. {
  555. //设置接口链接
  556. $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  557. //设置curl超时时间
  558. $this->curl_timeout = 3;
  559. }
  560. /**
  561. * 生成接口参数xml
  562. */
  563. function createXml()
  564. {
  565. try
  566. {
  567. if($this->parameters["bill_date"] == null )
  568. {
  569. throw new SDKRuntimeException("对账单接口中,缺少必填参数bill_date!"."<br>");
  570. }
  571. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  572. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  573. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  574. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  575. return $this->arrayToXml($this->parameters);
  576. }catch (SDKRuntimeException $e)
  577. {
  578. die($e->errorMessage());
  579. }
  580. }
  581. /**
  582. * 作用:获取结果,默认不使用证书
  583. */
  584. function getResult()
  585. {
  586. $this->postXml();
  587. $this->result = $this->xmlToArray($this->result_xml);
  588. return $this->result;
  589. }
  590. }
  591. /**
  592. * 短链接转换接口
  593. */
  594. class ShortUrl_pub extends Wxpay_client_pub
  595. {
  596. function __construct()
  597. {
  598. //设置接口链接
  599. $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
  600. //设置curl超时时间
  601. $this->curl_timeout = 3;
  602. }
  603. /**
  604. * 生成接口参数xml
  605. */
  606. function createXml()
  607. {
  608. try
  609. {
  610. if($this->parameters["long_url"] == null )
  611. {
  612. throw new SDKRuntimeException("短链接转换接口中,缺少必填参数long_url!"."<br>");
  613. }
  614. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  615. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  616. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  617. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  618. return $this->arrayToXml($this->parameters);
  619. }catch (SDKRuntimeException $e)
  620. {
  621. die($e->errorMessage());
  622. }
  623. }
  624. /**
  625. * 获取prepay_id
  626. */
  627. function getShortUrl()
  628. {
  629. $this->postXml();
  630. $prepay_id = $this->result["short_url"];
  631. return $prepay_id;
  632. }
  633. }
  634. /**
  635. * 响应型接口基类
  636. */
  637. class Wxpay_server_pub extends Common_util_pub
  638. {
  639. public $data;//接收到的数据,类型为关联数组
  640. var $returnParameters;//返回参数,类型为关联数组
  641. /**
  642. * 将微信的请求xml转换成关联数组,以方便数据处理
  643. */
  644. function saveData($xml)
  645. {
  646. $this->data = $this->xmlToArray($xml);
  647. }
  648. function checkSign()
  649. {
  650. $tmpData = $this->data;
  651. unset($tmpData['sign']);
  652. $sign = $this->getSign($tmpData);//本地签名
  653. if ($this->data['sign'] == $sign) {
  654. return TRUE;
  655. }
  656. return FALSE;
  657. }
  658. /**
  659. * 获取微信的请求数据
  660. */
  661. function getData()
  662. {
  663. return $this->data;
  664. }
  665. /**
  666. * 设置返回微信的xml数据
  667. */
  668. function setReturnParameter($parameter, $parameterValue)
  669. {
  670. $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  671. }
  672. /**
  673. * 生成接口参数xml
  674. */
  675. function createXml()
  676. {
  677. return $this->arrayToXml($this->returnParameters);
  678. }
  679. /**
  680. * 将xml数据返回微信
  681. */
  682. function returnXml()
  683. {
  684. $returnXml = $this->createXml();
  685. return $returnXml;
  686. }
  687. }
  688. /**
  689. * 通用通知接口
  690. */
  691. class Notify_pub extends Wxpay_server_pub
  692. {
  693. }
  694. /**
  695. * 请求商家获取商品信息接口
  696. */
  697. class NativeCall_pub extends Wxpay_server_pub
  698. {
  699. /**
  700. * 生成接口参数xml
  701. */
  702. function createXml()
  703. {
  704. if($this->returnParameters["return_code"] == "SUCCESS"){
  705. $this->returnParameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  706. $this->returnParameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  707. $this->returnParameters["nonce_str"] = $this->createNoncestr();//随机字符串
  708. $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//签名
  709. }
  710. return $this->arrayToXml($this->returnParameters);
  711. }
  712. /**
  713. * 获取product_id
  714. */
  715. function getProductId()
  716. {
  717. $product_id = $this->data["product_id"];
  718. return $product_id;
  719. }
  720. }
  721. /**
  722. * 静态链接二维码
  723. */
  724. class NativeLink_pub extends Common_util_pub
  725. {
  726. var $parameters;//静态链接参数
  727. var $url;//静态链接
  728. function __construct()
  729. {
  730. }
  731. /**
  732. * 设置参数
  733. */
  734. function setParameter($parameter, $parameterValue)
  735. {
  736. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  737. }
  738. /**
  739. * 生成Native支付链接二维码
  740. */
  741. function createLink()
  742. {
  743. try
  744. {
  745. if($this->parameters["product_id"] == null)
  746. {
  747. throw new SDKRuntimeException("缺少Native支付二维码链接必填参数product_id!"."<br>");
  748. }
  749. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  750. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  751. $time_stamp = time();
  752. $this->parameters["time_stamp"] = "$time_stamp";//时间戳
  753. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  754. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  755. $bizString = $this->formatBizQueryParaMap($this->parameters, false);
  756. $this->url = "weixin://wxpay/bizpayurl?".$bizString;
  757. }catch (SDKRuntimeException $e)
  758. {
  759. die($e->errorMessage());
  760. }
  761. }
  762. /**
  763. * 返回链接
  764. */
  765. function getUrl()
  766. {
  767. $this->createLink();
  768. return $this->url;
  769. }
  770. }
  771. /**
  772. * JSAPI支付——H5网页端调起支付接口
  773. */
  774. class JsApi_pub extends Common_util_pub
  775. {
  776. var $code;//code码,用以获取openid
  777. var $openid;//用户的openid
  778. var $parameters;//jsapi参数,格式为json
  779. var $prepay_id;//使用统一支付接口得到的预支付id
  780. var $curl_timeout;//curl超时时间
  781. function __construct()
  782. {
  783. //设置curl超时时间
  784. $this->curl_timeout = 3;
  785. }
  786. /**
  787. * 生成可以获得code的url,只能获取openid
  788. * @param $redirectUrl
  789. * @param $number
  790. * @return string
  791. */
  792. function createOauthOpenidForCode($redirectUrl,$number)
  793. {
  794. $urlObj["appid"] = Config::get('WECHAT_APPID');
  795. $urlObj["redirect_uri"] = "$redirectUrl";
  796. $urlObj["response_type"] = "code";
  797. $urlObj["scope"] = "snsapi_base";
  798. $urlObj["state"] = "$number"."#wechat_redirect";
  799. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  800. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  801. }
  802. /**
  803. * 生成可以获得code的url,不仅限于openid信息
  804. * @param $redirectUrl
  805. * @param $number
  806. * @return string
  807. */
  808. function createOauthOpenidAndMoreUrlForCode($redirectUrl,$number)
  809. {
  810. $urlObj["appid"] = Config::get('WECHAT_APPID');
  811. $urlObj["redirect_uri"] = urlencode($redirectUrl);
  812. $urlObj["response_type"] = "code";
  813. $urlObj["scope"] = "snsapi_userinfo";
  814. $urlObj["state"] = "$number"."#wechat_redirect";
  815. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  816. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  817. }
  818. /**
  819. * 作用:生成可以获得openid的url
  820. */
  821. function createOauthUrlForOpenid()
  822. {
  823. $urlObj["appid"] = Config::get('WECHAT_APPID');
  824. $urlObj["secret"] = Config::get('WECHAT_APPSECRET');
  825. $urlObj["code"] = $this->code;
  826. $urlObj["grant_type"] = "authorization_code";
  827. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  828. return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  829. }
  830. function httpPost($url){
  831. //初始化curl
  832. $ch = curl_init();
  833. //设置超时
  834. //curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
  835. curl_setopt($ch, CURLOPT_URL, $url);
  836. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  837. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  838. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  839. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  840. //运行curl,结果以json形式返回
  841. $res = curl_exec($ch);
  842. curl_close($ch);
  843. //取出openid
  844. $data = json_decode($res,true);
  845. return $data;
  846. }
  847. /**
  848. * 作用:通过curl向微信提交code,以获取openid
  849. */
  850. function getOpenidInfo()
  851. {
  852. $url = $this->createOauthUrlForOpenid();
  853. $data = $this->httpPost($url);
  854. return $data;
  855. }
  856. /**
  857. * 网页授权第四步:拉取用户信息
  858. * @param string $access_token
  859. * @param string $openid
  860. * @return bool|mixed
  861. */
  862. public function getOauthUserInfo($access_token='',$openid=''){
  863. if(empty($access_token)&& empty($openid)){
  864. return FALSE;
  865. }
  866. $url = 'https://api.weixin.qq.com/sns/userinfo?';
  867. $url .= 'access_token='.$access_token;
  868. $url .= '&openid='.$openid;
  869. $url .= '&lang=zh_CN';
  870. $res = $this->httpPost($url);
  871. if($res != FALSE){
  872. if(empty($res) || array_key_exists('errcode', $res)){
  873. return FALSE;
  874. }
  875. return $res;
  876. }
  877. return FALSE;
  878. }
  879. /**
  880. * 设置prepay_id
  881. * @param $prepayId
  882. */
  883. function setPrepayId($prepayId)
  884. {
  885. $this->prepay_id = $prepayId;
  886. }
  887. /**
  888. * 设置code
  889. * @param $code_
  890. */
  891. function setCode($code_)
  892. {
  893. $this->code = $code_;
  894. }
  895. /**
  896. * 作用:设置jsapi的参数
  897. */
  898. public function getParameters()
  899. {
  900. $jsApiObj["appId"] = Config::get('WECHAT_APPID');
  901. $timeStamp = time();
  902. $jsApiObj["timeStamp"] = "$timeStamp";
  903. $jsApiObj["nonceStr"] = $this->createNoncestr();
  904. $jsApiObj["package"] = "prepay_id=$this->prepay_id";
  905. $jsApiObj["signType"] = "MD5";
  906. $jsApiObj["paySign"] = $this->getSign($jsApiObj);
  907. $this->parameters = json_encode($jsApiObj);
  908. return $this->parameters;
  909. }
  910. }