WxPayPubHelper.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
  271. return $this->response;
  272. }
  273. /**
  274. * 作用:使用证书post请求xml
  275. */
  276. function postXmlSSL()
  277. {
  278. $xml = $this->createXml();
  279. var_dump($xml);
  280. $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
  281. var_dump($this->response);exit;
  282. return $this->response;
  283. }
  284. /**
  285. * 作用:获取结果,默认不使用证书
  286. */
  287. function getResult()
  288. {
  289. $this->postXml();
  290. $this->result = $this->xmlToArray($this->response);
  291. return $this->result;
  292. }
  293. }
  294. /**
  295. * 统一支付接口类
  296. */
  297. class UnifiedOrder_pub extends Wxpay_client_pub
  298. {
  299. function __construct()
  300. {
  301. //设置接口链接
  302. $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  303. //设置curl超时时间
  304. $this->curl_timeout = 3;
  305. }
  306. /**
  307. * 生成接口参数xml
  308. */
  309. function createXml()
  310. {
  311. try{
  312. //检测必填参数
  313. if($this->parameters["out_trade_no"] == null)
  314. {
  315. throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no!"."<br>");
  316. }elseif($this->parameters["body"] == null){
  317. throw new SDKRuntimeException("缺少统一支付接口必填参数body!"."<br>");
  318. }elseif ($this->parameters["total_fee"] == null ) {
  319. throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee!"."<br>");
  320. }elseif ($this->parameters["notify_url"] == null) {
  321. throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url!"."<br>");
  322. }elseif ($this->parameters["trade_type"] == null) {
  323. throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type!"."<br>");
  324. }elseif ($this->parameters["trade_type"] == "JSAPI" &&
  325. empty($this->parameters["openid"]) ){
  326. //throw new SDKRuntimeException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."<br>");
  327. }
  328. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  329. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  330. if(!empty(Config::get('WECHAT_SUB_MCHID'))){
  331. $this->parameters["sub_mch_id"] = Config::get('WECHAT_SUB_MCHID');//商户号
  332. }
  333. if(empty($this->parameters["openid"])){
  334. unset($this->parameters["openid"]);
  335. }
  336. if(!empty(Config::get('WECHAT_SUB_APPID'))){
  337. $this->parameters["sub_appid"] = Config::get('WECHAT_SUB_APPID');//商户号
  338. }
  339. if(empty(Config::get('WECHAT_SUB_APPID')) && !empty($this->parameters["sub_openid"])){
  340. $this->parameters["openid"] = $this->parameters["sub_openid"];
  341. unset($this->parameters["sub_openid"]);
  342. }
  343. $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
  344. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  345. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  346. $this->requestXml = $this->arrayToXml($this->parameters);
  347. return $this->requestXml;
  348. }catch (SDKRuntimeException $e){
  349. die($e->errorMessage());
  350. }
  351. }
  352. /**
  353. * 获取prepay_id
  354. */
  355. function getPrepayId()
  356. {
  357. $this->postXml();
  358. $this->result = $this->xmlToArray($this->response);
  359. if(!isset($this->result["prepay_id"])) {
  360. \app\common\service\HelperService::returnJson([
  361. 'data'=>$this->result,
  362. 'msg'=>'prepay_id失败',
  363. 'code'=>200,
  364. 'para'=>$this->requestXml
  365. ]);
  366. }
  367. $prepay_id = $this->result["prepay_id"];
  368. return $prepay_id;
  369. }
  370. }
  371. /**
  372. * 订单查询接口
  373. */
  374. class OrderQuery_pub extends Wxpay_client_pub
  375. {
  376. function __construct()
  377. {
  378. //设置接口链接
  379. $this->url = "https://api.mch.weixin.qq.com/pay/orderquery";
  380. //设置curl超时时间
  381. $this->curl_timeout = 3;
  382. }
  383. /**
  384. * 生成接口参数xml
  385. */
  386. function createXml()
  387. {
  388. try
  389. {
  390. //检测必填参数
  391. if($this->parameters["out_trade_no"] == null &&
  392. $this->parameters["transaction_id"] == null)
  393. {
  394. throw new SDKRuntimeException("订单查询接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  395. }
  396. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  397. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  398. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  399. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  400. return $this->arrayToXml($this->parameters);
  401. }catch (SDKRuntimeException $e)
  402. {
  403. die($e->errorMessage());
  404. }
  405. }
  406. }
  407. /**
  408. * 退款申请接口
  409. */
  410. class Refund_pub extends Wxpay_client_pub
  411. {
  412. function __construct() {
  413. //设置接口链接
  414. $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  415. //设置curl超时时间
  416. $this->curl_timeout = 3;
  417. }
  418. /**
  419. * 生成接口参数xml
  420. */
  421. function createXml()
  422. {
  423. try
  424. {
  425. //检测必填参数
  426. if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  427. throw new SDKRuntimeException("退款申请接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  428. }elseif($this->parameters["out_refund_no"] == null){
  429. throw new SDKRuntimeException("退款申请接口中,缺少必填参数out_refund_no!"."<br>");
  430. }elseif($this->parameters["total_fee"] == null){
  431. throw new SDKRuntimeException("退款申请接口中,缺少必填参数total_fee!"."<br>");
  432. }elseif($this->parameters["refund_fee"] == null){
  433. throw new SDKRuntimeException("退款申请接口中,缺少必填参数refund_fee!"."<br>");
  434. }elseif($this->parameters["op_user_id"] == null){
  435. throw new SDKRuntimeException("退款申请接口中,缺少必填参数op_user_id!"."<br>");
  436. }
  437. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  438. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  439. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  440. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  441. return $this->arrayToXml($this->parameters);
  442. }catch (SDKRuntimeException $e)
  443. {
  444. die($e->errorMessage());
  445. }
  446. }
  447. /**
  448. * 作用:获取结果,使用证书通信
  449. */
  450. function getResult()
  451. {
  452. $this->postXmlSSL();
  453. echo 111;exit;
  454. $this->result = $this->xmlToArray($this->response);
  455. return $this->result;
  456. }
  457. }
  458. /**
  459. * 提交刷卡支付
  460. */
  461. class MicroPay_pub extends Wxpay_client_pub
  462. {
  463. function __construct() {
  464. //设置接口链接
  465. $this->url = "https://api.mch.weixin.qq.com/pay/micropay";
  466. //设置curl超时时间
  467. $this->curl_timeout = 3;
  468. }
  469. /**
  470. * 生成接口参数xml
  471. */
  472. function createXml()
  473. {
  474. try
  475. {
  476. //检测必填参数
  477. if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  478. throw new SDKRuntimeException("付款码接口中,out_trade_no至少填一个!"."<br>");
  479. }elseif($this->parameters["spbill_create_ip"] == null){
  480. throw new SDKRuntimeException("付款码接口中,缺少必填参数spbill_create_ip!"."<br>");
  481. }elseif($this->parameters["total_fee"] == null){
  482. throw new SDKRuntimeException("付款码接口中,缺少必填参数total_fee!"."<br>");
  483. }elseif($this->parameters["auth_code"] == null){
  484. throw new SDKRuntimeException("付款码接口中,缺少必填参数auth_code!"."<br>");
  485. }
  486. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  487. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  488. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  489. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  490. return $this->arrayToXml($this->parameters);
  491. }catch (SDKRuntimeException $e){
  492. die($e->errorMessage());
  493. }
  494. }
  495. /**
  496. * 作用:获取结果,使用证书通信
  497. */
  498. function getResult()
  499. {
  500. $this->postXmlSSL();
  501. $this->result = $this->xmlToArray($this->response);
  502. return $this->result;
  503. }
  504. }
  505. /**
  506. * 退款查询接口
  507. */
  508. class RefundQuery_pub extends Wxpay_client_pub
  509. {
  510. function __construct() {
  511. //设置接口链接
  512. $this->url = "https://api.mch.weixin.qq.com/pay/refundquery";
  513. //设置curl超时时间
  514. $this->curl_timeout = 5;
  515. }
  516. /**
  517. * 生成接口参数xml
  518. */
  519. function createXml()
  520. {
  521. try
  522. {
  523. if($this->parameters["out_refund_no"] == null &&
  524. $this->parameters["out_trade_no"] == null &&
  525. $this->parameters["transaction_id"] == null &&
  526. $this->parameters["refund_id "] == null)
  527. {
  528. throw new SDKRuntimeException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"."<br>");
  529. }
  530. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  531. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  532. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  533. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  534. return $this->arrayToXml($this->parameters);
  535. }catch (SDKRuntimeException $e)
  536. {
  537. die($e->errorMessage());
  538. }
  539. }
  540. /**
  541. * 作用:获取结果,使用证书通信
  542. */
  543. function getResult()
  544. {
  545. $this->postXmlSSL();
  546. $this->result = $this->xmlToArray($this->response);
  547. return $this->result;
  548. }
  549. }
  550. /**
  551. * 对账单接口
  552. */
  553. class DownloadBill_pub extends Wxpay_client_pub
  554. {
  555. function __construct()
  556. {
  557. //设置接口链接
  558. $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  559. //设置curl超时时间
  560. $this->curl_timeout = 3;
  561. }
  562. /**
  563. * 生成接口参数xml
  564. */
  565. function createXml()
  566. {
  567. try
  568. {
  569. if($this->parameters["bill_date"] == null )
  570. {
  571. throw new SDKRuntimeException("对账单接口中,缺少必填参数bill_date!"."<br>");
  572. }
  573. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  574. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  575. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  576. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  577. return $this->arrayToXml($this->parameters);
  578. }catch (SDKRuntimeException $e)
  579. {
  580. die($e->errorMessage());
  581. }
  582. }
  583. /**
  584. * 作用:获取结果,默认不使用证书
  585. */
  586. function getResult()
  587. {
  588. $this->postXml();
  589. $this->result = $this->xmlToArray($this->result_xml);
  590. return $this->result;
  591. }
  592. }
  593. /**
  594. * 短链接转换接口
  595. */
  596. class ShortUrl_pub extends Wxpay_client_pub
  597. {
  598. function __construct()
  599. {
  600. //设置接口链接
  601. $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
  602. //设置curl超时时间
  603. $this->curl_timeout = 3;
  604. }
  605. /**
  606. * 生成接口参数xml
  607. */
  608. function createXml()
  609. {
  610. try
  611. {
  612. if($this->parameters["long_url"] == null )
  613. {
  614. throw new SDKRuntimeException("短链接转换接口中,缺少必填参数long_url!"."<br>");
  615. }
  616. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  617. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  618. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  619. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  620. return $this->arrayToXml($this->parameters);
  621. }catch (SDKRuntimeException $e)
  622. {
  623. die($e->errorMessage());
  624. }
  625. }
  626. /**
  627. * 获取prepay_id
  628. */
  629. function getShortUrl()
  630. {
  631. $this->postXml();
  632. $prepay_id = $this->result["short_url"];
  633. return $prepay_id;
  634. }
  635. }
  636. /**
  637. * 响应型接口基类
  638. */
  639. class Wxpay_server_pub extends Common_util_pub
  640. {
  641. public $data;//接收到的数据,类型为关联数组
  642. var $returnParameters;//返回参数,类型为关联数组
  643. /**
  644. * 将微信的请求xml转换成关联数组,以方便数据处理
  645. */
  646. function saveData($xml)
  647. {
  648. $this->data = $this->xmlToArray($xml);
  649. }
  650. function checkSign()
  651. {
  652. $tmpData = $this->data;
  653. unset($tmpData['sign']);
  654. $sign = $this->getSign($tmpData);//本地签名
  655. if ($this->data['sign'] == $sign) {
  656. return TRUE;
  657. }
  658. return FALSE;
  659. }
  660. /**
  661. * 获取微信的请求数据
  662. */
  663. function getData()
  664. {
  665. return $this->data;
  666. }
  667. /**
  668. * 设置返回微信的xml数据
  669. */
  670. function setReturnParameter($parameter, $parameterValue)
  671. {
  672. $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  673. }
  674. /**
  675. * 生成接口参数xml
  676. */
  677. function createXml()
  678. {
  679. return $this->arrayToXml($this->returnParameters);
  680. }
  681. /**
  682. * 将xml数据返回微信
  683. */
  684. function returnXml()
  685. {
  686. $returnXml = $this->createXml();
  687. return $returnXml;
  688. }
  689. }
  690. /**
  691. * 通用通知接口
  692. */
  693. class Notify_pub extends Wxpay_server_pub
  694. {
  695. }
  696. /**
  697. * 请求商家获取商品信息接口
  698. */
  699. class NativeCall_pub extends Wxpay_server_pub
  700. {
  701. /**
  702. * 生成接口参数xml
  703. */
  704. function createXml()
  705. {
  706. if($this->returnParameters["return_code"] == "SUCCESS"){
  707. $this->returnParameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  708. $this->returnParameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  709. $this->returnParameters["nonce_str"] = $this->createNoncestr();//随机字符串
  710. $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//签名
  711. }
  712. return $this->arrayToXml($this->returnParameters);
  713. }
  714. /**
  715. * 获取product_id
  716. */
  717. function getProductId()
  718. {
  719. $product_id = $this->data["product_id"];
  720. return $product_id;
  721. }
  722. }
  723. /**
  724. * 静态链接二维码
  725. */
  726. class NativeLink_pub extends Common_util_pub
  727. {
  728. var $parameters;//静态链接参数
  729. var $url;//静态链接
  730. function __construct()
  731. {
  732. }
  733. /**
  734. * 设置参数
  735. */
  736. function setParameter($parameter, $parameterValue)
  737. {
  738. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  739. }
  740. /**
  741. * 生成Native支付链接二维码
  742. */
  743. function createLink()
  744. {
  745. try
  746. {
  747. if($this->parameters["product_id"] == null)
  748. {
  749. throw new SDKRuntimeException("缺少Native支付二维码链接必填参数product_id!"."<br>");
  750. }
  751. $this->parameters["appid"] = Config::get('WECHAT_APPID');//公众账号ID
  752. $this->parameters["mch_id"] = Config::get('WECHAT_MCHID');//商户号
  753. $time_stamp = time();
  754. $this->parameters["time_stamp"] = "$time_stamp";//时间戳
  755. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  756. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  757. $bizString = $this->formatBizQueryParaMap($this->parameters, false);
  758. $this->url = "weixin://wxpay/bizpayurl?".$bizString;
  759. }catch (SDKRuntimeException $e)
  760. {
  761. die($e->errorMessage());
  762. }
  763. }
  764. /**
  765. * 返回链接
  766. */
  767. function getUrl()
  768. {
  769. $this->createLink();
  770. return $this->url;
  771. }
  772. }
  773. /**
  774. * JSAPI支付——H5网页端调起支付接口
  775. */
  776. class JsApi_pub extends Common_util_pub
  777. {
  778. var $code;//code码,用以获取openid
  779. var $openid;//用户的openid
  780. var $parameters;//jsapi参数,格式为json
  781. var $prepay_id;//使用统一支付接口得到的预支付id
  782. var $curl_timeout;//curl超时时间
  783. function __construct()
  784. {
  785. //设置curl超时时间
  786. $this->curl_timeout = 3;
  787. }
  788. /**
  789. * 生成可以获得code的url,只能获取openid
  790. * @param $redirectUrl
  791. * @param $number
  792. * @return string
  793. */
  794. function createOauthOpenidForCode($redirectUrl,$number)
  795. {
  796. $urlObj["appid"] = Config::get('WECHAT_APPID');
  797. $urlObj["redirect_uri"] = "$redirectUrl";
  798. $urlObj["response_type"] = "code";
  799. $urlObj["scope"] = "snsapi_base";
  800. $urlObj["state"] = "$number"."#wechat_redirect";
  801. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  802. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  803. }
  804. /**
  805. * 生成可以获得code的url,不仅限于openid信息
  806. * @param $redirectUrl
  807. * @param $number
  808. * @return string
  809. */
  810. function createOauthOpenidAndMoreUrlForCode($redirectUrl,$number)
  811. {
  812. $urlObj["appid"] = Config::get('WECHAT_APPID');
  813. $urlObj["redirect_uri"] = urlencode($redirectUrl);
  814. $urlObj["response_type"] = "code";
  815. $urlObj["scope"] = "snsapi_userinfo";
  816. $urlObj["state"] = "$number"."#wechat_redirect";
  817. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  818. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  819. }
  820. /**
  821. * 作用:生成可以获得openid的url
  822. */
  823. function createOauthUrlForOpenid()
  824. {
  825. $urlObj["appid"] = Config::get('WECHAT_APPID');
  826. $urlObj["secret"] = Config::get('WECHAT_APPSECRET');
  827. $urlObj["code"] = $this->code;
  828. $urlObj["grant_type"] = "authorization_code";
  829. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  830. return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  831. }
  832. function httpPost($url){
  833. //初始化curl
  834. $ch = curl_init();
  835. //设置超时
  836. //curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
  837. curl_setopt($ch, CURLOPT_URL, $url);
  838. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  839. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  840. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  841. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  842. //运行curl,结果以json形式返回
  843. $res = curl_exec($ch);
  844. curl_close($ch);
  845. //取出openid
  846. $data = json_decode($res,true);
  847. return $data;
  848. }
  849. /**
  850. * 作用:通过curl向微信提交code,以获取openid
  851. */
  852. function getOpenidInfo()
  853. {
  854. $url = $this->createOauthUrlForOpenid();
  855. $data = $this->httpPost($url);
  856. return $data;
  857. }
  858. /**
  859. * 网页授权第四步:拉取用户信息
  860. * @param string $access_token
  861. * @param string $openid
  862. * @return bool|mixed
  863. */
  864. public function getOauthUserInfo($access_token='',$openid=''){
  865. if(empty($access_token)&& empty($openid)){
  866. return FALSE;
  867. }
  868. $url = 'https://api.weixin.qq.com/sns/userinfo?';
  869. $url .= 'access_token='.$access_token;
  870. $url .= '&openid='.$openid;
  871. $url .= '&lang=zh_CN';
  872. $res = $this->httpPost($url);
  873. if($res != FALSE){
  874. if(empty($res) || array_key_exists('errcode', $res)){
  875. return FALSE;
  876. }
  877. return $res;
  878. }
  879. return FALSE;
  880. }
  881. /**
  882. * 设置prepay_id
  883. * @param $prepayId
  884. */
  885. function setPrepayId($prepayId)
  886. {
  887. $this->prepay_id = $prepayId;
  888. }
  889. /**
  890. * 设置code
  891. * @param $code_
  892. */
  893. function setCode($code_)
  894. {
  895. $this->code = $code_;
  896. }
  897. /**
  898. * 作用:设置jsapi的参数
  899. */
  900. public function getParameters()
  901. {
  902. $jsApiObj["appId"] = Config::get('WECHAT_APPID');
  903. $timeStamp = time();
  904. $jsApiObj["timeStamp"] = "$timeStamp";
  905. $jsApiObj["nonceStr"] = $this->createNoncestr();
  906. $jsApiObj["package"] = "prepay_id=$this->prepay_id";
  907. $jsApiObj["signType"] = "MD5";
  908. $jsApiObj["paySign"] = $this->getSign($jsApiObj);
  909. $this->parameters = json_encode($jsApiObj);
  910. return $this->parameters;
  911. }
  912. }