Skip to content

Commit

Permalink
don't run code to add token if save card is unchecked or vault is dis…
Browse files Browse the repository at this point in the history
…abled to address magento/magento2#5902, add logging to VaultHandler, version 1.0.8
  • Loading branch information
dlehren committed Jun 27, 2019
1 parent cfc0de7 commit 6a47644
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
1 change: 0 additions & 1 deletion Gateway/Request/AuthorizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function build(array $buildSubject)
'xExp' => sprintf('%02d%02d', $payment->getAdditionalInformation("cc_exp_month"), substr($payment->getAdditionalInformation("cc_exp_year"), -2)),
'xCVV' => $payment->getAdditionalInformation("xCVV"),
'xCommand' => 'cc:authonly',
// 'xToken' => $payment->getAdditionalInformation("xToken"),
'xInvoice' => $order->getOrderIncrementId(),
'xCurrency' => $order->getCurrencyCode(),
'xCardNum' => $payment->getAdditionalInformation("xCardNum"),
Expand Down
3 changes: 1 addition & 2 deletions Gateway/Request/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ public function build(array $buildSubject)

$order = $paymentDO->getOrder();

// $this->productMetadata->getEdition();
return [
'xVersion' => '4.5.8',
'xSoftwareName' => 'Magento ' . $this->productMetadata->getEdition() . " ". $this->productMetadata->getVersion(),
'xSoftwareVersion' => '1.0.7',
'xSoftwareVersion' => '1.0.8',
'xKey' => $this->config->getValue(
'cardknox_transaction_key',
$order->getStoreId()
Expand Down
45 changes: 32 additions & 13 deletions Gateway/Response/VaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//use Magento\Vault\Model\CreditCardTokenFactory;
use Magento\Payment\Model\InfoInterface;
use CardknoxDevelopment\Cardknox\Gateway\Config\Config;
use Magento\Payment\Model\Method\Logger;

class VaultHandler implements HandlerInterface
{
Expand All @@ -26,7 +27,7 @@ class VaultHandler implements HandlerInterface
/**
* @var CreditCardTokenFactory
*/
// protected $creditCardTokenFactory;
// protected $creditCardTokenFactory;

protected $paymentTokenFactory;
/**
Expand All @@ -35,6 +36,16 @@ class VaultHandler implements HandlerInterface
protected $paymentExtensionFactory;

protected $config;

/**
* @var Logger
*/
private $logger;

/**
* @param Logger $logger
*/

/**
* Constructor
*
Expand All @@ -44,11 +55,13 @@ class VaultHandler implements HandlerInterface
public function __construct(
PaymentTokenInterfaceFactory $paymentTokenFactory,
OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
Config $config
Config $config,
Logger $logger
) {
$this->paymentTokenFactory = $paymentTokenFactory;
$this->paymentExtensionFactory = $paymentExtensionFactory;
$this->config = $config;
$this->logger = $logger;
}

/**
Expand All @@ -60,7 +73,8 @@ public function __construct(
*/
public function handle(array $handlingSubject, array $response)
{
if (!isset($handlingSubject['payment'])
if (
!isset($handlingSubject['payment'])
|| !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
) {
throw new \InvalidArgumentException('Payment data object should be provided');
Expand All @@ -71,6 +85,12 @@ public function handle(array $handlingSubject, array $response)

$payment = $paymentDO->getPayment();

if ($payment->getAdditionalInformation("is_active_payment_token_enabler") == "") {
return;
}

$log['VaultHandler save card'] = true;

$xExp = "";
if (isset($response[$this::xExp])) {
$xExp = $response[$this::xExp];
Expand All @@ -86,6 +106,8 @@ public function handle(array $handlingSubject, array $response)
$extensionAttributes->setVaultPaymentToken($paymentToken);
}
}

$this->logger->debug($log);
}

/**
Expand All @@ -97,7 +119,7 @@ public function handle(array $handlingSubject, array $response)
private function getVaultPaymentToken(array $response, string $xExp)
{
// Check token existing in gateway response
if (isset($response[$this::xToken])){
if (isset($response[$this::xToken])) {
$token = $response[$this::xToken];
if (empty($token)) {
return null;
Expand All @@ -106,7 +128,6 @@ private function getVaultPaymentToken(array $response, string $xExp)
return null;
}


/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->paymentTokenFactory->create();
$paymentToken->setGatewayToken($token);
Expand All @@ -128,15 +149,14 @@ private function getExpirationDate(string $xExp)
{
$expDate = new \DateTime(
'20' . substr($xExp, -2)
. '-'
. substr($xExp, 0, 2)
. '-'
. '01'
. ' '
. '00:00:00',
. '-'
. substr($xExp, 0, 2)
. '-'
. '01'
. ' '
. '00:00:00',
new \DateTimeZone('UTC')
);
// $expDate->add(new \DateInterval('P1M'));
return $expDate->format('Y-m-d 00:00:00');
}
/**
Expand All @@ -158,7 +178,6 @@ private function convertDetailsToJSON($details)
*/
private function getCreditCardType($type)
{
// $replaced = str_replace(' ', '-', strtolower($type));
$mapper = $this->config->getCctypesMapper();
return $mapper[$type];
}
Expand Down
4 changes: 2 additions & 2 deletions Observer/DataAssignObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class DataAssignObserver extends AbstractDataAssignObserver
self::xCardNum,
self::xCVV,
self::cc_exp_month,
self::cc_exp_year

self::cc_exp_year,
"is_active_payment_token_enabler"
];

public function execute(Observer $observer)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cardknox/cardknox",
"description": "Cardknox integration for Magento 2",
"type": "magento2-module",
"version": "1.0.7",
"version": "1.0.8",
"license": [
"GPL-3.0"
],
Expand Down
6 changes: 6 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
</arguments>
</type>

<type name="CardknoxDevelopment\Cardknox\Gateway\Response\VaultHandler">
<arguments>
<argument name="logger" xsi:type="object">CardknoxLogger</argument>
</arguments>
</type>

<!-- Commands infrastructure -->
<virtualType name="CardknoxCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
<arguments>
Expand Down

0 comments on commit 6a47644

Please sign in to comment.