1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Refund.php |
4
|
|
|
* |
5
|
|
|
* Part of Overtrue\Wechat. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author jaring <[email protected]> |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
*Usage: |
14
|
|
|
* $business = new Business($appId, $appSecret, $mchId, $mchKey); |
15
|
|
|
* $business->setClientCert(dirname(__FILE__).'/cert/apiclient_cert.pem'); |
16
|
|
|
* $business->setClientKey(dirname(__FILE__).'/cert/apiclient_key.pem'); |
17
|
|
|
* $refund =new Refund($business); |
18
|
|
|
* $refund->out_refund_no= md5(uniqid(microtime()));//退单单号 |
19
|
|
|
* $refund->total_fee=1; //订单金额 |
20
|
|
|
* $refund->refund_fee=1;//退款金额 |
21
|
|
|
* $refund->out_trade_no=$order_id;//原商户订单号 |
22
|
|
|
* var_dump($trans->getResponse()); |
23
|
|
|
*/ |
24
|
|
|
namespace Overtrue\Wechat\Payment; |
25
|
|
|
|
26
|
|
|
use Overtrue\Wechat\Utils\XML; |
27
|
|
|
use Overtrue\Wechat\Utils\SignGenerator; |
28
|
|
|
use Overtrue\Wechat\Http; |
29
|
|
|
|
30
|
|
|
class Refund |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* 退款接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund |
34
|
|
|
*/ |
35
|
|
|
const REFUNDORDER_URL = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 商户信息 |
39
|
|
|
* |
40
|
|
|
* @var Business |
41
|
|
|
*/ |
42
|
|
|
protected $business; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 退款订单必填项 |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected static $required = array('out_refund_no', 'total_fee', 'refund_fee'); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 退款订单选填项 |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected static $optional = array('out_trade_no', 'transaction_id', 'device_info', 'fee_type', 'op_user_id'); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected static $params = array(); |
62
|
|
|
protected static $allowParams = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 退款返回信息 |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $refundInfo = null; |
70
|
|
|
|
71
|
|
|
public function __construct(Business $business = null) |
72
|
|
|
{ |
73
|
|
|
if (!is_null($business)) { |
74
|
|
|
$this->setBusiness($business); |
75
|
|
|
} |
76
|
|
|
if (count(static::$allowParams) === 0) { |
77
|
|
|
static::$allowParams = array_merge(static::$required, static::$optional); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 设置商户 |
83
|
|
|
* |
84
|
|
|
* @param Business $business |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
* |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function setBusiness(Business $business) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
if (!is_null($business)) { |
93
|
|
|
try { |
94
|
|
|
$business->checkParams(); |
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
throw new Exception($e->getMessage()); |
97
|
|
|
} |
98
|
|
|
$this->business = $business; |
99
|
|
|
$this->refundInfo = null; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 获取商户 |
107
|
|
|
* |
108
|
|
|
* @return Business |
109
|
|
|
*/ |
110
|
|
|
public function getBusiness() |
111
|
|
|
{ |
112
|
|
|
return $this->business; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 获取退款结果 |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
* |
120
|
|
|
* @throws Exception |
121
|
|
|
*/ |
122
|
|
|
public function getResponse() |
123
|
|
|
{ |
124
|
|
|
if (is_null($this->business)) { |
125
|
|
|
throw new Exception('Business is required'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
static::$params['appid'] = $this->business->appid; |
|
|
|
|
129
|
|
|
static::$params['mch_id'] = $this->business->mch_id; |
|
|
|
|
130
|
|
|
$this->checkParams(); |
131
|
|
|
$signGenerator = new SignGenerator(static::$params); |
132
|
|
|
$signGenerator->onSortAfter(function (SignGenerator $that) { |
133
|
|
|
$that->key = $this->business->mch_key; |
|
|
|
|
134
|
|
|
}); |
135
|
|
|
static::$params['sign'] = $signGenerator->getResult(); |
136
|
|
|
|
137
|
|
|
$request = XML::build(static::$params); |
138
|
|
|
//设置Http使用的证书 |
139
|
|
|
$options['sslcert_path'] = $this->business->getClientCert(); |
|
|
|
|
140
|
|
|
$options['sslkey_path'] = $this->business->getClientKey(); |
141
|
|
|
|
142
|
|
|
$http = new Http(); |
143
|
|
|
$response = $http->request(static::REFUNDORDER_URL, Http::POST, $request, $options); |
|
|
|
|
144
|
|
|
if (empty($response)) { |
145
|
|
|
throw new Exception('Get Refund Failure:'); |
146
|
|
|
} |
147
|
|
|
$refundOrder = XML::parse($response); |
|
|
|
|
148
|
|
|
|
149
|
|
View Code Duplication |
if (isset($refundOrder['return_code']) && |
|
|
|
|
150
|
|
|
$refundOrder['return_code'] === 'FAIL') { |
151
|
|
|
throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//返回签名数据校验 |
155
|
|
|
if (empty($refundOrder) || empty($refundOrder['sign'])) { |
156
|
|
|
throw new Exception('param sign is missing or empty'); |
157
|
|
|
} |
158
|
|
|
$sign = $refundOrder['sign']; |
159
|
|
|
unset($refundOrder['sign']); |
160
|
|
|
$signGenerator = new SignGenerator($refundOrder); |
|
|
|
|
161
|
|
|
$signGenerator->onSortAfter(function (SignGenerator $that) { |
162
|
|
|
$that->key = $this->business->mch_key; |
|
|
|
|
163
|
|
|
}); |
164
|
|
|
if ($sign !== $signGenerator->getResult()) { |
165
|
|
|
throw new Exception('check sign error'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
//返回结果判断 |
169
|
|
View Code Duplication |
if (isset($refundOrder['result_code']) && |
|
|
|
|
170
|
|
|
($refundOrder['result_code'] === 'FAIL')) { |
171
|
|
|
throw new Exception($refundOrder['err_code'].': '.$refundOrder['err_code_des']); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
if (isset($refundOrder['return_code']) && |
|
|
|
|
175
|
|
|
$refundOrder['return_code'] === 'FAIL') { |
176
|
|
|
throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->refundInfo = $refundOrder; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 检测参数值是否有效 |
184
|
|
|
* |
185
|
|
|
* @throws Exception |
186
|
|
|
*/ |
187
|
|
|
public function checkParams() |
188
|
|
|
{ |
189
|
|
|
foreach (static::$required as $paramName) { |
190
|
|
|
if (!array_key_exists($paramName, static::$params)) { |
191
|
|
|
throw new Exception(sprintf('"%s" is required', $paramName)); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (!array_key_exists('transaction_id', static::$params) && !array_key_exists('out_trade_no', static::$params)) { |
196
|
|
|
throw new Exception('transaction_id or out_trade_no is required'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (!array_key_exists('nonce_str', static::$params)) { |
200
|
|
|
static::$params['nonce_str'] = md5(uniqid(microtime())); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (!array_key_exists('op_user_id', static::$params)) { |
204
|
|
|
static::$params['op_user_id'] = $this->business->mch_id; |
|
|
|
|
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function __set($property, $value) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
if (!in_array($property, static::$allowParams, true)) { |
211
|
|
|
throw new Exception(sprintf('"%s" is not required', $property)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return static::$params[$property] = $value; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.