123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\common\server;
- use think\facade\Log;
- /**
- * 微信支付
- *
- * @author wj
- * @date 2022-12-02
- */
- /*
- 统一下订单回执
- <xml>
- <return_code><![CDATA[SUCCESS]]></return_code>
- <return_msg><![CDATA[OK]]></return_msg>
- <result_code><![CDATA[SUCCESS]]></result_code>
- <mch_id><![CDATA[10000100]]></mch_id>
- <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
- <sub_mch_id><![CDATA[375907253]]></sub_mch_id>
- <device_info><![CDATA[123001]]></device_info>
- <nonce_str><![CDATA[mb8t557HOLqv0soV]]></nonce_str>
- <sign><![CDATA[A530CC111F66A1A6B040D4923E2D2726]]></sign>
- <prepay_id><![CDATA[wx04025233661291269e5f7daa32c5aa0000]]></prepay_id>
- <trade_type><![CDATA[APP]]></trade_type>
- </xml>
- */
- class WatchPay
- {
- //统一下单api路径
- private $unifiedorderurl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
- //水猫工匠商户信息
- private $config = [
- 'appid' => "wxcacf6eb6e7478e29",
- 'appidApp' => "wxe554be4a48e92d5a",
- 'MCHID' => '1616316171',
- 'KEY' => "WaterCat20211001ShuiMaoGongJiang",
- ];
- public function setConfig($config)
- {
- foreach ($this->$config as $key => $value) {
- if (empty($value) != $config[$key]) {
- $this->config[$key] = $config[$key];
- }
- }
- }
- public function getConfig($field)
- {
- if (isset($this->config[$field])) {
- return $this->config[$field];
- }
- return false;
- }
- /**
- * 获取订单信息
- *
- * @return void
- * @author wj
- * @date 2022-12-02
- */
- public function getorderinfo($orderInfo)
- {
- $config = $this->config;
- $mchid = $config['MCHID'];
- $key = $config['KEY'];
- $body = $orderInfo['body'];
- $total_fee = $orderInfo['total_fee'];
- $notify_url = $orderInfo['notify_url'];
- //$appid = $config['appid'];
- $ordrNo = $orderInfo['orderNo'];
- $trade_type = $orderInfo['trade_type'];
- $appid = $orderInfo['appid'];
- $data = [
- "appid" => $appid, //服务商应用ID
- "mch_id" => $mchid, //服务商应用ID
- "nonce_str" => md5($mchid . time()),
- "body" => $body,
- "out_trade_no" => $ordrNo,
- "notify_url" => $notify_url,
- "total_fee" => $total_fee,
- "currency" => "CNY",
- "spbill_create_ip" => '8.8.8.8',
- 'trade_type' => $trade_type,
- ];
- if (isset($orderInfo['openid'])) {
- $openid = $orderInfo['openid'];
- $data['openid'] = $openid;
- }
- ksort($data);
- $sign_str = $this->ToUrlParams($data);
- $sign_str = $sign_str . "&key=" . $key;
- $data['sign'] = strtoupper(md5($sign_str));
- return $data;
- }
- /**
- * 创建订单 统一下单
- *
- * @param [type] $orderinfo
- * @return void
- * @author wj
- * @date 2022-12-02
- */
- public function crateOrder($orderinfo)
- {
- $data = $this->getorderinfo($orderinfo);
- Log::info($data);
- $xml = arrtoxml($data, null, 'xml');
- //$xml = $this->arrayToXml($data);
- $url = $this->unifiedorderurl;
- Log::info($url);
- Log::info($xml);
- $resultxml = $this->postXmlCurl($xml, $url, true);
- $result = $this->getpayback($resultxml);
- return $result;
- }
- /**
- * 解析并记录返回的xml
- *
- * @param [type] $xml
- * @return void
- * @author wj
- * @date 2022-12-02
- */
- public function getpayback($xml)
- {
- Log::info($xml);
- $result = xmltoarr($xml);
- Log::info($result);
- return $result;
- }
- /*
- * 用于微信支付转换认证的信息用的
- * by:leoyi
- * date:2018-4-8
- */
- public function ToUrlParams($data)
- {
- $buff = "";
- foreach ($data as $k => $v) {
- if ($k != "sign" && $v != "" && !is_array($v)) {
- $buff .= $k . "=" . $v . "&";
- }
- }
- $buff = trim($buff, "&");
- return $buff;
- }
- /**
- * 用户post方法请求xml信息用的
- * @author write by leoyi 2018-04-8
- */
- public function postXmlCurl($xml, $url, $useCert = false, $second = 10)
- {
- $ch = curl_init();
- //设置超时
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验2
- //设置header
- curl_setopt($ch, CURLOPT_HEADER, false);
- //要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
- //运行curl
- $data = curl_exec($ch);
- //返回结果
- if ($data) {
- curl_close($ch);
- return $data;
- } else {
- $error = curl_errno($ch);
- curl_close($ch);
- return $error;
- }
- }
- /**
- * 获取签名
- *
- * @return void
- * @author wj
- * @date 2022-12-02
- */
- public function getsign($data)
- {
- ksort($data);
- $sign_str = $this->ToUrlParams($data);
- $sign_str = $sign_str . "&key=" . $this->config['KEY'];
- $sign = strtoupper(md5($sign_str));
- return $sign;
- }
- public function arrayToXml($arr)
- {
- $xml = "<xml>";
- foreach ($arr as $key => $val) {
- if (is_numeric($val)) {
- $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
- } else {
- $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
- }
- }
- $xml .= "</xml>";
- return $xml;
- }
- }
|