Skip to content

Commit

Permalink
Merge pull request #10 from sudiptpa/upgrade/nab-direct-postv2
Browse files Browse the repository at this point in the history
NAB Transact direct post v2
  • Loading branch information
sudiptpa authored Nov 26, 2016
2 parents 7baeab2 + ff56ae8 commit 26d4ad4
Show file tree
Hide file tree
Showing 11 changed files with 661 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/DirectPostGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Omnipay\NABTransact;

use Omnipay\Common\AbstractGateway;

/**
* NABTransact Direct Post Gateway.
*/
class DirectPostGateway extends AbstractGateway
{
/**
* @var mixed
*/
public $transparentRedirect = true;

public function getName()
{
return 'NABTransact Direct Post';
}

public function getDefaultParameters()
{
return array(
'merchantId' => '',
'transactionPassword' => '',
'testMode' => false,
);
}

/**
* @return mixed
*/
public function getMerchantId()
{
return $this->getParameter('merchantId');
}

/**
* @param $value
*
* @return mixed
*/
public function setMerchantId($value)
{
return $this->setParameter('merchantId', $value);
}

/**
* @return mixed
*/
public function getTransactionPassword()
{
return $this->getParameter('transactionPassword');
}

/**
* @param $value
*
* @return mixed
*/
public function setTransactionPassword($value)
{
return $this->setParameter('transactionPassword', $value);
}

/**
* @param array $parameters
*
* @return mixed
*/
public function authorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostAuthorizeRequest', $parameters);
}

/**
* @param array $parameters
*
* @return mixed
*/
public function completeAuthorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostCompletePurchaseRequest', $parameters);
}

/**
* @param array $parameters
*
* @return mixed
*/
public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostPurchaseRequest', $parameters);
}

/**
* @param array $parameters
*
* @return mixed
*/
public function completePurchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostCompletePurchaseRequest', $parameters);
}
}
19 changes: 19 additions & 0 deletions src/Message/DirectPostAbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Omnipay\NABTransact\Message;

/**
* NABTransact Direct Post Abstract Request.
*/
abstract class DirectPostAbstractRequest extends AbstractRequest
{
/**
* @var string
*/
public $testEndpoint = 'https://transact.nab.com.au/test/directpostv2/authorise';

/**
* @var string
*/
public $liveEndpoint = 'https://transact.nab.com.au/live/directpostv2/authorise';
}
86 changes: 86 additions & 0 deletions src/Message/DirectPostAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Omnipay\NABTransact\Message;

/**
* NABTransact Direct Post Authorize Request.
*/
class DirectPostAuthorizeRequest extends DirectPostAbstractRequest
{
/**
* @var string
*/
public $txnType = '1';

/**
* @return mixed
*/
public function getData()
{
$this->validate('amount', 'returnUrl', 'card');

$data = array();
$data['EPS_MERCHANT'] = $this->getMerchantId();
$data['EPS_TXNTYPE'] = $this->txnType;
$data['EPS_IP'] = $this->getClientIp();
$data['EPS_AMOUNT'] = $this->getAmount();
$data['EPS_REFERENCEID'] = $this->getTransactionId();
$data['EPS_TIMESTAMP'] = gmdate('YmdHis');
$data['EPS_FINGERPRINT'] = $this->generateFingerprint($data);
$data['EPS_RESULTURL'] = $this->getReturnUrl();
$data['EPS_CALLBACKURL'] = $this->getNotifyUrl() ?: $this->getReturnUrl();
$data['EPS_REDIRECT'] = 'TRUE';
$data['EPS_CURRENCY'] = $this->getCurrency();

$data = array_replace($data, $this->getCardData());

return $data;
}

/**
* @param array $data
*/
public function generateFingerprint(array $data)
{
$hash = implode(
'|',
array(
$data['EPS_MERCHANT'],
$this->getTransactionPassword(),
$data['EPS_TXNTYPE'],
$data['EPS_REFERENCEID'],
$data['EPS_AMOUNT'],
$data['EPS_TIMESTAMP'],
)
);

return sha1($hash);
}

/**
* @param $data
*
* @return mixed
*/
public function sendData($data)
{
return $this->response = new DirectPostAuthorizeResponse($this, $data, $this->getEndpoint());
}

/**
* @return mixed
*/
protected function getCardData()
{
$this->getCard()->validate();

$data = array();

$data['EPS_CARDNUMBER'] = $this->getCard()->getNumber();
$data['EPS_EXPIRYMONTH'] = $this->getCard()->getExpiryMonth();
$data['EPS_EXPIRYYEAR'] = $this->getCard()->getExpiryYear();
$data['EPS_CCV'] = $this->getCard()->getCvv();

return $data;
}
}
61 changes: 61 additions & 0 deletions src/Message/DirectPostAuthorizeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Omnipay\NABTransact\Message;

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RedirectResponseInterface;
use Omnipay\Common\Message\RequestInterface;

/**
* NABTransact Direct Post Authorize Response.
*/
class DirectPostAuthorizeResponse extends AbstractResponse implements RedirectResponseInterface
{
/**
* @var mixed
*/
protected $redirectUrl;

/**
* @param RequestInterface $request
* @param $data
* @param $redirectUrl
*/
public function __construct(RequestInterface $request, $data, $redirectUrl)
{
$this->request = $request;
$this->data = $data;
$this->redirectUrl = $redirectUrl;
}

public function isSuccessful()
{
return false;
}

public function isRedirect()
{
return true;
}

/**
* @return mixed
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}

public function getRedirectMethod()
{
return 'POST';
}

/**
* @return mixed
*/
public function getRedirectData()
{
return $this->getData();
}
}
55 changes: 55 additions & 0 deletions src/Message/DirectPostCompletePurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Omnipay\NABTransact\Message;

use Omnipay\Common\Exception\InvalidRequestException;

/**
* NABTransact Direct Post Complete Purchase Request.
*/
class DirectPostCompletePurchaseRequest extends DirectPostAbstractRequest
{
/**
* @return mixed
*/
public function getData()
{
$data = $this->httpRequest->query->all();

if ($this->generateResponseFingerprint($data) !== $this->httpRequest->query->get('fingerprint')) {
throw new InvalidRequestException('Invalid fingerprint');
}

return $data;
}

/**
* @param $data
*/
public function generateResponseFingerprint($data)
{
$fields = implode(
'|',
array(
$data['merchant'],
$this->getTransactionPassword(),
$data['refid'],
$this->getAmount(),
$data['timestamp'],
$data['summarycode'],
)
);

return sha1($fields);
}

/**
* @param $data
*
* @return mixed
*/
public function sendData($data)
{
return $this->response = new DirectPostCompletePurchaseResponse($this, $data);
}
}
54 changes: 54 additions & 0 deletions src/Message/DirectPostCompletePurchaseResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Omnipay\NABTransact\Message;

use Omnipay\Common\Message\AbstractResponse;

/**
* NABTransact Direct Post Complete Purchase Response.
*/
class DirectPostCompletePurchaseResponse extends AbstractResponse
{
/**
* @return mixed
*/
public function isSuccessful()
{
return $this->summaryCode() && (int) $this->getCode() == 00;
}

public function summaryCode()
{
return isset($this->data['summarycode']) && (int) $this->data['summarycode'] == 1;
}

/**
* @return mixed
*/
public function getMessage()
{
if (isset($this->data['restext'])) {
return $this->data['restext'];
}
}

/**
* @return mixed
*/
public function getCode()
{
if (isset($this->data['rescode'])) {
return $this->data['rescode'];
}
}

/**
* @return mixed
*/
public function getTransactionReference()
{
if (isset($this->data['txnid'])) {
return $this->data['txnid'];
}
}
}
Loading

0 comments on commit 26d4ad4

Please sign in to comment.