Skip to content

Commit 26d4ad4

Browse files
authored
Merge pull request #10 from sudiptpa/upgrade/nab-direct-postv2
NAB Transact direct post v2
2 parents 7baeab2 + ff56ae8 commit 26d4ad4

11 files changed

+661
-0
lines changed

src/DirectPostGateway.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact;
4+
5+
use Omnipay\Common\AbstractGateway;
6+
7+
/**
8+
* NABTransact Direct Post Gateway.
9+
*/
10+
class DirectPostGateway extends AbstractGateway
11+
{
12+
/**
13+
* @var mixed
14+
*/
15+
public $transparentRedirect = true;
16+
17+
public function getName()
18+
{
19+
return 'NABTransact Direct Post';
20+
}
21+
22+
public function getDefaultParameters()
23+
{
24+
return array(
25+
'merchantId' => '',
26+
'transactionPassword' => '',
27+
'testMode' => false,
28+
);
29+
}
30+
31+
/**
32+
* @return mixed
33+
*/
34+
public function getMerchantId()
35+
{
36+
return $this->getParameter('merchantId');
37+
}
38+
39+
/**
40+
* @param $value
41+
*
42+
* @return mixed
43+
*/
44+
public function setMerchantId($value)
45+
{
46+
return $this->setParameter('merchantId', $value);
47+
}
48+
49+
/**
50+
* @return mixed
51+
*/
52+
public function getTransactionPassword()
53+
{
54+
return $this->getParameter('transactionPassword');
55+
}
56+
57+
/**
58+
* @param $value
59+
*
60+
* @return mixed
61+
*/
62+
public function setTransactionPassword($value)
63+
{
64+
return $this->setParameter('transactionPassword', $value);
65+
}
66+
67+
/**
68+
* @param array $parameters
69+
*
70+
* @return mixed
71+
*/
72+
public function authorize(array $parameters = array())
73+
{
74+
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostAuthorizeRequest', $parameters);
75+
}
76+
77+
/**
78+
* @param array $parameters
79+
*
80+
* @return mixed
81+
*/
82+
public function completeAuthorize(array $parameters = array())
83+
{
84+
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostCompletePurchaseRequest', $parameters);
85+
}
86+
87+
/**
88+
* @param array $parameters
89+
*
90+
* @return mixed
91+
*/
92+
public function purchase(array $parameters = array())
93+
{
94+
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostPurchaseRequest', $parameters);
95+
}
96+
97+
/**
98+
* @param array $parameters
99+
*
100+
* @return mixed
101+
*/
102+
public function completePurchase(array $parameters = array())
103+
{
104+
return $this->createRequest('\Omnipay\NABTransact\Message\DirectPostCompletePurchaseRequest', $parameters);
105+
}
106+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
/**
6+
* NABTransact Direct Post Abstract Request.
7+
*/
8+
abstract class DirectPostAbstractRequest extends AbstractRequest
9+
{
10+
/**
11+
* @var string
12+
*/
13+
public $testEndpoint = 'https://transact.nab.com.au/test/directpostv2/authorise';
14+
15+
/**
16+
* @var string
17+
*/
18+
public $liveEndpoint = 'https://transact.nab.com.au/live/directpostv2/authorise';
19+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
/**
6+
* NABTransact Direct Post Authorize Request.
7+
*/
8+
class DirectPostAuthorizeRequest extends DirectPostAbstractRequest
9+
{
10+
/**
11+
* @var string
12+
*/
13+
public $txnType = '1';
14+
15+
/**
16+
* @return mixed
17+
*/
18+
public function getData()
19+
{
20+
$this->validate('amount', 'returnUrl', 'card');
21+
22+
$data = array();
23+
$data['EPS_MERCHANT'] = $this->getMerchantId();
24+
$data['EPS_TXNTYPE'] = $this->txnType;
25+
$data['EPS_IP'] = $this->getClientIp();
26+
$data['EPS_AMOUNT'] = $this->getAmount();
27+
$data['EPS_REFERENCEID'] = $this->getTransactionId();
28+
$data['EPS_TIMESTAMP'] = gmdate('YmdHis');
29+
$data['EPS_FINGERPRINT'] = $this->generateFingerprint($data);
30+
$data['EPS_RESULTURL'] = $this->getReturnUrl();
31+
$data['EPS_CALLBACKURL'] = $this->getNotifyUrl() ?: $this->getReturnUrl();
32+
$data['EPS_REDIRECT'] = 'TRUE';
33+
$data['EPS_CURRENCY'] = $this->getCurrency();
34+
35+
$data = array_replace($data, $this->getCardData());
36+
37+
return $data;
38+
}
39+
40+
/**
41+
* @param array $data
42+
*/
43+
public function generateFingerprint(array $data)
44+
{
45+
$hash = implode(
46+
'|',
47+
array(
48+
$data['EPS_MERCHANT'],
49+
$this->getTransactionPassword(),
50+
$data['EPS_TXNTYPE'],
51+
$data['EPS_REFERENCEID'],
52+
$data['EPS_AMOUNT'],
53+
$data['EPS_TIMESTAMP'],
54+
)
55+
);
56+
57+
return sha1($hash);
58+
}
59+
60+
/**
61+
* @param $data
62+
*
63+
* @return mixed
64+
*/
65+
public function sendData($data)
66+
{
67+
return $this->response = new DirectPostAuthorizeResponse($this, $data, $this->getEndpoint());
68+
}
69+
70+
/**
71+
* @return mixed
72+
*/
73+
protected function getCardData()
74+
{
75+
$this->getCard()->validate();
76+
77+
$data = array();
78+
79+
$data['EPS_CARDNUMBER'] = $this->getCard()->getNumber();
80+
$data['EPS_EXPIRYMONTH'] = $this->getCard()->getExpiryMonth();
81+
$data['EPS_EXPIRYYEAR'] = $this->getCard()->getExpiryYear();
82+
$data['EPS_CCV'] = $this->getCard()->getCvv();
83+
84+
return $data;
85+
}
86+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
use Omnipay\Common\Message\RedirectResponseInterface;
7+
use Omnipay\Common\Message\RequestInterface;
8+
9+
/**
10+
* NABTransact Direct Post Authorize Response.
11+
*/
12+
class DirectPostAuthorizeResponse extends AbstractResponse implements RedirectResponseInterface
13+
{
14+
/**
15+
* @var mixed
16+
*/
17+
protected $redirectUrl;
18+
19+
/**
20+
* @param RequestInterface $request
21+
* @param $data
22+
* @param $redirectUrl
23+
*/
24+
public function __construct(RequestInterface $request, $data, $redirectUrl)
25+
{
26+
$this->request = $request;
27+
$this->data = $data;
28+
$this->redirectUrl = $redirectUrl;
29+
}
30+
31+
public function isSuccessful()
32+
{
33+
return false;
34+
}
35+
36+
public function isRedirect()
37+
{
38+
return true;
39+
}
40+
41+
/**
42+
* @return mixed
43+
*/
44+
public function getRedirectUrl()
45+
{
46+
return $this->redirectUrl;
47+
}
48+
49+
public function getRedirectMethod()
50+
{
51+
return 'POST';
52+
}
53+
54+
/**
55+
* @return mixed
56+
*/
57+
public function getRedirectData()
58+
{
59+
return $this->getData();
60+
}
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
use Omnipay\Common\Exception\InvalidRequestException;
6+
7+
/**
8+
* NABTransact Direct Post Complete Purchase Request.
9+
*/
10+
class DirectPostCompletePurchaseRequest extends DirectPostAbstractRequest
11+
{
12+
/**
13+
* @return mixed
14+
*/
15+
public function getData()
16+
{
17+
$data = $this->httpRequest->query->all();
18+
19+
if ($this->generateResponseFingerprint($data) !== $this->httpRequest->query->get('fingerprint')) {
20+
throw new InvalidRequestException('Invalid fingerprint');
21+
}
22+
23+
return $data;
24+
}
25+
26+
/**
27+
* @param $data
28+
*/
29+
public function generateResponseFingerprint($data)
30+
{
31+
$fields = implode(
32+
'|',
33+
array(
34+
$data['merchant'],
35+
$this->getTransactionPassword(),
36+
$data['refid'],
37+
$this->getAmount(),
38+
$data['timestamp'],
39+
$data['summarycode'],
40+
)
41+
);
42+
43+
return sha1($fields);
44+
}
45+
46+
/**
47+
* @param $data
48+
*
49+
* @return mixed
50+
*/
51+
public function sendData($data)
52+
{
53+
return $this->response = new DirectPostCompletePurchaseResponse($this, $data);
54+
}
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
7+
/**
8+
* NABTransact Direct Post Complete Purchase Response.
9+
*/
10+
class DirectPostCompletePurchaseResponse extends AbstractResponse
11+
{
12+
/**
13+
* @return mixed
14+
*/
15+
public function isSuccessful()
16+
{
17+
return $this->summaryCode() && (int) $this->getCode() == 00;
18+
}
19+
20+
public function summaryCode()
21+
{
22+
return isset($this->data['summarycode']) && (int) $this->data['summarycode'] == 1;
23+
}
24+
25+
/**
26+
* @return mixed
27+
*/
28+
public function getMessage()
29+
{
30+
if (isset($this->data['restext'])) {
31+
return $this->data['restext'];
32+
}
33+
}
34+
35+
/**
36+
* @return mixed
37+
*/
38+
public function getCode()
39+
{
40+
if (isset($this->data['rescode'])) {
41+
return $this->data['rescode'];
42+
}
43+
}
44+
45+
/**
46+
* @return mixed
47+
*/
48+
public function getTransactionReference()
49+
{
50+
if (isset($this->data['txnid'])) {
51+
return $this->data['txnid'];
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)