From 38d4b1f243ee3ef3439e98d5d1ac8165119cdb70 Mon Sep 17 00:00:00 2001 From: recca0120 Date: Fri, 14 Jan 2022 22:46:31 +0800 Subject: [PATCH] refactor: remove ECPay_* --- .gitignore | 3 +- phpunit.xml.dist | 28 +++++++++---- src/Item.php | 39 +++++++++++++++++ src/Message/AcceptNotificationResponse.php | 10 +---- src/Message/CompletePurchaseRequest.php | 26 +++++++++++- src/Message/CompletePurchaseResponse.php | 25 +---------- src/Message/PurchaseRequest.php | 20 ++------- src/Message/PurchaseResponse.php | 6 +-- src/Message/RefundRequest.php | 3 +- src/Message/VoidRequest.php | 4 +- src/Traits/HasECPay.php | 7 ++-- src/Traits/HasSendFields.php | 13 ++---- .../Message/AcceptNotificationRequestTest.php | 41 +++++++++++++++++- tests/Message/CompletePurchaseRequestTest.php | 42 ++++++++++++++++++- tests/Message/PurchaseRequestTest.php | 14 +++---- 15 files changed, 189 insertions(+), 92 deletions(-) create mode 100644 src/Item.php diff --git a/.gitignore b/.gitignore index 4ba2503..4f0c7b5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ composer.lock vendor build .idea -.php_cs.cache +.php-cs-fixer.cache +.phpunit.result.cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 535809e..99cf64d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,22 +1,32 @@ - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + + + src/ + + + + + + + - ./tests/ + tests - - - ./src - - + + + diff --git a/src/Item.php b/src/Item.php new file mode 100644 index 0000000..8fa66c0 --- /dev/null +++ b/src/Item.php @@ -0,0 +1,39 @@ +getParameter('currency') ?: 'TWD'; + } + + public function setCurrency($value) + { + return $this->setParameter('currency', $value); + } + + public function getUrl() + { + return $this->getParameter('url'); + } + + public function setUrl($value) + { + return $this->setParameter('url', $value); + } + + public function __toString() + { + return sprintf( + '#%s %d %s x %u', + $this->getName(), + $this->getPrice(), + $this->getCurrency(), + $this->getQuantity() + ); + } +} diff --git a/src/Message/AcceptNotificationResponse.php b/src/Message/AcceptNotificationResponse.php index ced8e30..0e3eda2 100644 --- a/src/Message/AcceptNotificationResponse.php +++ b/src/Message/AcceptNotificationResponse.php @@ -2,8 +2,6 @@ namespace Omnipay\ECPay\Message; -use Exception; - class AcceptNotificationResponse extends CompletePurchaseResponse { /** @@ -13,12 +11,6 @@ class AcceptNotificationResponse extends CompletePurchaseResponse */ public function getMessage() { - try { - $this->checkoutFeedback(); - - return '1|OK'; - } catch (Exception $e) { - return '0|'.$e->getMessage(); - } + return '1|OK'; } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 6d08c4e..992d6b9 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -2,17 +2,21 @@ namespace Omnipay\ECPay\Message; +use Exception; use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\Common\Message\AbstractRequest; use Omnipay\Common\Message\NotificationInterface; use Omnipay\Common\Message\ResponseInterface; use Omnipay\ECPay\Traits\HasCustomFields; use Omnipay\ECPay\Traits\HasDefaults; +use Omnipay\ECPay\Traits\HasECPay; use Omnipay\ECPay\Traits\HasMerchantTradeNo; use Omnipay\ECPay\Traits\HasStoreID; class CompletePurchaseRequest extends AbstractRequest implements NotificationInterface { + use HasECPay; use HasDefaults; use HasMerchantTradeNo; use HasStoreID; @@ -191,12 +195,13 @@ public function setCheckMacValue($value) /** * @return array * @throws InvalidRequestException + * @throws InvalidResponseException */ public function getData() { $this->validate('MerchantID', 'CheckMacValue'); - return [ + return $this->checkMacValue([ 'CustomField1' => $this->getCustomField1(), 'CustomField2' => $this->getCustomField2(), 'CustomField3' => $this->getCustomField3(), @@ -214,7 +219,7 @@ public function getData() 'TradeDate' => $this->getTradeDate(), 'TradeNo' => $this->getTransactionReference(), 'CheckMacValue' => $this->getCheckMacValue(), - ]; + ]); } /** @@ -243,4 +248,21 @@ private function getNotification() { return ! $this->response ? $this->send() : $this->response; } + + /** + * @param array $data + * @return array + * @throws InvalidResponseException + */ + private function checkMacValue($data) + { + try { + $this->updateCheckMacValueFromGlobals($data); + $this->createECPay($this)->CheckOutFeedback(); + } catch (Exception $e) { + throw new InvalidResponseException($e->getMessage(), $e->getCode(), $e); + } + + return $data; + } } diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index a93db9f..aeed035 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -2,17 +2,13 @@ namespace Omnipay\ECPay\Message; -use Exception; use Omnipay\Common\Message\NotificationInterface; -use Omnipay\ECPay\Traits\HasECPay; class CompletePurchaseResponse extends AbstractResponse implements NotificationInterface { - use HasECPay; - public function isSuccessful() { - return $this->valid() && $this->getCode() === '1'; + return $this->getCode() === '1'; } /** @@ -29,23 +25,4 @@ public function getTransactionStatus() { return $this->isSuccessful() ? self::STATUS_COMPLETED : self::STATUS_FAILED; } - - protected function checkoutFeedback() - { - $this->createECPay($this->request)->CheckOutFeedback(); - - return true; - } - - /** - * @return bool - */ - private function valid() - { - try { - return $this->checkoutFeedback(); - } catch (Exception $e) { - return false; - } - } } diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 733b4ec..f865361 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -2,8 +2,6 @@ namespace Omnipay\ECPay\Message; -use ECPay_InvoiceState; -use ECPay_PaymentMethod; use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Message\AbstractRequest; use Omnipay\ECPay\Traits\HasATMFields; @@ -152,10 +150,7 @@ private function getSendExtend($sendFields) */ private function getCreditFields($choosePayment) { - return in_array($choosePayment, [ - ECPay_PaymentMethod::ALL, - ECPay_PaymentMethod::Credit, - ], true) ? [ + return in_array($choosePayment, ['ALL', 'Credit'], true) ? [ 'CreditInstallment' => $this->getCreditInstallment(), 'InstallmentAmount' => $this->getInstallmentAmount(), 'Redeem' => $this->getRedeem(), @@ -177,10 +172,7 @@ private function getCreditFields($choosePayment) */ private function getATMFields($choosePayment) { - return in_array($choosePayment, [ - ECPay_PaymentMethod::ALL, - ECPay_PaymentMethod::ATM, - ], true) ? [ + return in_array($choosePayment, ['ALL', 'ATM'], true) ? [ 'ExpireDate' => $this->getExpireDate(), 'PaymentInfoURL' => $this->getPaymentInfoURL(), 'ClientRedirectURL' => $this->getClientRedirectURL(), @@ -193,11 +185,7 @@ private function getATMFields($choosePayment) */ private function getCvsFields($choosePayment) { - return in_array($choosePayment, [ - ECPay_PaymentMethod::ALL, - ECPay_PaymentMethod::CVS, - ECPay_PaymentMethod::BARCODE, - ], true) ? [ + return in_array($choosePayment, ['ALL', 'CVS', 'BARCODE'], true) ? [ 'Desc_1' => $this->getDesc_1(), 'Desc_2' => $this->getDesc_2(), 'Desc_3' => $this->getDesc_3(), @@ -214,7 +202,7 @@ private function getCvsFields($choosePayment) */ private function getInvoiceFields($invoiceMark) { - return $invoiceMark === ECPay_InvoiceState::Yes ? [ + return $invoiceMark === 'Y' ? [ 'RelateNumber' => $this->getRelateNumber(), 'CustomerIdentifier' => $this->getCustomerIdentifier(), 'CarruerType' => $this->getCarruerType(), diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index 2969bd6..73934d0 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -75,7 +75,6 @@ public function getRedirectData() } } - try { return static::htmlToArray($ecPay->CheckoutString()); } catch (Exception $e) { @@ -93,9 +92,8 @@ private static function htmlToArray($html) return ! $matches ? [] : array_reduce($matches[0], static function ($data, $input) { preg_match_all('/\s*([^=]+)=\"([^\"]*)\"*/', $input, $m); - list($type, $name, $value) = $m[2]; - if ($type !== 'submit') { - $data[$name] = $value; + if ($m[2][0] !== 'submit') { + $data[$m[2][1]] = $m[2][2]; } return $data; diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 1467c4b..394441b 100644 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -2,7 +2,6 @@ namespace Omnipay\ECPay\Message; -use ECPay_ActionType; use Omnipay\Common\Message\AbstractRequest; use Omnipay\ECPay\Traits\HasDefaults; use Omnipay\ECPay\Traits\HasECPay; @@ -33,7 +32,7 @@ public function getData() return [ 'MerchantTradeNo' => $this->getTransactionId(), 'TradeNo' => $this->getTransactionReference(), - 'Action' => ECPay_ActionType::R, + 'Action' => 'R', 'TotalAmount' => $this->getAmount(), ]; } diff --git a/src/Message/VoidRequest.php b/src/Message/VoidRequest.php index 5bc13b4..e3a4f22 100644 --- a/src/Message/VoidRequest.php +++ b/src/Message/VoidRequest.php @@ -2,8 +2,6 @@ namespace Omnipay\ECPay\Message; -use ECPay_ActionType; - class VoidRequest extends RefundRequest { public function setAction($value) @@ -13,7 +11,7 @@ public function setAction($value) public function getAction() { - return $this->getParameter('action') ?: ECPay_ActionType::N; + return $this->getParameter('action') ?: 'N'; } public function getData() diff --git a/src/Traits/HasECPay.php b/src/Traits/HasECPay.php index 5360ed4..280ba94 100644 --- a/src/Traits/HasECPay.php +++ b/src/Traits/HasECPay.php @@ -6,13 +6,14 @@ trait HasECPay { + private $globalBackup = []; + /** * @param $request * @return ECPay_AllInOne */ protected function createECPay($request) { - $this->updateCheckMacValueFromGlobals($request->getData()); $ecPay = new ECPay_AllInOne(); $ecPay->HashKey = $request->getHashKey(); $ecPay->HashIV = $request->getHashIV(); @@ -28,8 +29,8 @@ protected function createECPay($request) */ private function updateCheckMacValueFromGlobals($data) { - if (! empty($data['CheckMacValue']) && empty($_POST['CheckMacValue'])) { - $_POST = $data; + if (array_key_exists('CheckMacValue', $data)) { + $_POST = array_merge($_POST, $data); } return $data; diff --git a/src/Traits/HasSendFields.php b/src/Traits/HasSendFields.php index 7ee2478..d9a80b2 100644 --- a/src/Traits/HasSendFields.php +++ b/src/Traits/HasSendFields.php @@ -2,11 +2,6 @@ namespace Omnipay\ECPay\Traits; -use ECPay_ExtraPaymentInfo; -use ECPay_InvoiceState; -use ECPay_PaymentMethod; -use ECPay_PaymentMethodItem; - trait HasSendFields { use HasTotalAmount; @@ -93,7 +88,7 @@ public function setChoosePayment($value) */ public function getChoosePayment() { - return $this->getParameter('ChoosePayment') ?: ECPay_PaymentMethod::ALL; + return $this->getParameter('ChoosePayment') ?: 'ALL'; } /** @@ -154,7 +149,7 @@ public function setChooseSubPayment($value) */ public function getChooseSubPayment() { - return $this->getParameter('ChooseSubPayment') ?: ECPay_PaymentMethodItem::None; + return $this->getParameter('ChooseSubPayment'); } /** @@ -198,7 +193,7 @@ public function setNeedExtraPaidInfo($value) */ public function getNeedExtraPaidInfo() { - return $this->getParameter('NeedExtraPaidInfo') ?: ECPay_ExtraPaymentInfo::No; + return $this->getParameter('NeedExtraPaidInfo') ?: 'N'; } /** @@ -268,7 +263,7 @@ public function setInvoiceMark($value) */ public function getInvoiceMark() { - return $this->getParameter('InvoiceMark') ?: ECPay_InvoiceState::No; + return $this->getParameter('InvoiceMark'); } /** diff --git a/tests/Message/AcceptNotificationRequestTest.php b/tests/Message/AcceptNotificationRequestTest.php index bfec9cf..6f81e9b 100644 --- a/tests/Message/AcceptNotificationRequestTest.php +++ b/tests/Message/AcceptNotificationRequestTest.php @@ -3,6 +3,7 @@ namespace Omnipay\ECPay\Tests\Message; use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\Common\Message\NotificationInterface; use Omnipay\ECPay\Message\AcceptNotificationRequest; use Omnipay\Tests\TestCase; @@ -11,6 +12,7 @@ class AcceptNotificationRequestTest extends TestCase { /** * @throws InvalidRequestException + * @throws InvalidResponseException */ public function testGetData() { @@ -54,11 +56,48 @@ public function testGetData() */ public function testSendData($results) { - list($notification, $options) = $results; + $notification = $results[0]; + $options = $results[1]; self::assertEquals($options['MerchantTradeNo'], $notification->getTransactionId()); self::assertEquals($options['TradeNo'], $notification->getTransactionReference()); self::assertEquals(NotificationInterface::STATUS_COMPLETED, $notification->getTransactionStatus()); self::assertEquals('1|OK', $notification->getMessage()); } + + public function testInvalidCheckMacValue() + { + $this->expectException(InvalidResponseException::class); + $this->expectExceptionMessage('CheckMacValue verify fail.'); + + $options = [ + 'CustomField1' => '', + 'CustomField2' => '', + 'CustomField3' => '', + 'CustomField4' => '', + 'MerchantID' => '2000132', + 'MerchantTradeNo' => '2821567410556', + 'PaymentDate' => '2019/09/02 15:49:58', + 'PaymentType' => 'Credit_CreditCard', + 'PaymentTypeChargeFee' => '1', + 'RtnCode' => '1', + 'RtnMsg' => 'Succeeded', + 'SimulatePaid' => '0', + 'StoreID' => '', + 'TradeAmt' => '4250', + 'TradeDate' => '2019/09/02 15:49:16', + 'TradeNo' => '1909021549160081', + 'CheckMacValue' => '7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', + ]; + + $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); + $request->initialize(array_merge([ + 'HashKey' => '5294y06JbISpM5x9', + 'HashIV' => 'v77hoKGq4kWxNNIS', + 'EncryptType' => '1', + 'MerchantID' => '2000132', + ], $options)); + $request->setTestMode(true); + $request->send(); + } } diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index 9748f88..b174c55 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -3,6 +3,7 @@ namespace Omnipay\ECPay\Tests\Message; use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\ECPay\Message\CompletePurchaseRequest; use Omnipay\Tests\TestCase; @@ -10,6 +11,7 @@ class CompletePurchaseRequestTest extends TestCase { /** * @throws InvalidRequestException + * @throws InvalidResponseException */ public function testGetData() { @@ -53,7 +55,8 @@ public function testGetData() */ public function testSendData($result) { - list($response, $options) = $result; + $response = $result[0]; + $options = $result[1]; self::assertTrue($response->isSuccessful()); self::assertEquals('Succeeded', $response->getMessage()); @@ -61,4 +64,41 @@ public function testSendData($result) self::assertEquals($options['TradeNo'], $response->getTransactionReference()); self::assertEquals($options['MerchantTradeNo'], $response->getTransactionId()); } + + public function testInvalidCheckMacValue() + { + $this->expectException(InvalidResponseException::class); + $this->expectExceptionMessage('CheckMacValue verify fail.'); + + $options = [ + 'CustomField1' => '', + 'CustomField2' => '', + 'CustomField3' => '', + 'CustomField4' => '', + 'MerchantID' => '2000132', + 'MerchantTradeNo' => '2821567410556', + 'PaymentDate' => '2019/09/02 15:49:58', + 'PaymentType' => 'Credit_CreditCard', + 'PaymentTypeChargeFee' => '1', + 'RtnCode' => '1', + 'RtnMsg' => 'Succeeded', + 'SimulatePaid' => '0', + 'StoreID' => '', + 'TradeAmt' => '4250', + 'TradeDate' => '2019/09/02 15:49:16', + 'TradeNo' => '1909021549160081', + 'CheckMacValue' => '7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', + ]; + + $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); + $request->initialize(array_merge([ + 'HashKey' => '5294y06JbISpM5x9', + 'HashIV' => 'v77hoKGq4kWxNNIS', + 'EncryptType' => '1', + 'MerchantID' => '2000132', + ], $options)); + $request->setTestMode(true); + + $request->send(); + } } diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index 1e25dc0..9a43673 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -2,8 +2,6 @@ namespace Omnipay\ECPay\Tests\Message; -use ECPay_ExtraPaymentInfo; -use ECPay_PaymentMethod; use Omnipay\Common\Item; use Omnipay\ECPay\Message\PurchaseRequest; use Omnipay\Tests\TestCase; @@ -23,9 +21,9 @@ public function testGetData() 'PaymentType' => 'aio', 'TotalAmount' => 2000, 'TradeDesc' => 'good to drink', - 'ChoosePayment' => ECPay_PaymentMethod::Credit, + 'ChoosePayment' => 'Credit', 'Remark' => 'remark', - 'NeedExtraPaidInfo' => ECPay_ExtraPaymentInfo::No, + 'NeedExtraPaidInfo' => 'N', 'DeviceSource' => 'Desktop', 'Items' => [[ 'Name' => '歐付寶黑芝麻豆漿', @@ -42,10 +40,10 @@ public function testGetData() $request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $request->initialize(array_merge([ - 'HashKey' => '5294y06JbISpM5x9', //測試用Hashkey,請自行帶入ECPay提供的HashKey - 'HashIV' => 'v77hoKGq4kWxNNIS', //測試用HashIV,請自行帶入ECPay提供的HashIV - 'MerchantID' => '2000132', //測試用MerchantID,請自行帶入ECPay提供的MerchantID - 'EncryptType' => '1', //CheckMacValue加密類型,請固定填入1,使用SHA256加密 + 'HashKey' => '5294y06JbISpM5x9', + 'HashIV' => 'v77hoKGq4kWxNNIS', + 'MerchantID' => '2000132', + 'EncryptType' => '1', ], $options)); $request->setTestMode(true); $request->setReturnUrl($returnUrl);