首页 > 代码库 > 微信支付退款

微信支付退款

微信退款感觉比支付宝要容易一点

官网api:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1

直接上代码:

    public function refound(){
        $ref= md5("appid=1233213&mch_id=1233213&nonce_str=2194kfhaafaf384&op_user_id=1461000002"
            . "&out_refund_no=201704181551358393&out_trade_no=201704181551358393&refund_fee=1&total_fee=1"
            . "&key=yudsd7rfer7dsadh0UfdoReraRdsad4d");//sign加密MD5
        $refund=array(
            ‘appid‘=>‘12323123‘,
            ‘mch_id‘=>‘123213213‘,//商户号
            ‘nonce_str‘=>‘2194kfhaafaf384‘,//随机字符串
            ‘op_user_id‘=>‘1461000002‘,
            ‘out_refund_no‘=>‘201704181551358393‘,
            ‘out_trade_no‘=>‘201704181551358393‘,
            ‘refund_fee‘=>‘1‘,
            ‘total_fee‘=>‘1‘,
            ‘sign‘=>$ref
        );
        $url="https://api.mch.weixin.qq.com/secapi/pay/refund";;//微信退款地址,post请求
        $xml=ToXml($refund);
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);//证书检查
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘pem‘);
        curl_setopt($ch,CURLOPT_SSLCERT,‘plugins/payment/weixin/cert/apiclient_cert.pem‘);
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘pem‘);
        curl_setopt($ch,CURLOPT_SSLKEY,‘plugins/payment/weixin/cert/apiclient_key.pem‘);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data=curl_exec($ch);
        if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新
            curl_close($ch);
            dump($data);
        }else{
            $error=curl_errno($ch);
            echo "curl出错,错误代码:$error"."<br/>";
            echo "<a href=http://www.mamicode.com/‘http://curl.haxx.se/libcurl/c/libcurs.html‘>;错误原因查询
"; curl_close($ch); echo false; } }

function ToXml($arr)
{
if(!is_array($arr)
|| count($arr) <= 0)
{
throw new WxPayException("数组数据异常!");
}

$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;
}
 

最主要证书的添加,基本上就可以走通的

 

微信支付退款