|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Yansongda\Pay\Contract\PluginInterface; |
|
10
|
|
|
use Yansongda\Pay\Exception\InvalidConfigException; |
|
11
|
|
|
use Yansongda\Pay\Exception\InvalidResponseException; |
|
12
|
|
|
use Yansongda\Pay\Rocket; |
|
13
|
|
|
use Yansongda\Supports\Collection; |
|
14
|
|
|
|
|
15
|
|
|
class LaunchPlugin implements PluginInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
19
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
20
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
21
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidResponseException |
|
22
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
25
|
|
|
{ |
|
26
|
|
|
/* @var Rocket $rocket */ |
|
27
|
|
|
$rocket = $next($rocket); |
|
28
|
|
|
|
|
29
|
|
|
if ($rocket->getDestination() instanceof ResponseInterface) { |
|
30
|
|
|
return $rocket; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$rocket->setDestination($this->getMethodResponse($rocket)); |
|
34
|
|
|
|
|
35
|
|
|
return $rocket; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
40
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
41
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
42
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidResponseException |
|
43
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getMethodResponse(Rocket $rocket): Collection |
|
46
|
|
|
{ |
|
47
|
|
|
$response = Collection::wrap( |
|
48
|
|
|
$rocket->getDestination()->get($this->getResponseKey($rocket)) |
|
|
|
|
|
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$this->verifySign($rocket); |
|
52
|
|
|
|
|
53
|
|
|
if (10000 != $response->get('code', 10000)) { |
|
54
|
|
|
throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_CODE, 'Invalid response code', $response->all()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $response; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
62
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
63
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
64
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidResponseException |
|
65
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function verifySign(Rocket $rocket): void |
|
68
|
|
|
{ |
|
69
|
|
|
$response = $rocket->getDestination()->get($this->getResponseKey($rocket)); |
|
|
|
|
|
|
70
|
|
|
$sign = $rocket->getDestination()->get('sign', ''); |
|
71
|
|
|
|
|
72
|
|
|
if ('' === $sign || is_null($response)) { |
|
73
|
|
|
throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$result = openssl_verify(json_encode($response, JSON_UNESCAPED_UNICODE), base64_decode($sign), $this->getAlipayPublicKey($rocket), OPENSSL_ALGO_SHA256); |
|
77
|
|
|
|
|
78
|
|
|
if (1 !== $result) { |
|
79
|
|
|
throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getResponseKey(Rocket $rocket): string |
|
84
|
|
|
{ |
|
85
|
|
|
$method = $rocket->getPayload()->get('method'); |
|
86
|
|
|
|
|
87
|
|
|
return str_replace('.', '_', $method).'_response'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
92
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
93
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
94
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
protected function getAlipayPublicKey(Rocket $rocket) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$public = get_alipay_config($rocket->getParams())->get('alipay_public_cert_path'); |
|
99
|
|
|
|
|
100
|
|
|
if (is_null($public)) { |
|
101
|
|
|
throw new InvalidConfigException(InvalidConfigException::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_public_cert_path]'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return get_alipay_cert($public); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: