欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现微信退款的方法示例

程序员文章站 2023-09-04 21:24:11
本文实例讲述了php实现微信退款的方法。分享给大家供大家参考,具体如下: $obj = new wxrefund('参数'); $obj->refunda...

本文实例讲述了php实现微信退款的方法。分享给大家供大家参考,具体如下:

$obj = new wxrefund('参数');
$obj->refundapi();

直接能用 公众号的参数 自己加上吧 只能帮你们到这了!

<?php
namespace wechat;
/**
 * 微信退款
 * @author    zzy
 * @version   $v1.0.0$
 * @date    2018-11-9
 */
class wxrefund
{
  protected $sslcert_path ='';//证书
  protected $sslkey_path = '';//证书
  protected $opuserid = '';//商户号
  protected $key = '';//api密钥
  protected $appid = '';//appid
  function __construct($outtradeno, $totalfee, $outrefundno, $refundfee)
  {
    //初始化退款类需要的变量
    $this->totalfee = $totalfee;//订单金额
    $this->refundfee = $refundfee;//退款金额
    $this->outtradeno = $outtradeno;//订单号
    $this->outrefundno = $outrefundno;//退款订单
  }
  /**
   * 通过微信api进行退款流程 唯一对外接口
   * @return string
   */
  public function refundapi()
  {
    $parma = array(
      'appid' => $this->appid,
      'mch_id' => $this->opuserid,
      'nonce_str' => randoms(32),//这个是随机数 自己封装去吧。。。
      'out_refund_no' => $this->outrefundno,
      'out_trade_no' => $this->outtradeno,
      'total_fee' => intval($this->totalfee * 100),
      'refund_fee' => intval($this->refundfee * 100),
    );
    $parma['sign'] = $this->getsign($parma, $this->key);
    $xmldata = $this->arraytoxml($parma);
    $xmlresult = $this->postxmlsslcurl($xmldata, 'https://api.mch.weixin.qq.com/secapi/pay/refund');
    $result = $this->arraytoxml($xmlresult);
    return $result;
  }
  /**
   * 数组转xml
   * @param $arr
   * @return string
   */
  protected function arraytoxml($arr)
  {
    $xml = "<xml>";
    foreach ($arr as $key => $val) {
      if (is_numeric($val)) {
        $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
      } else {
        $xml .= "<" . $key . "><![cdata[" . $val . "]]></" . $key . ">";
      }
    }
    $xml .= "</xml>";
    return $xml;
  }
  /**
   * 签名加密
   * @param $params
   * @param $key
   */
  protected function getsign($params, $key)
  {
    ksort($params, sort_string);
    $unsignparastring = $this->formatqueryparamap($params, false);
    return $signstr = strtoupper(md5($unsignparastring . "&key=" . $key));
  }
  /**
   * 排序
   * @param $paramap
   * @param bool $urlencode
   * @return bool|string
   */
  protected function formatqueryparamap($paramap, $urlencode = false)
  {
    $buff = "";
    ksort($paramap);
    foreach ($paramap as $k => $v) {
      if (null != $v && "null" != $v) {
        if ($urlencode) {
          $v = urlencode($v);
        }
        $buff .= $k . "=" . $v . "&";
      }
    }
    $reqpar = '';
    if (strlen($buff) > 0) {
      $reqpar = substr($buff, 0, strlen($buff) - 1);
    }
    return $reqpar;
  }
  /**
   * 需要使用证书的请求
   * @param $xml
   * @param $url
   * @param int $second
   * @return bool|mixed
   */
  protected function postxmlsslcurl($xml, $url, $second = 30)
  {
    $ch = curl_init();
    curl_setopt($ch, curlopt_timeout, $second);
    curl_setopt($ch, curlopt_url, $url);
    curl_setopt($ch, curlopt_ssl_verifypeer, false);
    curl_setopt($ch, curlopt_ssl_verifyhost, false);
    curl_setopt($ch, curlopt_header, false);
    curl_setopt($ch, curlopt_returntransfer, true);
    curl_setopt($ch, curlopt_sslcerttype, 'pem');
    curl_setopt($ch, curlopt_sslcert, $this->sslcert_path);
    curl_setopt($ch, curlopt_sslkeytype, 'pem');
    curl_setopt($ch, curlopt_sslkey, $this->sslkey_path);
    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;
    }
  }
}

更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php curl用法总结》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php中json格式数据操作技巧汇总》及《php针对xml文件操作技巧总结

希望本文所述对大家php程序设计有所帮助。