-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from steffenbrem/feature/notify-action
Added NotifyAction
- Loading branch information
Showing
4 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |