Skip to content

Commit 38d4b1f

Browse files
committed
refactor: remove ECPay_*
1 parent f33e778 commit 38d4b1f

15 files changed

+189
-92
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ composer.lock
22
vendor
33
build
44
.idea
5-
.php_cs.cache
5+
.php-cs-fixer.cache
6+
.phpunit.result.cache

phpunit.xml.dist

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
43
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
56
colors="true"
7+
verbose="true"
68
convertErrorsToExceptions="true"
79
convertNoticesToExceptions="true"
810
convertWarningsToExceptions="true"
911
processIsolation="false"
1012
stopOnFailure="false"
11-
syntaxCheck="false">
13+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">src/</directory>
17+
</include>
18+
<report>
19+
<clover outputFile="build/logs/clover.xml"/>
20+
<html outputDirectory="build/coverage"/>
21+
<text outputFile="build/coverage.txt"/>
22+
</report>
23+
</coverage>
1224
<testsuites>
1325
<testsuite name="Omnipay Test Suite">
14-
<directory>./tests/</directory>
26+
<directory>tests</directory>
1527
</testsuite>
1628
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory>./src</directory>
20-
</whitelist>
21-
</filter>
29+
<logging>
30+
<junit outputFile="build/report.junit.xml"/>
31+
</logging>
2232
</phpunit>

src/Item.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Omnipay\ECPay;
4+
5+
use Omnipay\Common\Item as BaseItem;
6+
7+
class Item extends BaseItem
8+
{
9+
public function getCurrency()
10+
{
11+
return $this->getParameter('currency') ?: 'TWD';
12+
}
13+
14+
public function setCurrency($value)
15+
{
16+
return $this->setParameter('currency', $value);
17+
}
18+
19+
public function getUrl()
20+
{
21+
return $this->getParameter('url');
22+
}
23+
24+
public function setUrl($value)
25+
{
26+
return $this->setParameter('url', $value);
27+
}
28+
29+
public function __toString()
30+
{
31+
return sprintf(
32+
'#%s %d %s x %u',
33+
$this->getName(),
34+
$this->getPrice(),
35+
$this->getCurrency(),
36+
$this->getQuantity()
37+
);
38+
}
39+
}

src/Message/AcceptNotificationResponse.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Omnipay\ECPay\Message;
44

5-
use Exception;
6-
75
class AcceptNotificationResponse extends CompletePurchaseResponse
86
{
97
/**
@@ -13,12 +11,6 @@ class AcceptNotificationResponse extends CompletePurchaseResponse
1311
*/
1412
public function getMessage()
1513
{
16-
try {
17-
$this->checkoutFeedback();
18-
19-
return '1|OK';
20-
} catch (Exception $e) {
21-
return '0|'.$e->getMessage();
22-
}
14+
return '1|OK';
2315
}
2416
}

src/Message/CompletePurchaseRequest.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
namespace Omnipay\ECPay\Message;
44

5+
use Exception;
56
use Omnipay\Common\Exception\InvalidRequestException;
7+
use Omnipay\Common\Exception\InvalidResponseException;
68
use Omnipay\Common\Message\AbstractRequest;
79
use Omnipay\Common\Message\NotificationInterface;
810
use Omnipay\Common\Message\ResponseInterface;
911
use Omnipay\ECPay\Traits\HasCustomFields;
1012
use Omnipay\ECPay\Traits\HasDefaults;
13+
use Omnipay\ECPay\Traits\HasECPay;
1114
use Omnipay\ECPay\Traits\HasMerchantTradeNo;
1215
use Omnipay\ECPay\Traits\HasStoreID;
1316

1417
class CompletePurchaseRequest extends AbstractRequest implements NotificationInterface
1518
{
19+
use HasECPay;
1620
use HasDefaults;
1721
use HasMerchantTradeNo;
1822
use HasStoreID;
@@ -191,12 +195,13 @@ public function setCheckMacValue($value)
191195
/**
192196
* @return array
193197
* @throws InvalidRequestException
198+
* @throws InvalidResponseException
194199
*/
195200
public function getData()
196201
{
197202
$this->validate('MerchantID', 'CheckMacValue');
198203

199-
return [
204+
return $this->checkMacValue([
200205
'CustomField1' => $this->getCustomField1(),
201206
'CustomField2' => $this->getCustomField2(),
202207
'CustomField3' => $this->getCustomField3(),
@@ -214,7 +219,7 @@ public function getData()
214219
'TradeDate' => $this->getTradeDate(),
215220
'TradeNo' => $this->getTransactionReference(),
216221
'CheckMacValue' => $this->getCheckMacValue(),
217-
];
222+
]);
218223
}
219224

220225
/**
@@ -243,4 +248,21 @@ private function getNotification()
243248
{
244249
return ! $this->response ? $this->send() : $this->response;
245250
}
251+
252+
/**
253+
* @param array $data
254+
* @return array
255+
* @throws InvalidResponseException
256+
*/
257+
private function checkMacValue($data)
258+
{
259+
try {
260+
$this->updateCheckMacValueFromGlobals($data);
261+
$this->createECPay($this)->CheckOutFeedback();
262+
} catch (Exception $e) {
263+
throw new InvalidResponseException($e->getMessage(), $e->getCode(), $e);
264+
}
265+
266+
return $data;
267+
}
246268
}

src/Message/CompletePurchaseResponse.php

+1-24
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
namespace Omnipay\ECPay\Message;
44

5-
use Exception;
65
use Omnipay\Common\Message\NotificationInterface;
7-
use Omnipay\ECPay\Traits\HasECPay;
86

97
class CompletePurchaseResponse extends AbstractResponse implements NotificationInterface
108
{
11-
use HasECPay;
12-
139
public function isSuccessful()
1410
{
15-
return $this->valid() && $this->getCode() === '1';
11+
return $this->getCode() === '1';
1612
}
1713

1814
/**
@@ -29,23 +25,4 @@ public function getTransactionStatus()
2925
{
3026
return $this->isSuccessful() ? self::STATUS_COMPLETED : self::STATUS_FAILED;
3127
}
32-
33-
protected function checkoutFeedback()
34-
{
35-
$this->createECPay($this->request)->CheckOutFeedback();
36-
37-
return true;
38-
}
39-
40-
/**
41-
* @return bool
42-
*/
43-
private function valid()
44-
{
45-
try {
46-
return $this->checkoutFeedback();
47-
} catch (Exception $e) {
48-
return false;
49-
}
50-
}
5128
}

src/Message/PurchaseRequest.php

+4-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Omnipay\ECPay\Message;
44

5-
use ECPay_InvoiceState;
6-
use ECPay_PaymentMethod;
75
use Omnipay\Common\Exception\InvalidRequestException;
86
use Omnipay\Common\Message\AbstractRequest;
97
use Omnipay\ECPay\Traits\HasATMFields;
@@ -152,10 +150,7 @@ private function getSendExtend($sendFields)
152150
*/
153151
private function getCreditFields($choosePayment)
154152
{
155-
return in_array($choosePayment, [
156-
ECPay_PaymentMethod::ALL,
157-
ECPay_PaymentMethod::Credit,
158-
], true) ? [
153+
return in_array($choosePayment, ['ALL', 'Credit'], true) ? [
159154
'CreditInstallment' => $this->getCreditInstallment(),
160155
'InstallmentAmount' => $this->getInstallmentAmount(),
161156
'Redeem' => $this->getRedeem(),
@@ -177,10 +172,7 @@ private function getCreditFields($choosePayment)
177172
*/
178173
private function getATMFields($choosePayment)
179174
{
180-
return in_array($choosePayment, [
181-
ECPay_PaymentMethod::ALL,
182-
ECPay_PaymentMethod::ATM,
183-
], true) ? [
175+
return in_array($choosePayment, ['ALL', 'ATM'], true) ? [
184176
'ExpireDate' => $this->getExpireDate(),
185177
'PaymentInfoURL' => $this->getPaymentInfoURL(),
186178
'ClientRedirectURL' => $this->getClientRedirectURL(),
@@ -193,11 +185,7 @@ private function getATMFields($choosePayment)
193185
*/
194186
private function getCvsFields($choosePayment)
195187
{
196-
return in_array($choosePayment, [
197-
ECPay_PaymentMethod::ALL,
198-
ECPay_PaymentMethod::CVS,
199-
ECPay_PaymentMethod::BARCODE,
200-
], true) ? [
188+
return in_array($choosePayment, ['ALL', 'CVS', 'BARCODE'], true) ? [
201189
'Desc_1' => $this->getDesc_1(),
202190
'Desc_2' => $this->getDesc_2(),
203191
'Desc_3' => $this->getDesc_3(),
@@ -214,7 +202,7 @@ private function getCvsFields($choosePayment)
214202
*/
215203
private function getInvoiceFields($invoiceMark)
216204
{
217-
return $invoiceMark === ECPay_InvoiceState::Yes ? [
205+
return $invoiceMark === 'Y' ? [
218206
'RelateNumber' => $this->getRelateNumber(),
219207
'CustomerIdentifier' => $this->getCustomerIdentifier(),
220208
'CarruerType' => $this->getCarruerType(),

src/Message/PurchaseResponse.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public function getRedirectData()
7575
}
7676
}
7777

78-
7978
try {
8079
return static::htmlToArray($ecPay->CheckoutString());
8180
} catch (Exception $e) {
@@ -93,9 +92,8 @@ private static function htmlToArray($html)
9392

9493
return ! $matches ? [] : array_reduce($matches[0], static function ($data, $input) {
9594
preg_match_all('/\s*([^=]+)=\"([^\"]*)\"*/', $input, $m);
96-
list($type, $name, $value) = $m[2];
97-
if ($type !== 'submit') {
98-
$data[$name] = $value;
95+
if ($m[2][0] !== 'submit') {
96+
$data[$m[2][1]] = $m[2][2];
9997
}
10098

10199
return $data;

src/Message/RefundRequest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Omnipay\ECPay\Message;
44

5-
use ECPay_ActionType;
65
use Omnipay\Common\Message\AbstractRequest;
76
use Omnipay\ECPay\Traits\HasDefaults;
87
use Omnipay\ECPay\Traits\HasECPay;
@@ -33,7 +32,7 @@ public function getData()
3332
return [
3433
'MerchantTradeNo' => $this->getTransactionId(),
3534
'TradeNo' => $this->getTransactionReference(),
36-
'Action' => ECPay_ActionType::R,
35+
'Action' => 'R',
3736
'TotalAmount' => $this->getAmount(),
3837
];
3938
}

src/Message/VoidRequest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Omnipay\ECPay\Message;
44

5-
use ECPay_ActionType;
6-
75
class VoidRequest extends RefundRequest
86
{
97
public function setAction($value)
@@ -13,7 +11,7 @@ public function setAction($value)
1311

1412
public function getAction()
1513
{
16-
return $this->getParameter('action') ?: ECPay_ActionType::N;
14+
return $this->getParameter('action') ?: 'N';
1715
}
1816

1917
public function getData()

src/Traits/HasECPay.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
trait HasECPay
88
{
9+
private $globalBackup = [];
10+
911
/**
1012
* @param $request
1113
* @return ECPay_AllInOne
1214
*/
1315
protected function createECPay($request)
1416
{
15-
$this->updateCheckMacValueFromGlobals($request->getData());
1617
$ecPay = new ECPay_AllInOne();
1718
$ecPay->HashKey = $request->getHashKey();
1819
$ecPay->HashIV = $request->getHashIV();
@@ -28,8 +29,8 @@ protected function createECPay($request)
2829
*/
2930
private function updateCheckMacValueFromGlobals($data)
3031
{
31-
if (! empty($data['CheckMacValue']) && empty($_POST['CheckMacValue'])) {
32-
$_POST = $data;
32+
if (array_key_exists('CheckMacValue', $data)) {
33+
$_POST = array_merge($_POST, $data);
3334
}
3435

3536
return $data;

0 commit comments

Comments
 (0)