Skip to content

Commit 3bfcfef

Browse files
committed
Improve AbstractResponse::redirect()
1 parent 8541b44 commit 3bfcfef

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/Omnipay/Common/Message/AbstractResponse.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function getTransactionReference()
6262

6363
/**
6464
* Automatically perform any required redirect
65+
*
66+
* This method is meant to be a helper for simple scenarios. If you want to customize the
67+
* redirection page, just call the getRedirectUrl() and getRedirectData() methods directly.
6568
*/
6669
public function redirect()
6770
{
@@ -70,44 +73,39 @@ public function redirect()
7073
}
7174

7275
if ('GET' === $this->getRedirectMethod()) {
73-
return HttpRedirectResponse::create($this->getRedirectUrl())->send();
76+
HttpRedirectResponse::create($this->getRedirectUrl())->send();
77+
exit;
7478
} elseif ('POST' === $this->getRedirectMethod()) {
75-
$hiddenFields = implode(
76-
"\n",
77-
array_map(
78-
function ($name, $value) {
79-
return sprintf(
80-
'<input type="hidden" name="%1$s" value="%2$s" />',
81-
htmlspecialchars($name, ENT_QUOTES, 'UTF-8'),
82-
htmlspecialchars($value, ENT_QUOTES, 'UTF-8')
83-
);
84-
},
85-
$this->getRedirectData()
86-
)
87-
);
88-
89-
$output = <<<EOF
90-
<!DOCTYPE html>
79+
$hiddenFields = '';
80+
foreach ($this->getRedirectData() as $key => $value) {
81+
$hiddenFields .= sprintf(
82+
'<input type="hidden" name="%1$s" value="%2$s" />',
83+
htmlspecialchars($key, ENT_QUOTES, 'UTF-8'),
84+
htmlspecialchars($value, ENT_QUOTES, 'UTF-8')
85+
)."\n";
86+
}
87+
88+
$output = '<!DOCTYPE html>
9189
<html>
9290
<head>
9391
<title>Redirecting...</title>
9492
</head>
9593
<body onload="document.forms[0].submit();">
9694
<form action="%1$s" method="post">
97-
<p>Redirecting to payment gateway...</p>
95+
<p>Redirecting to payment page...</p>
9896
<p>
9997
%2$s
10098
<input type="submit" value="Continue" />
10199
</p>
102100
</form>
103101
</body>
104102
</html>';
105-
EOF;
106103
$output = sprintf($output, htmlspecialchars($this->redirectUrl, ENT_QUOTES, 'UTF-8'), $hiddenFields);
107104

108-
return HttpResponse::create($output)->send();
105+
HttpResponse::create($output)->send();
106+
exit;
109107
}
110108

111-
throw new RuntimeException("Unexpected redirect method '{$response->getRedirectMethod()}'");
109+
throw new RuntimeException('Invalid redirect method "'.$response->getRedirectMethod().'".');
112110
}
113111
}

0 commit comments

Comments
 (0)