Skip to content

Commit

Permalink
Merge pull request #28 from steffenbrem/feature/notify-action
Browse files Browse the repository at this point in the history
Added NotifyAction
  • Loading branch information
makasim authored Oct 10, 2016
2 parents 8a95c77 + 43f09e4 commit 85f22db
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Payum\OmnipayBridge\Action;

use Omnipay\Common\GatewayInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Notify;

/**
* @author Steffen Brem <steffenbrem@gmail.com>
*/
class NotifyAction implements ApiAwareInterface, ActionInterface
{
use ApiAwareTrait;

public function __construct()
{
$this->apiClass = GatewayInterface::class;
}

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

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

if (method_exists($this->api, 'fetchTransaction')) {
$response = $this->api->fetchTransaction($details->toUnsafeArray())->send();
} else if (method_exists($this->api, 'acceptNotification')) {
$response = $this->api->acceptNotification($details->toUnsafeArray())->send();
}

$details->replace((array)$response->getData());

$details['_status'] = $response->isSuccessful() ? 'captured' : 'failed';
$details['_status_code'] = $response->getCode();
$details['_status_message'] = $response->isSuccessful() ? '' : $response->getMessage();

throw new HttpResponse('OK', 200);
}

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof Notify &&
$request->getModel() instanceof \ArrayAccess && (
method_exists($this->api, 'fetchTransaction') ||
method_exists($this->api, 'acceptNotification')
)
;
}
}
4 changes: 3 additions & 1 deletion src/OmnipayGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Payum\Core\GatewayFactoryInterface;
use Payum\OmnipayBridge\Action\CaptureAction;
use Payum\OmnipayBridge\Action\ConvertPaymentAction;
use Payum\OmnipayBridge\Action\NotifyAction;
use Payum\OmnipayBridge\Action\OffsiteCaptureAction;
use Payum\OmnipayBridge\Action\StatusAction;

Expand Down Expand Up @@ -50,6 +51,7 @@ protected function populateConfig(ArrayObject $config)
'payum.action.capture_offsite' => new OffsiteCaptureAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),
'payum.action.status' => new StatusAction(),
'payum.action.notify' => new NotifyAction(),
]);

if (false == $config['payum.api']) {
Expand Down Expand Up @@ -91,4 +93,4 @@ protected function populateConfig(ArrayObject $config)
};
}
}
}
}
102 changes: 102 additions & 0 deletions tests/Action/NotifyActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Payum\OmnipayBridge\Tests\Action;

use Omnipay\Common\Message\AbstractResponse as OmnipayAbstractResponse;
use Omnipay\Common\Message\RequestInterface as OmnipayRequestInterface;
use Omnipay\Common\Message\ResponseInterface as OmnipayResponseInterface;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Notify;
use Payum\Core\Tests\GenericActionTest;
use Payum\OmnipayBridge\Action\NotifyAction;
use Payum\OmnipayBridge\Tests\MollieGateway;

/**
* @author Steffen Brem <steffenbrem@gmail.com>
*/
class NotifyActionTest extends GenericActionTest
{
protected $actionClass = NotifyAction::class;

protected $requestClass = Notify::class;

protected function setUp()
{
$this->action = new $this->actionClass();
$this->action->setApi(new MollieGateway());
}

/**
* @test
*/
public function shouldSetStatusCapturedWhenSuccessful()
{
$model = new \ArrayObject([]);

$responseMock = $this->getMock(OmnipayResponseInterface::class);
$responseMock
->method('isSuccessful')
->willReturn(true)
;

$requestMock = $this->getMock(OmnipayRequestInterface::class);
$requestMock
->expects($this->once())
->method('send')
->willReturn($responseMock)
;

$action = new NotifyAction();

$gateway = new MollieGateway();
$gateway->returnFetchTransaction = $requestMock;
$action->setApi($gateway);

try {
$action->execute(new Notify($model));
} catch (HttpResponse $e) {
$this->assertEquals(200, $e->getStatusCode());
}

$details = iterator_to_array($model);

$this->assertEquals('captured', $details['_status']);
}

/**
* @test
*/
public function shouldSetStatusFailedWhenNotSuccessful()
{
$model = new \ArrayObject([]);

$responseMock = $this->getMock(OmnipayResponseInterface::class);
$responseMock
->method('isSuccessful')
->willReturn(false)
;

$requestMock = $this->getMock(OmnipayRequestInterface::class);
$requestMock
->expects($this->once())
->method('send')
->willReturn($responseMock)
;

$action = new NotifyAction();

$gateway = new MollieGateway();
$gateway->returnFetchTransaction = $requestMock;
$action->setApi($gateway);

try {
$action->execute(new Notify($model));
} catch (HttpResponse $e) {
$this->assertEquals(200, $e->getStatusCode());
}

$details = iterator_to_array($model);

$this->assertEquals('failed', $details['_status']);
}
}
20 changes: 20 additions & 0 deletions tests/MollieGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Payum\OmnipayBridge\Tests;

use Omnipay\Common\AbstractGateway;

/**
* @author Steffen Brem <steffenbrem@gmail.com>
*/
class MollieGateway extends AbstractGateway
{
public $returnFetchTransaction;

public function getName() {}

public function fetchTransaction()
{
return $this->returnFetchTransaction;
}
}

0 comments on commit 85f22db

Please sign in to comment.