1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Provider; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Yansongda\Pay\Contract\ShortcutInterface; |
10
|
|
|
use Yansongda\Pay\Exception\InvalidParamsException; |
11
|
|
|
use Yansongda\Pay\Pay; |
12
|
|
|
use Yansongda\Pay\Plugin\Alipay\CallbackPlugin; |
13
|
|
|
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin; |
14
|
|
|
use Yansongda\Pay\Plugin\Alipay\PreparePlugin; |
15
|
|
|
use Yansongda\Pay\Plugin\Alipay\RadarPlugin; |
16
|
|
|
use Yansongda\Pay\Plugin\Alipay\SignPlugin; |
17
|
|
|
use Yansongda\Pay\Plugin\ParserPlugin; |
18
|
|
|
use Yansongda\Supports\Collection; |
19
|
|
|
use Yansongda\Supports\Str; |
20
|
|
|
|
21
|
|
|
class Alipay extends AbstractProvider |
22
|
|
|
{ |
23
|
|
|
public const URL = [ |
24
|
|
|
Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8', |
25
|
|
|
Pay::MODE_SANDBOX => 'https://openapi.alipaydev.com/gateway.do?charset=utf-8', |
26
|
|
|
Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
31
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
32
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
33
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
34
|
|
|
* |
35
|
|
|
* @return \Yansongda\Supports\Collection|\Psr\Http\Message\ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
public function __call(string $shortcut, array $params) |
38
|
|
|
{ |
39
|
|
|
$plugin = '\\Yansongda\\Pay\\Plugin\\Alipay\\Shortcut\\'. |
40
|
|
|
Str::studly($shortcut).'Shortcut'; |
41
|
|
|
|
42
|
|
|
if (!class_exists($plugin) || !in_array(ShortcutInterface::class, class_implements($plugin))) { |
43
|
|
|
throw new InvalidParamsException(InvalidParamsException::SHORTCUT_NOT_FOUND, "[$plugin] is not incompatible"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* @var ShortcutInterface $money */ |
47
|
|
|
$money = Pay::get($plugin); |
48
|
|
|
|
49
|
|
|
return $this->pay( |
50
|
|
|
$this->mergeCommonPlugins($money->getPlugins(...$params)), |
|
|
|
|
51
|
|
|
...$params |
|
|
|
|
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|array $order |
57
|
|
|
* |
58
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
59
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
60
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
61
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
62
|
|
|
*/ |
63
|
|
|
public function find($order): Collection |
64
|
|
|
{ |
65
|
|
|
$order = is_array($order) ? $order : ['out_trade_no' => $order]; |
66
|
|
|
|
67
|
|
|
return $this->__call('query', [$order]); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|array $order |
72
|
|
|
* |
73
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
74
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
75
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
76
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
77
|
|
|
*/ |
78
|
|
|
public function cancel($order): Collection |
79
|
|
|
{ |
80
|
|
|
$order = is_array($order) ? $order : ['out_trade_no' => $order]; |
81
|
|
|
|
82
|
|
|
return $this->__call('cancel', [$order]); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string|array $order |
87
|
|
|
* |
88
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
89
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
90
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
91
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
92
|
|
|
*/ |
93
|
|
|
public function close($order): Collection |
94
|
|
|
{ |
95
|
|
|
$order = is_array($order) ? $order : ['out_trade_no' => $order]; |
96
|
|
|
|
97
|
|
|
return $this->__call('close', [$order]); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
102
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
103
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
104
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
105
|
|
|
*/ |
106
|
|
|
public function refund(array $order): Collection |
107
|
|
|
{ |
108
|
|
|
return $this->__call('refund', [$order]); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array|ServerRequestInterface|null $contents |
113
|
|
|
* |
114
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
115
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
116
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException |
117
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
118
|
|
|
*/ |
119
|
|
|
public function verify($contents = null, ?array $config = null): Collection |
120
|
|
|
{ |
121
|
|
|
$response = $this->getCallbackParams($contents); |
122
|
|
|
|
123
|
|
|
return $this->pay( |
|
|
|
|
124
|
|
|
[CallbackPlugin::class], $response->merge($config)->all() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function mergeCommonPlugins(array $plugins): array |
129
|
|
|
{ |
130
|
|
|
return array_merge( |
131
|
|
|
[PreparePlugin::class], |
132
|
|
|
$plugins, |
133
|
|
|
[SignPlugin::class, RadarPlugin::class], |
134
|
|
|
[LaunchPlugin::class, ParserPlugin::class], |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array|ServerRequestInterface|null $contents |
140
|
|
|
*/ |
141
|
|
|
protected function getCallbackParams($contents = null): Collection |
142
|
|
|
{ |
143
|
|
|
if (is_array($contents)) { |
144
|
|
|
return Collection::wrap($contents); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($contents instanceof ServerRequestInterface) { |
148
|
|
|
return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() : |
149
|
|
|
$contents->getParsedBody()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$request = ServerRequest::fromGlobals(); |
153
|
|
|
|
154
|
|
|
return Collection::wrap( |
155
|
|
|
array_merge($request->getQueryParams(), $request->getParsedBody()) |
|
|
|
|
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|