Skip to content

Commit

Permalink
Add generateTranasctionId() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Oram committed Nov 21, 2013
1 parent 095796f commit 1aeaf40
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 66 deletions.
31 changes: 19 additions & 12 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class Module implements
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
return [
'Zend\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
],
],
];
}

/**
Expand All @@ -45,15 +45,22 @@ public function getConfig()
*/
public function getServiceConfig()
{
return array(
'invokables' => array(
return [
'invokables' => [
'SclZfCartSagepay\Service\CryptService' => 'SclZfCartSagepay\Service\CryptService',
'SclZfCartSagepay\Encryption\Cipher' => 'SclZfCartSagepay\Encryption\Cipher',
),
'factories' => array(
],
'factories' => [
'SclZfCartSagepay\Sagepay' => 'SclZfCartSagepay\Service\SagepayFactory',
'SclZfCartSagepay\Options\SagepayOptions' => 'SclZfCartSagepay\Service\SagepayOptionsFactory',
),
);
'SclZfCartSagepay\Service\CallbackService' => function ($sm) {
return new \SclZfCartSagepay\Service\CallbackService(
$sm->get('SclZfCartSagepay\Encryption\Cipher'),
$sm->get('SclZfCartSagepay\Options\SagepayOptions')->getConnectionOptions(),
$sm->get('SclZfCartSagepay\Service\CryptService')
);
},
],
];
}
}
12 changes: 2 additions & 10 deletions src/SclZfCartSagepay/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@ public function successAction()

$serviceLocator = $this->getServiceLocator();

$cipher = $serviceLocator->get('SclZfCartSagepay\Encryption\Cipher');
$options = $serviceLocator->get('SclZfCartSagepay\Options\SagepayOptions');
$service = $serviceLocator->get('SclZfCartSagepay\Service\CallbackService');

$cryptService = $serviceLocator->get('SclZfCartSagepay\Service\CryptService');

$data = $cipher->decrypt(
$crypt,
$options->getConnectionOptions()->getPassword()
);

var_dump($cryptService->processResponseData($data));
$service->processResponse($crypt);

return [];
}
Expand Down
60 changes: 32 additions & 28 deletions src/SclZfCartSagepay/Sagepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ public function name()
return $this->options->getName();
}

/**
* {@inheritDoc}
*
* @return string
*/
public function generateTransactionId()
{
// @todo Get format string from config
return sprintf('SAGEPAY-%06d', $this->sequenceGenerator->get('SAGEPAY_PAYMENT_TX_ID'));
}

/**
*
* @param Form $form
Expand All @@ -120,15 +131,17 @@ private function addHiddenField(Form $form, $name, $value)

/**
* @param OrderIntefface $order
* @param string $transactionId
*
* @return string
*/
private function getCrypt(OrderInterface $order)
private function getCrypt(OrderInterface $order, $transactionId)
{
$data = $this->cryptService->createCryptData(
$order,
$this->customer,
// @todo Use the SequenceGenerator
$this->getTransactionId(),
$transactionId,
$this->options->getCurrency(),
$this->getCallbackUrl('success'),
$this->getCallbackUrl('failure')
Expand All @@ -140,31 +153,6 @@ private function getCrypt(OrderInterface $order)
);
}

/**
* getTransactionId
*
* @return string
*/
private function getTransactionId()
{
return 'TEST-SCL-TX-' . $this->sequenceGenerator->get('SAGEPAY-PAYMENT-TX-ID');
}

/**
* getCallbackUrl
*
* @param string $type
* @return string
*/
private function getCallbackUrl($type)
{
return 'http://localhost/SclAdmin/public' . $this->urlBuilder->getUrl(
'scl-zf-cart-sagepay/' . $type //,
//[],
//['force_canonical' => true]
);
}

/**
* {@inheritDoc}
*
Expand All @@ -181,7 +169,7 @@ public function updateCompleteForm(Form $form, OrderInterface $order, PaymentInt
$this->addHiddenField($form, self::VAR_PROTOCOL, $this->options->getVersion());
$this->addHiddenField($form, self::VAR_TYPE, self::TX_TYPE_PAYMENT);
$this->addHiddenField($form, self::VAR_ACCOUNT, $this->options->getAccount());
$this->addHiddenField($form, self::VAR_CRYPT, $this->getCrypt($order));
$this->addHiddenField($form, self::VAR_CRYPT, $this->getCrypt($order, $payment->getTransactionId()));
}

/**
Expand All @@ -193,4 +181,20 @@ public function updateCompleteForm(Form $form, OrderInterface $order, PaymentInt
public function complete(array $data)
{
}

/**
* getCallbackUrl
*
* @param string $type
* @return string
*/
private function getCallbackUrl($type)
{
return 'http://localhost/SclAdmin/public' . $this->urlBuilder->getUrl(
'scl-zf-cart-sagepay/' . $type //,
//[],
//['force_canonical' => true]
);
}

}
40 changes: 40 additions & 0 deletions src/SclZfCartSagepay/Service/CallbackService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace SclZfCartSagepay\Service;

use SclZfCartSagepay\Encryption\Cipher;
use SclZfCartSagepay\Options\ConnectionOptions;
use SclZfCartSagepay\Service\CryptService;

class CallbackService
{
private $cipher;

private $options;

private $cryptService;

/**
* Set collaborator objects.
*
* @param Cipher $cipher
* @param ConnectionOptions $options
* @param CryptService $cryptService
*/
public function __construct(
Cipher $cipher,
ConnectionOptions $options,
CryptService $cryptService
) {
$this->cipher = $cipher;
$this->options = $options;
$this->cryptService = $cryptService;
}

public function processResponse($encryptedData)
{
$data = $this->cipher->decrypt($encryptedData, $this->options->getPassword());

var_dump($this->cryptService->processResponseData($data));
}
}
7 changes: 7 additions & 0 deletions src/SclZfCartSagepay/Service/CryptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function createCryptData(
));
}

/**
* Takes a response string and proccesses it into a CallbackResponse object.
*
* @param string $data
*
* @return CallbackResponse
*/
public function processResponseData($data)
{
/*
Expand Down
6 changes: 0 additions & 6 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* SclZfGenericMapper (https://github.com/SCLInternet/SclZfGenericMapper)
*
* @link https://github.com/SCLInternet/SclZfGenericMapper for the canonical source repository
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

class TestBootstrap
{
Expand Down
49 changes: 39 additions & 10 deletions tests/SclZfCartSagepayTests/SagepayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SclZfCartSagepay\Service\CryptService;
use SclZfCart\Entity\Order;
use Zend\Form\Form;
use SclZfCartPayment\Entity\Payment;

/**
* Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-02-15 at 17:30:38.
Expand Down Expand Up @@ -78,6 +79,14 @@ public function test_service_manager_creates_instance()
);
}

public function test_is_an_instance_of_PaymentMethodInterface()
{
$this->assertInstanceOf(
'SclZfCartPayment\PaymentMethodInterface',
$this->sagepay
);
}

/*
* name()
*/
Expand All @@ -87,6 +96,30 @@ public function test_name_returns_the_name_of_the_module()
$this->assertEquals(self::TEST_NAME, $this->sagepay->name());
}

/*
* generateTransactionId()
*/

public function test_generateTransactionId_fetches_sequence_number()
{
$this->sequenceGenerator
->expects($this->once())
->method('get')
->with('SAGEPAY_PAYMENT_TX_ID');

$this->sagepay->generateTransactionId();
}

public function test_generateTransactionId_returns_transaction_id_string()
{
$this->sequenceGenerator
->expects($this->any())
->method('get')
->will($this->returnValue('7'));

$this->assertEquals('SAGEPAY-000007', $this->sagepay->generateTransactionId());
}

/*
* updateCompleteForm()
*/
Expand Down Expand Up @@ -149,12 +182,12 @@ public function test_updateCompleteForm_creates_failure_url()

public function test_updateCompleteForm_createCryptData()
{
$transactionId = 'TX-000123';

$order = new Order();

$this->sequenceGenerator
->expects($this->once())
->method('get')
->will($this->returnValue(7));
$payment = new Payment();
$payment->setTransactionId($transactionId);

$this->urlBuilder
->expects($this->at(0))
Expand All @@ -172,17 +205,13 @@ public function test_updateCompleteForm_createCryptData()
->with(
$this->identicalTo($order),
$this->identicalTo($this->customer),
$this->equalTo('TEST-SCL-TX-7'),
$this->equalTo($transactionId),
$this->equalTo(self::TEST_CURRENCY),
$this->equalTo('http://scl.co.uk/success'),
$this->equalTo('http://scl.co.uk/failure')
);

$this->sagepay->updateCompleteForm(
new Form(),
$order,
$this->getMock('SclZfCartPayment\Entity\PaymentInterface')
);
$this->sagepay->updateCompleteForm(new Form(), $order, $payment);
}

public function test_updateCompleteForm_encrypts_crypt_data()
Expand Down
Loading

0 comments on commit 1aeaf40

Please sign in to comment.