Skip to content

Commit

Permalink
+Component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hakito committed Feb 29, 2020
1 parent 8aa99de commit 2b7f478
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 47 deletions.
77 changes: 30 additions & 47 deletions src/Controller/Component/PayPalComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace PayPal\Controller\Component;

use Cake\Core\Configure;
use Cake\Controller\Component;
use Cake\ORM\TableRegistry;

use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Api\ItemList;
use PayPal\Api\Item;

use Cake\Controller\Component;

class PayPalComponent extends Component
{

Expand All @@ -23,12 +25,17 @@ class PayPalComponent extends Component
/** @var PayPalPaymentsTable */
public $PayPalPayments;

public function initialize()
public function initialize(array $config)
{
parent::initialize();
$this->config = Configure::read('PayPalPlugin');
parent::initialize($config);
$this->config = Configure::read('PayPal');
$this->items = [];
$this->loadModel('PayPal.PayPalPayments');
$this->PayPalPayments = TableRegistry::getTableLocator()->get('PayPal.PayPalPayments');
}

public function startup($event)
{
$this->Controller = $event->getSubject();
}

/**
Expand Down Expand Up @@ -60,21 +67,14 @@ public function AddArticle($name, $quantity, $price, $id = null)
/**
*
* @param string $remittanceIdentifier id to be used for the callback function
* @param \PayPal\Api\CreditCard $creditCard
* @param string Description text for the transaction
* @throws \Cake\ORM\Exception\PersistenceFailedException when creation of database entry failed
*/
public function PaymentRedirect($remittanceIdentifier, $okUrl, $cancelUrl, $creditCard = null, $description = null)
public function PaymentRedirect($remittanceIdentifier, $okUrl, $cancelUrl, $description = null)
{

$payer = new Payer();

if ($creditCard == null)
{
$payer->setPaymentMethod("paypal");
} else
{
throw new NotImplementedException("no implementation for credit card payments");
}
$payer->setPaymentMethod("paypal");

$itemSum = 0;
$itemArray = array();
Expand Down Expand Up @@ -112,32 +112,20 @@ public function PaymentRedirect($remittanceIdentifier, $okUrl, $cancelUrl, $cred
$payment->setTransactions(array($transaction));
$payment->setIntent('sale');

if (!$this->PayPalPayments->createPayment($remittanceIdentifier, $payment, $okUrl, $cancelUrl))
{
$exception = new PayPalPaymentRedirectException('Could not save payment.');
$exception->errors = $this->PayPalPayments->validationErrors;
throw $exception;
}
$this->PayPalPayments->createPayment($remittanceIdentifier, $payment, $okUrl, $cancelUrl);

if ($creditCard == null)
foreach ($payment->getLinks() as $link)
{
foreach ($payment->getLinks() as $link)
if ($link->getRel() == 'approval_url')
{
if ($link->getRel() == 'approval_url')
{
$redirectUrl = $link->getHref();
break;
}
$redirectUrl = $link->getHref();
break;
}
}

if(isset($redirectUrl)) {

header("Location: $redirectUrl");
exit;
}
} else
if(isset($redirectUrl))
{
// TODO handle credit card payments
$this->Controller->redirect($redirectUrl);
}
}

Expand Down Expand Up @@ -185,32 +173,27 @@ public static function CalculateFee($amount)

/**
*
* @param type $amount
* @param type $amountInCents
* @return type amount plus neutralization amount so when PayPal subtract it's fee
* the intended amount will be received.
* @throws InvalidArgumentException if PayPal conditions are not set in config
*/
public static function NeutralizeFee($amount)
public static function NeutralizeFee($amountInCents)
{
$conditions = self::_getConditionsFromConfig();
return $amount + ceil(self::CalculateFee($amount) / ( 1 - $conditions['fee_relative'] ));
return $amountInCents + ceil(self::CalculateFee($amountInCents) / ( 1 - $conditions['fee_relative'] ));
}

private static function _getConditionsFromConfig()
{
$config = Configure::read('PayPalPlugin');
$config = Configure::read('PayPal');
if (empty($config['conditions']))
throw new InvalidArgumentException('Missing PayPal conditions.');
throw new \InvalidArgumentException('Missing PayPal conditions.');

$conditions = $config['conditions'];
if (!isset($conditions['fee']) || !isset($conditions['fee_relative']))
throw new InvalidArgumentException('Missing PayPal condition fees.');
throw new \InvalidArgumentException('Missing PayPal condition fees.');

return $conditions;
}
}

class PayPalPaymentRedirectException extends Exception
{
public $errors;
}
115 changes: 115 additions & 0 deletions tests/TestCase/Controller/Component/PayPalComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace PayPal\Test\TestCase\Controller\Component;

use Cake\Cache\Cache;
use Cake\Controller\ComponentRegistry;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\TestSuite\TestCase;


use PayPal\Controller\Component\PayPalComponent;
use PayPal\Model\Table\PayPalPaymentsTable;

/**
* @var PayPalComponent PayPal
*/
class PayPalComponentTest extends TestCase
{

public function setUp()
{
parent::setUp();

$this->Controller = $this->getMockBuilder('\Cake\Controller\Controller')
->setMethods(['redirect'])
->getMock();

$registry = new ComponentRegistry($this->Controller);
$this->PayPal = new PayPalComponent($registry);

$event = new Event('Controller.startup', $this->Controller);
$this->PayPal->startup($event);

$this->items = new \ReflectionProperty(PayPalComponent::class, 'items');
$this->items->setAccessible(true);

// $event = new Event('Controller.startup', $this->Controller);
// $this->Eps->startup($event);

// Plugin::$SoCommunicator = $this->getMockBuilder('at\externet\eps_bank_transfer\SoCommunicator')
// ->getMock();
// Plugin::$EnableLogging = false;
// Cache::clear();
}

public function testInitialized()
{
$this->assertEquals(Configure::read('PayPal'), $this->PayPal->config);
$this->assertInstanceOf(PayPalPaymentsTable::class, $this->PayPal->PayPalPayments);
}

public function testAddArticle()
{
$this->PayPal->AddArticle('foo', 3, 1234);
$items = $this->items->getValue($this->PayPal);
$this->assertEquals(1, count($items));
$this->assertEquals('foo', $items[0]->getName());
$this->assertEquals(3, $items[0]->getQuantity());
$this->assertEquals(12.34, $items[0]->getPrice());
$this->assertEquals('EUR', $items[0]->getCurrency());
$this->assertEquals(null, $items[0]->getSku());
}

public function testAddArticleWithId()
{
$this->PayPal->AddArticle('foo', 3, 1234, 'id');
$items = $this->items->getValue($this->PayPal);
$this->assertEquals(1, count($items));
$this->assertEquals('foo', $items['id']->getName());
$this->assertEquals(3, $items['id']->getQuantity());
$this->assertEquals(12.34, $items['id']->getPrice());
$this->assertEquals('EUR', $items['id']->getCurrency());
$this->assertEquals('id', $items['id']->getSku());
}

public function testPaymentRedirectCreatePaymentFails()
{
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments');
$this->PayPal->PayPalPayments->expects($this->once())
->method('createPayment')
->with('ri', $this->anything(), 'https://ok', 'https://cancel')
->will($this->throwException(new \Exception('dummy')));
$this->expectException(\Exception::class, 'dummy');
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr');
}

public function testPaymentRedirect()
{
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments');
$this->PayPal->PayPalPayments->expects($this->once())
->method('createPayment')
->with('ri', $this->anything(), 'https://ok', 'https://cancel')
->will($this->returnCallback(function($remittanceIdentifier, $payment, $okUrl, $cancelUrl){
$links = new \PayPal\Api\Links();
$links
->setRel('approval_url')
->setHref('https://pay');
$payment->setLinks([$links]);
return true;
}));

$this->Controller->expects($this->once())
->method('redirect')
->with('https://pay');

$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr');
}

public function testNeutralizeFee()
{
$actual = PayPalComponent::NeutralizeFee(1234);
$this->assertEquals(1314, $actual);
}
}

0 comments on commit 2b7f478

Please sign in to comment.