Skip to content

Commit 0d80eed

Browse files
authored
Upgrade unionpay direct post (#11)
* Added UnionPay support with DirectPost v2 * Apply fixes from StyleCI * UnionPay Integration * Apply fixes from StyleCI * Updated PSR2 fixes
1 parent 9d090e6 commit 0d80eed

7 files changed

+223
-32
lines changed

src/Message/DirectPostAbstractRequest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,46 @@ abstract class DirectPostAbstractRequest extends AbstractRequest
1616
* @var string
1717
*/
1818
public $liveEndpoint = 'https://transact.nab.com.au/live/directpostv2/authorise';
19+
20+
/**
21+
* @param array $data
22+
*/
23+
public function generateFingerprint(array $data)
24+
{
25+
$hash = implode(
26+
'|',
27+
array(
28+
$data['EPS_MERCHANT'],
29+
$this->getTransactionPassword(),
30+
$data['EPS_TXNTYPE'],
31+
$data['EPS_REFERENCEID'],
32+
$data['EPS_AMOUNT'],
33+
$data['EPS_TIMESTAMP'],
34+
)
35+
);
36+
37+
return sha1($hash);
38+
}
39+
40+
/**
41+
* @return mixed
42+
*/
43+
public function getBaseData()
44+
{
45+
$data = array();
46+
47+
$data['EPS_MERCHANT'] = $this->getMerchantId();
48+
$data['EPS_TXNTYPE'] = $this->txnType;
49+
$data['EPS_IP'] = $this->getClientIp();
50+
$data['EPS_AMOUNT'] = $this->getAmount();
51+
$data['EPS_REFERENCEID'] = $this->getTransactionId();
52+
$data['EPS_TIMESTAMP'] = gmdate('YmdHis');
53+
$data['EPS_FINGERPRINT'] = $this->generateFingerprint($data);
54+
$data['EPS_RESULTURL'] = $this->getReturnUrl();
55+
$data['EPS_CALLBACKURL'] = $this->getNotifyUrl() ?: $this->getReturnUrl();
56+
$data['EPS_REDIRECT'] = 'TRUE';
57+
$data['EPS_CURRENCY'] = $this->getCurrency();
58+
59+
return $data;
60+
}
1961
}

src/Message/DirectPostAuthorizeRequest.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,13 @@ public function getData()
1919
{
2020
$this->validate('amount', 'returnUrl', 'card');
2121

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();
22+
$data = $this->getBaseData();
3423

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

3726
return $data;
3827
}
3928

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-
6029
/**
6130
* @param $data
6231
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
/**
6+
* UnionPayCompletePurchaseRequest.
7+
*/
8+
class UnionPayCompletePurchaseRequest extends DirectPostCompletePurchaseRequest
9+
{
10+
/**
11+
* @param $data
12+
*
13+
* @return mixed
14+
*/
15+
public function sendData($data)
16+
{
17+
$this->response = new UnionPayCompletePurchaseResponse($this, $data);
18+
19+
return $this->response;
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
use Omnipay\Common\Message\RequestInterface;
6+
7+
/**
8+
* UnionPayCompletePurchaseResponse.
9+
*/
10+
class UnionPayCompletePurchaseResponse extends DirectPostCompletePurchaseResponse
11+
{
12+
/**
13+
* @param RequestInterface $request
14+
* @param $data
15+
*/
16+
public function __construct(RequestInterface $request, $data)
17+
{
18+
if (!is_array($data)) {
19+
parse_str($data, $data);
20+
}
21+
22+
parent::__construct($request, $data);
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact\Message;
4+
5+
/**
6+
* UnionPayPurchaseRequest.
7+
*/
8+
class UnionPayPurchaseRequest extends DirectPostAbstractRequest
9+
{
10+
/**
11+
* @var string
12+
*/
13+
public $txnType = '0';
14+
15+
/**
16+
* @return mixed
17+
*/
18+
public function getData()
19+
{
20+
$this->validate('amount', 'returnUrl', 'transactionId');
21+
22+
$data = $this->getBaseData();
23+
24+
$data['EPS_PAYMENTCHOICE'] = 'UPOP';
25+
26+
return $data;
27+
}
28+
29+
/**
30+
* @param $data
31+
*
32+
* @return mixed
33+
*/
34+
public function sendData($data)
35+
{
36+
$redirectUrl = $this->getEndpoint().'?'.http_build_query($data);
37+
38+
return $this->response = new UnionPayPurchaseResponse($this, $data, $redirectUrl);
39+
}
40+
}
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+
* UnionPayPurchaseResponse.
11+
*/
12+
class UnionPayPurchaseResponse 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 'GET';
52+
}
53+
54+
/**
55+
* @return mixed
56+
*/
57+
public function getRedirectData()
58+
{
59+
return $this->getData();
60+
}
61+
}

src/UnionPayGateway.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Omnipay\NABTransact;
4+
5+
/**
6+
* NABTransact UnionPay Gateway.
7+
*/
8+
class UnionPayGateway extends DirectPostGateway
9+
{
10+
public function getName()
11+
{
12+
return 'NABTransact UnionPay';
13+
}
14+
15+
/**
16+
* @param array $parameters
17+
*
18+
* @return mixed
19+
*/
20+
public function purchase(array $parameters = array())
21+
{
22+
return $this->createRequest('\Omnipay\NABTransact\Message\UnionPayPurchaseRequest', $parameters);
23+
}
24+
25+
/**
26+
* @param array $parameters
27+
*
28+
* @return mixed
29+
*/
30+
public function completePurchase(array $parameters = array())
31+
{
32+
return $this->createRequest('\Omnipay\NABTransact\Message\UnionPayCompletePurchaseRequest', $parameters);
33+
}
34+
}

0 commit comments

Comments
 (0)