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\Event; |
14
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
15
|
|
|
use Yansongda\Artful\Exception\InvalidParamsException; |
16
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
17
|
|
|
use Yansongda\Artful\Plugin\AddPayloadBodyPlugin; |
18
|
|
|
use Yansongda\Artful\Plugin\ParserPlugin; |
19
|
|
|
use Yansongda\Artful\Rocket; |
20
|
|
|
use Yansongda\Pay\Contract\ProviderInterface; |
21
|
|
|
use Yansongda\Pay\Event\CallbackReceived; |
22
|
|
|
use Yansongda\Pay\Event\MethodCalled; |
23
|
|
|
use Yansongda\Pay\Exception\Exception; |
24
|
|
|
use Yansongda\Pay\Pay; |
25
|
|
|
use Yansongda\Pay\Plugin\Unipay\AddRadarPlugin; |
26
|
|
|
use Yansongda\Pay\Plugin\Unipay\Open\AddPayloadSignaturePlugin; |
27
|
|
|
use Yansongda\Pay\Plugin\Unipay\Open\CallbackPlugin; |
28
|
|
|
use Yansongda\Pay\Plugin\Unipay\Open\StartPlugin; |
29
|
|
|
use Yansongda\Pay\Plugin\Unipay\Open\VerifySignaturePlugin; |
30
|
|
|
use Yansongda\Supports\Collection; |
31
|
|
|
use Yansongda\Supports\Str; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @method ResponseInterface|Rocket web(array $order) 电脑支付 |
35
|
|
|
* @method ResponseInterface|Rocket h5(array $order) H5支付 |
36
|
|
|
* @method Collection|Rocket pos(array $order) 刷卡支付(付款码,被扫码) |
37
|
|
|
* @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫) |
38
|
|
|
*/ |
39
|
|
|
class Unipay implements ProviderInterface |
40
|
|
|
{ |
41
|
|
|
public const URL = [ |
42
|
|
|
Pay::MODE_NORMAL => 'https://gateway.95516.com/', |
43
|
|
|
Pay::MODE_SANDBOX => 'https://gateway.test.95516.com/', |
44
|
|
|
Pay::MODE_SERVICE => 'https://gateway.95516.com/', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws ContainerException |
49
|
|
|
* @throws InvalidParamsException |
50
|
|
|
* @throws ServiceNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket |
53
|
|
|
{ |
54
|
|
|
$plugin = '\\Yansongda\\Pay\\Shortcut\\Unipay\\'.Str::studly($shortcut).'Shortcut'; |
55
|
|
|
|
56
|
|
|
return Artful::shortcut($plugin, ...$params); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws ContainerException |
61
|
|
|
* @throws InvalidParamsException |
62
|
|
|
*/ |
63
|
|
|
public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket |
64
|
|
|
{ |
65
|
|
|
return Artful::artful($plugins, $params); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws ContainerException |
70
|
|
|
* @throws InvalidParamsException |
71
|
|
|
* @throws ServiceNotFoundException |
72
|
|
|
*/ |
73
|
|
|
public function query(array $order): Collection|Rocket |
74
|
|
|
{ |
75
|
|
|
Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null)); |
76
|
|
|
|
77
|
|
|
return $this->__call('query', [$order]); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws ContainerException |
82
|
|
|
* @throws InvalidParamsException |
83
|
|
|
* @throws ServiceNotFoundException |
84
|
|
|
*/ |
85
|
|
|
public function cancel(array $order): Collection|Rocket |
86
|
|
|
{ |
87
|
|
|
Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null)); |
88
|
|
|
|
89
|
|
|
return $this->__call('cancel', [$order]); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws InvalidParamsException |
94
|
|
|
*/ |
95
|
|
|
public function close(array $order): Collection|Rocket |
96
|
|
|
{ |
97
|
|
|
throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 银联不支持 close API'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws ContainerException |
102
|
|
|
* @throws InvalidParamsException |
103
|
|
|
* @throws ServiceNotFoundException |
104
|
|
|
*/ |
105
|
|
|
public function refund(array $order): Collection|Rocket |
106
|
|
|
{ |
107
|
|
|
Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null)); |
108
|
|
|
|
109
|
|
|
return $this->__call('refund', [$order]); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws ContainerException |
114
|
|
|
* @throws InvalidParamsException |
115
|
|
|
*/ |
116
|
|
|
public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket |
117
|
|
|
{ |
118
|
|
|
$request = $this->getCallbackParams($contents); |
119
|
|
|
|
120
|
|
|
Event::dispatch(new CallbackReceived('unipay', $request->all(), $params, null)); |
121
|
|
|
|
122
|
|
|
return $this->pay( |
|
|
|
|
123
|
|
|
[CallbackPlugin::class], |
124
|
|
|
$request->merge($params)->all() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function success(): ResponseInterface |
129
|
|
|
{ |
130
|
|
|
return new Response(200, [], 'success'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function mergeCommonPlugins(array $plugins): array |
134
|
|
|
{ |
135
|
|
|
return array_merge( |
136
|
|
|
[StartPlugin::class], |
137
|
|
|
$plugins, |
138
|
|
|
[AddPayloadSignaturePlugin::class, AddPayloadBodyPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ParserPlugin::class], |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection |
143
|
|
|
{ |
144
|
|
|
if (is_array($contents)) { |
145
|
|
|
return Collection::wrap($contents); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($contents instanceof ServerRequestInterface) { |
149
|
|
|
return Collection::wrap($contents->getParsedBody()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$request = ServerRequest::fromGlobals(); |
153
|
|
|
|
154
|
|
|
return Collection::wrap($request->getParsedBody()); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|