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


Posted in PHP onMarch 26, 2019

本文实例讲述了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 fckeditor 调用的函数
Jun 21 PHP
PHP 代码规范小结
Mar 08 PHP
说说PHP的autoLoad自动加载机制
Sep 27 PHP
PHP中比较时间大小实例
Aug 21 PHP
PHP中加密解密函数与DES加密解密实例
Oct 17 PHP
thinkphp文件引用与分支结构用法实例
Nov 26 PHP
PHP整合PayPal支付
Jun 11 PHP
PHP版QQ互联OAuth示例代码分享
Jul 05 PHP
php上传图片并压缩的实现方法
Dec 22 PHP
Yii中的cookie的发送和读取
Jul 27 PHP
vmware linux系统安装最新的php7图解
Apr 14 PHP
PHP字符串与数组处理函数用法小结
Jan 07 PHP
PHP设计模式之单例模式定义与用法分析
Mar 26 #PHP
php+mysql开发中的经验与常识小结
Mar 25 #PHP
PHP设计模式之抽象工厂模式实例分析
Mar 25 #PHP
PHP设计模式之简单工厂和工厂模式实例分析
Mar 25 #PHP
PHP实现无限极分类的两种方式示例【递归和引用方式】
Mar 25 #PHP
详解PHP神奇又有用的Trait
Mar 25 #PHP
PHP自动载入类文件函数__autoload的使用方法
Mar 25 #PHP
You might like
发款php蜘蛛统计插件只要有mysql就可用
2010/10/12 PHP
Yii实现显示静态页的方法
2016/04/25 PHP
php数组实现根据某个键值将相同键值合并生成新二维数组的方法
2017/04/26 PHP
PHP Laravel 上传图片、文件等类封装
2017/08/16 PHP
Prototype Selector对象学习
2009/07/23 Javascript
JQuery中操作Css样式的方法
2014/02/12 Javascript
点击显示指定元素隐藏其他同辈元素的方法
2014/02/19 Javascript
JQuery遍历json数组的3种方法
2014/11/08 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
png在IE6 下无法透明的解决方法汇总
2015/05/21 Javascript
jQuery之动画效果大全
2016/11/09 Javascript
微信小程序中单位rpx和rem的使用
2016/12/06 Javascript
nodejs利用ajax实现网页无刷新上传图片实例代码
2017/06/06 NodeJs
node.js的http.createServer过程深入解析
2019/06/06 Javascript
Python3编程实现获取阿里云ECS实例及监控的方法
2017/08/18 Python
Python通过future处理并发问题
2017/10/17 Python
Python实现读取json文件到excel表
2017/11/18 Python
Python2.7.10以上pip更新及其他包的安装教程
2018/06/12 Python
python3.6数独问题的解决
2019/01/21 Python
Django学习笔记之为Model添加Action
2019/04/30 Python
Python常用模块之requests模块用法分析
2019/05/15 Python
python基于FTP实现文件传输相关功能代码实例
2019/09/28 Python
Python jieba库用法及实例解析
2019/11/04 Python
CSS3实现全景图特效示例代码
2018/03/26 HTML / CSS
canvas实现俄罗斯方块的方法示例
2018/12/13 HTML / CSS
Snapfish英国:在线照片打印和个性化照片礼品
2017/01/13 全球购物
实习生体会的自我评价范文
2013/11/28 职场文书
会计专业自荐信
2013/12/02 职场文书
《恐龙》教学反思
2014/04/27 职场文书
人事专员岗位说明书
2014/07/29 职场文书
七一建党节演讲稿
2014/09/11 职场文书
自主招生学校推荐信
2014/09/26 职场文书
幼儿教师年度个人总结
2015/02/05 职场文书
杨善洲电影观后感
2015/06/04 职场文书
创业计划书之校园超市
2019/09/12 职场文书
您对思维方式了解多少?
2019/12/09 职场文书