WatchPay.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\api\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. public function setConfig($config)
  38. {
  39. foreach ($this->$config as $key => $value) {
  40. if (empty($value) != $config[$key]) {
  41. $this->config[$key] = $config[$key];
  42. }
  43. }
  44. }
  45. public function getConfig($field)
  46. {
  47. if (isset($this->config[$field])) {
  48. return $this->config[$field];
  49. }
  50. return false;
  51. }
  52. /**
  53. * 获取订单信息
  54. *
  55. * @return void
  56. * @author wj
  57. * @date 2022-12-02
  58. */
  59. public function getorderinfo($orderInfo)
  60. {
  61. $config = $this->config;
  62. $mchid = $config['MCHID'];
  63. $key = $config['KEY'];
  64. $body = $orderInfo['body'];
  65. $total_fee = $orderInfo['total_fee'];
  66. $notify_url = $orderInfo['notify_url'];
  67. //$appid = $config['appid'];
  68. $ordrNo = $orderInfo['orderNo'];
  69. $trade_type = $orderInfo['trade_type'];
  70. $appid = $orderInfo['appid'];
  71. $data = [
  72. "appid" => $appid, //服务商应用ID
  73. "mch_id" => $mchid, //服务商应用ID
  74. "nonce_str" => md5($mchid . time()),
  75. "body" => $body,
  76. "out_trade_no" => $ordrNo,
  77. "notify_url" => $notify_url,
  78. "total_fee" => $total_fee,
  79. "currency" => "CNY",
  80. "spbill_create_ip" => '8.8.8.8',
  81. 'trade_type' => $trade_type,
  82. ];
  83. if (isset($orderInfo['openid'])) {
  84. $openid = $orderInfo['openid'];
  85. $data['openid'] = $openid;
  86. }
  87. ksort($data);
  88. $sign_str = $this->ToUrlParams($data);
  89. $sign_str = $sign_str . "&key=" . $key;
  90. $data['sign'] = strtoupper(md5($sign_str));
  91. return $data;
  92. }
  93. /**
  94. * 创建订单 统一下单
  95. *
  96. * @param [type] $orderinfo
  97. * @return void
  98. * @author wj
  99. * @date 2022-12-02
  100. */
  101. public function crateOrder($orderinfo)
  102. {
  103. $data = $this->getorderinfo($orderinfo);
  104. Log::info($data);
  105. $xml = arrtoxml($data, null, 'xml');
  106. //$xml = $this->arrayToXml($data);
  107. $url = $this->unifiedorderurl;
  108. Log::info($url);
  109. Log::info($xml);
  110. $resultxml = $this->postXmlCurl($xml, $url, true);
  111. $result = $this->getpayback($resultxml);
  112. return $result;
  113. }
  114. /**
  115. * 解析并记录返回的xml
  116. *
  117. * @param [type] $xml
  118. * @return void
  119. * @author wj
  120. * @date 2022-12-02
  121. */
  122. public function getpayback($xml)
  123. {
  124. Log::info($xml);
  125. $result = xmltoarr($xml);
  126. Log::info($result);
  127. return $result;
  128. }
  129. /*
  130. * 用于微信支付转换认证的信息用的
  131. * by:leoyi
  132. * date:2018-4-8
  133. */
  134. public function ToUrlParams($data)
  135. {
  136. $buff = "";
  137. foreach ($data as $k => $v) {
  138. if ($k != "sign" && $v != "" && !is_array($v)) {
  139. $buff .= $k . "=" . $v . "&";
  140. }
  141. }
  142. $buff = trim($buff, "&");
  143. return $buff;
  144. }
  145. /**
  146. * 用户post方法请求xml信息用的
  147. * @author write by leoyi 2018-04-8
  148. */
  149. public function postXmlCurl($xml, $url, $useCert = false, $second = 10)
  150. {
  151. $ch = curl_init();
  152. //设置超时
  153. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  154. curl_setopt($ch, CURLOPT_URL, $url);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  156. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验2
  157. //设置header
  158. curl_setopt($ch, CURLOPT_HEADER, false);
  159. //要求结果为字符串且输出到屏幕上
  160. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  161. curl_setopt($ch, CURLOPT_POST, true);
  162. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  163. //运行curl
  164. $data = curl_exec($ch);
  165. //返回结果
  166. if ($data) {
  167. curl_close($ch);
  168. return $data;
  169. } else {
  170. $error = curl_errno($ch);
  171. curl_close($ch);
  172. return $error;
  173. }
  174. }
  175. /**
  176. * 获取签名
  177. *
  178. * @return void
  179. * @author wj
  180. * @date 2022-12-02
  181. */
  182. public function getsign($data)
  183. {
  184. ksort($data);
  185. $sign_str = $this->ToUrlParams($data);
  186. $sign_str = $sign_str . "&key=" . $this->config['KEY'];
  187. $sign = strtoupper(md5($sign_str));
  188. return $sign;
  189. }
  190. public function arrayToXml($arr)
  191. {
  192. $xml = "<xml>";
  193. foreach ($arr as $key => $val) {
  194. if (is_numeric($val)) {
  195. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  196. } else {
  197. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  198. }
  199. }
  200. $xml .= "</xml>";
  201. return $xml;
  202. }
  203. }