PHP 微信申请退款

目录

  • 微信退款
    • 申请退款
    • 对要发送到微信统一下单接口的数据进行签名
    • 排序并格式化
    • 数组转字符串方法
    • 需要使用证书的请求
    • XML数据转换为数组

微信退款

申请退款

public function getWeChatRefund()
    {
        $baseConfigModel = new \app\admin\model\Baseconfig();
        $app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');
        $mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');
        $payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');
        //通过微信api进行退款流程
        $out_trade_no = 'YS202307089954985798100';//原商户订单号
        $parma = array(
            'appid'=> $app_id,  //APPID
            'mch_id'=> $mch_id, //商户id
            'nonce_str'=> $this->generate_password(30),//随机字符串
            'out_refund_no'=> $this->order_number('R'),//退款单号
            'out_trade_no'=> $out_trade_no,//原支付交易对应的商户订单号
            'total_fee'=> 1, //订单总金额
            'refund_fee'=> 1, //退款金额
            'op_user_id' => $mch_id, //商户id
        );
        $parma['sign'] = $this->getSign1($parma,$payment_key);
        $xmldata = $this->arrayToXml($parma);
        $xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');
        $result = $this->FromXml($xmlresult);
        halt($result);
        return $result;
    }

对要发送到微信统一下单接口的数据进行签名

    protected function getSign1($Obj,$payment_key){
        foreach ($Obj as $k => $v){
            $Parameters[$k] = $v;
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //签名步骤二:在string后加入KEY
        $String = $String."&key=".$payment_key;
        //签名步骤三:MD5加密
        $String = md5($String);
        //签名步骤四:所有字符转为大写
        $result_ = strtoupper($String);
        return $result_;
    }

排序并格式化

    protected function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

数组转字符串方法

protected function arrayToXml($arr){
        $xml = "";
        foreach ($arr as $key=>$val)
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val.".$key.">";
            }else{
                $xml.="<".$key.">.$val."]]>.$key.">";
            }
        }
        $xml.="";
        return $xml;
    }

需要使用证书的请求

function postXmlSSLCurl($xml,$url,$second=30)
    {
        $apiclient_cert = '../public/apiclient_cert';
        $payment_key = '../public/payment_key';
        $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, $apiclient_cert);
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, $payment_key);
        //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"."
"
; curl_close($ch); return false; } }

XML数据转换为数组

public function FromXml($xml)
    {
        if (!$xml) {
            echo "xml数据异常!";
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }

你可能感兴趣的:(php,微信,android)