1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Unipay\Open; |
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
|
|
|
use function Yansongda\Artful\filter_params; |
15
|
|
|
|
16
|
|
|
class ResponseHtmlPlugin implements PluginInterface |
17
|
|
|
{ |
18
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
19
|
|
|
{ |
20
|
|
|
/* @var Rocket $rocket */ |
21
|
|
|
$rocket = $next($rocket); |
22
|
|
|
|
23
|
|
|
Logger::debug('[Unipay][ResponseHtmlPlugin] 插件开始装载', ['rocket' => $rocket]); |
24
|
|
|
|
25
|
|
|
$radar = $rocket->getRadar(); |
26
|
|
|
$payload = $rocket->getPayload(); |
27
|
|
|
|
28
|
|
|
$response = $this->buildHtml($radar->getUri()->__toString(), filter_params($payload)); |
29
|
|
|
|
30
|
|
|
$rocket->setDestination($response); |
31
|
|
|
|
32
|
|
|
Logger::info('[Unipay][ResponseHtmlPlugin] 插件装载完毕', ['rocket' => $rocket]); |
33
|
|
|
|
34
|
|
|
return $rocket; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function buildHtml(string $endpoint, Collection $payload): Response |
38
|
|
|
{ |
39
|
|
|
$sHtml = "<form id='pay_form' name='pay_form' action='".$endpoint."' method='POST'>"; |
40
|
|
|
foreach ($payload->all() as $key => $val) { |
41
|
|
|
$sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>"; |
42
|
|
|
} |
43
|
|
|
$sHtml .= "<input type='submit' value='ok' style='display:none;'></form>"; |
44
|
|
|
$sHtml .= "<script>document.forms['pay_form'].submit();</script>"; |
45
|
|
|
|
46
|
|
|
return new Response(200, [], $sHtml); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|