WxPayPubHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. //decode by qq784939922
  3. require_once 'WxPay.Exception.php';
  4. require_once 'WxPay.Config.php';
  5. class Common_util_pub
  6. {
  7. function __construct()
  8. {
  9. }
  10. function trimString($_var_0)
  11. {
  12. $_var_1 = null;
  13. if (null != $_var_0) {
  14. $_var_1 = $_var_0;
  15. if (strlen($_var_1) == 0) {
  16. $_var_1 = null;
  17. }
  18. }
  19. return $_var_1;
  20. }
  21. public function createNoncestr($_var_2 = 32)
  22. {
  23. $_var_3 = 'abcdefghijklmnopqrstuvwxyz0123456789';
  24. $_var_4 = "";
  25. for ($_var_5 = 0; $_var_5 < $_var_2; $_var_5++) {
  26. $_var_4 .= substr($_var_3, mt_rand(0, strlen($_var_3) - 1), 1);
  27. }
  28. return $_var_4;
  29. }
  30. function formatBizQueryParaMap($_var_6, $_var_7)
  31. {
  32. $_var_8 = "";
  33. ksort($_var_6);
  34. foreach ($_var_6 as $_var_9 => $_var_10) {
  35. if ($_var_7) {
  36. $_var_10 = urlencode($_var_10);
  37. }
  38. $_var_8 .= $_var_9 . '=' . $_var_10 . '&';
  39. }
  40. $_var_11;
  41. if (strlen($_var_8) > 0) {
  42. $_var_11 = substr($_var_8, 0, strlen($_var_8) - 1);
  43. }
  44. return $_var_11;
  45. }
  46. public function getSign($_var_12)
  47. {
  48. foreach ($_var_12 as $_var_9 => $_var_10) {
  49. $_var_13[$_var_9] = $_var_10;
  50. }
  51. ksort($_var_13);
  52. $_var_14 = $this->formatBizQueryParaMap($_var_13, false);
  53. $_var_14 = $_var_14 . '&key=' . WxPayConfig::$KEY;
  54. $_var_14 = md5($_var_14);
  55. $_var_15 = strtoupper($_var_14);
  56. return $_var_15;
  57. }
  58. function arrayToXml($_var_16)
  59. {
  60. $_var_17 = '<xml>';
  61. foreach ($_var_16 as $_var_18 => $_var_19) {
  62. if (is_numeric($_var_19)) {
  63. $_var_17 .= '<' . $_var_18 . '>' . $_var_19 . '</' . $_var_18 . '>';
  64. } else {
  65. $_var_17 .= '<' . $_var_18 . '><![CDATA[' . $_var_19 . ']]></' . $_var_18 . '>';
  66. }
  67. }
  68. $_var_17 .= '</xml>';
  69. return $_var_17;
  70. }
  71. public function xmlToArray($_var_17)
  72. {
  73. $_var_20 = json_decode(json_encode(simplexml_load_string($_var_17, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  74. return $_var_20;
  75. }
  76. public function postXmlCurl($_var_17, $_var_21, $_var_22 = 30)
  77. {
  78. $_var_23 = curl_init();
  79. curl_setopt($_var_23, CURLOPT_TIMEOUT, $_var_22);
  80. curl_setopt($_var_23, CURLOPT_URL, $_var_21);
  81. curl_setopt($_var_23, CURLOPT_SSL_VERIFYPEER, FALSE);
  82. curl_setopt($_var_23, CURLOPT_SSL_VERIFYHOST, FALSE);
  83. curl_setopt($_var_23, CURLOPT_HEADER, FALSE);
  84. curl_setopt($_var_23, CURLOPT_RETURNTRANSFER, TRUE);
  85. curl_setopt($_var_23, CURLOPT_POST, TRUE);
  86. curl_setopt($_var_23, CURLOPT_POSTFIELDS, $_var_17);
  87. $_var_24 = curl_exec($_var_23);
  88. curl_close($_var_23);
  89. if ($_var_24) {
  90. return $_var_24;
  91. } else {
  92. $_var_25 = curl_errno($_var_23);
  93. echo "curl出错,错误码:{$_var_25}" . '<br>';
  94. echo '<a href=\'http://curl.haxx.se/libcurl/c/libcurl-errors.html\'>错误原因查询</a></br>';
  95. curl_close($_var_23);
  96. return false;
  97. }
  98. }
  99. function postXmlSSLCurl($_var_17, $_var_21, $_var_22 = 30)
  100. {
  101. $_var_23 = curl_init();
  102. curl_setopt($_var_23, CURLOPT_TIMEOUT, $_var_22);
  103. curl_setopt($_var_23, CURLOPT_URL, $_var_21);
  104. curl_setopt($_var_23, CURLOPT_SSL_VERIFYPEER, FALSE);
  105. curl_setopt($_var_23, CURLOPT_SSL_VERIFYHOST, FALSE);
  106. curl_setopt($_var_23, CURLOPT_HEADER, FALSE);
  107. curl_setopt($_var_23, CURLOPT_RETURNTRANSFER, TRUE);
  108. curl_setopt($_var_23, CURLOPT_SSLCERTTYPE, 'PEM');
  109. curl_setopt($_var_23, CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
  110. curl_setopt($_var_23, CURLOPT_SSLKEYTYPE, 'PEM');
  111. curl_setopt($_var_23, CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);
  112. curl_setopt($_var_23, CURLOPT_POST, true);
  113. curl_setopt($_var_23, CURLOPT_POSTFIELDS, $_var_17);
  114. $_var_24 = curl_exec($_var_23);
  115. if ($_var_24) {
  116. return $_var_24;
  117. } else {
  118. $_var_25 = curl_errno($_var_23);
  119. echo "curl出错,错误码:{$_var_25}" . '<br>';
  120. echo '<a href=\'http://curl.haxx.se/libcurl/c/libcurl-errors.html\'>错误原因查询</a></br>';
  121. curl_close($_var_23);
  122. return false;
  123. }
  124. }
  125. function printErr($_var_26 = '', $_var_27 = '')
  126. {
  127. print_r('<pre>');
  128. echo $_var_26 . '</br>';
  129. var_dump($_var_27);
  130. print_r('</pre>');
  131. }
  132. }
  133. class Wxpay_client_pub extends Common_util_pub
  134. {
  135. var $parameters;
  136. public $response;
  137. public $result;
  138. var $url;
  139. var $curl_timeout;
  140. function setParameter($_var_28, $_var_29)
  141. {
  142. $this->parameters[$this->trimString($_var_28)] = $this->trimString($_var_29);
  143. }
  144. function createXml()
  145. {
  146. $this->parameters['appid'] = WxPayConfig::$APPID;
  147. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  148. $this->parameters['nonce_str'] = $this->createNoncestr();
  149. $this->parameters['sign'] = $this->getSign($this->parameters);
  150. return $this->arrayToXml($this->parameters);
  151. }
  152. function postXml()
  153. {
  154. $_var_17 = $this->createXml();
  155. $this->response = $this->postXmlCurl($_var_17, $this->url, $this->curl_timeout);
  156. return $this->response;
  157. }
  158. function postXmlSSL()
  159. {
  160. $_var_17 = $this->createXml();
  161. $this->response = $this->postXmlSSLCurl($_var_17, $this->url, $this->curl_timeout);
  162. return $this->response;
  163. }
  164. function getResult()
  165. {
  166. $this->postXml();
  167. $this->result = $this->xmlToArray($this->response);
  168. return $this->result;
  169. }
  170. }
  171. class UnifiedOrder_pub extends Wxpay_client_pub
  172. {
  173. function __construct()
  174. {
  175. $this->url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  176. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  177. }
  178. function createXml()
  179. {
  180. try {
  181. if ($this->parameters['out_trade_no'] == null) {
  182. throw new WxPayException('缺少统一支付接口必填参数out_trade_no!' . '<br>');
  183. } elseif ($this->parameters['body'] == null) {
  184. throw new WxPayException('缺少统一支付接口必填参数body!' . '<br>');
  185. } elseif ($this->parameters['total_fee'] == null) {
  186. throw new WxPayException('缺少统一支付接口必填参数total_fee!' . '<br>');
  187. } elseif ($this->parameters['notify_url'] == null) {
  188. throw new WxPayException('缺少统一支付接口必填参数notify_url!' . '<br>');
  189. } elseif ($this->parameters['trade_type'] == null) {
  190. throw new WxPayException('缺少统一支付接口必填参数trade_type!' . '<br>');
  191. } elseif ($this->parameters['trade_type'] == 'JSAPI' && $this->parameters['openid'] == NULL) {
  192. throw new WxPayException('统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!' . '<br>');
  193. }
  194. $this->parameters['appid'] = WxPayConfig::$APPID;
  195. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  196. $this->parameters['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
  197. $this->parameters['nonce_str'] = $this->createNoncestr();
  198. $this->parameters['sign'] = $this->getSign($this->parameters);
  199. return $this->arrayToXml($this->parameters);
  200. } catch (WxPayException $_var_30) {
  201. die($_var_30->errorMessage());
  202. }
  203. }
  204. function getPrepayId()
  205. {
  206. $this->postXml();
  207. $this->result = $this->xmlToArray($this->response);
  208. $_var_31 = $this->result['prepay_id'];
  209. return $_var_31;
  210. }
  211. }
  212. class OrderQuery_pub extends Wxpay_client_pub
  213. {
  214. function __construct()
  215. {
  216. $this->url = 'https://api.mch.weixin.qq.com/pay/orderquery';
  217. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  218. }
  219. function createXml()
  220. {
  221. try {
  222. if ($this->parameters['out_trade_no'] == null && $this->parameters['transaction_id'] == null) {
  223. throw new WxPayException('订单查询接口中,out_trade_no、transaction_id至少填一个!' . '<br>');
  224. }
  225. $this->parameters['appid'] = WxPayConfig::$APPID;
  226. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  227. $this->parameters['nonce_str'] = $this->createNoncestr();
  228. $this->parameters['sign'] = $this->getSign($this->parameters);
  229. return $this->arrayToXml($this->parameters);
  230. } catch (WxPayException $_var_30) {
  231. die($_var_30->errorMessage());
  232. }
  233. }
  234. }
  235. class Refund_pub extends Wxpay_client_pub
  236. {
  237. function __construct()
  238. {
  239. $this->url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
  240. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  241. }
  242. function createXml()
  243. {
  244. try {
  245. if ($this->parameters['out_trade_no'] == null && $this->parameters['transaction_id'] == null) {
  246. throw new WxPayException('退款申请接口中,out_trade_no、transaction_id至少填一个!' . '<br>');
  247. } elseif ($this->parameters['out_refund_no'] == null) {
  248. throw new WxPayException('退款申请接口中,缺少必填参数out_refund_no!' . '<br>');
  249. } elseif ($this->parameters['total_fee'] == null) {
  250. throw new WxPayException('退款申请接口中,缺少必填参数total_fee!' . '<br>');
  251. } elseif ($this->parameters['refund_fee'] == null) {
  252. throw new WxPayException('退款申请接口中,缺少必填参数refund_fee!' . '<br>');
  253. } elseif ($this->parameters['op_user_id'] == null) {
  254. throw new WxPayException('退款申请接口中,缺少必填参数op_user_id!' . '<br>');
  255. }
  256. $this->parameters['appid'] = WxPayConfig::$APPID;
  257. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  258. $this->parameters['nonce_str'] = $this->createNoncestr();
  259. $this->parameters['sign'] = $this->getSign($this->parameters);
  260. return $this->arrayToXml($this->parameters);
  261. } catch (WxPayException $_var_30) {
  262. die($_var_30->errorMessage());
  263. }
  264. }
  265. function getResult()
  266. {
  267. $this->postXmlSSL();
  268. $this->result = $this->xmlToArray($this->response);
  269. return $this->result;
  270. }
  271. }
  272. class RefundQuery_pub extends Wxpay_client_pub
  273. {
  274. function __construct()
  275. {
  276. $this->url = 'https://api.mch.weixin.qq.com/pay/refundquery';
  277. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  278. }
  279. function createXml()
  280. {
  281. try {
  282. if ($this->parameters['out_refund_no'] == null && $this->parameters['out_trade_no'] == null && $this->parameters['transaction_id'] == null && $this->parameters['refund_id '] == null) {
  283. throw new WxPayException('退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!' . '<br>');
  284. }
  285. $this->parameters['appid'] = WxPayConfig::$APPID;
  286. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  287. $this->parameters['nonce_str'] = $this->createNoncestr();
  288. $this->parameters['sign'] = $this->getSign($this->parameters);
  289. return $this->arrayToXml($this->parameters);
  290. } catch (WxPayException $_var_30) {
  291. die($_var_30->errorMessage());
  292. }
  293. }
  294. function getResult()
  295. {
  296. $this->postXmlSSL();
  297. $this->result = $this->xmlToArray($this->response);
  298. return $this->result;
  299. }
  300. }
  301. class DownloadBill_pub extends Wxpay_client_pub
  302. {
  303. function __construct()
  304. {
  305. $this->url = 'https://api.mch.weixin.qq.com/pay/downloadbill';
  306. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  307. }
  308. function createXml()
  309. {
  310. try {
  311. if ($this->parameters['bill_date'] == null) {
  312. throw new WxPayException('对账单接口中,缺少必填参数bill_date!' . '<br>');
  313. }
  314. $this->parameters['appid'] = WxPayConfig::$APPID;
  315. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  316. $this->parameters['nonce_str'] = $this->createNoncestr();
  317. $this->parameters['sign'] = $this->getSign($this->parameters);
  318. return $this->arrayToXml($this->parameters);
  319. } catch (WxPayException $_var_30) {
  320. die($_var_30->errorMessage());
  321. }
  322. }
  323. function getResult()
  324. {
  325. $this->postXml();
  326. $this->result = $this->xmlToArray($this->result_xml);
  327. return $this->result;
  328. }
  329. }
  330. class ShortUrl_pub extends Wxpay_client_pub
  331. {
  332. function __construct()
  333. {
  334. $this->url = 'https://api.mch.weixin.qq.com/tools/shorturl';
  335. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  336. }
  337. function createXml()
  338. {
  339. try {
  340. if ($this->parameters['long_url'] == null) {
  341. throw new WxPayException('短链接转换接口中,缺少必填参数long_url!' . '<br>');
  342. }
  343. $this->parameters['appid'] = WxPayConfig::$APPID;
  344. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  345. $this->parameters['nonce_str'] = $this->createNoncestr();
  346. $this->parameters['sign'] = $this->getSign($this->parameters);
  347. return $this->arrayToXml($this->parameters);
  348. } catch (WxPayException $_var_30) {
  349. die($_var_30->errorMessage());
  350. }
  351. }
  352. function getShortUrl()
  353. {
  354. $this->postXml();
  355. $_var_31 = $this->result['short_url'];
  356. return $_var_31;
  357. }
  358. }
  359. class Wxpay_server_pub extends Common_util_pub
  360. {
  361. public $data;
  362. var $returnParameters;
  363. function saveData($_var_17)
  364. {
  365. $this->data = $this->xmlToArray($_var_17);
  366. }
  367. function checkSign()
  368. {
  369. $_var_32 = $this->data;
  370. unset($_var_32['sign']);
  371. $_var_33 = $this->getSign($_var_32);
  372. if ($this->data['sign'] == $_var_33) {
  373. return TRUE;
  374. }
  375. return FALSE;
  376. }
  377. function getData()
  378. {
  379. return $this->data;
  380. }
  381. function setReturnParameter($_var_28, $_var_29)
  382. {
  383. $this->returnParameters[$this->trimString($_var_28)] = $this->trimString($_var_29);
  384. }
  385. function createXml()
  386. {
  387. return $this->arrayToXml($this->returnParameters);
  388. }
  389. function returnXml()
  390. {
  391. $_var_34 = $this->createXml();
  392. return $_var_34;
  393. }
  394. }
  395. class Notify_pub extends Wxpay_server_pub
  396. {
  397. }
  398. class NativeCall_pub extends Wxpay_server_pub
  399. {
  400. function createXml()
  401. {
  402. if ($this->returnParameters['return_code'] == 'SUCCESS') {
  403. $this->returnParameters['appid'] = WxPayConfig::$APPID;
  404. $this->returnParameters['mch_id'] = WxPayConfig::$MCHID;
  405. $this->returnParameters['nonce_str'] = $this->createNoncestr();
  406. $this->returnParameters['sign'] = $this->getSign($this->returnParameters);
  407. }
  408. return $this->arrayToXml($this->returnParameters);
  409. }
  410. function getProductId()
  411. {
  412. $_var_35 = $this->data['product_id'];
  413. return $_var_35;
  414. }
  415. }
  416. class NativeLink_pub extends Common_util_pub
  417. {
  418. var $parameters;
  419. var $url;
  420. function __construct()
  421. {
  422. }
  423. function setParameter($_var_28, $_var_29)
  424. {
  425. $this->parameters[$this->trimString($_var_28)] = $this->trimString($_var_29);
  426. }
  427. function createLink()
  428. {
  429. try {
  430. if ($this->parameters['product_id'] == null) {
  431. throw new WxPayException('缺少Native支付二维码链接必填参数product_id!' . '<br>');
  432. }
  433. $this->parameters['appid'] = WxPayConfig::$APPID;
  434. $this->parameters['mch_id'] = WxPayConfig::$MCHID;
  435. $_var_36 = time();
  436. $this->parameters['time_stamp'] = "{$_var_36}";
  437. $this->parameters['nonce_str'] = $this->createNoncestr();
  438. $this->parameters['sign'] = $this->getSign($this->parameters);
  439. $_var_37 = $this->formatBizQueryParaMap($this->parameters, false);
  440. $this->url = 'weixin://wxpay/bizpayurl?' . $_var_37;
  441. } catch (WxPayException $_var_30) {
  442. die($_var_30->errorMessage());
  443. }
  444. }
  445. function getUrl()
  446. {
  447. $this->createLink();
  448. return $this->url;
  449. }
  450. }
  451. class JsApi_pub extends Common_util_pub
  452. {
  453. var $code;
  454. var $openid;
  455. var $parameters;
  456. var $prepay_id;
  457. var $curl_timeout;
  458. function __construct()
  459. {
  460. $this->curl_timeout = WxPayConfig::$CURL_TIMEOUT;
  461. }
  462. function createOauthUrlForCode($_var_38)
  463. {
  464. $_var_39['appid'] = WxPayConfig::$APPID;
  465. $_var_39['redirect_uri'] = "{$_var_38}";
  466. $_var_39['response_type'] = 'code';
  467. $_var_39['scope'] = 'snsapi_base';
  468. $_var_39['state'] = 'STATE' . '#wechat_redirect';
  469. $_var_37 = $this->formatBizQueryParaMap($_var_39, false);
  470. return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . $_var_37;
  471. }
  472. function createOauthUrlForOpenid()
  473. {
  474. $_var_39['appid'] = WxPayConfig::$APPID;
  475. $_var_39['secret'] = WxPayConfig::$APPSECRET;
  476. $_var_39['code'] = $this->code;
  477. $_var_39['grant_type'] = 'authorization_code';
  478. $_var_37 = $this->formatBizQueryParaMap($_var_39, false);
  479. return 'https://api.weixin.qq.com/sns/oauth2/access_token?' . $_var_37;
  480. }
  481. function getOpenid()
  482. {
  483. $_var_21 = $this->createOauthUrlForOpenid();
  484. $_var_23 = curl_init();
  485. curl_setopt($_var_23, CURLOP_TIMEOUT, $this->curl_timeout);
  486. curl_setopt($_var_23, CURLOPT_URL, $_var_21);
  487. curl_setopt($_var_23, CURLOPT_SSL_VERIFYPEER, FALSE);
  488. curl_setopt($_var_23, CURLOPT_SSL_VERIFYHOST, FALSE);
  489. curl_setopt($_var_23, CURLOPT_HEADER, FALSE);
  490. curl_setopt($_var_23, CURLOPT_RETURNTRANSFER, TRUE);
  491. $_var_40 = curl_exec($_var_23);
  492. curl_close($_var_23);
  493. $_var_24 = json_decode($_var_40, true);
  494. $this->openid = $_var_24['openid'];
  495. return $this->openid;
  496. }
  497. function setPrepayId($_var_41)
  498. {
  499. $this->prepay_id = $_var_41;
  500. }
  501. function setCode($_var_42)
  502. {
  503. $this->code = $_var_42;
  504. }
  505. public function getParameters()
  506. {
  507. $_var_43['appId'] = WxPayConfig::$APPID;
  508. $_var_44 = time();
  509. $_var_43['timeStamp'] = "{$_var_44}";
  510. $_var_43['nonceStr'] = $this->createNoncestr();
  511. $_var_43['package'] = "prepay_id={$this->prepay_id}";
  512. $_var_43['signType'] = 'MD5';
  513. $_var_43['paySign'] = $this->getSign($_var_43);
  514. $this->parameters = json_encode($_var_43);
  515. return $this->parameters;
  516. }
  517. }