diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index e68cc83..53d142b 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -16,16 +16,29 @@ public function getData() { $data = $this->httpRequest->request->all(); $tradeSha = $this->tradeSha($data['TradeInfo']); - $data['Result'] = $this->decrypt($data['TradeInfo']); if (! hash_equals($tradeSha, $data['TradeSha'])) { throw new InvalidResponseException('Incorrect TradeSha'); } - return $data; + $responseText = $this->decrypt($data['TradeInfo']); + + $decode = json_decode($responseText, true); + if (json_last_error() === JSON_ERROR_NONE) { + $decode = array_merge([ + 'Status' => $decode['Status'], + 'Message' => $decode['Message'], + ], $decode['Result']); + } else { + $decode = []; + parse_str($responseText, $decode); + } + + return $decode; } public function sendData($data) { + return $this->response = new CompletePurchaseResponse($this, $data); } } diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index e9e2305..f41effc 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -11,7 +11,7 @@ public function isSuccessful(): bool public function getMessage() { - return $this->data['Result']['Message']; + return $this->data['Message']; } public function getCode() @@ -21,15 +21,11 @@ public function getCode() public function getTransactionReference() { - return array_key_exists('Result', $this->data['Result']) - ? $this->data['Result']['Result']['TradeNo'] - : $this->data['Result']['TradeNo']; + return $this->data['TradeNo']; } public function getTransactionId() { - return array_key_exists('Result', $this->data['Result']) - ? $this->data['Result']['Result']['MerchantOrderNo'] - : $this->data['Result']['MerchantOrderNo']; + return $this->data['MerchantOrderNo']; } } diff --git a/src/Message/FetchTransactionRequest.php b/src/Message/FetchTransactionRequest.php index b4c1b71..e0f555d 100644 --- a/src/Message/FetchTransactionRequest.php +++ b/src/Message/FetchTransactionRequest.php @@ -44,6 +44,7 @@ public function getGateway() /** * @throws InvalidRequestException + * @throws InvalidResponseException */ public function getData() { @@ -61,35 +62,32 @@ public function getData() $data['CheckValue'] = $this->checkValue($data); - return $data; - } - - /** - * @throws InvalidResponseException - */ - public function sendData($data) - { $response = $this->httpClient->request('POST', $this->getEndpoint(), [ 'Content-Type' => 'application/x-www-form-urlencoded', ], http_build_query($data)); - $body = trim((string) $response->getBody()); - $result = json_decode($body, true); + $responseText = trim((string) $response->getBody()); + $result = json_decode($responseText, true); if (json_last_error() === JSON_ERROR_NONE) { - - if (! hash_equals($result['Result']['CheckCode'], $this->checkCode($result['Result']))) { - throw new InvalidResponseException('Incorrect CheckCode'); - } + $decode = array_merge([ + 'Status' => $result['Status'], + 'Message' => $result['Message'], + ], $result['Result']); } else { - $result = []; - parse_str($body, $result); + $decode = []; + parse_str($responseText, $decode); + } - if (! hash_equals($result['CheckCode'], $this->checkCode($result))) { - throw new InvalidResponseException('Incorrect CheckCode'); - } + if (! hash_equals($decode['CheckCode'], $this->checkCode($decode))) { + throw new InvalidResponseException('Incorrect CheckCode'); } - return $this->response = new FetchTransactionResponse($this, $result); + return $decode; + } + + public function sendData($data) + { + return $this->response = new FetchTransactionResponse($this, $data); } } diff --git a/src/Message/FetchTransactionResponse.php b/src/Message/FetchTransactionResponse.php index bf4122e..c6223ef 100644 --- a/src/Message/FetchTransactionResponse.php +++ b/src/Message/FetchTransactionResponse.php @@ -16,9 +16,7 @@ public function isCancelled() public function getCode() { - return array_key_exists('Result', $this->data) - ? $this->data['Result']['TradeStatus'] - : $this->data['TradeStatus']; + return $this->data['TradeStatus']; } public function getMessage() @@ -36,15 +34,11 @@ public function getMessage() public function getTransactionReference() { - return array_key_exists('Result', $this->data) - ? $this->data['Result']['TradeNo'] - : $this->data['TradeNo']; + return $this->data['TradeNo']; } public function getTransactionId() { - return array_key_exists('Result', $this->data) - ? $this->data['Result']['MerchantOrderNo'] - : $this->data['MerchantOrderNo']; + return $this->data['MerchantOrderNo']; } } diff --git a/src/Message/VoidRequest.php b/src/Message/VoidRequest.php index 127ae2c..6be8e8d 100644 --- a/src/Message/VoidRequest.php +++ b/src/Message/VoidRequest.php @@ -87,9 +87,8 @@ public function sendData($data) 'PostData_' => $this->encrypt($data), ])); - $body = trim((string) $response->getBody()); - - $result = json_decode($body, true); + $responseText = trim((string) $response->getBody()); + $result = json_decode($responseText, true); if (json_last_error() === JSON_ERROR_NONE) { if (! hash_equals($result['Result']['CheckCode'], $this->checkCode($result['Result']))) { diff --git a/src/Traits/HasDefaults.php b/src/Traits/HasDefaults.php index 5953ce2..36ff782 100644 --- a/src/Traits/HasDefaults.php +++ b/src/Traits/HasDefaults.php @@ -3,10 +3,11 @@ namespace Omnipay\NewebPay\Traits; use Omnipay\Common\Exception\InvalidRequestException; -use Omnipay\NewebPay\Encryptor; trait HasDefaults { + use HasEncryptor; + /** * 藍新金流商店代號 * @@ -158,47 +159,4 @@ public function getAmt() return (int) $this->getAmount(); } - public function encrypt(array $data) - { - $encryptor = new Encryptor($this->getHashKey(), $this->getHashIv()); - - return $encryptor->encrypt($data); - } - - public function decrypt(string $plainText) - { - $encryptor = new Encryptor($this->getHashKey(), $this->getHashIv()); - $result = $encryptor->decrypt($plainText); - - $data = json_decode($result, true); - if (json_last_error() === JSON_ERROR_NONE) { - return $data; - } - - $data = []; - parse_str($result, $data); - - return $data; - } - - public function tradeSha($plainText) - { - $encryptor = new Encryptor($this->getHashKey(), $this->getHashIv()); - - return $encryptor->tradeSha($plainText); - } - - public function checkValue($data) - { - $encryptor = new Encryptor($this->getHashKey(), $this->getHashIv()); - - return $encryptor->checkValue($data); - } - - public function checkCode($data) - { - $encryptor = new Encryptor($this->getHashKey(), $this->getHashIv()); - - return $encryptor->checkCode($data); - } } diff --git a/src/Traits/HasEncryptor.php b/src/Traits/HasEncryptor.php new file mode 100644 index 0000000..385f0ab --- /dev/null +++ b/src/Traits/HasEncryptor.php @@ -0,0 +1,38 @@ +getEncryptor()->encrypt($data); + } + + public function decrypt(string $plainText) + { + return $this->getEncryptor()->decrypt($plainText); + } + + public function tradeSha($plainText) + { + return $this->getEncryptor()->tradeSha($plainText); + } + + public function checkValue($data) + { + return $this->getEncryptor()->checkValue($data); + } + + public function checkCode($data) + { + return $this->getEncryptor()->checkCode($data); + } + + private function getEncryptor() + { + return new Encryptor($this->getHashKey(), $this->getHashIv()); + } +} diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index e620916..6cf6549 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -29,35 +29,28 @@ public function testGetData(): array self::assertEquals([ 'Status' => 'SUCCESS', + 'Message' => '授權成功', 'MerchantID' => 'MS127874575', - 'Version' => '2.0', - 'TradeInfo' => 'ee11d1501e6dc8433c75988258f2343d11f4d0a423be672e8e02aaf373c53c2363aeffdb4992579693277359b3e449ebe644d2075fdfbc10150b1c40e7d24cb215febefdb85b16a5cde449f6b06c58a5510d31e8d34c95284d459ae4b52afc1509c2800976a5c0b99ef24cfd28a2dfc8004215a0c98a1d3c77707773c2f2132f9a9a4ce3475cb888c2ad372485971876f8e2fec0589927544c3463d30c785c2d3bd947c06c8c33cf43e131f57939e1f7e3b3d8c3f08a84f34ef1a67a08efe177f1e663ecc6bedc7f82640a1ced807b548633cfa72d060864271ec79854ee2f5a170aa902000e7c61d1269165de330fce7d10663d1668c711571776365bfdcd7ddc915dcb90d31a9f27af9b79a443ca8302e508b0dbaac817d44cfc44247ae613075dde4ac960f1bdff4173b915e4344bc4567bd32e86be7d796e6d9b9cf20476e4996e98ccc315f1ed03a34139f936797d971f2a3f90bc18f8a155a290bcbcf04f4277171c305bf554f5cba243154b30082748a81f2e5aa432ef9950cc9668cd4330ef7c37537a6dcb5e6ef01b4eca9705e4b097cf6913ee96e81d0389e5f775', - 'TradeSha' => 'C80876AEBAC0036268C0E240E5BFF69C0470DE9606EEE083C5C8DD64FDB3347A', - 'Result' => [ - 'Status' => 'SUCCESS', - 'Message' => '授權成功', - 'MerchantID' => 'MS127874575', - 'Amt' => '30', - 'TradeNo' => '23092714215835071', - 'MerchantOrderNo' => 'Vanespl_ec_1695795668', - 'RespondType' => 'String', - 'IP' => '123.51.237.115', - 'EscrowBank' => 'HNCB', - 'PaymentType' => 'CREDIT', - 'RespondCode' => '00', - 'Auth' => '115468', - 'Card6No' => '400022', - 'Card4No' => '1111', - 'Exp' => '2609', - 'AuthBank' => 'KGI', - 'TokenUseStatus' => '0', - 'InstFirst' => '0', - 'InstEach' => '0', - 'Inst' => '0', - 'ECI' => '', - 'PayTime' => '2023-09-27 14:21:59', - 'PaymentMethod' => 'CREDIT', - ], + 'Amt' => '30', + 'TradeNo' => '23092714215835071', + 'MerchantOrderNo' => 'Vanespl_ec_1695795668', + 'RespondType' => 'String', + 'IP' => '123.51.237.115', + 'EscrowBank' => 'HNCB', + 'PaymentType' => 'CREDIT', + 'RespondCode' => '00', + 'Auth' => '115468', + 'Card6No' => '400022', + 'Card4No' => '1111', + 'Exp' => '2609', + 'AuthBank' => 'KGI', + 'TokenUseStatus' => '0', + 'InstFirst' => '0', + 'InstEach' => '0', + 'Inst' => '0', + 'ECI' => '', + 'PayTime' => '2023-09-27 14:21:59', + 'PaymentMethod' => 'CREDIT', ], $data); return [$request->send(), $data]; @@ -99,38 +92,29 @@ public function testGetDataForJSON() self::assertEquals([ 'Status' => 'SUCCESS', - 'MerchantID' => 'MS350766787', - 'Version' => '2.0', - 'TradeInfo' => '1c666a338762a97c40f28d5d7fe5dfa52bb5a65f7c460fe5970ac7a993529669a9e11cb3a6ffb7e3447d0482f263eab8fbb4a2e9024a074a1b28e5b07a3cb19a03b493114841831ad6fec058b5b1d9d43bf256b42007f53336ced1687d58984fcf6b54cbafbdb4b31070c2434f00467135f0d60c91dc55d588661b47151842bec5698938743e1dce75de3f26b0ef3bbe69730ef7f15c979cfa9f394e7830d46b318fd97036d4116e59108cf3d67ce1b69db1ce531061a81b91ed6a10ec797fd6f84242dab56a60ce16d02a03af70b0d0746fbbfb04bda214c1d676ee0aeedb01ea9d818e0b06b318af974e93bd4fdffea536f367b52962cd04951dbe805a0f5465e203c924258cc5a2b4d8f742ad60f2a0649c5f00ca39b267d6ee8f21ab0a29ad47abf7f5ade2b4b8b6717b647f9952ead032b9c3fdb0a82d5f9611950f852639d8891f3652761b4004f9c13ad346550b9126fdc3e21e8ca4f94909093cbe746be38df5a1b7d4aad321cb8fea0b1c2f08c8836f7ec32153d6a553724dfc41b06425304af71b91dd4b014f837bc3a028dd626901e324b16be9b9d845bb3b2bc0bf6b089161fa5dd8b786dfa96ca58aa0f37c6361cbe81f33ff888208d6ea60c998ddedc15266ddfd95b7b6b1f293ec4740ac8f56d95b3c1063ed7e7bd9d81c238bc4fb4105862b903d6e6f216dc38a6a0039b6b32fef3a8cf7f51b3eb954bbd3', - 'TradeSha' => 'E70392EF67FADF8D08C60E0F1B3AE0E5510476FD17DFAF18221A1B200A9A4A13', - 'Result' => [ - 'Status' => 'SUCCESS', - 'Message' => '授權成功', - 'Result' => [ - 'MerchantID' => 'MS127874575', - 'Amt' => 30, - 'TradeNo' => '23092714215835071', - 'MerchantOrderNo' => 'Vanespl_ec_1695795668', - 'RespondType' => 'JSON', - 'IP' => '123.51.237.115', - 'EscrowBank' => 'HNCB', - 'ItemDesc' => 'test', - 'PaymentType' => 'CREDIT', - 'PayTime' => '2023-09-27 14:21:59', - 'RespondCode' => '00', - 'Auth' => '115468', - 'Card6No' => '400022', - 'Card4No' => '1111', - 'Exp' => '2609', - 'TokenUseStatus' => 0, - 'InstFirst' => 0, - 'InstEach' => 0, - 'Inst' => 0, - 'ECI' => '', - 'PaymentMethod' => 'CREDIT', - 'AuthBank' => 'KGI', - ], - ], + 'Message' => '授權成功', + 'MerchantID' => 'MS127874575', + 'Amt' => '30', + 'TradeNo' => '23092714215835071', + 'MerchantOrderNo' => 'Vanespl_ec_1695795668', + 'RespondType' => 'JSON', + 'IP' => '123.51.237.115', + 'EscrowBank' => 'HNCB', + 'PaymentType' => 'CREDIT', + 'RespondCode' => '00', + 'Auth' => '115468', + 'Card6No' => '400022', + 'Card4No' => '1111', + 'Exp' => '2609', + 'AuthBank' => 'KGI', + 'TokenUseStatus' => '0', + 'InstFirst' => '0', + 'InstEach' => '0', + 'Inst' => '0', + 'ECI' => '', + 'PayTime' => '2023-09-27 14:21:59', + 'PaymentMethod' => 'CREDIT', + 'ItemDesc' => 'test', ], $data); return [$request->send(), $data]; diff --git a/tests/Message/FetchTransactionRequestTest.php b/tests/Message/FetchTransactionRequestTest.php index 98a4b24..9f4b437 100644 --- a/tests/Message/FetchTransactionRequestTest.php +++ b/tests/Message/FetchTransactionRequestTest.php @@ -31,8 +31,8 @@ public function testSendData() self::assertTrue($response->isSuccessful()); self::assertEquals(1, $response->getCode()); self::assertEquals('付款成功', $response->getMessage()); -// self::assertEquals('Vanespl_ec_1695795668', $response->getTransactionId()); -// self::assertEquals('23092714215835071', $response->getTransactionReference()); + self::assertEquals('Vanespl_ec_1695795668', $response->getTransactionId()); + self::assertEquals('23092714215835071', $response->getTransactionReference()); } public function testSendDataForJSON(): void