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\Artful\Artful; |
||
13 | use Yansongda\Artful\Exception\ContainerException; |
||
14 | use Yansongda\Artful\Exception\InvalidParamsException; |
||
15 | use Yansongda\Artful\Exception\ServiceNotFoundException; |
||
16 | use Yansongda\Artful\Plugin\AddPayloadBodyPlugin; |
||
17 | use Yansongda\Artful\Plugin\ParserPlugin; |
||
18 | use Yansongda\Artful\Plugin\StartPlugin; |
||
19 | use Yansongda\Artful\Rocket; |
||
20 | use Yansongda\Pay\Contract\ProviderInterface; |
||
21 | use Yansongda\Pay\Event; |
||
22 | use Yansongda\Pay\Event\CallbackReceived; |
||
23 | use Yansongda\Pay\Event\MethodCalled; |
||
24 | use Yansongda\Pay\Pay; |
||
25 | use Yansongda\Pay\Plugin\Wechat\AddRadarPlugin; |
||
26 | use Yansongda\Pay\Plugin\Wechat\ResponsePlugin; |
||
27 | use Yansongda\Pay\Plugin\Wechat\V3\AddPayloadSignaturePlugin; |
||
28 | use Yansongda\Pay\Plugin\Wechat\V3\CallbackPlugin; |
||
29 | use Yansongda\Pay\Plugin\Wechat\V3\VerifySignaturePlugin; |
||
30 | use Yansongda\Supports\Collection; |
||
31 | use Yansongda\Supports\Str; |
||
32 | |||
33 | /** |
||
34 | * @method Collection|Rocket app(array $order) APP 支付 |
||
35 | * @method Collection|Rocket mini(array $order) 小程序支付 |
||
36 | * @method Collection|Rocket mp(array $order) 公众号支付 |
||
37 | * @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫) |
||
38 | * @method Collection|Rocket h5(array $order) H5 支付 |
||
39 | * @method Collection|Rocket transfer(array $order) 帐户转账 |
||
40 | */ |
||
41 | class Wechat implements ProviderInterface |
||
42 | { |
||
43 | public const AUTH_TAG_LENGTH_BYTE = 16; |
||
44 | |||
45 | public const MCH_SECRET_KEY_LENGTH_BYTE = 32; |
||
46 | |||
47 | public const URL = [ |
||
48 | Pay::MODE_NORMAL => 'https://api.mch.weixin.qq.com/', |
||
49 | Pay::MODE_SANDBOX => 'https://api.mch.weixin.qq.com/sandboxnew/', |
||
50 | Pay::MODE_SERVICE => 'https://api.mch.weixin.qq.com/', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @throws ContainerException |
||
55 | * @throws InvalidParamsException |
||
56 | * @throws ServiceNotFoundException |
||
57 | */ |
||
58 | public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket |
||
59 | { |
||
60 | $plugin = '\Yansongda\Pay\Shortcut\Wechat\\'.Str::studly($shortcut).'Shortcut'; |
||
61 | |||
62 | return Artful::shortcut($plugin, ...$params); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @throws ContainerException |
||
67 | * @throws InvalidParamsException |
||
68 | */ |
||
69 | public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket |
||
70 | { |
||
71 | return Artful::artful($plugins, $params); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @throws ContainerException |
||
76 | * @throws InvalidParamsException |
||
77 | * @throws ServiceNotFoundException |
||
78 | */ |
||
79 | public function query(array $order): Collection|Rocket |
||
80 | { |
||
81 | Event::dispatch(new MethodCalled('wechat', __METHOD__, $order, null)); |
||
82 | |||
83 | return $this->__call('query', [$order]); |
||
0 ignored issues
–
show
The expression
return $this->__call('query', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection . Consider adding an additional type-check to rule them out.
![]() |
|||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @throws ContainerException |
||
88 | * @throws InvalidParamsException |
||
89 | * @throws ServiceNotFoundException |
||
90 | */ |
||
91 | public function cancel(array $order): Collection|Rocket |
||
92 | { |
||
93 | Event::dispatch(new MethodCalled('wechat', __METHOD__, $order, null)); |
||
94 | |||
95 | return $this->__call('cancel', [$order]); |
||
0 ignored issues
–
show
The expression
return $this->__call('cancel', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection . Consider adding an additional type-check to rule them out.
![]() |
|||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @throws ContainerException |
||
100 | * @throws InvalidParamsException |
||
101 | * @throws ServiceNotFoundException |
||
102 | */ |
||
103 | public function close(array $order): Collection|Rocket |
||
104 | { |
||
105 | Event::dispatch(new MethodCalled('wechat', __METHOD__, $order, null)); |
||
106 | |||
107 | $this->__call('close', [$order]); |
||
108 | |||
109 | return new Collection(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @throws ContainerException |
||
114 | * @throws InvalidParamsException |
||
115 | * @throws ServiceNotFoundException |
||
116 | */ |
||
117 | public function refund(array $order): Collection|Rocket |
||
118 | { |
||
119 | Event::dispatch(new MethodCalled('wechat', __METHOD__, $order, null)); |
||
120 | |||
121 | return $this->__call('refund', [$order]); |
||
0 ignored issues
–
show
The expression
return $this->__call('refund', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection . Consider adding an additional type-check to rule them out.
![]() |
|||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @throws ContainerException |
||
126 | * @throws InvalidParamsException |
||
127 | */ |
||
128 | public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket |
||
129 | { |
||
130 | $request = $this->getCallbackParams($contents); |
||
131 | |||
132 | Event::dispatch(new CallbackReceived('wechat', clone $request, $params, null)); |
||
133 | |||
134 | return $this->pay( |
||
0 ignored issues
–
show
The expression
return $this->pay(array(... '_params' => $params)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection . Consider adding an additional type-check to rule them out.
![]() |
|||
135 | [CallbackPlugin::class], |
||
136 | ['_request' => $request, '_params' => $params] |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | public function success(): ResponseInterface |
||
141 | { |
||
142 | return new Response( |
||
143 | 200, |
||
144 | ['Content-Type' => 'application/json'], |
||
145 | json_encode(['code' => 'SUCCESS', 'message' => '成功']), |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | public function mergeCommonPlugins(array $plugins): array |
||
150 | { |
||
151 | return array_merge( |
||
152 | [StartPlugin::class], |
||
153 | $plugins, |
||
154 | [AddPayloadBodyPlugin::class, AddPayloadSignaturePlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class], |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): ServerRequestInterface |
||
159 | { |
||
160 | if (is_array($contents) && isset($contents['body'], $contents['headers'])) { |
||
161 | return new ServerRequest('POST', 'http://localhost', $contents['headers'], $contents['body']); |
||
162 | } |
||
163 | |||
164 | if (is_array($contents)) { |
||
165 | return new ServerRequest('POST', 'http://localhost', [], json_encode($contents)); |
||
166 | } |
||
167 | |||
168 | if ($contents instanceof ServerRequestInterface) { |
||
169 | return $contents; |
||
170 | } |
||
171 | |||
172 | return ServerRequest::fromGlobals(); |
||
173 | } |
||
174 | } |
||
175 |