WxPayPubHelper.php 29 KB

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