Wechat.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. namespace fast\payment;
  3. use Exception;
  4. use think\Config;
  5. /**
  6. * @link https://github.com/zhangv/wechat-pay
  7. */
  8. class Wechat
  9. {
  10. const TRADETYPE_JSAPI = 'JSAPI', TRADETYPE_NATIVE = 'NATIVE', TRADETYPE_APP = 'APP';
  11. const URL_UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  12. const URL_ORDERQUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
  13. const URL_CLOSEORDER = 'https://api.mch.weixin.qq.com/pay/closeorder';
  14. const URL_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
  15. const URL_REFUNDQUERY = 'https://api.mch.weixin.qq.com/pay/refundquery';
  16. const URL_DOWNLOADBILL = 'https://api.mch.weixin.qq.com/pay/downloadbill';
  17. const URL_REPORT = 'https://api.mch.weixin.qq.com/payitil/report';
  18. const URL_SHORTURL = 'https://api.mch.weixin.qq.com/tools/shorturl';
  19. const URL_MICROPAY = 'https://api.mch.weixin.qq.com/pay/micropay';
  20. /**
  21. * 错误信息
  22. */
  23. public $error = null;
  24. /**
  25. * 错误信息XML
  26. */
  27. public $errorXML = null;
  28. /**
  29. * 微信支付配置数组
  30. * appid 公众账号appid
  31. * mch_id 商户号
  32. * apikey 加密key
  33. * appsecret 公众号appsecret
  34. * sslcertPath 证书路径(apiclient_cert.pem)
  35. * sslkeyPath 密钥路径(apiclient_key.pem)
  36. */
  37. private $_config;
  38. /**
  39. * @param $options 微信支付配置数组
  40. */
  41. public function __construct($options = [])
  42. {
  43. if ($config = Config::get('payment.wechat'))
  44. {
  45. $this->config = array_merge($this->config, $config);
  46. }
  47. $this->_config = array_merge($this->_config, is_array($options) ? $options : []);
  48. }
  49. /**
  50. * JSAPI获取prepay_id
  51. *
  52. * @param string $body
  53. * @param string $out_trade_no
  54. * @param int $total_fee
  55. * @param string $openid
  56. * @param array $ext
  57. * @return string
  58. */
  59. public function getPrepayId($body, $out_trade_no, $total_fee, $openid, $ext = null)
  60. {
  61. $data = $ext? : [];
  62. $data["nonce_str"] = $this->getNonceStr();
  63. $data["body"] = $body;
  64. $data["out_trade_no"] = $out_trade_no;
  65. $data["total_fee"] = $total_fee;
  66. $data["spbill_create_ip"] = $_SERVER["REMOTE_ADDR"];
  67. $data["trade_type"] = self::TRADETYPE_JSAPI;
  68. $data["openid"] = $openid;
  69. $result = $this->unifiedOrder($data);
  70. if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS")
  71. {
  72. return $result["prepay_id"];
  73. }
  74. else
  75. {
  76. $this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
  77. $this->errorXML = $this->array2xml($result);
  78. return null;
  79. }
  80. }
  81. private function getNonceStr()
  82. {
  83. return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32);
  84. }
  85. /**
  86. * 统一下单接口
  87. */
  88. public function unifiedOrder($params)
  89. {
  90. $data = array();
  91. $data["appid"] = $this->_config["appid"];
  92. $data["mch_id"] = $this->_config["mch_id"];
  93. $data["device_info"] = (isset($params['device_info']) && trim($params['device_info']) != '') ? $params['device_info'] : null;
  94. $data["nonce_str"] = $this->getNonceStr();
  95. $data["body"] = $params['body'];
  96. $data["detail"] = isset($params['detail']) ? $params['detail'] : null; //optional
  97. $data["attach"] = isset($params['attach']) ? $params['attach'] : null; //optional
  98. $data["out_trade_no"] = isset($params['out_trade_no']) ? $params['out_trade_no'] : null;
  99. $data["fee_type"] = isset($params['fee_type']) ? $params['fee_type'] : 'CNY';
  100. $data["total_fee"] = $params['total_fee'];
  101. $data["spbill_create_ip"] = $params['spbill_create_ip'];
  102. $data["time_start"] = isset($params['time_start']) ? $params['time_start'] : null; //optional
  103. $data["time_expire"] = isset($params['time_expire']) ? $params['time_expire'] : null; //optional
  104. $data["goods_tag"] = isset($params['goods_tag']) ? $params['goods_tag'] : null;
  105. $data["notify_url"] = isset($params['notify_url']) ? $params['notify_url'] : $this->_config['notify_url'];
  106. $data["trade_type"] = $params['trade_type'];
  107. $data["product_id"] = isset($params['product_id']) ? $params['product_id'] : null; //required when trade_type = NATIVE
  108. $data["openid"] = isset($params['openid']) ? $params['openid'] : null; //required when trade_type = JSAPI
  109. $result = $this->post(self::URL_UNIFIEDORDER, $data);
  110. return $result;
  111. }
  112. private function post($url, $data, $cert = false)
  113. {
  114. if (!isset($data['sign']))
  115. $data["sign"] = $this->sign($data);
  116. $xml = $this->array2xml($data);
  117. $ch = curl_init();
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  120. curl_setopt($ch, CURLOPT_POST, 1);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  123. curl_setopt($ch, CURLOPT_URL, $url);
  124. if ($cert == true)
  125. {
  126. //使用证书:cert 与 key 分别属于两个.pem文件
  127. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
  128. curl_setopt($ch, CURLOPT_SSLCERT, $this->_config['sslcertPath']);
  129. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
  130. curl_setopt($ch, CURLOPT_SSLKEY, $this->_config['sslkeyPath']);
  131. }
  132. $content = curl_exec($ch);
  133. $array = $this->xml2array($content);
  134. return $array;
  135. }
  136. /**
  137. * 扫码支付(模式二)获取支付二维码
  138. *
  139. * @param string $body
  140. * @param string $out_trade_no
  141. * @param int $total_fee
  142. * @param string $product_id
  143. * @param array $ext
  144. * @return string
  145. */
  146. public function getCodeUrl($body, $out_trade_no, $total_fee, $product_id, $ext = null)
  147. {
  148. $data = $ext ? $ext : [];
  149. $data["nonce_str"] = $this->getNonceStr();
  150. $data["body"] = $body;
  151. $data["out_trade_no"] = $out_trade_no;
  152. $data["total_fee"] = $total_fee;
  153. $data["spbill_create_ip"] = $_SERVER["SERVER_ADDR"];
  154. $data["trade_type"] = self::TRADETYPE_NATIVE;
  155. $data["product_id"] = $product_id;
  156. $result = $this->unifiedOrder($data);
  157. if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS")
  158. {
  159. return $result["code_url"];
  160. }
  161. else
  162. {
  163. $this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
  164. return null;
  165. }
  166. }
  167. /**
  168. * 查询订单
  169. * @param $transaction_id
  170. * @param $out_trade_no
  171. * @return array
  172. */
  173. public function orderQuery($transaction_id, $out_trade_no)
  174. {
  175. $data = array();
  176. $data["appid"] = $this->_config["appid"];
  177. $data["mch_id"] = $this->_config["mch_id"];
  178. $data["transaction_id"] = $transaction_id;
  179. $data["out_trade_no"] = $out_trade_no;
  180. $data["nonce_str"] = $this->getNonceStr();
  181. $result = $this->post(self::URL_ORDERQUERY, $data);
  182. return $result;
  183. }
  184. /**
  185. * 关闭订单
  186. * @param $out_trade_no
  187. * @return array
  188. */
  189. public function closeOrder($out_trade_no)
  190. {
  191. $data = array();
  192. $data["appid"] = $this->_config["appid"];
  193. $data["mch_id"] = $this->_config["mch_id"];
  194. $data["out_trade_no"] = $out_trade_no;
  195. $data["nonce_str"] = $this->getNonceStr();
  196. $result = $this->post(self::URL_CLOSEORDER, $data);
  197. return $result;
  198. }
  199. /**
  200. * 申请退款 - 使用商户订单号
  201. * @param $out_trade_no 商户订单号
  202. * @param $out_refund_no 退款单号
  203. * @param $total_fee 总金额(单位:分)
  204. * @param $refund_fee 退款金额(单位:分)
  205. * @param $op_user_id 操作员账号
  206. * @return array
  207. */
  208. public function refund($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $op_user_id)
  209. {
  210. $data = array();
  211. $data["appid"] = $this->_config["appid"];
  212. $data["mch_id"] = $this->_config["mch_id"];
  213. $data["nonce_str"] = $this->getNonceStr();
  214. $data["out_trade_no"] = $out_trade_no;
  215. $data["out_refund_no"] = $out_refund_no;
  216. $data["total_fee"] = $total_fee;
  217. $data["refund_fee"] = $refund_fee;
  218. $data["op_user_id"] = $op_user_id;
  219. $result = $this->post(self::URL_REFUND, $data, true);
  220. return $result;
  221. }
  222. /**
  223. * 申请退款 - 使用微信订单号
  224. * @param $transaction_id 微信订单号
  225. * @param $out_refund_no 退款单号
  226. * @param $total_fee 总金额(单位:分)
  227. * @param $refund_fee 退款金额(单位:分)
  228. * @param $op_user_id 操作员账号
  229. * @return array
  230. */
  231. public function refundByTransId($transaction_id, $out_refund_no, $total_fee, $refund_fee, $op_user_id)
  232. {
  233. $data = array();
  234. $data["appid"] = $this->_config["appid"];
  235. $data["mch_id"] = $this->_config["mch_id"];
  236. $data["nonce_str"] = $this->getNonceStr();
  237. $data["transaction_id"] = $transaction_id;
  238. $data["out_refund_no"] = $out_refund_no;
  239. $data["total_fee"] = $total_fee;
  240. $data["refund_fee"] = $refund_fee;
  241. $data["op_user_id"] = $op_user_id;
  242. $result = $this->post(self::URL_REFUND, $data, true);
  243. return $result;
  244. }
  245. /**
  246. * 下载对账单
  247. * @param $bill_date 下载对账单的日期,格式:20140603
  248. * @param string $bill_type 类型
  249. * @return array
  250. */
  251. public function downloadBill($bill_date, $bill_type = 'ALL')
  252. {
  253. $data = array();
  254. $data["appid"] = $this->_config["appid"];
  255. $data["mch_id"] = $this->_config["mch_id"];
  256. $data["bill_date"] = $bill_date;
  257. $data["bill_type"] = $bill_type;
  258. $data["nonce_str"] = $this->getNonceStr();
  259. $result = $this->post(self::URL_DOWNLOADBILL, $data);
  260. return $result;
  261. }
  262. /**
  263. * 扫码原生支付模式一中的二维码链接转成短链接
  264. * @param $long_url 需要转换的URL,签名用原串,传输需URLencode
  265. * @return array
  266. */
  267. public function shortUrl($long_url)
  268. {
  269. $data = array();
  270. $data["appid"] = $this->_config["appid"];
  271. $data["mch_id"] = $this->_config["mch_id"];
  272. $data["long_url"] = $long_url;
  273. $data["nonce_str"] = $this->getNonceStr();
  274. $data["sign"] = $this->sign($data);
  275. $data["long_url"] = urlencode($long_url);
  276. $result = $this->post(self::URL_SHORTURL, $data);
  277. return $result;
  278. }
  279. /**
  280. * 获取jsapi支付所需参数
  281. *
  282. * @param string $prepay_id
  283. * @return array
  284. */
  285. public function getPackageData($prepay_id)
  286. {
  287. $data = array();
  288. $data["appId"] = $this->_config["appid"];
  289. //解决微信支付调用JSAPI缺少参数:timeStamp
  290. $data["timeStamp"] = time();
  291. $data["nonceStr"] = $this->getNonceStr();
  292. $data["package"] = "prepay_id=$prepay_id";
  293. $data["signType"] = "MD5";
  294. $data["paySign"] = $this->sign($data);
  295. return $data;
  296. }
  297. /**
  298. * 获取发送到通知地址的数据(在通知地址内使用)
  299. * @return string 结果数组,如果不是微信服务器发送的数据返回null
  300. * appid
  301. * bank_type
  302. * cash_fee
  303. * fee_type
  304. * is_subscribe
  305. * mch_id
  306. * nonce_str
  307. * openid
  308. * out_trade_no 商户订单号
  309. * result_code
  310. * return_code
  311. * sign
  312. * time_end
  313. * total_fee 总金额
  314. * trade_type
  315. * transaction_id 微信支付订单号
  316. */
  317. public function getNotifyData()
  318. {
  319. $xml = file_get_contents("php://input");
  320. $data = $this->xml2array($xml);
  321. if ($this->validate($data))
  322. {
  323. return $data;
  324. }
  325. else
  326. {
  327. return null;
  328. }
  329. }
  330. /**
  331. * 验证数据签名
  332. * @param $data 数据数组
  333. * @return 数据校验结果
  334. */
  335. public function validate($data)
  336. {
  337. if (!isset($data["sign"]))
  338. {
  339. return false;
  340. }
  341. $sign = $data["sign"];
  342. unset($data["sign"]);
  343. return $this->sign($data) == $sign;
  344. }
  345. /**
  346. * 响应微信支付后台通知
  347. * @param string $return_code 返回状态码 SUCCESS/FAIL
  348. * @param $return_msg 返回信息
  349. */
  350. public function response_back($return_code = "SUCCESS", $return_msg = null)
  351. {
  352. $data = array();
  353. $data["return_code"] = $return_code;
  354. if ($return_msg)
  355. {
  356. $data["return_msg"] = $return_msg;
  357. }
  358. $xml = $this->array2xml($data);
  359. print $xml;
  360. }
  361. /**
  362. * 数据签名
  363. * @param $data
  364. * @return string
  365. */
  366. private function sign($data)
  367. {
  368. ksort($data);
  369. $string1 = "";
  370. foreach ($data as $k => $v)
  371. {
  372. if ($v && trim($v) != '')
  373. {
  374. $string1 .= "$k=$v&";
  375. }
  376. }
  377. $stringSignTemp = $string1 . "key=" . $this->_config["apikey"];
  378. $sign = strtoupper(md5($stringSignTemp));
  379. return $sign;
  380. }
  381. private function array2xml($array)
  382. {
  383. $xml = "<xml>" . PHP_EOL;
  384. foreach ($array as $k => $v)
  385. {
  386. if ($v && trim($v) != '')
  387. $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
  388. }
  389. $xml .= "</xml>";
  390. return $xml;
  391. }
  392. private function xml2array($xml)
  393. {
  394. $array = array();
  395. $tmp = null;
  396. try
  397. {
  398. $tmp = (array) simplexml_load_string($xml);
  399. }
  400. catch (Exception $e)
  401. {
  402. }
  403. if ($tmp && is_array($tmp))
  404. {
  405. foreach ($tmp as $k => $v)
  406. {
  407. $array[$k] = (string) $v;
  408. }
  409. }
  410. return $array;
  411. }
  412. }