福州網站建設>網站新聞>php技術

        微信支付 退款 V2版本源代碼 親測可用

        發布日期:2022-12-08瀏覽次數:539 來源:福州網站建設

        /**
           * [refund description]
           * @Author   www.txwhj.cn
           * @DateTime 2022-12-08T13:39:50+0800
           * @param    [type]                   $appid         [小程序或公眾號appid]
           * @param    [type]                   $mch_id        [商戶ID]
           * @param    [type]                   $nonce_str     [隨機數]
           * @param    [type]                   $out_trade_no  [原訂單單號]
           * @param    [type]                   $out_refund_no [退款單號]
           * @param    [type]                   $total_fee     [訂單金額]
           * @param    [type]                   $refund_fee    [退款金額]
           * @param    [type]                   $mckey         [商戶32位key]
           * @return   [type]                                  [description]
           */
          public function refund($appid,$mch_id,$nonce_str,$out_trade_no,$out_refund_no,$total_fee,$refund_fee,$mckey)
          {
            $arrdata = array("mch_id" => $mch_id,"appid" => $appid, "nonce_str" => $nonce_str, "out_trade_no" => $out_trade_no, "out_refund_no" => $out_refund_no, "total_fee" => $total_fee, "refund_fee" => $refund_fee);
        
        
            ksort($arrdata);
            $paramstring = "";
            foreach($arrdata as $key => $value)
            {
            if(strlen($paramstring) == 0)
              $paramstring .= $key . "=" . $value;
              else
              $paramstring .= "&" . $key . "=" . $value;
            }
        
            $stringSignTemp = $paramstring . "&key=" . $mckey;
        
            $signValue = strtoupper(md5($stringSignTemp));
              
            $returValue = '';
            foreach($arrdata as $key => $value)
            {
              $returValue .= "<$key>".$value."";
            
            }
            $returValue .= "".$signValue."";
            $returValue .= '';
             var_dump($returValue);
            $result = $this->order_refund_request("https://api.mch.weixin.qq.com/secapi/pay/refund",$returValue);
            
        
            if ($result)
            {
              $json = $this->xmlToArray($result);
              return $json;
            }
            return false;
          }
        
        
          function order_refund_request($url, $data = null)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            //證書設置
            curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLCERT, DDROOT . '/cert/apiclient_cert.pem');//客戶端cert路徑 DDROOT是我這邊系統常量自行改下
            curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLKEY, DDROOT . '/cert/apiclient_key.pem');//客戶端key路徑
        
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }
        
        
          public function xmlToArray($xml)
          {   
                //將XML轉為array        
                $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);    
            return $array_data;
          }

        以上是由福州網站建設的小編為你分享了"微信支付 退款 V2版本源代碼 親測可用"文章,如果你在這方面有什么問題,隨時聯系我們

        php技術有關的文章
        如果您有什么問題,歡迎咨詢我們客服! 點擊QQ咨詢