Alipay.php
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Provider;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\CallbackPlugin;
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
use Yansongda\Pay\Plugin\Alipay\RadarSignPlugin;
use Yansongda\Pay\Plugin\ParserPlugin;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;
/**
* @method ResponseInterface app(array $order) APP 支付
* @method Collection pos(array $order) 刷卡支付
* @method Collection scan(array $order) 扫码支付
* @method Collection transfer(array $order) 帐户转账
* @method ResponseInterface wap(array $order) 手机网站支付
* @method ResponseInterface web(array $order) 电脑支付
* @method Collection mini(array $order) 小程序支付
*/
class Alipay extends AbstractProvider
{
public const URL = [
Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
Pay::MODE_SANDBOX => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8',
Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
];
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function __call(string $shortcut, array $params)
{
$plugin = '\\Yansongda\\Pay\\Plugin\\Alipay\\Shortcut\\'.
Str::studly($shortcut).'Shortcut';
return $this->call($plugin, ...$params);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function find($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('query', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function cancel($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('cancel', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function close($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('close', [$order]);
}
/**
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function refund(array $order)
{
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('refund', [$order]);
}
/**
* @param null|array|ServerRequestInterface $contents
*
* @throws ContainerException
* @throws InvalidParamsException
*/
public function callback($contents = null, ?array $params = null): Collection
{
$request = $this->getCallbackParams($contents);
Event::dispatch(new Event\CallbackReceived('alipay', $request->all(), $params, null));
return $this->pay(
[CallbackPlugin::class],
$request->merge($params)->all()
);
}
public function success(): ResponseInterface
{
return new Response(200, [], 'success');
}
public function mergeCommonPlugins(array $plugins): array
{
return array_merge(
[PreparePlugin::class],
$plugins,
[RadarSignPlugin::class],
[LaunchPlugin::class, ParserPlugin::class],
);
}
/**
* @param null|array|ServerRequestInterface $contents
*/
protected function getCallbackParams($contents = null): Collection
{
if (is_array($contents)) {
return Collection::wrap($contents);
}
if ($contents instanceof ServerRequestInterface) {
return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() :
$contents->getParsedBody());
}
$request = ServerRequest::fromGlobals();
return Collection::wrap(
array_merge($request->getQueryParams(), $request->getParsedBody())
);
}
}