使用PHP如何实现一个微信退款功能

本篇文章给大家分享的是有关使用PHP如何实现一个微信退款功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

php有什么用

php是一个嵌套的缩写名称,是英文超级文本预处理语言,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。

具体如下:

$obj = new WXRefund('参数');
$obj->refundApi();

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

<?php
namespace Wechat;

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如何实现一个微信退款功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。