福州网站建设>网站新闻>php技术

微信支付 退款 V2版本源代码 亲测可用

发布日期:2022-12-08浏览次数:349 来源:福州网站建设

/**
   * [refund description]
   * @Author   www.xl779.com
   * @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咨询