*/ class WatchPay { //统一下单api路径 private $unifiedorderurl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //水猫工匠商户信息 /*private $config = [ 'appid' => "wxcacf6eb6e7478e29", 'appidApp' => "wxe554be4a48e92d5a", 'MCHID' => '1616316171', 'KEY' => "WaterCat20211001ShuiMaoGongJiang", ];*/ /** * 易益康养商户信息 * * @var array * @author wj * @date 2023-02-18 */ private $config = [ 'appid' => '', 'appidApp' => "wxea0b365a48661060", 'MCHID' => "1638159037", 'KEY' => "yOT6Rn0LfhLZoAtpDPkqkIQHBfqfNJJ4", ]; 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 = ""; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else { $xml .= "<" . $key . ">"; } } $xml .= ""; return $xml; } }