WatchPay.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\common\server;
  3. use think\facade\Log;
  4. /**
  5. * 微信支付
  6. *
  7. * @author wj
  8. * @date 2022-12-02
  9. */
  10. /*
  11. 统一下订单回执
  12. <xml>
  13.   <return_code><![CDATA[SUCCESS]]></return_code>
  14.   <return_msg><![CDATA[OK]]></return_msg>
  15.   <result_code><![CDATA[SUCCESS]]></result_code>
  16.   <mch_id><![CDATA[10000100]]></mch_id>
  17.   <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
  18.   <sub_mch_id><![CDATA[375907253]]></sub_mch_id>
  19.   <device_info><![CDATA[123001]]></device_info>
  20.   <nonce_str><![CDATA[mb8t557HOLqv0soV]]></nonce_str>
  21.   <sign><![CDATA[A530CC111F66A1A6B040D4923E2D2726]]></sign>
  22.   <prepay_id><![CDATA[wx04025233661291269e5f7daa32c5aa0000]]></prepay_id>
  23.   <trade_type><![CDATA[APP]]></trade_type>
  24. </xml>
  25. */
  26. class WatchPay
  27. {
  28. //统一下单api路径
  29. private $unifiedorderurl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  30. //水猫工匠商户信息
  31. /*private $config = [
  32. 'appid' => "wxcacf6eb6e7478e29",
  33. 'appidApp' => "wxe554be4a48e92d5a",
  34. 'MCHID' => '1616316171',
  35. 'KEY' => "WaterCat20211001ShuiMaoGongJiang",
  36. ];*/
  37. /**
  38. * appid’ =>"wxea0b365a48661060"
  39. * MCHID =>"1638159037"
  40. * KEY=>"yOT6Rn0LfhLZoAtpDPkqkIQHBfqfNJJ4"
  41. */
  42. /**
  43. * 易益康养
  44. *
  45. * @var array
  46. * @author wj
  47. * @date 2023-02-18
  48. */
  49. private $config = [
  50. 'appid' => "wxcacf6eb6e7478e29", //微信
  51. 'appidApp' => "wxea0b365a48661060", //app
  52. 'MCHID' => '1638159037',
  53. 'KEY' => "yOT6Rn0LfhLZoAtpDPkqkIQHBfqfNJJ4",
  54. ];
  55. public function setConfig($config)
  56. {
  57. foreach ($this->$config as $key => $value) {
  58. if (empty($value) != $config[$key]) {
  59. $this->config[$key] = $config[$key];
  60. }
  61. }
  62. }
  63. public function getConfig($field)
  64. {
  65. if (isset($this->config[$field])) {
  66. return $this->config[$field];
  67. }
  68. return false;
  69. }
  70. /**
  71. * 获取订单信息
  72. *
  73. * @return void
  74. * @author wj
  75. * @date 2022-12-02
  76. */
  77. public function getorderinfo($orderInfo)
  78. {
  79. $config = $this->config;
  80. $mchid = $config['MCHID'];
  81. $key = $config['KEY'];
  82. $body = $orderInfo['body'];
  83. $total_fee = $orderInfo['total_fee'];
  84. $notify_url = $orderInfo['notify_url'];
  85. //$appid = $config['appid'];
  86. $ordrNo = $orderInfo['orderNo'];
  87. $trade_type = $orderInfo['trade_type'];
  88. $appid = $orderInfo['appid'];
  89. $data = [
  90. "appid" => $appid, //服务商应用ID
  91. "mch_id" => $mchid, //服务商应用ID
  92. "nonce_str" => md5($mchid . time()),
  93. "body" => $body,
  94. "out_trade_no" => $ordrNo,
  95. "notify_url" => $notify_url,
  96. "total_fee" => $total_fee,
  97. "currency" => "CNY",
  98. "spbill_create_ip" => '8.8.8.8',
  99. 'trade_type' => $trade_type,
  100. ];
  101. if (isset($orderInfo['openid'])) {
  102. $openid = $orderInfo['openid'];
  103. $data['openid'] = $openid;
  104. }
  105. ksort($data);
  106. $sign_str = $this->ToUrlParams($data);
  107. $sign_str = $sign_str . "&key=" . $key;
  108. $data['sign'] = strtoupper(md5($sign_str));
  109. return $data;
  110. }
  111. /**
  112. * 创建订单 统一下单
  113. *
  114. * @param [type] $orderinfo
  115. * @return void
  116. * @author wj
  117. * @date 2022-12-02
  118. */
  119. public function crateOrder($orderinfo)
  120. {
  121. $data = $this->getorderinfo($orderinfo);
  122. Log::info($data);
  123. $xml = arrtoxml($data, null, 'xml');
  124. //$xml = $this->arrayToXml($data);
  125. $url = $this->unifiedorderurl;
  126. Log::info($url);
  127. Log::info($xml);
  128. $resultxml = $this->postXmlCurl($xml, $url, true);
  129. $result = $this->getpayback($resultxml);
  130. return $result;
  131. }
  132. /**
  133. * 解析并记录返回的xml
  134. *
  135. * @param [type] $xml
  136. * @return void
  137. * @author wj
  138. * @date 2022-12-02
  139. */
  140. public function getpayback($xml)
  141. {
  142. Log::info($xml);
  143. $result = xmltoarr($xml);
  144. Log::info($result);
  145. return $result;
  146. }
  147. /*
  148. * 用于微信支付转换认证的信息用的
  149. * by:leoyi
  150. * date:2018-4-8
  151. */
  152. public function ToUrlParams($data)
  153. {
  154. $buff = "";
  155. foreach ($data as $k => $v) {
  156. if ($k != "sign" && $v != "" && !is_array($v)) {
  157. $buff .= $k . "=" . $v . "&";
  158. }
  159. }
  160. $buff = trim($buff, "&");
  161. return $buff;
  162. }
  163. /**
  164. * 用户post方法请求xml信息用的
  165. * @author write by leoyi 2018-04-8
  166. */
  167. public function postXmlCurl($xml, $url, $useCert = false, $second = 10)
  168. {
  169. $ch = curl_init();
  170. //设置超时
  171. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  172. curl_setopt($ch, CURLOPT_URL, $url);
  173. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  174. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验2
  175. //设置header
  176. curl_setopt($ch, CURLOPT_HEADER, false);
  177. //要求结果为字符串且输出到屏幕上
  178. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  179. curl_setopt($ch, CURLOPT_POST, true);
  180. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  181. //运行curl
  182. $data = curl_exec($ch);
  183. //返回结果
  184. if ($data) {
  185. curl_close($ch);
  186. return $data;
  187. } else {
  188. $error = curl_errno($ch);
  189. curl_close($ch);
  190. return $error;
  191. }
  192. }
  193. /**
  194. * 获取签名
  195. *
  196. * @return void
  197. * @author wj
  198. * @date 2022-12-02
  199. */
  200. public function getsign($data)
  201. {
  202. ksort($data);
  203. $sign_str = $this->ToUrlParams($data);
  204. $sign_str = $sign_str . "&key=" . $this->config['KEY'];
  205. $sign = strtoupper(md5($sign_str));
  206. return $sign;
  207. }
  208. public function arrayToXml($arr)
  209. {
  210. $xml = "<xml>";
  211. foreach ($arr as $key => $val) {
  212. if (is_numeric($val)) {
  213. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  214. } else {
  215. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  216. }
  217. }
  218. $xml .= "</xml>";
  219. return $xml;
  220. }
  221. }