From e64cf8bc90421b27caaec2db67691b1f5a9c9a7f Mon Sep 17 00:00:00 2001 From: Sujip Thapa Date: Tue, 6 Dec 2022 17:10:31 +0545 Subject: [PATCH 1/2] Added EMV 3DSC --- README.md | 6 +- src/DirectPostGateway.php | 18 ++++-- src/Enums/TransactionType.php | 20 +++++++ src/Message/AbstractRequest.php | 13 ++++ src/Message/DirectPostAbstractRequest.php | 59 ++++++++++++++++--- .../DirectPostCompletePurchaseRequest.php | 6 +- src/Message/DirectPostWebhookRequest.php | 48 +++++++++++++++ src/Message/SecureXMLRiskPurchaseRequest.php | 3 + 8 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 src/Enums/TransactionType.php create mode 100644 src/Message/DirectPostWebhookRequest.php diff --git a/README.md b/README.md index 8c1bbc0..10b1a1f 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ The following gateways are provided by this package: } else { echo sprintf('Transaction %s failed: %s', $response->getTransactionReference(), $response->getMessage()); } +``` ### NAB Transact DirectPost v2 @@ -113,8 +114,8 @@ The following gateways are provided by this package: $gateway->setMerchantId('XYZ0010'); $gateway->setTransactionPassword('abcd1234'); - $gateway->setTestMode(true); + $gateway->setHasEMV3DSEnabled(true); $card = new CreditCard(array( 'firstName' => 'Sujip', @@ -128,8 +129,10 @@ The following gateways are provided by this package: $response = $gateway->purchase(array( 'amount' => '12.00', 'transactionId' => 'ORDER-ZYX8', + 'transactionReference' => '11fc42b0-bb7a-41a4-8b3c-096b3fd4d402' 'currency' => 'AUD', 'card' => $card, + 'clientIp' => '192.168.1.1' )) ->send(); @@ -185,6 +188,7 @@ The following gateways are provided by this package: $response = $gateway->completePurchase(array( 'amount' => '12.00', 'transactionId' => '1234566789205067', + 'transactionReference' => '11fc42b0-bb7a-41a4-8b3c-096b3fd4d402' 'currency' => 'AUD', 'returnUrl' => 'http://example.com/payment/response', )) diff --git a/src/DirectPostGateway.php b/src/DirectPostGateway.php index 8027a3e..1a8ce70 100644 --- a/src/DirectPostGateway.php +++ b/src/DirectPostGateway.php @@ -11,11 +11,6 @@ */ class DirectPostGateway extends AbstractGateway { - /** - * @var bool - */ - public $transparentRedirect = true; - public function getName() { return 'NABTransact Direct Post'; @@ -62,6 +57,19 @@ public function setTransactionPassword($value) return $this->setParameter('transactionPassword', $value); } + public function getHasEMV3DSEnabled() + { + return $this->getParameter('hasEMV3DSEnabled'); + } + + /** + * @param $value + */ + public function setHasEMV3DSEnabled($value) + { + return $this->setParameter('hasEMV3DSEnabled', $value); + } + /** * @param array $parameters * diff --git a/src/Enums/TransactionType.php b/src/Enums/TransactionType.php new file mode 100644 index 0000000..ee5aa1e --- /dev/null +++ b/src/Enums/TransactionType.php @@ -0,0 +1,20 @@ +setParameter('transactionPassword', $value); } + public function getHasEMV3DSEnabled() + { + return $this->getParameter('hasEMV3DSEnabled'); + } + + /** + * @param $value + */ + public function setHasEMV3DSEnabled($value) + { + return $this->setParameter('hasEMV3DSEnabled', $value); + } + /** * @return string */ diff --git a/src/Message/DirectPostAbstractRequest.php b/src/Message/DirectPostAbstractRequest.php index 1ad56f3..212e605 100644 --- a/src/Message/DirectPostAbstractRequest.php +++ b/src/Message/DirectPostAbstractRequest.php @@ -2,6 +2,8 @@ namespace Omnipay\NABTransact\Message; +use Omnipay\NABTransact\Enums\TransactionType; + /** * NABTransact Direct Post Abstract Request. */ @@ -22,14 +24,22 @@ abstract class DirectPostAbstractRequest extends AbstractRequest */ public function generateFingerprint(array $data) { - $hash = implode('|', [ + $hashable = [ $data['EPS_MERCHANT'], $this->getTransactionPassword(), $data['EPS_TXNTYPE'], $data['EPS_REFERENCEID'], $data['EPS_AMOUNT'], $data['EPS_TIMESTAMP'], - ]); + ]; + + if ($this->getHasEMV3DSEnabled()) { + $hashable = array_merge( + $hashable, [$data['EPS_ORDERID']] + ); + } + + $hash = implode('|', $hashable); return hash_hmac('sha256', $hash, $this->getTransactionPassword()); } @@ -43,15 +53,50 @@ public function getBaseData() $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_AMOUNT'] = $this->getAmount(); $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_IP'] = $this->getClientIp(); $data['EPS_REDIRECT'] = 'TRUE'; - $data['EPS_CURRENCY'] = $this->getCurrency(); + + if ($this->getNotifyUrl()) { + $data['EPS_CALLBACKURL'] = $this->getNotifyUrl(); + } + + if ($currency = $this->getCurrency()) { + $data['EPS_CURRENCY'] = $currency; + } + + $card = $this->getCard(); + + if ($billingPostcode = $card->getBillingPostcode()) { + $data['EPS_ZIPCODE'] = $billingPostcode; + } + + if ($billingCity = $card->getBillingCity()) { + $data['EPS_TOWN'] = $billingCity; + } + + if ($billingCountry = $card->getBillingCountry()) { + $data['EPS_BILLINGCOUNTRY'] = $billingCountry; + } + + if ($shippingCountry = $card->getShippingCountry()) { + $data['EPS_DELIVERYCOUNTRY'] = $shippingCountry; + } + + if ($emailAddress = $card->getEmail()) { + $data['EPS_EMAILADDRESS'] = $emailAddress; + } + + if ($this->getHasEMV3DSEnabled()) { + $data['EPS_ORDERID'] = $this->getTransactionReference(); + + $data['EPS_TXNTYPE'] = TransactionType::PAYMENT_3DS_EMV3DS; + } + + $data['EPS_FINGERPRINT'] = $this->generateFingerprint($data); return $data; } diff --git a/src/Message/DirectPostCompletePurchaseRequest.php b/src/Message/DirectPostCompletePurchaseRequest.php index 9cb86f5..d4b1e39 100644 --- a/src/Message/DirectPostCompletePurchaseRequest.php +++ b/src/Message/DirectPostCompletePurchaseRequest.php @@ -28,14 +28,16 @@ public function getData() */ public function generateResponseFingerprint($data) { - $hash = implode('|', [ + $hashable = [ $data['merchant'], $this->getTransactionPassword(), $data['refid'], $this->getAmount(), $data['timestamp'], $data['summarycode'], - ]); + ]; + + $hash = implode('|', $hashable); return hash_hmac('sha256', $hash, $this->getTransactionPassword()); } diff --git a/src/Message/DirectPostWebhookRequest.php b/src/Message/DirectPostWebhookRequest.php new file mode 100644 index 0000000..643b489 --- /dev/null +++ b/src/Message/DirectPostWebhookRequest.php @@ -0,0 +1,48 @@ +data = $data; + } + + public function generateResponseFingerprint($data) + { + $hashable = [ + $data['merchant'], + $data['txn_password'], + $data['refid'], + $data['amount'], + $data['timestamp'], + $data['summarycode'], + ]; + + $hash = implode('|', $hashable); + + return hash_hmac('sha256', $hash, $data['txn_password']); + } + + public function vefiyFingerPrint($fingerprint) + { + $data = $this->data; + + if ($fingerprint !== $this->generateResponseFingerprint($data)) { + $data['restext'] = $data['restext'] . ', Invalid fingerprint.'; + $data['summarycode'] = 3; + } + + return new DirectPostCompletePurchaseResponse($this, $data); + } + + public function getData() + { + return $this->data; + } + + public function sendData($data){} +} diff --git a/src/Message/SecureXMLRiskPurchaseRequest.php b/src/Message/SecureXMLRiskPurchaseRequest.php index da331e7..bf278f3 100644 --- a/src/Message/SecureXMLRiskPurchaseRequest.php +++ b/src/Message/SecureXMLRiskPurchaseRequest.php @@ -45,8 +45,11 @@ public function getData() $xml = $this->getBasePaymentXMLWithCard(); $buyer = $xml->addChild('BuyerInfo'); + $buyer->addChild('ip', $this->getIp('ip')); + $card = $this->getCard(); + if ($firstName = $card->getFirstName()) { $buyer->addChild('firstName', $firstName); } From 6716b1a51cb27444c0d4766dade346ecacfb4596 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 6 Dec 2022 11:26:02 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Message/DirectPostAbstractRequest.php | 3 ++- src/Message/DirectPostWebhookRequest.php | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Message/DirectPostAbstractRequest.php b/src/Message/DirectPostAbstractRequest.php index 212e605..465a62b 100644 --- a/src/Message/DirectPostAbstractRequest.php +++ b/src/Message/DirectPostAbstractRequest.php @@ -35,7 +35,8 @@ public function generateFingerprint(array $data) if ($this->getHasEMV3DSEnabled()) { $hashable = array_merge( - $hashable, [$data['EPS_ORDERID']] + $hashable, + [$data['EPS_ORDERID']] ); } diff --git a/src/Message/DirectPostWebhookRequest.php b/src/Message/DirectPostWebhookRequest.php index 643b489..f5f5a9e 100644 --- a/src/Message/DirectPostWebhookRequest.php +++ b/src/Message/DirectPostWebhookRequest.php @@ -32,7 +32,7 @@ public function vefiyFingerPrint($fingerprint) $data = $this->data; if ($fingerprint !== $this->generateResponseFingerprint($data)) { - $data['restext'] = $data['restext'] . ', Invalid fingerprint.'; + $data['restext'] = $data['restext'].', Invalid fingerprint.'; $data['summarycode'] = 3; } @@ -44,5 +44,7 @@ public function getData() return $this->data; } - public function sendData($data){} + public function sendData($data) + { + } }