yansongda /
pay
| 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()) |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | : $this->buildHtml($radar->getUri()->__toString(), $rocket->getPayload()); |
||||
|
0 ignored issues
–
show
It seems like
$rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Ali...HtmlPlugin::buildHtml() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 |