Wechat.php
8.1 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
namespace Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Traits\HasHttpRequest;
abstract class Wechat implements GatewayInterface
{
use HasHttpRequest;
/**
* @var string
*/
protected $endpoint = 'https://api.mch.weixin.qq.com/';
/**
* @var string
*/
protected $gateway_order = 'pay/unifiedorder';
/**
* @var string
*/
protected $gateway_query = 'pay/orderquery';
/**
* @var string
*/
protected $gateway_close = 'pay/closeorder';
/**
* @var string
*/
protected $gateway_refund = 'secapi/pay/refund';
/**
* @var array
*/
protected $config;
/**
* @var \Yansongda\Pay\Support\Config
*/
protected $user_config;
/**
* [__construct description].
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config
*/
public function __construct(array $config)
{
$this->user_config = new Config($config);
$this->config = [
'appid' => $this->user_config->get('app_id', ''),
'mch_id' => $this->user_config->get('mch_id', ''),
'nonce_str' => $this->createNonceStr(),
'sign_type' => 'MD5',
'notify_url' => $this->user_config->get('notify_url', ''),
'trade_type' => $this->getTradeType(),
];
if ($endpoint = $this->user_config->get('endpoint_url')) {
$this->endpoint = $endpoint;
}
}
/**
* pay a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config_biz
*
* @return mixed
*/
abstract public function pay(array $config_biz = []);
/**
* refund.
*
* @author yansongda <me@yansongda.cn>
*
* @return string|bool
*/
public function refund($config_biz = [])
{
if (isset($config_biz['miniapp'])) {
$this->config['appid'] = $this->user_config->get('miniapp_id');
unset($config_biz['miniapp']);
}
$this->config = array_merge($this->config, $config_biz);
$this->unsetTradeTypeAndNotifyUrl();
return $this->getResult($this->gateway_refund, true);
}
/**
* close a order.
*
* @author yansongda <me@yansongda.cn>
*
* @return array|bool
*/
public function close($out_trade_no = '')
{
$this->config['out_trade_no'] = $out_trade_no;
$this->unsetTradeTypeAndNotifyUrl();
return $this->getResult($this->gateway_close);
}
/**
* find a order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $out_trade_no
*
* @return array|bool
*/
public function find($out_trade_no = '')
{
$this->config['out_trade_no'] = $out_trade_no;
$this->unsetTradeTypeAndNotifyUrl();
return $this->getResult($this->gateway_query);
}
/**
* verify the notify.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $data
* @param string $sign
* @param bool $sync
*
* @return array|bool
*/
public function verify($data, $sign = null, $sync = false)
{
$data = $this->fromXml($data);
$sign = is_null($sign) ? $data['sign'] : $sign;
return $this->getSign($data) === $sign ? $data : false;
}
/**
* get trade type config.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
abstract protected function getTradeType();
/**
* pre order.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $config_biz
*
* @return array
*/
protected function preOrder($config_biz = [])
{
$this->config = array_merge($this->config, $config_biz);
return $this->getResult($this->gateway_order);
}
/**
* get api result.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $path
* @param bool $cert
*
* @return array
*/
protected function getResult($path, $cert = false)
{
$this->config['sign'] = $this->getSign($this->config);
if ($cert) {
$data = $this->fromXml($this->post(
$this->endpoint.$path,
$this->toXml($this->config),
[
'cert' => $this->user_config->get('cert_client', ''),
'ssl_key' => $this->user_config->get('cert_key', ''),
]
));
} else {
$data = $this->fromXml($this->post($this->endpoint.$path, $this->toXml($this->config)));
}
if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
$error = 'getResult error:'.$data['return_msg'];
$error .= isset($data['err_code_des']) ? ' - '.$data['err_code_des'] : '';
}
if (!isset($error) && $this->getSign($data) !== $data['sign']) {
$error = 'getResult error: return data sign error';
}
if (isset($error)) {
throw new GatewayException(
$error,
20000,
$data);
}
return $data;
}
/**
* sign.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $data
*
* @return string
*/
protected function getSign($data)
{
if (is_null($this->user_config->get('key'))) {
throw new InvalidArgumentException('Missing Config -- [key]');
}
ksort($data);
$string = md5($this->getSignContent($data).'&key='.$this->user_config->get('key'));
return strtoupper($string);
}
/**
* get sign content.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $data
*
* @return string
*/
protected function getSignContent($data)
{
$buff = '';
foreach ($data as $k => $v) {
$buff .= ($k != 'sign' && $v != '' && !is_array($v)) ? $k.'='.$v.'&' : '';
}
return trim($buff, '&');
}
/**
* create random string.
*
* @author yansongda <me@yansongda.cn>
*
* @param int $length
*
* @return string
*/
protected function createNonceStr($length = 16)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* convert to xml.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $data
*
* @return string
*/
protected function toXml($data)
{
if (!is_array($data) || count($data) <= 0) {
throw new InvalidArgumentException('convert to xml error!invalid array!');
}
$xml = '<xml>';
foreach ($data as $key => $val) {
$xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
'<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
}
$xml .= '</xml>';
return $xml;
}
/**
* convert to array.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $xml
*
* @return array
*/
protected function fromXml($xml)
{
if (!$xml) {
throw new InvalidArgumentException('convert to array error !invalid xml');
}
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
}
/**
* delete trade_type and notify_url.
*
* @author yansongda <me@yansongda.cn>
*
* @return bool
*/
protected function unsetTradeTypeAndNotifyUrl()
{
unset($this->config['notify_url']);
unset($this->config['trade_type']);
return true;
}
}