Skip to content

Commit 4ef8179

Browse files
committed
feat: throw invalid request exception
1 parent f37ae93 commit 4ef8179

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/Message/CompletePurchaseRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Omnipay\Common\Exception\InvalidRequestException;
66
use Omnipay\Common\Message\AbstractRequest;
77
use Omnipay\Common\Message\NotificationInterface;
8+
use Omnipay\Common\Message\ResponseInterface;
89
use Omnipay\ECPay\Traits\HasCustomFields;
910
use Omnipay\ECPay\Traits\HasDefaults;
1011
use Omnipay\ECPay\Traits\HasMerchantTradeNo;
@@ -236,7 +237,7 @@ public function getMessage()
236237
}
237238

238239
/**
239-
* @return NotificationInterface
240+
* @return ResponseInterface
240241
*/
241242
private function getNotification()
242243
{

src/Message/PurchaseResponse.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Omnipay\ECPay\Message;
44

5+
use Exception;
6+
use Omnipay\Common\Exception\InvalidRequestException;
57
use Omnipay\Common\Message\AbstractResponse;
68
use Omnipay\Common\Message\RedirectResponseInterface;
79
use Omnipay\ECPay\Traits\HasECPay;
@@ -54,13 +56,18 @@ public function getRedirectMethod()
5456
* Gets the redirect form data array, if the redirect method is POST.
5557
*
5658
* @return array
59+
* @throws InvalidRequestException
5760
*/
5861
public function getRedirectData()
5962
{
6063
$ecPay = $this->createECPay($this->request);
6164
$ecPay->ServiceURL = $this->getRedirectUrl();
6265

63-
return static::htmlToArray($ecPay->CheckoutString());
66+
try {
67+
return static::htmlToArray($ecPay->CheckoutString());
68+
} catch (Exception $e) {
69+
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
70+
}
6471
}
6572

6673
/**
@@ -69,21 +76,16 @@ public function getRedirectData()
6976
*/
7077
private static function htmlToArray($html)
7178
{
72-
preg_match_all('/<input[^>]*>/i', $html, $matches);
73-
74-
if (! $matches) {
75-
return [];
76-
}
79+
preg_match_all('/<input(?!type="submit")[^>]*>/i', $html, $matches);
7780

78-
$data = [];
79-
foreach ($matches[0] as $input) {
81+
return ! $matches ? [] : array_reduce($matches[0], static function ($data, $input) {
8082
preg_match_all('/\s*([^=]+)=\"([^\"]*)\"*/', $input, $m);
8183
list($type, $name, $value) = $m[2];
8284
if ($type !== 'submit') {
8385
$data[$name] = $value;
8486
}
85-
}
8687

87-
return $data;
88+
return $data;
89+
}, []);
8890
}
8991
}

tests/Message/FetchTransactionRequestTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function testGetData()
2525
'HashIV' => 'v77hoKGq4kWxNNIS',
2626
'EncryptType' => '1',
2727
'MerchantID' => '2000132',
28-
'testMode' => true,
2928
], $options));
3029
$request->setTestMode(true);
3130

tests/Message/PurchaseRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function testGetData()
1717
$returnUrl = 'https://foo.bar/return_url';
1818
$notifyUrl = 'https://foo.bar/notify_url';
1919
$options = [
20-
2120
'ReturnURL' => $notifyUrl,
2221
'ClientBackURL' => 'https://foo.bar/client_back_url',
2322
'OrderResultURL' => $returnUrl,
@@ -77,6 +76,7 @@ public function testSendData($results)
7776

7877
$redirectData = $response->getRedirectData();
7978

79+
self::assertArrayNotHasKey('__paymentButton', $redirectData);
8080
self::assertFalse($response->isSuccessful());
8181
self::assertTrue($response->isRedirect());
8282
self::assertEquals('POST', $response->getRedirectMethod());

0 commit comments

Comments
 (0)