PHP 微信小程序退费

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/php_lzr/article/details/85089467
流程非常简单 没有什么坑 基本官方文档写的非常清楚了
<?php
require_once "wxxcxpay.conf.php";
class wxxcxrefundExt {
    //退费
    public static function wxrefundapi($outtradeno,$totalfee,$refundfee){
        //通过微信api进行退款流程
        $parma = array(
            'appid'=> ,//appid
            'mch_id'=> ,//商户id
            'nonce_str'=> self::nonce_str(),
            'out_trade_no'=> $outtradeno,//商户退款单号
            'out_refund_no'=>'refund' . date('YmdHis') . rand(1000, 9999),
            'total_fee'=> $totalfee*100,//订单金额
            'refund_fee'=> $refundfee*100//退款金额
//            'op_user_id' => opUserId,//商户号之前文档需要
        );
        $parma['sign'] = self::sign($parma);
        $xmldata = self::arrayToXml($parma);
        $xmlresult = self::postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');
        $result = self::xmlToArray($xmlresult);
        return $result;
    }
//需要使用证书的请求
    public static function postXmlSSLCurl($xml,$url,$second=30)
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置header
        curl_setopt($ch,CURLOPT_HEADER,FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        //设置证书
        //使用证书:cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT, ROOT.XCX_SSLCERT_PATH);//证书
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, ROOT.XCX_SSLKEY_PATH);//证书
        //post提交方式
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);

            echo "curl出错,错误码:$error"."<br>";
            curl_close($ch);
            return $data;
        }
    }

    //随机32位字符串
    private static function nonce_str(){
        $result = '';
        $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
        for ($i=0;$i<32;$i++){
            $result .= $str[rand(0,48)];
        }
        return $result;
    }
    //签名
    public static function sign($data){
        ksort($data);
        $stringA = '';
        foreach ($data as $key=>$value){
            if(!$value) continue;
            if($stringA)
                $stringA .= '&'.$key."=".$value;
            else $stringA = $key."=".$value;
        }
        $wx_key = XCXKEY;//申请支付后有给予一个商户账号和密码,登陆后自己设置的key
        $stringSignTemp = $stringA.'&key='.$wx_key;
        return strtoupper(md5($stringSignTemp));
    }




    //数组转换成xml
    private static function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }
    //xml转换成数组
    public static function xmlToArray($xml) {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring), true);

        return $val;
    }
}

猜你喜欢

转载自blog.csdn.net/php_lzr/article/details/85089467
今日推荐