From 9f53df680991bebc1c5b29a123770ec88c79ccc5 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 15 Sep 2016 14:51:23 +0300 Subject: [PATCH] [paypal-ec] add support of cancel request. --- Action/Api/DoVoidAction.php | 3 +- Action/CancelAction.php | 23 ++-- Api.php | 5 - Tests/Action/CancelActionTest.php | 184 +++--------------------------- 4 files changed, 32 insertions(+), 183 deletions(-) diff --git a/Action/Api/DoVoidAction.php b/Action/Api/DoVoidAction.php index 27930e7..8f79ed5 100644 --- a/Action/Api/DoVoidAction.php +++ b/Action/Api/DoVoidAction.php @@ -24,10 +24,11 @@ public function __construct() /** * {@inheritdoc} + * + * @param $request DoVoid */ public function execute($request) { - /** @var $request DoCapture */ RequestNotSupportedException::assertSupports($this, $request); $model = ArrayObject::ensureArrayObject($request->getModel()); diff --git a/Action/CancelAction.php b/Action/CancelAction.php index 66513b6..825e149 100644 --- a/Action/CancelAction.php +++ b/Action/CancelAction.php @@ -1,16 +1,19 @@ getModel()); - $details['PAYMENTREQUEST_0_PAYMENTACTION'] = Api::PAYMENTACTION_VOID; - if (empty($details['AUTHORIZATIONID']) && !empty($details['TRANSACTIONID'])) { - $details['AUTHORIZATIONID'] = $details['TRANSACTIONID']; + if (!$details['TRANSACTIONID']) { + return; } - foreach (range(0, 9) as $index) { - if (Api::PENDINGREASON_AUTHORIZATION == $details['PAYMENTINFO_'.$index.'_PENDINGREASON']) { - $this->gateway->execute(new DoVoid($details, $index)); - } - } + $voidDetails = new ArrayObject([ + 'AUTHORIZATIONID' => $details['TRANSACTIONID'], + ]); + $this->gateway->execute(new DoVoid($voidDetails)); $this->gateway->execute(new Sync($request->getModel())); } diff --git a/Api.php b/Api.php index ca0a2ab..b6f4f02 100644 --- a/Api.php +++ b/Api.php @@ -135,11 +135,6 @@ class Api */ const PAYMENTACTION_ORDER = 'Order'; - /** - * Void – This is the voiding of an authorized order that has not been captured. - */ - const PAYMENTACTION_VOID = 'Void'; - /** * Payment has not been authorized by the user. */ diff --git a/Tests/Action/CancelActionTest.php b/Tests/Action/CancelActionTest.php index 54bc528..52356fa 100644 --- a/Tests/Action/CancelActionTest.php +++ b/Tests/Action/CancelActionTest.php @@ -1,6 +1,9 @@ assertTrue($rc->isSubclassOf('Payum\Core\Action\GatewayAwareAction')); + $this->assertTrue($rc->isSubclassOf(ActionInterface::class)); } /** * @test */ - public function couldBeConstructedWithoutAnyArguments() + public function shouldImplementGatewayAwareInterface() { - new CancelAction(); - } + $rc = new \ReflectionClass(CancelAction::class); - /** - * @test - */ - public function shouldSetZeroGatewayActionAsVoid() - { - $action = new CancelAction(); - $action->setGateway($this->createGatewayMock()); - - $action->execute($request = new Cancel([])); - - $model = $request->getModel(); - $this->assertArrayHasKey('PAYMENTREQUEST_0_PAYMENTACTION', $model); - $this->assertEquals(Api::PAYMENTACTION_VOID, $model['PAYMENTREQUEST_0_PAYMENTACTION']); + $this->assertTrue($rc->isSubclassOf(GatewayAwareInterface::class)); } /** * @test */ - public function shouldForcePaymentActionVoid() + public function couldBeConstructedWithoutAnyArguments() { - $action = new CancelAction(); - $action->setGateway($this->createGatewayMock()); - - $action->execute($request = new Cancel([ - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'FooBarBaz', - ])); - - $model = $request->getModel(); - $this->assertArrayHasKey('PAYMENTREQUEST_0_PAYMENTACTION', $model); - $this->assertEquals(Api::PAYMENTACTION_VOID, $model['PAYMENTREQUEST_0_PAYMENTACTION']); + new CancelAction(); } /** @@ -66,7 +47,7 @@ public function shouldSupportEmptyModel() { $action = new CancelAction(); - $request = new Cancel(array()); + $request = new Cancel([]); $this->assertTrue($action->supports($request)); } @@ -140,19 +121,18 @@ public function throwIfNotSupportedRequestGivenAsArgumentForExecute() /** * @test */ - public function shouldNotExecuteDoVoidIfPaymentInfoPendingReasonNotSet() + public function shouldNotExecuteDoVoidIfTransactionIdNotSet() { $gatewayMock = $this->createGatewayMock(); $gatewayMock - ->expects($this->once()) + ->expects($this->never()) ->method('execute') - ->with($this->isInstanceOf(Sync::class)) ; $action = new CancelAction(); $action->setGateway($gatewayMock); - $request = new Cancel(array()); + $request = new Cancel([]); $action->execute($request); } @@ -160,7 +140,7 @@ public function shouldNotExecuteDoVoidIfPaymentInfoPendingReasonNotSet() /** * @test */ - public function shouldExecuteDoVoidIfPaymentInfoPendingReasonSet() + public function shouldExecuteDoVoidIfTransactionIdSet() { $gatewayMock = $this->createGatewayMock(); $gatewayMock @@ -176,145 +156,17 @@ public function shouldExecuteDoVoidIfPaymentInfoPendingReasonSet() $action->setGateway($gatewayMock); $request = new Cancel(array( - 'PAYMENTINFO_0_PENDINGREASON' => Api::PENDINGREASON_AUTHORIZATION, - )); - - $action->execute($request); - } - - /** - * @test - */ - public function shouldExecuteDoVoidForEachPaymentInfoPendingReasonSetIndexedToNine() - { - $gatewayMock = $this->createGatewayMock(); - $gatewayMock - ->expects($this->exactly(4)) - ->method('execute') - ->withConsecutive( - array($this->isInstanceOf(DoVoid::class)), - array($this->isInstanceOf(DoVoid::class)), - array($this->isInstanceOf(DoVoid::class)), - array($this->isInstanceOf(Sync::class)) - ) - ; - - $action = new CancelAction(); - $action->setGateway($gatewayMock); - - $request = new Cancel(array( - 'PAYMENTINFO_0_PENDINGREASON' => Api::PENDINGREASON_AUTHORIZATION, - 'PAYMENTINFO_1_PENDINGREASON' => Api::PENDINGREASON_AUTHORIZATION, - 'PAYMENTINFO_9_PENDINGREASON' => Api::PENDINGREASON_AUTHORIZATION, + 'TRANSACTIONID' => 'theId', )); $action->execute($request); } - /** - * @test - */ - public function shouldNotExecuteDoVoidForPaymentInfoPendingReasonIndexedOverNine() - { - $gatewayMock = $this->createGatewayMock(); - $gatewayMock - ->expects($this->once()) - ->method('execute') - ->with($this->isInstanceOf(Sync::class)) - ; - - $action = new CancelAction(); - $action->setGateway($gatewayMock); - - $request = new Cancel(array( - 'PAYMENTINFO_10_PENDINGREASON' => Api::PENDINGREASON_AUTHORIZATION, - )); - - $action->execute($request); - } - - /** - * @test - */ - public function shouldNotSetAuthorizationIdFromTransactionIdIfBothNotSet() - { - $gatewayMock = $this->createGatewayMock(); - $gatewayMock - ->expects($this->at(0)) - ->method('execute') - ->with($this->isInstanceOf(Sync::class)) - ; - - $action = new CancelAction(); - $action->setGateway($gatewayMock); - - $request = new Cancel(array()); - - $action->execute($request); - - $details = $request->getModel(); - - $this->assertFalse(isset($details['AUTHORIZATIONID'])); - } - - /** - * @test - */ - public function shouldSetAuthorizationIdFromTransactionIdIfNotSet() - { - $gatewayMock = $this->createGatewayMock(); - $gatewayMock - ->expects($this->at(0)) - ->method('execute') - ->with($this->isInstanceOf(Sync::class)) - ; - - $action = new CancelAction(); - $action->setGateway($gatewayMock); - - $request = new Cancel(array( - 'TRANSACTIONID' => 'theOriginalTransactionId', - )); - - $action->execute($request); - - $details = $request->getModel(); - - $this->assertEquals($details['AUTHORIZATIONID'], 'theOriginalTransactionId'); - } - - /** - * @test - */ - public function shouldNotOverrideAuthorizationIdWithTransactionIdIfSet() - { - $gatewayMock = $this->createGatewayMock(); - $gatewayMock - ->expects($this->at(0)) - ->method('execute') - ->with($this->isInstanceOf(Sync::class)) - ; - - $action = new CancelAction(); - $action->setGateway($gatewayMock); - - $request = new Cancel(array( - 'TRANSACTIONID' => 'theReauthorizedTransactionId', - 'AUTHORIZATIONID' => 'theOriginalTransactionId', - )); - - $action->execute($request); - - $details = $request->getModel(); - - $this->assertEquals($details['AUTHORIZATIONID'], 'theOriginalTransactionId'); - } - /** * @return \PHPUnit_Framework_MockObject_MockObject|\Payum\Core\GatewayInterface */ protected function createGatewayMock() { - return $this->getMock('Payum\Core\GatewayInterface'); + return $this->getMock(GatewayInterface::class); } }