HtmlResponsePlugin.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay\Plugin\Unipay;
  4. use Closure;
  5. use GuzzleHttp\Psr7\Response;
  6. use Yansongda\Pay\Contract\PluginInterface;
  7. use Yansongda\Pay\Logger;
  8. use Yansongda\Pay\Rocket;
  9. use Yansongda\Supports\Collection;
  10. class HtmlResponsePlugin implements PluginInterface
  11. {
  12. public function assembly(Rocket $rocket, Closure $next): Rocket
  13. {
  14. /* @var Rocket $rocket */
  15. $rocket = $next($rocket);
  16. Logger::debug('[unipay][HtmlResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
  17. $radar = $rocket->getRadar();
  18. $response = $this->buildHtml($radar->getUri()->__toString(), $rocket->getPayload());
  19. $rocket->setDestination($response);
  20. Logger::info('[unipay][HtmlResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
  21. return $rocket;
  22. }
  23. protected function buildHtml(string $endpoint, Collection $payload): Response
  24. {
  25. $sHtml = "<form id='pay_form' name='pay_form' action='".$endpoint."' method='POST'>";
  26. foreach ($payload->all() as $key => $val) {
  27. $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
  28. }
  29. $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
  30. $sHtml .= "<script>document.forms['pay_form'].submit();</script>";
  31. return new Response(200, [], $sHtml);
  32. }
  33. }