Wechat.php
9.9 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
355
356
357
358
359
360
361
362
363
364
365
366
<?php
namespace Yansongda\Pay\Gateways;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Yansongda\Pay\Contracts\GatewayApplicationInterface;
use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Events;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Exceptions\InvalidGatewayException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Yansongda\Pay\Gateways\Wechat\Support;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Config;
use Yansongda\Supports\Str;
/**
* @method Response app(array $config) APP 支付
* @method Collection groupRedpack(array $config) 分裂红包
* @method Collection miniapp(array $config) 小程序支付
* @method Collection mp(array $config) 公众号支付
* @method Collection pos(array $config) 刷卡支付
* @method Collection redpack(array $config) 普通红包
* @method Collection scan(array $config) 扫码支付
* @method Collection transfer(array $config) 企业付款
* @method RedirectResponse web(array $config) Web 扫码支付
* @method RedirectResponse wap(array $config) H5 支付
*/
class Wechat implements GatewayApplicationInterface
{
/**
* 普通模式.
*/
const MODE_NORMAL = 'normal';
/**
* 沙箱模式.
*/
const MODE_DEV = 'dev';
/**
* 香港钱包 API.
*/
const MODE_HK = 'hk';
/**
* 境外 API.
*/
const MODE_US = 'us';
/**
* 服务商模式.
*/
const MODE_SERVICE = 'service';
/**
* Const url.
*/
const URL = [
self::MODE_NORMAL => 'https://api.mch.weixin.qq.com/',
self::MODE_DEV => 'https://api.mch.weixin.qq.com/sandboxnew/',
self::MODE_HK => 'https://apihk.mch.weixin.qq.com/',
self::MODE_SERVICE => 'https://api.mch.weixin.qq.com/',
self::MODE_US => 'https://apius.mch.weixin.qq.com/',
];
/**
* Wechat payload.
*
* @var array
*/
protected $payload;
/**
* Wechat gateway.
*
* @var string
*/
protected $gateway;
/**
* Bootstrap.
*
* @author yansongda <me@yansongda.cn>
*
* @throws Exception
*/
public function __construct(Config $config)
{
$this->gateway = Support::create($config)->getBaseUri();
$this->payload = [
'appid' => $config->get('app_id', ''),
'mch_id' => $config->get('mch_id', ''),
'nonce_str' => Str::random(),
'notify_url' => $config->get('notify_url', ''),
'sign' => '',
'trade_type' => '',
'spbill_create_ip' => Request::createFromGlobals()->getClientIp(),
];
if ($config->get('mode', self::MODE_NORMAL) === static::MODE_SERVICE) {
$this->payload = array_merge($this->payload, [
'sub_mch_id' => $config->get('sub_mch_id'),
'sub_appid' => $config->get('sub_app_id', ''),
]);
}
}
/**
* Magic pay.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $method
* @param string $params
*
* @throws InvalidGatewayException
*
* @return Response|Collection
*/
public function __call($method, $params)
{
return self::pay($method, ...$params);
}
/**
* Pay an order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $gateway
* @param array $params
*
* @throws InvalidGatewayException
*
* @return Response|Collection
*/
public function pay($gateway, $params = [])
{
Events::dispatch(new Events\PayStarting('Wechat', $gateway, $params));
$this->payload = array_merge($this->payload, $params);
$gateway = get_class($this).'\\'.Str::studly($gateway).'Gateway';
if (class_exists($gateway)) {
return $this->makePay($gateway);
}
throw new InvalidGatewayException("Pay Gateway [{$gateway}] Not Exists");
}
/**
* Verify data.
*
* @author yansongda <me@yansongda.cn>
*
* @param string|null $content
*
* @throws InvalidSignException
* @throws InvalidArgumentException
*/
public function verify($content = null, bool $refund = false): Collection
{
$content = $content ?? Request::createFromGlobals()->getContent();
Events::dispatch(new Events\RequestReceived('Wechat', '', [$content]));
$data = Support::fromXml($content);
if ($refund) {
$decrypt_data = Support::decryptRefundContents($data['req_info']);
$data = array_merge(Support::fromXml($decrypt_data), $data);
}
Log::debug('Resolved The Received Wechat Request Data', $data);
if ($refund || Support::generateSign($data) === $data['sign']) {
return new Collection($data);
}
Events::dispatch(new Events\SignFailed('Wechat', '', $data));
throw new InvalidSignException('Wechat Sign Verify FAILED', $data);
}
/**
* Query an order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string|array $order
*
* @throws GatewayException
* @throws InvalidSignException
* @throws InvalidArgumentException
*/
public function find($order, string $type = 'wap'): Collection
{
if ('wap' != $type) {
unset($this->payload['spbill_create_ip']);
}
$gateway = get_class($this).'\\'.Str::studly($type).'Gateway';
if (!class_exists($gateway) || !is_callable([new $gateway(), 'find'])) {
throw new GatewayException("{$gateway} Done Not Exist Or Done Not Has FIND Method");
}
$config = call_user_func([new $gateway(), 'find'], $order);
$this->payload = Support::filterPayload($this->payload, $config['order']);
Events::dispatch(new Events\MethodCalled('Wechat', 'Find', $this->gateway, $this->payload));
return Support::requestApi(
$config['endpoint'],
$this->payload,
$config['cert']
);
}
/**
* Refund an order.
*
* @author yansongda <me@yansongda.cn>
*
* @throws GatewayException
* @throws InvalidSignException
* @throws InvalidArgumentException
*/
public function refund(array $order): Collection
{
$this->payload = Support::filterPayload($this->payload, $order, true);
Events::dispatch(new Events\MethodCalled('Wechat', 'Refund', $this->gateway, $this->payload));
return Support::requestApi(
'secapi/pay/refund',
$this->payload,
true
);
}
/**
* Cancel an order.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $order
*
* @throws GatewayException
* @throws InvalidSignException
* @throws InvalidArgumentException
*/
public function cancel($order): Collection
{
unset($this->payload['spbill_create_ip']);
$this->payload = Support::filterPayload($this->payload, $order);
Events::dispatch(new Events\MethodCalled('Wechat', 'Cancel', $this->gateway, $this->payload));
return Support::requestApi(
'secapi/pay/reverse',
$this->payload,
true
);
}
/**
* Close an order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string|array $order
*
* @throws GatewayException
* @throws InvalidSignException
* @throws InvalidArgumentException
*/
public function close($order): Collection
{
unset($this->payload['spbill_create_ip']);
$this->payload = Support::filterPayload($this->payload, $order);
Events::dispatch(new Events\MethodCalled('Wechat', 'Close', $this->gateway, $this->payload));
return Support::requestApi('pay/closeorder', $this->payload);
}
/**
* Echo success to server.
*
* @author yansongda <me@yansongda.cn>
*
* @throws InvalidArgumentException
*/
public function success(): Response
{
Events::dispatch(new Events\MethodCalled('Wechat', 'Success', $this->gateway));
return new Response(
Support::toXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']),
200,
['Content-Type' => 'application/xml']
);
}
/**
* Download the bill.
*
* @author yansongda <me@yansongda.cn>
*
* @throws GatewayException
* @throws InvalidArgumentException
*/
public function download(array $params): string
{
unset($this->payload['spbill_create_ip']);
$this->payload = Support::filterPayload($this->payload, $params, true);
Events::dispatch(new Events\MethodCalled('Wechat', 'Download', $this->gateway, $this->payload));
$result = Support::getInstance()->post(
'pay/downloadbill',
Support::getInstance()->toXml($this->payload)
);
if (is_array($result)) {
throw new GatewayException('Get Wechat API Error: '.$result['return_msg'], $result);
}
return $result;
}
/**
* Make pay gateway.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $gateway
*
* @throws InvalidGatewayException
*
* @return Response|Collection
*/
protected function makePay($gateway)
{
$app = new $gateway();
if ($app instanceof GatewayInterface) {
return $app->pay($this->gateway, array_filter($this->payload, function ($value) {
return '' !== $value && !is_null($value);
}));
}
throw new InvalidGatewayException("Pay Gateway [{$gateway}] Must Be An Instance Of GatewayInterface");
}
}