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\AddRadarPlugin; |
||
18 | use Yansongda\Artful\Plugin\ParserPlugin; |
||
19 | use Yansongda\Artful\Plugin\StartPlugin; |
||
20 | use Yansongda\Artful\Rocket; |
||
21 | use Yansongda\Pay\Contract\ProviderInterface; |
||
22 | use Yansongda\Pay\Event; |
||
23 | use Yansongda\Pay\Event\CallbackReceived; |
||
24 | use Yansongda\Pay\Event\MethodCalled; |
||
25 | use Yansongda\Pay\Exception\Exception; |
||
26 | use Yansongda\Pay\Pay; |
||
27 | use Yansongda\Pay\Plugin\Douyin\V1\Pay\AddPayloadSignaturePlugin; |
||
28 | use Yansongda\Pay\Plugin\Douyin\V1\Pay\CallbackPlugin; |
||
29 | use Yansongda\Pay\Plugin\Douyin\V1\Pay\ResponsePlugin; |
||
30 | use Yansongda\Supports\Collection; |
||
31 | use Yansongda\Supports\Str; |
||
32 | |||
33 | /** |
||
34 | * @method Collection|Rocket mini(array $order) 小程序支付 |
||
35 | */ |
||
36 | class Douyin implements ProviderInterface |
||
37 | { |
||
38 | public const URL = [ |
||
39 | Pay::MODE_NORMAL => 'https://developer.toutiao.com/', |
||
40 | Pay::MODE_SANDBOX => 'https://open-sandbox.douyin.com/', |
||
41 | Pay::MODE_SERVICE => 'https://developer.toutiao.com/', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @throws ContainerException |
||
46 | * @throws InvalidParamsException |
||
47 | * @throws ServiceNotFoundException |
||
48 | */ |
||
49 | public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket |
||
50 | { |
||
51 | $plugin = '\Yansongda\Pay\Shortcut\Douyin\\'.Str::studly($shortcut).'Shortcut'; |
||
52 | |||
53 | return Artful::shortcut($plugin, ...$params); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @throws ContainerException |
||
58 | * @throws InvalidParamsException |
||
59 | */ |
||
60 | public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket |
||
61 | { |
||
62 | return Artful::artful($plugins, $params); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @throws ContainerException |
||
67 | * @throws InvalidParamsException |
||
68 | * @throws ServiceNotFoundException |
||
69 | */ |
||
70 | public function query(array $order): Collection|Rocket |
||
71 | { |
||
72 | Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null)); |
||
73 | |||
74 | 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.
![]() |
|||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @throws InvalidParamsException |
||
79 | */ |
||
80 | public function cancel(array $order): Collection|Rocket |
||
81 | { |
||
82 | throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 抖音不支持 cancel API'); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @throws InvalidParamsException |
||
87 | */ |
||
88 | public function close(array $order): Collection|Rocket |
||
89 | { |
||
90 | throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 抖音不支持 close API'); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @throws ContainerException |
||
95 | * @throws InvalidParamsException |
||
96 | * @throws ServiceNotFoundException |
||
97 | */ |
||
98 | public function refund(array $order): Collection|Rocket |
||
99 | { |
||
100 | Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null)); |
||
101 | |||
102 | 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.
![]() |
|||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @throws ContainerException |
||
107 | * @throws InvalidParamsException |
||
108 | */ |
||
109 | public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket |
||
110 | { |
||
111 | $request = $this->getCallbackParams($contents); |
||
112 | |||
113 | Event::dispatch(new CallbackReceived('douyin', $request->all(), $params, null)); |
||
114 | |||
115 | return $this->pay([CallbackPlugin::class], $request->merge($params)->all()); |
||
0 ignored issues
–
show
The expression
return $this->pay(array(...>merge($params)->all()) 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.
![]() |
|||
116 | } |
||
117 | |||
118 | public function success(): ResponseInterface |
||
119 | { |
||
120 | return new Response( |
||
121 | 200, |
||
122 | ['Content-Type' => 'application/json'], |
||
123 | json_encode(['err_no' => 0, 'err_tips' => 'success']), |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | public function mergeCommonPlugins(array $plugins): array |
||
128 | { |
||
129 | return array_merge( |
||
130 | [StartPlugin::class], |
||
131 | $plugins, |
||
132 | [AddPayloadSignaturePlugin::class, AddPayloadBodyPlugin::class, AddRadarPlugin::class, ResponsePlugin::class, ParserPlugin::class], |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection |
||
137 | { |
||
138 | if (is_array($contents)) { |
||
139 | return Collection::wrap($contents); |
||
140 | } |
||
141 | |||
142 | if (!$contents instanceof ServerRequestInterface) { |
||
143 | $contents = ServerRequest::fromGlobals(); |
||
144 | } |
||
145 | |||
146 | $body = Collection::wrap($contents->getParsedBody()); |
||
147 | |||
148 | if ($body->isNotEmpty()) { |
||
149 | return $body; |
||
150 | } |
||
151 | |||
152 | return Collection::wrapJson((string) $contents->getBody()); |
||
153 | } |
||
154 | } |
||
155 |