Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Jan 18, 2017
1 parent 684f3af commit e6924c0
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 247 deletions.
22 changes: 9 additions & 13 deletions src/Action/Api/CreateTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

$result = $this->api->createTransaction((array) $details);

if (isset($result['RtnCode']) === true) {
$details->replace($result);

return;
if (empty($details['GoodsAmount']) === true) {
$result = $this->api->createCvsMapTransaction((array) $details);
$endpoint = $this->api->getApiEndpoint();

throw new HttpPostRedirect(
$endpoint,
$result
);
}

$endpoint = $this->api->getApiEndpoint();

throw new HttpPostRedirect(
$endpoint,
$result
);
$details->replace($this->api->createTransaction((array) $details));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Action/Api/GetTransactionDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

$result = $this->api->getTransactionData((array) $details);

$details->replace($result);
$details->replace($this->api->getTransactionData((array) $details));
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Action/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public function execute($request)
$details = ArrayObject::ensureArrayObject($request->getModel());

$this->gateway->execute(new CancelTransaction($details));

$this->gateway->execute(new Sync($details));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use PayumTW\Ecpay\Action\Api\BaseApiAwareAction;

class CaptureAction implements ActionInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
class CaptureAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
{
use GatewayAwareTrait;
use GenericTokenFactoryAwareTrait;
Expand All @@ -33,7 +34,10 @@ public function execute($request)
$this->gateway->execute($httpRequest);

if (isset($httpRequest->request['RtnCode']) === true) {
$this->gateway->execute(new Sync($details));
if ($this->api->verifyHash($httpRequest->request) === false) {
$httpRequest->request['RtnCode'] = '10400002';
}
$details->replace($httpRequest->request);

return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Action/CaptureLogisticsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use PayumTW\Ecpay\Action\Api\BaseApiAwareAction;

class CaptureLogisticsAction implements ActionInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
class CaptureLogisticsAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
{
use GatewayAwareTrait;
use GenericTokenFactoryAwareTrait;
Expand All @@ -32,16 +33,17 @@ public function execute($request)
$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);

// CVS
if (isset($httpRequest->request['CVSStoreID']) === true) {
$this->gateway->execute(new Sync($details));
$details->replace($httpRequest->request);

return;
}

$token = $request->getToken();
$targetUrl = $token->getTargetUrl();

if ($details['GoodsAmount'] === 0) {
if (empty($details['GoodsAmount']) === true) {
if (empty($details['ServerReplyURL']) === true) {
$details['ServerReplyURL'] = $targetUrl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Action/ConvertPaymentLogisticsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute($request)

$details['MerchantTradeNo'] = $payment->getNumber();
$details['ReceiverEmail'] = $payment->getClientEmail();
$details['GoodsAmount'] = (int) $payment->getTotalAmount();
$details['GoodsAmount'] = $payment->getTotalAmount();
$details['TradeDesc'] = $payment->getDescription();

$details['AllPayLogisticsID'] = $details['MerchantTradeNo'];
Expand Down
2 changes: 1 addition & 1 deletion src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function execute($request)
throw new HttpResponse('0|CheckMacValue verify fail.', 400, ['Content-Type' => 'text/plain']);
}

$this->gateway->execute(new Sync($details));
$details->replace($httpRequest->request);

throw new HttpResponse('1|OK', 200, ['Content-Type' => 'text/plain']);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Action/RefundAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public function execute($request)
$details = ArrayObject::ensureArrayObject($request->getModel());

$this->gateway->execute(new RefundTransaction($details));

$this->gateway->execute(new Sync($details));
}

/**
Expand Down
7 changes: 0 additions & 7 deletions src/Action/SyncAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);

$details->replace([
'response' => $httpRequest->request,
]);

$this->gateway->execute(new GetTransactionData($details));
}

Expand Down
17 changes: 4 additions & 13 deletions src/EcpayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,10 @@ public function refundTransaction($params)
*/
public function getTransactionData($params)
{
$details = [];
if (empty($params['response']) === false) {
if ($this->verifyHash($params['response']) === false) {
$details['RtnCode'] = '10400002';
} else {
$details = $params['response'];
}
} else {
$this->api->ServiceURL = $this->getApiEndpoint('QueryTradeInfo');
$this->api->Query['MerchantTradeNo'] = $params['MerchantTradeNo'];
$details = $this->api->QueryTradeInfo();
$details['RtnCode'] = $details['TradeStatus'] === '1' ? '1' : '2';
}
$this->api->ServiceURL = $this->getApiEndpoint('QueryTradeInfo');
$this->api->Query['MerchantTradeNo'] = $params['MerchantTradeNo'];
$details = $this->api->QueryTradeInfo();
$details['RtnCode'] = $details['TradeStatus'] === '1' ? '1' : '2';

return $details;
}
Expand Down
26 changes: 15 additions & 11 deletions src/EcpayLogisticsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ public function createPrintFamilyC2CBillTransaction(array $params)
*/
public function createTransaction(array $params)
{
if ($params['GoodsAmount'] === 0) {
return $this->createCvsMapTransaction($params);
}

$this->api->Send = array_merge($this->api->Send, [
'MerchantTradeNo' => '',
'MerchantTradeDate' => date('Y/m/d H:i:s'),
Expand Down Expand Up @@ -217,6 +213,9 @@ public function createTransaction(array $params)
array_intersect_key($params, $this->api->Send)
);

$this->api->Send['GoodsAmount'] = (int) $this->api->Send['GoodsAmount'];
$this->api->Send['CollectionAmount'] = (int) $this->api->Send['CollectionAmount'];

if (empty($this->api->Send['LogisticsType']) === true) {
$this->api->Send['LogisticsType'] = LogisticsType::CVS;
switch ($this->api->Send['LogisticsSubType']) {
Expand Down Expand Up @@ -380,13 +379,18 @@ public function cancelTransaction($params)
*/
public function getTransactionData($params)
{
$details = [];
if (empty($params['response']) === false) {
$details = $params['response'];
} else {
$details = $params;
}
$supportedParams = [
'AllPayLogisticsID' => '',
'PlatformID' => '',
];

$this->api->Send = array_merge($this->api->Send, $supportedParams);

$this->api->Send = array_replace(
$this->api->Send,
array_intersect_key($params, $this->api->Send)
);

return $details;
return $this->api->QueryLogisticsInfo();
}
}
11 changes: 7 additions & 4 deletions tests/Action/Api/CreateTransactionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function test_api_http_post_redirect()
$request = m::spy('PayumTW\Ecpay\Request\Api\CreateTransaction');
$details = new ArrayObject([
'foo' => 'bar',
'GoodsAmount' => 1
]);
$endpoint = 'foo.endpoint';

Expand Down Expand Up @@ -57,7 +58,6 @@ public function test_api_http_post_redirect()

$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('createTransaction')->with((array) $details)->once();
$api->shouldHaveReceived('getApiEndpoint')->once();
}

public function test_logistics_api_http_post_redirect()
Expand Down Expand Up @@ -85,7 +85,7 @@ public function test_logistics_api_http_post_redirect()
->shouldReceive('getModel')->andReturn($details);

$api
->shouldReceive('createTransaction')->andReturn((array) $details)
->shouldReceive('createCvsMapTransaction')->andReturn((array) $details)
->shouldReceive('getApiEndpoint')->andReturn($endpoint);

$action = new CreateTransactionAction();
Expand All @@ -104,7 +104,7 @@ public function test_logistics_api_http_post_redirect()
}

$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('createTransaction')->with((array) $details)->once();
$api->shouldHaveReceived('createCvsMapTransaction')->with((array) $details)->once();
$api->shouldHaveReceived('getApiEndpoint')->once();
}

Expand All @@ -118,7 +118,10 @@ public function test_logistics_api_when_isset_rtn_code()

$api = m::spy('PayumTW\Ecpay\Api');
$request = m::spy('PayumTW\Ecpay\Request\Api\CreateTransaction');
$details = new ArrayObject(['RtnCode' => '300']);
$details = new ArrayObject([
'RtnCode' => '300',
'GoodsAmount' => 1
]);

/*
|------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion tests/Action/CancelActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ public function test_cancel()

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('PayumTW\Ecpay\Request\Api\CancelTransaction'))->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}
}
3 changes: 2 additions & 1 deletion tests/Action/CaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function test_execute_when_rtn_code_exists()
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Ecpay\Api');
$gateway = m::spy('Payum\Core\GatewayInterface');
$tokenFactory = m::spy('Payum\Core\Security\GenericTokenFactoryInterface');
$request = m::spy('Payum\Core\Request\Capture');
Expand Down Expand Up @@ -106,6 +107,7 @@ public function test_execute_when_rtn_code_exists()
});

$action = new CaptureAction();
$action->setApi($api);
$action->setGateway($gateway);
$action->setGenericTokenFactory($tokenFactory);
$action->execute($request);
Expand All @@ -118,6 +120,5 @@ public function test_execute_when_rtn_code_exists()

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}
}
1 change: 0 additions & 1 deletion tests/Action/CaptureLogisticsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,5 @@ public function test_execute_when_csv_store_id_exists()

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}
}
1 change: 0 additions & 1 deletion tests/Action/NotifyActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function test_notify_success()
$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$api->shouldHaveReceived('verifyHash')->with($returnValue)->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}

public function test_notify_when_checksum_fail()
Expand Down
1 change: 0 additions & 1 deletion tests/Action/RefundActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ public function test_refund()

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('PayumTW\Ecpay\Request\Api\RefundTransaction'))->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}
}
Loading

0 comments on commit e6924c0

Please sign in to comment.