thinkphp5 微信退款

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"; // 退款证书cert
    private $apiclient_key = Env::get('ROOT_PATH')."public/wxp/apiclient_key.pem"; // 退款证书key
	private $appid = ''; // 微信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'){
    
    
			
		}
	}
	
	/**
	* 退款请求
    * @return array $unifiedArray 微信返回信息
	*/
    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', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
            'out_trade_no' => $this->order_number, //商户订单号
            'out_refund_no' => time(), //商户退款单号(自己设置这里为了方便用了时间戳)
            'refund_desc' => $this->answer, //退款原因(选填
		];
		$unified['sign'] = $this->MakeSign($unified);// 生成签名
		$xmlStr = $this->ToXml($unified); // 转为xml
		$responseXml  = $this->curlPost($this->url,$xmlStr); // 请求微信接口
		$unifiedArray = $this->FromXml($responseXml); // 解析微信返回数据
		return $unifiedArray;
    }

    /**
     * 生成签名
     * @return string 签名
     */
    private function MakeSign($data = [])
    {
    
    
        //签名步骤一:按字典序排序参数
        if(empty($data)) $data = $this->values;
        ksort($data);
        $string = $this->ToUrlParams($data);
        //签名步骤二:在string后加入KEY
        $string = $string . "&key=".$this->key;
        //签名步骤三:MD5加密或者HMAC-SHA256
        $string = md5($string);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }

    /**
     * 格式化参数格式化成url参数
     * @return string $buff;
     */
    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;
    }

    /**
     * 输出xml字符
     * @throws Exception
    **/
    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; 
    }

    /**
     * 将xml转为array
     * @param string $xml
     * @throws Exception
     */
    private function FromXml($xml)
    {
    
       
        if(!$xml){
    
    
            throw new \Exception("xml数据异常!");
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $this->values;
    }

    /**
     * curl 请求
     * @param string $url 请求路径
     * @param array $postData 请求参数
     * @throws Exception
     */
    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); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
    
    
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //第一种方法,cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLCERT, $this->apiclient_cert);//绝对路径
        //默认格式为PEM,可以注释
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLKEY, $this->apiclient_key);//绝对路径
        //第二种方式,两个文件合成一个.pem文件
        // curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
        $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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39393201/article/details/108012124