|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Yansongda\Pay\Contract\PluginInterface; |
|
9
|
|
|
use Yansongda\Pay\Exception\Exception; |
|
10
|
|
|
use Yansongda\Pay\Exception\InvalidResponseException; |
|
11
|
|
|
use Yansongda\Pay\Logger; |
|
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
|
|
|
Logger::info('[alipay][LaunchPlugin] 插件开始装载', ['rocket' => $rocket]); |
|
30
|
|
|
|
|
31
|
|
|
if (should_do_http_request($rocket)) { |
|
32
|
|
|
$this->verifySign($rocket); |
|
33
|
|
|
|
|
34
|
|
|
$rocket->setDestination( |
|
35
|
|
|
Collection::wrap( |
|
36
|
|
|
$rocket->getDestination()->get($this->getResultKey($rocket->getPayload())) |
|
|
|
|
|
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
Logger::info('[alipay][LaunchPlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
42
|
|
|
|
|
43
|
|
|
return $rocket; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
48
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
49
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
50
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidResponseException |
|
51
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function verifySign(Rocket $rocket): void |
|
54
|
|
|
{ |
|
55
|
|
|
$response = $rocket->getDestination(); |
|
56
|
|
|
$result = $response->get($this->getResultKey($rocket->getPayload())); |
|
|
|
|
|
|
57
|
|
|
$sign = $response->get('sign', ''); |
|
58
|
|
|
|
|
59
|
|
|
if ('' === $sign || is_null($result)) { |
|
60
|
|
|
throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Alipay Response Sign Failed', $response); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
verify_alipay_sign($rocket->getParams(), json_encode($result, JSON_UNESCAPED_UNICODE), base64_decode($sign)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getResultKey(Collection $payload): string |
|
67
|
|
|
{ |
|
68
|
|
|
$method = $payload->get('method'); |
|
69
|
|
|
|
|
70
|
|
|
return str_replace('.', '_', $method).'_response'; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|