|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Response; |
|
8
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
|
9
|
|
|
use Psr\Http\Message\MessageInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Yansongda\Pay\Event; |
|
13
|
|
|
use Yansongda\Pay\Exception\ContainerException; |
|
14
|
|
|
use Yansongda\Pay\Exception\Exception; |
|
15
|
|
|
use Yansongda\Pay\Exception\InvalidParamsException; |
|
16
|
|
|
use Yansongda\Pay\Exception\ServiceNotFoundException; |
|
17
|
|
|
use Yansongda\Pay\Pay; |
|
18
|
|
|
use Yansongda\Pay\Plugin\ParserPlugin; |
|
19
|
|
|
use Yansongda\Pay\Plugin\Wechat\CallbackPlugin; |
|
20
|
|
|
use Yansongda\Pay\Plugin\Wechat\LaunchPlugin; |
|
21
|
|
|
use Yansongda\Pay\Plugin\Wechat\PreparePlugin; |
|
22
|
|
|
use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin; |
|
23
|
|
|
use Yansongda\Supports\Collection; |
|
24
|
|
|
use Yansongda\Supports\Str; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @method Collection app(array $order) APP 支付 |
|
28
|
|
|
* @method Collection mini(array $order) 小程序支付 |
|
29
|
|
|
* @method Collection mp(array $order) 公众号支付 |
|
30
|
|
|
* @method Collection scan(array $order) 扫码支付 |
|
31
|
|
|
* @method Collection wap(array $order) H5 支付 |
|
32
|
|
|
* @method Collection transfer(array $order) 帐户转账 |
|
33
|
|
|
* @method Collection papay(array $order) 支付时签约(委托代扣) |
|
34
|
|
|
* @method Collection papayApply(array $order) 申请代扣(委托代扣) |
|
35
|
|
|
* @method Collection papayContract(array $order) 申请代扣(委托代扣) |
|
36
|
|
|
*/ |
|
37
|
|
|
class Wechat extends AbstractProvider |
|
38
|
|
|
{ |
|
39
|
|
|
public const AUTH_TAG_LENGTH_BYTE = 16; |
|
40
|
|
|
|
|
41
|
|
|
public const MCH_SECRET_KEY_LENGTH_BYTE = 32; |
|
42
|
|
|
|
|
43
|
|
|
public const URL = [ |
|
44
|
|
|
Pay::MODE_NORMAL => 'https://api.mch.weixin.qq.com/', |
|
45
|
|
|
Pay::MODE_SANDBOX => 'https://api.mch.weixin.qq.com/sandboxnew/', |
|
46
|
|
|
Pay::MODE_SERVICE => 'https://api.mch.weixin.qq.com/', |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws ContainerException |
|
51
|
|
|
* @throws InvalidParamsException |
|
52
|
|
|
* @throws ServiceNotFoundException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __call(string $shortcut, array $params): null|Collection|MessageInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$plugin = '\\Yansongda\\Pay\\Plugin\\Wechat\\Shortcut\\'. |
|
57
|
|
|
Str::studly($shortcut).'Shortcut'; |
|
58
|
|
|
|
|
59
|
|
|
return $this->call($plugin, ...$params); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws ContainerException |
|
64
|
|
|
* @throws InvalidParamsException |
|
65
|
|
|
* @throws ServiceNotFoundException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function query(array $order): array|Collection |
|
68
|
|
|
{ |
|
69
|
|
|
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null)); |
|
70
|
|
|
|
|
71
|
|
|
return $this->__call('query', [$order]); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @throws InvalidParamsException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function cancel(array|string $order): null|array|Collection |
|
78
|
|
|
{ |
|
79
|
|
|
throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Wechat does not support cancel api'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws ContainerException |
|
84
|
|
|
* @throws InvalidParamsException |
|
85
|
|
|
* @throws ServiceNotFoundException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function close(array|string $order): null|array|Collection |
|
88
|
|
|
{ |
|
89
|
|
|
$order = is_array($order) ? $order : ['out_trade_no' => $order]; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null)); |
|
92
|
|
|
|
|
93
|
|
|
$this->__call('close', [$order]); |
|
94
|
|
|
|
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @throws ContainerException |
|
100
|
|
|
* @throws InvalidParamsException |
|
101
|
|
|
* @throws ServiceNotFoundException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function refund(array $order): array|Collection |
|
104
|
|
|
{ |
|
105
|
|
|
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null)); |
|
106
|
|
|
|
|
107
|
|
|
return $this->__call('refund', [$order]); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @throws ContainerException |
|
112
|
|
|
* @throws InvalidParamsException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection |
|
115
|
|
|
{ |
|
116
|
|
|
$request = $this->getCallbackParams($contents); |
|
117
|
|
|
|
|
118
|
|
|
Event::dispatch(new Event\CallbackReceived('wechat', clone $request, $params, null)); |
|
119
|
|
|
|
|
120
|
|
|
return $this->pay( |
|
|
|
|
|
|
121
|
|
|
[CallbackPlugin::class], |
|
122
|
|
|
['request' => $request, 'params' => $params] |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function success(): ResponseInterface |
|
127
|
|
|
{ |
|
128
|
|
|
return new Response( |
|
129
|
|
|
200, |
|
130
|
|
|
['Content-Type' => 'application/json'], |
|
131
|
|
|
json_encode(['code' => 'SUCCESS', 'message' => '成功']), |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function mergeCommonPlugins(array $plugins): array |
|
136
|
|
|
{ |
|
137
|
|
|
return array_merge( |
|
138
|
|
|
[PreparePlugin::class], |
|
139
|
|
|
$plugins, |
|
140
|
|
|
[RadarSignPlugin::class], |
|
141
|
|
|
[LaunchPlugin::class, ParserPlugin::class], |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): ServerRequestInterface |
|
146
|
|
|
{ |
|
147
|
|
|
if (is_array($contents) && isset($contents['body'], $contents['headers'])) { |
|
148
|
|
|
return new ServerRequest('POST', 'http://localhost', $contents['headers'], $contents['body']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (is_array($contents)) { |
|
152
|
|
|
return new ServerRequest('POST', 'http://localhost', [], json_encode($contents)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($contents instanceof ServerRequestInterface) { |
|
156
|
|
|
return $contents; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return ServerRequest::fromGlobals(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|