|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay\V2; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
|
10
|
|
|
use Yansongda\Artful\Logger; |
|
11
|
|
|
use Yansongda\Artful\Rocket; |
|
12
|
|
|
use Yansongda\Supports\Collection; |
|
13
|
|
|
|
|
14
|
|
|
class ResponseHtmlPlugin implements PluginInterface |
|
15
|
|
|
{ |
|
16
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
17
|
|
|
{ |
|
18
|
|
|
/* @var Rocket $rocket */ |
|
19
|
|
|
$rocket = $next($rocket); |
|
20
|
|
|
|
|
21
|
|
|
Logger::debug('[Alipay][ResponseHtmlPlugin] 插件开始装载', ['rocket' => $rocket]); |
|
22
|
|
|
|
|
23
|
|
|
$radar = $rocket->getRadar(); |
|
24
|
|
|
|
|
25
|
|
|
$response = 'GET' === $radar->getMethod() |
|
26
|
|
|
? $this->buildRedirect($radar->getUri()->__toString(), $rocket->getPayload()) |
|
|
|
|
|
|
27
|
|
|
: $this->buildHtml($radar->getUri()->__toString(), $rocket->getPayload()); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$rocket->setDestination($response); |
|
30
|
|
|
|
|
31
|
|
|
Logger::info('[Alipay][ResponseHtmlPlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
32
|
|
|
|
|
33
|
|
|
return $rocket; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function buildRedirect(string $endpoint, Collection $payload): Response |
|
37
|
|
|
{ |
|
38
|
|
|
$url = $endpoint.(!str_contains($endpoint, '?') ? '?' : '&').$payload->query(); |
|
39
|
|
|
|
|
40
|
|
|
$content = sprintf( |
|
41
|
|
|
'<!DOCTYPE html> |
|
42
|
|
|
<html lang="en"> |
|
43
|
|
|
<head> |
|
44
|
|
|
<meta charset="UTF-8" /> |
|
45
|
|
|
<meta http-equiv="refresh" content="0;url=\'%1$s\'" /> |
|
46
|
|
|
|
|
47
|
|
|
<title>Redirecting to %1$s</title> |
|
48
|
|
|
</head> |
|
49
|
|
|
<body> |
|
50
|
|
|
Redirecting to %1$s. |
|
51
|
|
|
</body> |
|
52
|
|
|
</html>', |
|
53
|
|
|
htmlspecialchars($url, ENT_QUOTES) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return new Response(302, ['Location' => $url], $content); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function buildHtml(string $endpoint, Collection $payload): Response |
|
60
|
|
|
{ |
|
61
|
|
|
$sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='POST'>"; |
|
62
|
|
|
foreach ($payload->all() as $key => $val) { |
|
63
|
|
|
$val = str_replace("'", ''', $val); |
|
64
|
|
|
$sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>"; |
|
65
|
|
|
} |
|
66
|
|
|
$sHtml .= "<input type='submit' value='ok' style='display:none;'></form>"; |
|
67
|
|
|
$sHtml .= "<script>document.forms['alipay_submit'].submit();</script>"; |
|
68
|
|
|
|
|
69
|
|
|
return new Response(200, [], $sHtml); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|