Skip to content

Commit

Permalink
Fix DoVoid PayPal express NVP implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Israel J. Carberry committed Sep 15, 2016
1 parent de501d4 commit b63682a
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 18 deletions.
22 changes: 18 additions & 4 deletions Action/Api/DoVoidAction.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?php
namespace Payum\Paypal\ExpressCheckout\Nvp\Action\Api;

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\Exception\LogicException;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Paypal\ExpressCheckout\Nvp\Api;
use Payum\Paypal\ExpressCheckout\Nvp\Request\Api\DoVoid;

class DoVoidAction extends BaseApiAwareAction
class DoVoidAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
{
use ApiAwareTrait;
use GatewayAwareTrait;

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

/**
* {@inheritdoc}
*/
Expand All @@ -18,8 +32,8 @@ public function execute($request)

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

if (null === $model['TRANSACTIONID']) {
throw new LogicException('TRANSACTIONID must be set. Has user not authorized this transaction?');
if (null === $model['AUTHORIZATIONID']) {
throw new LogicException('AUTHORIZATIONID must be set. Has user not authorized this transaction?');
}

$model->replace(
Expand Down
7 changes: 5 additions & 2 deletions Action/CancelAction.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Payum\Paypal\ExpressCheckout\Nvp\Action;

use Payum\Core\Action\GatewayAwareAction;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\Cancel;
use Payum\Core\Request\Sync;
use Payum\Core\Action\GatewayAwareAction;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Paypal\ExpressCheckout\Nvp\Api;
use Payum\Paypal\ExpressCheckout\Nvp\Request\Api\DoVoid;

Expand All @@ -22,6 +22,9 @@ public function execute($request)
$details = ArrayObject::ensureArrayObject($request->getModel());

$details['PAYMENTREQUEST_0_PAYMENTACTION'] = Api::PAYMENTACTION_VOID;
if (empty($details['AUTHORIZATIONID']) && !empty($details['TRANSACTIONID'])) {
$details['AUTHORIZATIONID'] = $details['TRANSACTIONID'];
}

foreach (range(0, 9) as $index) {
if (Api::PENDINGREASON_AUTHORIZATION == $details['PAYMENTINFO_'.$index.'_PENDINGREASON']) {
Expand Down
5 changes: 1 addition & 4 deletions Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function doCapture(array $fields)
}

/**
* Require: TRANSACTIONID
* Require: AUTHORIZATIONID
*
* @param array $fields
*
Expand All @@ -543,9 +543,6 @@ public function doCapture(array $fields)
public function doVoid(array $fields)
{
$fields['METHOD'] = 'DoVoid';
if (empty($fields['AUTHORIZATIONID'])) {
$fields['AUTHORIZATIONID'] = $fields['TRANSACTIONID'];
}

$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
Expand Down
56 changes: 48 additions & 8 deletions Tests/Action/Api/DoVoidActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,51 @@ class DoVoidActionTest extends \PHPUnit_Framework_TestCase
/**
* @test
*/
public function shouldBeSubClassOfBaseApiAwareAction()
public function shouldImplementActionInterface()
{
$rc = new \ReflectionClass('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\DoVoidAction');

$this->assertTrue($rc->isSubclassOf('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\BaseApiAwareAction'));
$this->assertTrue($rc->implementsInterface('Payum\Core\Action\ActionInterface'));
}

/**
* @test
*/
public function shouldImplementApiAwareInterface()
{
$rc = new \ReflectionClass('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\DoVoidAction');

$this->assertTrue($rc->implementsInterface('Payum\Core\ApiAwareInterface'));
}

/**
* @test
*/
public function shouldImplementGatewayAwareInterface()
{
$rc = new \ReflectionClass('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\DoVoidAction');

$this->assertTrue($rc->implementsInterface('Payum\Core\GatewayAwareInterface'));
}

/**
* @test
*/
public function shouldUseApiAwareTrait()
{
$rc = new \ReflectionClass('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\DoVoidAction');

$this->assertContains('Payum\Core\ApiAwareTrait', $rc->getTraitNames());
}

/**
* @test
*/
public function shouldUseGatewayAwareTrait()
{
$rc = new \ReflectionClass('Payum\Paypal\ExpressCheckout\Nvp\Action\Api\DoVoidAction');

$this->assertContains('Payum\Core\GatewayAwareTrait', $rc->getTraitNames());
}

/**
Expand Down Expand Up @@ -62,9 +102,9 @@ public function throwIfNotSupportedRequestGivenAsArgumentForExecute()
* @test
*
* @expectedException \Payum\Core\Exception\LogicException
* @expectedExceptionMessage TRANSACTIONID must be set. Has user not authorized this transaction?
* @expectedExceptionMessage AUTHORIZATIONID must be set. Has user not authorized this transaction?
*/
public function throwIfTransactionIdNotSetInModel()
public function throwIfAuthorizationIdNotSetInModel()
{
$action = new DoVoidAction();

Expand All @@ -85,8 +125,8 @@ public function shouldCallApiDoVoidMethodWithExpectedRequiredArguments()
->expects($this->once())
->method('DoVoid')
->will($this->returnCallback(function (array $fields) use ($testCase) {
$testCase->assertArrayHasKey('TRANSACTIONID', $fields);
$testCase->assertEquals('theTransactionId', $fields['TRANSACTIONID']);
$testCase->assertArrayHasKey('AUTHORIZATIONID', $fields);
$testCase->assertEquals('theOriginalTransactionId', $fields['AUTHORIZATIONID']);

return array();
}))
Expand All @@ -96,7 +136,7 @@ public function shouldCallApiDoVoidMethodWithExpectedRequiredArguments()
$action->setApi($apiMock);

$request = new DoVoid(array(
'TRANSACTIONID' => 'theTransactionId',
'AUTHORIZATIONID' => 'theOriginalTransactionId',
));

$action->execute($request);
Expand All @@ -123,7 +163,7 @@ public function shouldCallApiDoVoidMethodAndUpdateModelFromResponseOnSuccess()
$action->setApi($apiMock);

$request = new DoVoid(array(
'TRANSACTIONID' => 'theTransactionId',
'AUTHORIZATIONID' => 'theTransactionId',
));

$action->execute($request);
Expand Down
Loading

0 comments on commit b63682a

Please sign in to comment.