php知识总结

微信退款

2020-04-26  本文已影响0人  响呼雷
<?php
namespace wechatpay;

class WechatRefund extends WechatPay
{

//    protected $SSLCERT_PATH = '/cert/apiclient_cert.pem';//证书路径
//
//    protected $SSLKEY_PATH = '/cert/apiclient_key.pem';//证书路径

    protected $outRefundNo; //商户退款单号

    protected $outTradeNo;  //商户订单号(退款时:商户订单号和微信流水号二选一,32位)

    protected $transaction_id;  //微信流水号(退款时:商户订单号和微信流水号二选一,32位)

    protected $refundFee;   //退款金额

    protected $apiclient_key;

    protected $apiclient_cert;

    public function __construct($openid, $transaction_id, $outRefundNo, $totalFee, $refundFee)
    {
        $this->openid = $openid;
        $this->transaction_id = $transaction_id;
        $this->outRefundNo = $outRefundNo;
        $this->totalFee = $totalFee;
        $this->refundFee = $refundFee;
        $this->apiclient_cert = ROOT_PATH . 'public' . DS . $this->SSLCERT_PATH;
        $this->apiclient_key = ROOT_PATH . 'public' . DS . $this->SSLKEY_PATH;
    }

    public function refund()
    {
        echo $this->apiclient_key;die;
        //对外暴露的退款接口
        $result = $this->wxrefundapi();
        return $result;
    }

    public function wxrefundapi()
    {
        //通过微信api进行退款流程
        $parma = array(
            'appid' => $this->APPID,
            'mch_id' => $this->MCHID,
            'nonce_str' => $this->createNoncestr(),
            'out_refund_no' => $this->outRefundNo,
//            'out_trade_no' => $this->outTradeNo,
            'transaction_id'    =>  $this->transaction_id,
            'total_fee' => $this->totalFee,
            'refund_fee' => $this->refundFee,
        );
        $parma['sign'] = $this->getSign($parma);
        $xmldata = $this->arrayToXml($parma);
        $xmlresult = $this->postData('https://api.mch.weixin.qq.com/secapi/pay/refund',$xmldata);
        $result = $this->xmlToArray($xmlresult);
        return $result;
    }

    //发送数据
    protected function postData($url,$xml,$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, $this->apiclient_cert);
        //默认格式为PEM,可以注释
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLKEY, $this->apiclient_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" . "<br>";
            curl_close($ch);
            return false;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读