Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Jan 23, 2017
1 parent 65d4832 commit 2eb327f
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 224 deletions.
34 changes: 34 additions & 0 deletions src/Action/Api/GetTransactionDataAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace PayumTW\Mypay\Action\Api;

use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\GetTransactionData;
use Payum\Core\Exception\RequestNotSupportedException;

class GetTransactionDataAction extends BaseApiAwareAction
{
/**
* {@inheritdoc}
*
* @param $request GetTransactionData
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

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

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

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof GetTransactionData &&
$request->getModel() instanceof \ArrayAccess;
}
}
13 changes: 4 additions & 9 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PayumTW\Mypay\Action;

use PayumTW\Mypay\Api;
use Payum\Core\Request\Sync;
use Payum\Core\Request\Capture;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\GatewayAwareInterface;
Expand Down Expand Up @@ -30,21 +31,15 @@ public function execute($request)
RequestNotSupportedException::assertSupports($this, $request);
$details = ArrayObject::ensureArrayObject($request->getModel());

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

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

return;
}

$token = $request->getToken();

$targetUrl = $token->getTargetUrl();

if (empty($details['success_returl']) === true) {
$details['success_returl'] = $targetUrl;
}
Expand Down
13 changes: 10 additions & 3 deletions src/Action/StatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ public function execute($request)

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

$code = null;
if (isset($details['prc']) === true) {
$code = $details['prc'];
} else if (isset($details['code']) === true) {
$code = $details['code'];
}

if (is_null($code) === false) {

/*
* 290 交易成功,但資訊不符  交易成功,但資訊不符(包含金額、已逾期...等)
*/
if (in_array($details['prc'], ['250', '600', '290'], true) === true) {
if (in_array($code, ['250', '290', '600'], true) === true) {
$request->markCaptured();

return;
Expand All @@ -34,13 +41,13 @@ public function execute($request)
/*
* 280 儲值/WEBATM­線上待付款,但需要等到使用者線上確認交易
*/
if (in_array($details['prc'], ['260', '270', '280', 'A0002'], true) === true) {
if (in_array($code, ['200', '260', '270', '280', 'A0002'], true) === true) {
$request->markPending();

return;
}

if ($details['prc'] === '380') {
if ($code === '380') {
$request->markExpired();

return;
Expand Down
40 changes: 40 additions & 0 deletions src/Action/SyncAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PayumTW\Mypay\Action;

use Payum\Core\Request\Sync;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\GetTransactionData;
use Payum\Core\Exception\RequestNotSupportedException;

class SyncAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritdoc}
*
* @param Refund $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

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

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

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof Sync &&
$request->getModel() instanceof \ArrayAccess;
}
}
2 changes: 2 additions & 0 deletions src/MypayGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ protected function populateConfig(ArrayObject $config)
'payum.action.capture' => new CaptureAction(),
'payum.action.notify' => new NotifyAction(),
'payum.action.notify_null' => new NotifyNullAction(),
'payum.action.sync' => new SyncAction(),
'payum.action.status' => new StatusAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),

'payum.action.api.create_transaction' => new CreateTransactionAction(),
'payum.action.api.get_transaction_data' => new GetTransactionDataAction(),
]);

$httpClient = $this->getDefaultHttpClient($config);
Expand Down
9 changes: 9 additions & 0 deletions src/Request/Api/GetTransactionData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PayumTW\Mypay\Request\Api;

use Payum\Core\Request\Generic;

class GetTransactionData extends Generic
{
}
54 changes: 54 additions & 0 deletions tests/Action/Api/GetTransactionDataActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use Mockery as m;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Action\Api\GetTransactionDataAction;

class GetTransactionDataActionTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}

public function test_get_transaction_data()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Mypay\Api');
$request = m::spy('PayumTW\Mypay\Request\Api\GetTransactionData');
$details = m::mock(new ArrayObject([]));

/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/

$request
->shouldReceive('getModel')->andReturn($details);

$api
->shouldReceive('getTransactionData')->andReturn([
'RspCode' => '1',
]);

$action = new GetTransactionDataAction();
$action->setApi($api);
$action->execute($request);

/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/

$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('getTransactionData')->once();
$details->shouldHaveReceived('replace')->once();
}
}
68 changes: 2 additions & 66 deletions tests/Action/CaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function test_redirect_to_mypay()
*/

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$request->shouldHaveReceived('getToken')->once();
$token->shouldHaveReceived('getTargetUrl')->once();
$token->shouldHaveReceived('getGatewayName')->once();
Expand All @@ -86,11 +85,10 @@ public function test_captured_success()
$gateway = m::spy('Payum\Core\GatewayInterface');
$request = m::spy('Payum\Core\Request\Capture');
$tokenFactory = m::spy('Payum\Core\Security\GenericTokenFactoryInterface');
$details = new ArrayObject([]);

$response = [
'uid' => 1,
];
$details = new ArrayObject($response);

/*
|------------------------------------------------------------
Expand All @@ -101,13 +99,6 @@ public function test_captured_success()
$request
->shouldReceive('getModel')->andReturn($details);

$gateway
->shouldReceive('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->andReturnUsing(function ($httpRequest) use ($response) {
$httpRequest->request = $response;

return $httpRequest;
});

$api
->shouldReceive('verifyHash')->with($response, $details)->andReturn(true);

Expand All @@ -124,61 +115,6 @@ public function test_captured_success()
*/

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$api->shouldHaveReceived('verifyHash')->with($response, $details)->once();
}

public function test_captured_fail()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Mypay\Api');
$gateway = m::spy('Payum\Core\GatewayInterface');
$request = m::spy('Payum\Core\Request\Capture');
$tokenFactory = m::spy('Payum\Core\Security\GenericTokenFactoryInterface');
$details = new ArrayObject([]);

$response = [
'uid' => 1,
];

/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/

$request
->shouldReceive('getModel')->andReturn($details);

$gateway
->shouldReceive('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->andReturnUsing(function ($httpRequest) use ($response) {
$httpRequest->request = $response;

return $httpRequest;
});

$api
->shouldReceive('verifyHash')->with($response, $details)->andReturn(false);

$action = new CaptureAction();
$action->setApi($api);
$action->setGateway($gateway);
$action->setGenericTokenFactory($tokenFactory);
$action->execute($request);

/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/

$request->shouldHaveReceived('getModel')->twice();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\GetHttpRequest'))->once();
$api->shouldHaveReceived('verifyHash')->with($response, $details)->once();
$gateway->shouldHaveReceived('execute')->with(m::type('Payum\Core\Request\Sync'))->once();
}
}
Loading

0 comments on commit 2eb327f

Please sign in to comment.