Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NotifyAction #28

Merged
merged 4 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

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\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Notify;

/**
* @author Steffen Brem <[email protected]>
*/
class NotifyAction extends BaseApiAwareAction implements GatewayAwareInterface
class NotifyAction implements ApiAwareInterface, ActionInterface
{
use GatewayAwareTrait;
use ApiAwareTrait;

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

/**
* {@inheritdoc}
Expand All @@ -25,7 +32,11 @@ public function execute($request)

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not we try call acceptNotification first?

$response = $this->api->acceptNotification($details->toUnsafeArray())->send();
}

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

Expand All @@ -43,8 +54,10 @@ public function supports($request)
{
return
$request instanceof Notify &&
$request->getModel() instanceof \ArrayAccess &&
method_exists($this->omnipayGateway, 'fetchTransaction')
$request->getModel() instanceof \ArrayAccess && (
method_exists($this->api, 'fetchTransaction') ||
method_exists($this->api, 'acceptNotification')
)
;
}
}
100 changes: 100 additions & 0 deletions tests/Action/NotifyActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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 <[email protected]>
*/
class NotifyActionTest extends GenericActionTest
{
protected $actionClass = NotifyAction::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 <[email protected]>
*/
class MollieGateway extends AbstractGateway
{
public $returnFetchTransaction;

public function getName() {}

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