您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168939

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

微信小程序退款

直接照搬代码就可以用

<?php
 
namespace app\api\controller; 
class Wxrefund
{
 	private $appid = '';
 	private $mch_id = '';
 	private $key = '';
 	private $apiclient_cert = './wx/apiclient_cert.pem';
 	private $apiclient_key = './wx/apiclient_key.pem';
 	
	      
    public function refund()
    {
    	$data = request()->param();
        $order_sn = 'sadsadsa23213';
        $refund_fee = $data['total_fee'];
 
        if ($refund_fee){
            $out_trade_no = 'trade'.time();;
            //$out_refund_no 商户退款单号 自定义而已
            $out_refund_no = 'refund'.time();
 
            //统一下单退款参数构造
            $unifiedorder = array(
                'appid' =>  $this->appid,
                'mch_id' => $this->mch_id,
                'nonce_str' => self::getNonceStr(),
                'out_trade_no' => $out_trade_no, //商户订单号	商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*@ ,且在同一个商户号下唯一。
                'out_refund_no' => $out_refund_no, //商户退款单号	商户系统内部的退款单号,商户系统内部唯一,只能是数字、大小写字母_-|*@ ,同一退款单号多次请求只退一笔。
                'total_fee' => $total_fee, //订单金额
                'refund_fee' => intval(floatval($refund_fee)), //退款金额	
            );
//          return self::getNonceStr().'---$out_trade_no---'.$out_trade_no.'---$out_refund_no---'.$out_refund_no.'---$total_fee---'.$total_fee.'---$refund_fee---'.$refund_fee;
            $unifiedorder['sign'] = self::makeSign($unifiedorder);
            return $unifiedorder['sign'];
            //请求数据
            $xmldata = self::array2xml($unifiedorder);
         
            $opUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund";
            $res = self::curl_post_ssl_refund($opUrl, $xmldata);
            if (!$res) {
                   return  json_encode(array('status' => $status, 'result' => 'fail', 'errmsg' => "Can't connect the server")); 
            }
            $content = self::xml2array($res);
            if (strval($content['result_code']) ==  "FAIL" ) {
            
           return  json_encode(array("status" => $status, "result" => 'fail', "errmsg" => strval($content["err_code_des"]))) ;
         
            }
            if (strval($content['return_code']) ==  "FAIL") {
           return  json_encode(array("status" => $status, "result" => "fail", "errmsg" => strval($content['return_msg']))) ;
                
            }
            return "退款成功!";
        }else{
            return "不符合退款订单!";
        }
    }
    
    // -用到的函数- 
 
    protected function curl_post_ssl_refund($url, $vars, $second=30,$aHeader=array())
    {
        $ch = curl_init(); 
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     
         /*
         这里设置代理,如果有的话
         curl_setopt($ch,CURLOPT_PROXY, "10.206.30.98");
       	 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);
        //TODO 以下两种方式需选择一种 
        //默认格式为PEM,可以注释 
        curl_setopt($ch,CURLOPT_SSLCERT,$this->apiclient_cert);
        //默认格式为PEM,可以注释 
        curl_setopt($ch,CURLOPT_SSLKEY,$this->apiclient_key);  
        if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
 
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            //echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }
   
 
/**********下方代码可以放到公共函数中**********/
    /*
     *
     * 产生随机字符串,不长于32* @param int $length
     * @return 产生的随机字符串
     */
    protected function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
 
    /*
     * 生成签名
     * @return 签名
     */
    protected function makeSign($data)
    {
        //获取微信支付秘钥
        $key =  $this->key ;
        // 去空
        $data = array_filter($data);
        //签名步骤一:按字典序排序参数
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        //签名步骤二:在string后加入KEY
        //$config=$this->config;
        $string_sign_temp = $string_a . "&key=" . $key;
        //签名步骤三:MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四:所有字符转为大写
        $result = strtoupper($sign);
        return $result;
    }
 	
 	 //将一个数组转换为 XML 结构的字符串
 
    protected function array2xml($arr, $level = 1)
    {
        $s = $level == 1 ? "<xml>" : "";
        foreach ($arr as $tagname => $value) {
            if (is_numeric($tagname)) {
                $tagname = $value['TagName'];
                unset($value["TagName"]);
            }
            if (!is_array($value)) {
                $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : "") . $value . (!is_numeric($value) ? ']]>' : "") . "</{$tagname}>";
            } else {
                $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1) . "</{$tagname}>";
            }
        }
        $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
        return $level == 1 ? $s . "</xml>" : $s;
    }
 
    /*
     * 将xml转为array
     * @param  string $xml xml字符串
     * @return array    转换得到的数组
     */
    protected function xml2array($xml)
    {
      
        libxml_disable_entity_loader(true);
        $result = json_decode(json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA)), true);
        return $result;
    }
 
} 
 

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进