本文实例讲述了PHP实现微信退款的方法。分享给大家供大家参考,具体如下:
?
1
2
|
$obj = new WXRefund( '参数' );
$obj ->refundApi();
|
直接能用 公众号的参数 自己加上吧 只能帮你们到这了!
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
<?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程序设计有所帮助。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 37
-
2025-05-25 52
-
2025-05-25 56
-
2025-06-04 81
-
2025-05-29 36
热门评论