thinkphp5 微信退款
<?php
namespace app\admin\controller;
use think\facade\Env;
use think\Request;
class Wxpay
{
private $order_number = '';
private $order_money = '';
private $money = '';
private $answer = '';
private $apiclient_cert = Env::get('ROOT_PATH')."public/wxp/apiclient_cert.pem";
private $apiclient_key = Env::get('ROOT_PATH')."public/wxp/apiclient_key.pem";
private $appid = '';
private $mch_id = '';
private $key = '';
function __construct($order_number,$order_money,$money,$answer='')
{
$this->order_number = $order_number;
$this->order_money = $order_money;
$this->money = $money;
$this->answer= $answer;
}
public function getWechat()
{
$msg = $this->refund();
if ($msg['return_code'] == 'SUCCESS' && $msg['result_code'] == 'SUCCESS'){
}
}
private function refund()
{
$unified = [
'appid' => $this->appid,
'mch_id' => $this->mch_id,
'nonce_str' => uniqid(),
'total_fee' => intval($this->order_money * 100) ,
'refund_fee' => intval($this->money * 100) ,
'sign_type' => 'MD5',
'out_trade_no' => $this->order_number,
'out_refund_no' => time(),
'refund_desc' => $this->answer,
];
$unified['sign'] = $this->MakeSign($unified);
$xmlStr = $this->ToXml($unified);
$responseXml = $this->curlPost($this->url,$xmlStr);
$unifiedArray = $this->FromXml($responseXml);
return $unifiedArray;
}
private function MakeSign($data = [])
{
if(empty($data)) $data = $this->values;
ksort($data);
$string = $this->ToUrlParams($data);
$string = $string . "&key=".$this->key;
$string = md5($string);
$result = strtoupper($string);
return $result;
}
private function ToUrlParams($data)
{
$buff = "";
foreach ($data as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
private function ToXml()
{
if(!is_array($this->values) || count($this->values) <= 0)
{
throw new \Exception("数组数据异常!");
}
$xml = "<xml>";
foreach ($this->values as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
private function FromXml($xml)
{
if(!$xml){
throw new \Exception("xml数据异常!");
}
libxml_disable_entity_loader(true);
$this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $this->values;
}
private function curlPost($url = '', $postData = '', $options = array()) {
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $this->apiclient_cert);
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $this->apiclient_key);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error" . "<br>";
curl_close($ch);
return false;
}
}
}