WxPay.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. namespace wxpay;
  3. /**
  4. * 微信插件 使用需 require_once("./Plugins/WxPay/WxPay.php");
  5. * @author Jack_YanTC <627495692@qq.com>
  6. */
  7. class WxPay {
  8. /**
  9. * 初始化参数
  10. *
  11. * @param array $options
  12. * @param $options ['app_id'] APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
  13. * @param $options ['mch_id'] MCHID:商户号(必须配置,开户邮件中可查看)
  14. * @param $options ['key'] KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
  15. * @param $options ['appsecret'] 公众帐号secert(仅JSAPI支付的时候需要配置),
  16. * @param $options ['notify_url'] 支付宝回调地址
  17. */
  18. public function __construct($options = array()) {
  19. $this->config = !empty($options) ? $options : config('wxchatpay');
  20. }
  21. /**
  22. * 微信支付App
  23. * @param string $data 业务参数 body out_trade_no total_fee
  24. * @param string $data ['out_trade_no'] 订单号 必填
  25. * @param string $data ['total_fee'] 订单金额 必填
  26. * @param string $data ['body'] 订单详情 必填
  27. * @return $response 返回app所需字符串
  28. */
  29. public function WxPayApp($d) {
  30. $wxConfig = $this->config;
  31. $out_trade_no = $d['out_trade_no'];
  32. $total_fee = abs(floatval($d['total_fee'])) * 100;// 微信支付 单位为分
  33. $nonce_str = $this->getRandChar(32);
  34. $ip = $this->get_client_ip();
  35. if ($ip == '::1')
  36. $ip = '1.1.1.1';
  37. $data ["appid"] = $wxConfig["app_id"];
  38. $data ["body"] = $d['body'];
  39. $data ["mch_id"] = $wxConfig['mch_id'];
  40. $data ["nonce_str"] = $nonce_str;
  41. $data ["notify_url"] = $wxConfig["notify_url"];
  42. $data ["out_trade_no"] = $out_trade_no;
  43. $data ["spbill_create_ip"] = $ip;
  44. $data ["total_fee"] = $total_fee;
  45. $data ["trade_type"] = "APP";
  46. $data['time_expire'] = date('YmdHis', time() + 120);
  47. $s = $this->getSign($data);
  48. $data ["sign"] = $s;
  49. $xml = $this->arrayToXml($data);
  50. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  51. $response = $this->postXmlCurl($xml, $url);
  52. $re = $this->xmlstr_to_array($response);
  53. if ($re ['return_code'] == 'FAIL') {
  54. return $re['return_msg'];
  55. }
  56. // 二次签名
  57. $reapp = $this->getOrder($re['prepay_id']);
  58. return $reapp;
  59. }
  60. public function WxPayJs($d) {
  61. $wxConfig = $this->config;
  62. $out_trade_no = $d['out_trade_no'];
  63. $total_fee = abs(floatval($d['total_fee'])) * 100;// 微信支付 单位为分
  64. // $total_fee = 1;// 微信支付 单位为分
  65. $nonce_str = $this->getRandChar(32);
  66. $ip = $this->get_client_ip();
  67. if ($ip == '::1')
  68. $ip = '1.1.1.1';
  69. $trade_type = 'JSAPI';
  70. $post = [
  71. 'appid' =>$wxConfig["app_id"] ,
  72. 'body' =>$d['body'],
  73. 'mch_id' =>$wxConfig['mch_id'],
  74. 'nonce_str' =>$nonce_str,
  75. 'notify_url' =>$wxConfig["notify_url"],
  76. 'openid' =>$d['openid'],
  77. 'out_trade_no' =>$out_trade_no,
  78. 'spbill_create_ip' =>$ip,
  79. 'time_expire' => date('YmdHis', time() + 120),
  80. 'total_fee' => $total_fee,
  81. 'trade_type' =>$trade_type,
  82. ];
  83. $sign = $this->sign($post);
  84. $post_xml = '<xml>
  85. <appid>'.$wxConfig["app_id"].'</appid>
  86. <body>'.$d['body'].'</body>
  87. <mch_id>'.$wxConfig['mch_id'].'</mch_id>
  88. <nonce_str>'.$nonce_str.'</nonce_str>
  89. <notify_url>'.$wxConfig["notify_url"].'</notify_url>
  90. <openid>'.$d['openid'].'</openid>
  91. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  92. <spbill_create_ip>'.$ip.'</spbill_create_ip>
  93. <time_expire>'.$post["time_expire"].'</time_expire>
  94. <total_fee>'.$post["total_fee"].'</total_fee>
  95. <trade_type>'.$trade_type.'</trade_type>
  96. <sign>'.$sign.'</sign>
  97. </xml>';
  98. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  99. $xml = httpRequest($url,'POST',$post_xml);
  100. $array = $this->xml($xml);//全要大写1
  101. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS')
  102. {
  103. $time = (string)time();
  104. $tmp = [];
  105. $tmp['appId'] = $wxConfig["app_id"];
  106. $tmp['nonceStr'] = $nonce_str;
  107. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  108. $tmp['signType'] = 'MD5';
  109. $tmp['timeStamp'] = $time;
  110. //return ajaxReturn(callback(1,'签名成功',$tmp));
  111. $data['state'] = 200;
  112. $data['appid'] = $wxConfig["app_id"];
  113. $data['timeStamp'] = $time;
  114. $data['nonceStr'] = $nonce_str;
  115. $data['signType'] = 'MD5';
  116. $data['package'] = 'prepay_id='.$array['PREPAY_ID']; //同意下单接口返回的值 prepay_id
  117. $data['paySign'] = $this->sign($tmp);
  118. $data['out_trade_no'] = $out_trade_no;
  119. }
  120. else
  121. {
  122. $data['state'] = 0;
  123. $data['text'] = 'error';
  124. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  125. $data['RESULT_CODE'] = $array['RESULT_CODE'];
  126. }
  127. return $data;
  128. }
  129. /**
  130. * 微信App支付退款申请
  131. * @param array $d 业务参数 body out_trade_no total_fee
  132. * @param string $data ['out_trade_no'] 订单号 必填
  133. * @param string $data ['total_fee'] 订单金额 必填
  134. * @return $response 返回app所需字符串
  135. */
  136. public function WxPayRefund($d) {
  137. $wxConfig = $this->config;
  138. // $out_trade_no = $d['out_trade_no'];
  139. // $transaction_id = $d['trade_no'];
  140. // $total_fee = $d['total_amount'];// 微信支付 单位为分
  141. // $refund_reason = $d['refund_reason'];
  142. $nonce_str = $this->getRandChar(32);
  143. $data ["appid"] = $wxConfig["app_id"];
  144. $data ["mch_id"] = $wxConfig['mch_id'];
  145. $data ["nonce_str"] = $nonce_str;
  146. if ($wxConfig["refund_notify"]) {
  147. $data ["notify_url"] = $wxConfig["refund_notify"];
  148. }
  149. // $data ["out_trade_no"] = $out_trade_no;
  150. $data ["transaction_id"] = $d['transaction_id'];
  151. $data ["out_refund_no"] = $d['out_refund_no'];
  152. $data ["total_fee"] = $d['total_fee']; //1 微信总金额 单位为分
  153. $data ["refund_fee"] = $d['refund_fee']; //1 微信退款金额 单位为分
  154. $data ['refund_desc'] = $d['refund_desc'];
  155. $s = $this->getSign($data);
  156. $data ["sign"] = $s;
  157. $xml = $this->arrayToXml($data);
  158. $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
  159. $response = $this->postXmlCurl($xml, $url);
  160. $re = $this->xmlstr_to_array($response);
  161. if ($re ['return_code'] == 'FAIL') {
  162. return $re['return_msg'];
  163. }
  164. return $re;
  165. }
  166. /**
  167. * 微信退款通知
  168. * @return array 验证正确返回状态及订单数据
  169. */
  170. public function WxPayRefundNotifyCheck() {
  171. $postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
  172. if (!$postStr) {
  173. $postStr = file_get_contents("php://input");
  174. }
  175. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  176. if ($postObj === false) {
  177. error_log('parse xml error', 3, './wechat_errorlog.txt');
  178. }
  179. if ($postObj->return_code != 'SUCCESS') {
  180. error_log($postObj->return_msg, 3, './wechat_errorlog.txt');
  181. }
  182. $arr = (array)$postObj;
  183. file_put_contents('wx_refund_error_logs.txt', date('Y-m-d H:i:s').'退款数据1!'.json_encode($arr), FILE_APPEND);
  184. if($arr['return_code'] == 'SUCCESS') {
  185. $wxConfig = $this->config;
  186. //解密信息
  187. require_once("Plugins/WxPay/OpenSSLAES.php");
  188. $aes = new \OpenSSLAES(md5($wxConfig['key']));
  189. $decrypted = $aes->decrypt($arr['req_info']);
  190. $reqObj = simplexml_load_string($decrypted, 'SimpleXMLElement', LIBXML_NOCDATA);
  191. $arr['req_info'] = (array)$reqObj;
  192. file_put_contents('wx_refund_error_logs.txt', date('Y-m-d H:i:s').'退款数据2!'.json_encode($arr), FILE_APPEND);
  193. return array('status' => true, 'data' => $arr);
  194. }
  195. return array('status' => false);
  196. }
  197. /**
  198. * 微信签名验证
  199. * @param string $data 业务参数
  200. * @return array
  201. */
  202. public function WxPayNotifyCheck() {
  203. // $postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
  204. // if(!$postStr){
  205. $postStr = file_get_contents("php://input");
  206. // }
  207. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  208. if ($postObj === false) {
  209. error_log('parse xml error', 3, './wechat_errorlog.txt');
  210. }
  211. if ($postObj->return_code != 'SUCCESS') {
  212. error_log($postObj->return_msg, 3, './wechat_errorlog.txt');
  213. }
  214. $arr = (array)$postObj;
  215. unset($arr['sign']);
  216. if ($this->getSign($arr) == $postObj->sign) {
  217. return array('status' => true, 'data' => $arr);
  218. } else
  219. return array('status' => false);
  220. }
  221. /**
  222. * 以下为微信所需相关方法,请勿修改
  223. */
  224. // 执行第二次签名,才能返回给客户端使用
  225. public function getOrder($prepayId) {
  226. $data ["appid"] = $this->config ["app_id"];
  227. $data ["noncestr"] = $this->getRandChar(32);;
  228. $data ["package"] = "Sign=WXPay";
  229. $data ["partnerid"] = $this->config ['mch_id'];
  230. $data ["prepayid"] = $prepayId;
  231. $data ["timestamp"] = time();
  232. $s = $this->getSign($data);
  233. $data ["sign"] = $s;
  234. return $data;
  235. }
  236. //生成签名
  237. function getSign($Obj) {
  238. foreach ($Obj as $k => $v) {
  239. $Parameters [strtolower($k)] = $v;
  240. }
  241. // 签名步骤一:按字典序排序参数
  242. ksort($Parameters);
  243. $String = $this->formatBizQueryParaMap($Parameters, false);
  244. // echo "【string】 =".$String."</br>";
  245. // 签名步骤二:在string后加入KEY
  246. $String = $String . "&key=" . $this->config ['key'];
  247. // echo "<textarea style='width: 50%; height: 150px;'>$String</textarea> <br />";
  248. // 签名步骤三:MD5加密
  249. $result_ = strtoupper(md5($String));
  250. return $result_;
  251. }
  252. // 获取指定长度的随机字符串
  253. function getRandChar($length) {
  254. $str = null;
  255. $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  256. $max = strlen($strPol) - 1;
  257. for ($i = 0; $i < $length; $i++) {
  258. $str .= $strPol [rand(0, $max)]; // rand($min,$max)生成介于min和max两个数之间的一个随机整数
  259. }
  260. return $str;
  261. }
  262. // 数组转xml
  263. function arrayToXml($arr) {
  264. $xml = "<xml>";
  265. foreach ($arr as $key => $val) {
  266. if (is_numeric($val)) {
  267. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  268. } else
  269. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  270. }
  271. $xml .= "</xml>";
  272. return $xml;
  273. }
  274. // post https请求,CURLOPT_POSTFIELDS xml格式
  275. function postXmlCurl($xml, $url, $second = 30) {
  276. // 初始化curl
  277. $ch = curl_init();
  278. // 超时时间
  279. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  280. // 这里设置代理,如果有的话
  281. // curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  282. // curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  283. curl_setopt($ch, CURLOPT_URL, $url);
  284. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  285. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  286. // 设置header
  287. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  288. // 要求结果为字符串且输出到屏幕上
  289. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  290. // post提交方式
  291. curl_setopt($ch, CURLOPT_POST, TRUE);
  292. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  293. // 证书
  294. curl_setopt($ch, CURLOPT_SSLKEY, dirname(__FILE__) . '/cert/apiclient_key.pem');
  295. curl_setopt($ch, CURLOPT_SSLCERT, dirname(__FILE__) . '/cert/apiclient_cert.pem');
  296. // 运行curl
  297. $data = curl_exec($ch);
  298. // 返回结果
  299. if ($data) {
  300. curl_close($ch);
  301. return $data;
  302. } else {
  303. $error = curl_errno($ch);
  304. echo "curl出错,错误码:$error" . "<br>";
  305. echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
  306. curl_close($ch);
  307. return false;
  308. }
  309. }
  310. //获取当前服务器的IP
  311. function get_client_ip() {
  312. if ($_SERVER ['REMOTE_ADDR']) {
  313. $cip = $_SERVER ['REMOTE_ADDR'];
  314. } elseif (getenv("REMOTE_ADDR")) {
  315. $cip = getenv("REMOTE_ADDR");
  316. } elseif (getenv("HTTP_CLIENT_IP")) {
  317. $cip = getenv("HTTP_CLIENT_IP");
  318. } else {
  319. $cip = "unknown";
  320. }
  321. return $cip;
  322. }
  323. // 将数组转成uri字符串
  324. function formatBizQueryParaMap($paraMap, $urlencode) {
  325. $buff = "";
  326. ksort($paraMap);
  327. foreach ($paraMap as $k => $v) {
  328. if ($urlencode) {
  329. $v = urlencode($v);
  330. }
  331. $buff .= strtolower($k) . "=" . $v . "&";
  332. }
  333. if (strlen($buff) > 0) {
  334. $reqPar = substr($buff, 0, strlen($buff) - 1);
  335. }
  336. return $reqPar;
  337. }
  338. //xml转成数组
  339. function xmlstr_to_array($xmlstr) {
  340. $doc = new \DOMDocument ();
  341. $doc->loadXML($xmlstr);
  342. return $this->domnode_to_array($doc->documentElement);
  343. }
  344. //dom转成数组
  345. function domnode_to_array($node) {
  346. $output = array();
  347. switch ($node->nodeType) {
  348. case XML_CDATA_SECTION_NODE :
  349. case XML_TEXT_NODE :
  350. $output = trim($node->textContent);
  351. break;
  352. case XML_ELEMENT_NODE :
  353. for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
  354. $child = $node->childNodes->item($i);
  355. $v = $this->domnode_to_array($child);
  356. if (isset ($child->tagName)) {
  357. $t = $child->tagName;
  358. if (!isset ($output [$t])) {
  359. $output [$t] = array();
  360. }
  361. $output [$t] [] = $v;
  362. } elseif ($v) {
  363. $output = ( string )$v;
  364. }
  365. }
  366. if (is_array($output)) {
  367. if ($node->attributes->length) {
  368. $a = array();
  369. foreach ($node->attributes as $attrName => $attrNode) {
  370. $a [$attrName] = ( string )$attrNode->value;
  371. }
  372. $output ['@attributes'] = $a;
  373. }
  374. foreach ($output as $t => $v) {
  375. if (is_array($v) && count($v) == 1 && $t != '@attributes') {
  376. $output [$t] = $v [0];
  377. }
  378. }
  379. }
  380. break;
  381. }
  382. return $output;
  383. }
  384. //签名排序
  385. private function sign($data)
  386. {
  387. $stringA = '';
  388. foreach ($data as $key => $value) {
  389. if(!$value) continue;
  390. if($stringA) $stringA .= '&'.$key."=".$value;
  391. else $stringA = $key."=".$value;
  392. }
  393. $wx_key = $this->config['key'];
  394. $stringSignTemp = $stringA.'&key='.$wx_key;
  395. return strtoupper(md5($stringSignTemp));
  396. }
  397. //xml转换
  398. private function xml($xml)
  399. {
  400. $p = xml_parser_create();
  401. xml_parse_into_struct($p ,$xml ,$vals , $index);
  402. xml_parser_free($p);
  403. $data = [];
  404. foreach ($index as $key => $value) {
  405. if($key =='xml' || $key=='XML') continue;
  406. $tag = $vals[$value[0]]['tag'];
  407. $value = $vals[$value[0]]['value'];
  408. $data[$tag] = $value;
  409. }
  410. return $data;
  411. }
  412. }