Skip to content

Commit

Permalink
Add callback object
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Oram committed Nov 15, 2013
1 parent 5f85541 commit 095796f
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 7 deletions.
24 changes: 20 additions & 4 deletions src/SclZfCartSagepay/Controller/PaymentController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php

namespace SclZfCartSagepay;
namespace SclZfCartSagepay\Controller;

use Zend\Mvc\Controller\AbstractActionController;

Expand All @@ -14,11 +14,27 @@ class PaymentController extends AbstractActionController
{
public function successAction()
{
return array();
$crypt = $this->getRequest()->getQuery('crypt');

$serviceLocator = $this->getServiceLocator();

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

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

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

var_dump($cryptService->processResponseData($data));

return [];
}

public function failureAction()
{
return array();
return [];
}
}
153 changes: 153 additions & 0 deletions src/SclZfCartSagepay/Model/CallbackResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace SclZfCartSagepay\Model;

class CallbackResponse
{
/**
* Transaction completed successfully with authorisation.
*/
const STATUS_OK = 'OK';

/**
* PENDING
*/
const STATUS_PENDING = 'PENDING';

/**
* The Sage Pay system could not authorise the
* transaction because the details provided by the Customer were
* incorrect, or insufficient funds were available. However the
* Transaction has completed through the Sage Pay System.
*/
const STATUS_NOTAUTHED = 'NOTAUTHED';

/**
* Input message was missing fields or badly formatted
* - normally will only occur during development and vendor
* integration.
*/
const STATUS_MALFORMED = 'MALFORMED';

/**
* Transaction was not registered because although the
* POST format was valid, some information supplied was invalid. e.g.
* incorrect vendor name or currency.
*/
const STATUS_INVALID = 'INVALID';

/**
* The Transaction could not be completed because the user
* clicked the CANCEL button on the payment pages, or went inactive
* for 15 minutes or longer.
*/
const STATUS_ABORT = 'ABORT';

/**
* The Sage Pay System rejected the transaction because
* of the fraud screening rules you have set on your account.
* Note : The bank may have authorised the transaction but your own
* rule bases for AVS/CV2 or 3D-Secure caused the transaction to be
* rejected.
*/
const STATUS_REJECTED = 'REJECTED';

/**
* The 3D-Secure checks were performed
* successfully and the card details secured at Sage Pay. Only returned
* if TxType is AUTHENTICATE.
*/
const STATUS_AUTHENTICATED = 'AUTHENTICATED';

/**
* 3D-Secure checks failed or were not performed, but
* the card details are still secured at Sage Pay. Only returned if TxType
* is AUTHENTICATE.
*/
const STATUS_REGISTERED = 'REGISTERED';

/**
* A problem occurred at Sage Pay which prevented
* transaction completion.
* Please notify Sage Pay if a Status report of ERROR is seen, together
* with your VendorTxCode and the StatusDetail text.
*/
const STATUS_ERROR = 'ERROR';

private $vendorTxCode;

private $vpsTxId;

private $status;

private $statusDetail;

private $txAuthNo;

private $avsCv2;

private $addressResult;

private $postCodeResult;

private $cv2Result;

private $giftAid;

private $secureStatus3D;

private $cavv;

private $cardType;

private $last4Digits;

private $declineCode;

private $amount;

private $bankAuthCode;

public function __construct(
$vendorTxCode,
$vpsTxId,
$status,
$statusDetail,
$txAuthNo,
$avsCv2,
$addressResult,
$postCodeResult,
$cv2Result,
$giftAid,
$secureStatus3D,
$cavv,
$cardType,
$last4Digits,
$declineCode,
$amount,
$bankAuthCode
) {
$this->vendorTxCode = $vendorTxCode;
$this->vpsTxId = $vpsTxId;
$this->status = $status;
$this->statusDetail = $statusDetail;
$this->txAuthNo = $txAuthNo;
$this->avsCv2 = $avsCv2;
$this->addressResult = $addressResult;
$this->postCodeResult = $postCodeResult;
$this->cv2Result = $cv2Result;
$this->giftAid = $giftAid;
$this->secureStatus3D = $secureStatus3D;
$this->cavv = $cavv;
$this->cardType = $cardType;
$this->last4Digits = $last4Digits;
$this->declineCode = $declineCode;
$this->amount = $amount;
$this->bankAuthCode = $bankAuthCode;
}

public function __get($name)
{
return $this->$name;
}
}
8 changes: 5 additions & 3 deletions src/SclZfCartSagepay/Sagepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ private function getTransactionId()
*/
private function getCallbackUrl($type)
{
// @todo Get server name from the environment
return 'http://scl.co.uk'
. $this->urlBuilder->getUrl('scl-zf-cart-sagepay/' . $type);
return 'http://localhost/SclAdmin/public' . $this->urlBuilder->getUrl(
'scl-zf-cart-sagepay/' . $type //,
//[],
//['force_canonical' => true]
);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/SclZfCartSagepay/Service/CryptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SclZfCart\Customer\CustomerInterface;
use SclZfCart\Entity\OrderInterface;
use SclZfCartSagepay\Model\CallbackResponse;

class CryptService
{
Expand Down Expand Up @@ -41,6 +42,55 @@ public function createCryptData(
));
}

public function processResponseData($data)
{
/*
VendorTxCode=TEST-SCL-TX-21
VPSTxId={FA960C35-1331-FDD4-1611-447B4C76038C}
Status=OK
StatusDetail=0000 : The Authorisation was Successful.
TxAuthNo=5820406
AVSCV2=ALL MATCH
AddressResult=MATCHED
PostCodeResult=MATCHED
CV2Result=MATCHED
GiftAid=0
3DSecureStatus=OK
CAVV=AAABARR5kwAAAAAAAAAAAAAAAAA=
CardType=VISA
Last4Digits=0006
DeclineCode=00
Amount=11.99
BankAuthCode=999777
*/
parse_str($data, $values);

return new CallbackResponse(
$this->valueIfExists($values, 'VendorTxCode'),
$this->valueIfExists($values, 'VPSTxId'),
$this->valueIfExists($values, 'Status'),
$this->valueIfExists($values, 'StatusDetail'),
$this->valueIfExists($values, 'TxAuthNo'),
$this->valueIfExists($values, 'AVSCV2'),
$this->valueIfExists($values, 'AddressResult'),
$this->valueIfExists($values, 'PostCodeResult'),
$this->valueIfExists($values, 'CV2Result'),
$this->valueIfExists($values, 'GiftAid'),
$this->valueIfExists($values, '3DSecureStatus'),
$this->valueIfExists($values, 'CAVV'),
$this->valueIfExists($values, 'CardType'),
$this->valueIfExists($values, 'Last4Digits'),
$this->valueIfExists($values, 'DeclineCode'),
$this->valueIfExists($values, 'Amount'),
$this->valueIfExists($values, 'BankAuthCode')
);
}

private function valueIfExists(array $data, $key)
{
return isset($data[$key]) ? $data[$key] : null;
}

/**
* Returns a HTTP GET style parameter string.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/SclZfCartSagepayTests/Service/CryptServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,60 @@ public function test_createCryptData()
$this->assertVarStringContainsContact('Delivery', $cryptData);
}

/*
* processResponseData()
*/

public function test_processResponseData_return_CallbackResponse()
{
$this->assertInstanceOf(
'SclZfCartSagepay\Model\CallbackResponse',
$this->service->processResponseData('')
);
}

public function test_processResponseData_sets_venderTxCode()
{
$data = 'VendorTxCode=TX123'
. '&VPSTxId={123}'
. '&Status=OK'
. '&StatusDetail=0000 : The Authorisation was Successful.'
. '&TxAuthNo=123456'
. '&AVSCV2=ALL MATCH'
. '&AddressResult=MATCHED'
. '&PostCodeResult=MATCHED'
. '&CV2Result=MATCHED'
. '&GiftAid=0'
. '&3DSecureStatus=OK'
. '&CAVV=AAABBB'
. '&CardType=VISA'
. '&Last4Digits=0001'
. '&DeclineCode=00'
. '&Amount=9.99'
. '&BankAuthCode=999'
;

$response = $this->service->processResponseData($data);

$this->assertEquals('TX123', $response->vendorTxCode);
$this->assertEquals('{123}', $response->vpsTxId);
$this->assertEquals('OK', $response->status);
$this->assertEquals('0000 : The Authorisation was Successful.', $response->statusDetail);
$this->assertEquals('123456', $response->txAuthNo);
$this->assertEquals('ALL MATCH', $response->avsCv2);
$this->assertEquals('MATCHED', $response->addressResult);
$this->assertEquals('MATCHED', $response->postCodeResult);
$this->assertEquals('MATCHED', $response->cv2Result);
$this->assertEquals('0', $response->giftAid);
$this->assertEquals('OK', $response->secureStatus3D);
$this->assertEquals('AAABBB', $response->cavv);
$this->assertEquals('VISA', $response->cardType);
$this->assertEquals('0001', $response->last4Digits);
$this->assertEquals('00', $response->declineCode);
$this->assertEquals('9.99', $response->amount);
$this->assertEquals('999', $response->bankAuthCode);
}

/*
* Private methods
*/
Expand Down

0 comments on commit 095796f

Please sign in to comment.