Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 13, 2023
1 parent 72da487 commit 890ea58
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 147 deletions.
17 changes: 15 additions & 2 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 3 additions & 7 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function isSuccessful(): bool

public function getMessage()
{
return $this->data['Result']['Message'];
return $this->data['Message'];
}

public function getCode()
Expand All @@ -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'];
}
}
38 changes: 18 additions & 20 deletions src/Message/FetchTransactionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getGateway()

/**
* @throws InvalidRequestException
* @throws InvalidResponseException
*/
public function getData()
{
Expand All @@ -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);
}
}
12 changes: 3 additions & 9 deletions src/Message/FetchTransactionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'];
}
}
5 changes: 2 additions & 3 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']))) {
Expand Down
46 changes: 2 additions & 44 deletions src/Traits/HasDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Omnipay\NewebPay\Traits;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\NewebPay\Encryptor;

trait HasDefaults
{
use HasEncryptor;

/**
* 藍新金流商店代號
*
Expand Down Expand Up @@ -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);
}
}
38 changes: 38 additions & 0 deletions src/Traits/HasEncryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Omnipay\NewebPay\Traits;

use Omnipay\NewebPay\Encryptor;

trait HasEncryptor
{
public function encrypt(array $data)
{
return $this->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());
}
}
104 changes: 44 additions & 60 deletions tests/Message/CompletePurchaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions tests/Message/FetchTransactionRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 890ea58

Please sign in to comment.