|
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\Plugin\ParserPlugin; |
|
17
|
|
|
use Yansongda\Artful\Rocket; |
|
18
|
|
|
use Yansongda\Pay\Contract\ProviderInterface; |
|
19
|
|
|
use Yansongda\Pay\Plugin\Epay\AddPayloadSignPlugin; |
|
20
|
|
|
use Yansongda\Pay\Plugin\Epay\AddRadarPlugin; |
|
21
|
|
|
use Yansongda\Pay\Plugin\Epay\CallbackPlugin; |
|
22
|
|
|
use Yansongda\Pay\Plugin\Epay\ResponsePlugin; |
|
23
|
|
|
use Yansongda\Pay\Plugin\Epay\StartPlugin; |
|
24
|
|
|
use Yansongda\Pay\Plugin\Epay\VerifySignaturePlugin; |
|
25
|
|
|
use Yansongda\Pay\Event\CallbackReceived; |
|
26
|
|
|
use Yansongda\Pay\Event\MethodCalled; |
|
27
|
|
|
use Yansongda\Pay\Exception\Exception; |
|
28
|
|
|
use Yansongda\Pay\Pay; |
|
29
|
|
|
use Yansongda\Supports\Collection; |
|
30
|
|
|
use Yansongda\Supports\Str; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @method Collection|Rocket scan(array $order) 扫码支付[微信支付宝都可扫描] |
|
34
|
|
|
*/ |
|
35
|
|
|
class Epay implements ProviderInterface |
|
36
|
|
|
{ |
|
37
|
|
|
public const URL = [ |
|
38
|
|
|
Pay::MODE_NORMAL => 'https://mybank.jsbchina.cn:577/eis/merchant/merchantServices.htm', |
|
39
|
|
|
Pay::MODE_SANDBOX => 'https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
public function __call($name, $params) |
|
43
|
|
|
{ |
|
44
|
|
|
$plugin = '\\Yansongda\\Pay\\Shortcut\\Epay\\'.Str::studly($name).'Shortcut'; |
|
45
|
|
|
|
|
46
|
|
|
return Artful::shortcut($plugin, ...$params); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws ContainerException |
|
51
|
|
|
* @throws InvalidParamsException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket |
|
54
|
|
|
{ |
|
55
|
|
|
return Artful::artful($plugins, $params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function mergeCommonPlugins(array $plugins): array |
|
59
|
|
|
{ |
|
60
|
|
|
return array_merge( |
|
61
|
|
|
[StartPlugin::class], |
|
62
|
|
|
$plugins, |
|
63
|
|
|
[AddPayloadSignPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class], |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function cancel($order): Collection|Rocket |
|
68
|
|
|
{ |
|
69
|
|
|
$order = is_array($order) ? $order : ['outTradeNo' => $order]; |
|
70
|
|
|
|
|
71
|
|
|
Event::dispatch(new MethodCalled('epay', __METHOD__, $order, null)); |
|
72
|
|
|
|
|
73
|
|
|
return $this->__call('cancel', [$order]); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function close($order): Collection|Rocket |
|
77
|
|
|
{ |
|
78
|
|
|
throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Epay does not support close api'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function refund(array $order): Collection|Rocket |
|
82
|
|
|
{ |
|
83
|
|
|
Event::dispatch(new MethodCalled('epay', __METHOD__, $order, null)); |
|
84
|
|
|
|
|
85
|
|
|
return $this->__call('refund', [$order]); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket |
|
89
|
|
|
{ |
|
90
|
|
|
$request = $this->getCallbackParams($contents); |
|
91
|
|
|
|
|
92
|
|
|
Event::dispatch(new CallbackReceived('epay', $request, $params, null)); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
return $this->pay( |
|
|
|
|
|
|
95
|
|
|
[CallbackPlugin::class], |
|
96
|
|
|
['request' => $request, 'params' => $params] |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function success(): ResponseInterface |
|
101
|
|
|
{ |
|
102
|
|
|
return new Response( |
|
103
|
|
|
200, |
|
104
|
|
|
['Content-Type' => 'text/html'], |
|
105
|
|
|
'success', |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function query(array $order): Collection|Rocket |
|
110
|
|
|
{ |
|
111
|
|
|
$order = is_array($order) ? $order : ['outTradeNo' => $order]; |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
Event::dispatch(new MethodCalled('epay', __METHOD__, $order, null)); |
|
114
|
|
|
|
|
115
|
|
|
return $this->__call('query', [$order]); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function getCallbackParams($contents = null): Collection |
|
119
|
|
|
{ |
|
120
|
|
|
if (is_array($contents)) { |
|
121
|
|
|
return Collection::wrap($contents); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($contents instanceof ServerRequestInterface) { |
|
125
|
|
|
return Collection::wrap($contents->getParsedBody()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$request = ServerRequest::fromGlobals(); |
|
129
|
|
|
|
|
130
|
|
|
return Collection::wrap($request->getParsedBody()); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|