Skip to content

Commit d6ebc0c

Browse files
authored
don't run code to add token if save card is unchecked or vault is disabled to address magento/magento2#5902, add logging to VaultHandler, version 1.0.8 (#3)
1 parent cfc0de7 commit d6ebc0c

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed

Gateway/Request/AuthorizationRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public function build(array $buildSubject)
3939
'xExp' => sprintf('%02d%02d', $payment->getAdditionalInformation("cc_exp_month"), substr($payment->getAdditionalInformation("cc_exp_year"), -2)),
4040
'xCVV' => $payment->getAdditionalInformation("xCVV"),
4141
'xCommand' => 'cc:authonly',
42-
// 'xToken' => $payment->getAdditionalInformation("xToken"),
4342
'xInvoice' => $order->getOrderIncrementId(),
4443
'xCurrency' => $order->getCurrencyCode(),
4544
'xCardNum' => $payment->getAdditionalInformation("xCardNum"),

Gateway/Request/BaseRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ public function build(array $buildSubject)
5353

5454
$order = $paymentDO->getOrder();
5555

56-
// $this->productMetadata->getEdition();
5756
return [
5857
'xVersion' => '4.5.8',
5958
'xSoftwareName' => 'Magento ' . $this->productMetadata->getEdition() . " ". $this->productMetadata->getVersion(),
60-
'xSoftwareVersion' => '1.0.7',
59+
'xSoftwareVersion' => '1.0.8',
6160
'xKey' => $this->config->getValue(
6261
'cardknox_transaction_key',
6362
$order->getStoreId()

Gateway/Response/VaultHandler.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//use Magento\Vault\Model\CreditCardTokenFactory;
1616
use Magento\Payment\Model\InfoInterface;
1717
use CardknoxDevelopment\Cardknox\Gateway\Config\Config;
18+
use Magento\Payment\Model\Method\Logger;
1819

1920
class VaultHandler implements HandlerInterface
2021
{
@@ -26,7 +27,7 @@ class VaultHandler implements HandlerInterface
2627
/**
2728
* @var CreditCardTokenFactory
2829
*/
29-
// protected $creditCardTokenFactory;
30+
// protected $creditCardTokenFactory;
3031

3132
protected $paymentTokenFactory;
3233
/**
@@ -35,6 +36,16 @@ class VaultHandler implements HandlerInterface
3536
protected $paymentExtensionFactory;
3637

3738
protected $config;
39+
40+
/**
41+
* @var Logger
42+
*/
43+
private $logger;
44+
45+
/**
46+
* @param Logger $logger
47+
*/
48+
3849
/**
3950
* Constructor
4051
*
@@ -44,11 +55,13 @@ class VaultHandler implements HandlerInterface
4455
public function __construct(
4556
PaymentTokenInterfaceFactory $paymentTokenFactory,
4657
OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
47-
Config $config
58+
Config $config,
59+
Logger $logger
4860
) {
4961
$this->paymentTokenFactory = $paymentTokenFactory;
5062
$this->paymentExtensionFactory = $paymentExtensionFactory;
5163
$this->config = $config;
64+
$this->logger = $logger;
5265
}
5366

5467
/**
@@ -60,7 +73,8 @@ public function __construct(
6073
*/
6174
public function handle(array $handlingSubject, array $response)
6275
{
63-
if (!isset($handlingSubject['payment'])
76+
if (
77+
!isset($handlingSubject['payment'])
6478
|| !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
6579
) {
6680
throw new \InvalidArgumentException('Payment data object should be provided');
@@ -71,6 +85,12 @@ public function handle(array $handlingSubject, array $response)
7185

7286
$payment = $paymentDO->getPayment();
7387

88+
if ($payment->getAdditionalInformation("is_active_payment_token_enabler") == "") {
89+
return;
90+
}
91+
92+
$log['VaultHandler save card'] = true;
93+
7494
$xExp = "";
7595
if (isset($response[$this::xExp])) {
7696
$xExp = $response[$this::xExp];
@@ -86,6 +106,8 @@ public function handle(array $handlingSubject, array $response)
86106
$extensionAttributes->setVaultPaymentToken($paymentToken);
87107
}
88108
}
109+
110+
$this->logger->debug($log);
89111
}
90112

91113
/**
@@ -97,7 +119,7 @@ public function handle(array $handlingSubject, array $response)
97119
private function getVaultPaymentToken(array $response, string $xExp)
98120
{
99121
// Check token existing in gateway response
100-
if (isset($response[$this::xToken])){
122+
if (isset($response[$this::xToken])) {
101123
$token = $response[$this::xToken];
102124
if (empty($token)) {
103125
return null;
@@ -106,7 +128,6 @@ private function getVaultPaymentToken(array $response, string $xExp)
106128
return null;
107129
}
108130

109-
110131
/** @var PaymentTokenInterface $paymentToken */
111132
$paymentToken = $this->paymentTokenFactory->create();
112133
$paymentToken->setGatewayToken($token);
@@ -128,15 +149,14 @@ private function getExpirationDate(string $xExp)
128149
{
129150
$expDate = new \DateTime(
130151
'20' . substr($xExp, -2)
131-
. '-'
132-
. substr($xExp, 0, 2)
133-
. '-'
134-
. '01'
135-
. ' '
136-
. '00:00:00',
152+
. '-'
153+
. substr($xExp, 0, 2)
154+
. '-'
155+
. '01'
156+
. ' '
157+
. '00:00:00',
137158
new \DateTimeZone('UTC')
138159
);
139-
// $expDate->add(new \DateInterval('P1M'));
140160
return $expDate->format('Y-m-d 00:00:00');
141161
}
142162
/**
@@ -158,7 +178,6 @@ private function convertDetailsToJSON($details)
158178
*/
159179
private function getCreditCardType($type)
160180
{
161-
// $replaced = str_replace(' ', '-', strtolower($type));
162181
$mapper = $this->config->getCctypesMapper();
163182
return $mapper[$type];
164183
}

Observer/DataAssignObserver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class DataAssignObserver extends AbstractDataAssignObserver
3232
self::xCardNum,
3333
self::xCVV,
3434
self::cc_exp_month,
35-
self::cc_exp_year
36-
35+
self::cc_exp_year,
36+
"is_active_payment_token_enabler"
3737
];
3838

3939
public function execute(Observer $observer)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cardknox/cardknox",
33
"description": "Cardknox integration for Magento 2",
44
"type": "magento2-module",
5-
"version": "1.0.7",
5+
"version": "1.0.8",
66
"license": [
77
"GPL-3.0"
88
],

etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@
115115
</arguments>
116116
</type>
117117

118+
<type name="CardknoxDevelopment\Cardknox\Gateway\Response\VaultHandler">
119+
<arguments>
120+
<argument name="logger" xsi:type="object">CardknoxLogger</argument>
121+
</arguments>
122+
</type>
123+
118124
<!-- Commands infrastructure -->
119125
<virtualType name="CardknoxCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
120126
<arguments>

0 commit comments

Comments
 (0)