From e1efa209d55f3fc567515f2716ab71eaf7c4181f Mon Sep 17 00:00:00 2001 From: Cristi Date: Thu, 27 Feb 2020 16:59:52 +0200 Subject: [PATCH] Version 3.6 Async payment methods Adding Async payment methdos. --- Cron/AsyncJob.php | 23 ++++- Model/Payment/Adyen/Mapper.php | 120 ++++++++++++++++++++++++++ Model/Payment/Cybersorurce/Mapper.php | 57 ++++++++++-- Setup/UpgradeSchema.php | 10 +++ composer.json | 2 +- etc/config.xml | 2 +- etc/module.xml | 2 +- 7 files changed, 204 insertions(+), 12 deletions(-) create mode 100644 Model/Payment/Adyen/Mapper.php diff --git a/Cron/AsyncJob.php b/Cron/AsyncJob.php index 7ec25cf4..9c993448 100644 --- a/Cron/AsyncJob.php +++ b/Cron/AsyncJob.php @@ -2,11 +2,13 @@ namespace Signifyd\Connect\Cron; +use Exception; use Magento\Sales\Model\OrderFactory; use Signifyd\Connect\Helper\PurchaseHelper; use Signifyd\Connect\Helper\Retry; use Signifyd\Connect\Logger\Logger; use Signifyd\Connect\Model\Casedata; +use Signifyd\Connect\Model\Payment\Adyen\Mapper as AdyenMapper; use Signifyd\Connect\Model\Payment\Cybersorurce\Mapper; class AsyncJob @@ -35,6 +37,10 @@ class AsyncJob * @var Mapper */ private $cybersource; + /** + * @var AdyenMapper + */ + private $adyen_cc; public function __construct( PurchaseHelper $helper, @@ -42,7 +48,8 @@ public function __construct( Retry $caseRetryObj, Casedata $caseData, OrderFactory $orderFactory, - Mapper $cybersource + Mapper $cybersource, + AdyenMapper $adyen ) { $this->caseRetryObj = $caseRetryObj; $this->logger = $logger; @@ -50,6 +57,7 @@ public function __construct( $this->orderFactory = $orderFactory; $this->caseData = $caseData; $this->cybersource = $cybersource; + $this->adyen_cc = $adyen; } public function execute() @@ -63,8 +71,8 @@ public function execute() $this->logger->debug($message, ['entity' => $case]); $caseObj = $this->caseData->load($case->getOrderIncrement()); $order = $this->getOrder($case['order_increment']); - $data = $this->checkData($order); $retries = (int)$caseObj->getData('retries') + 1; + $data = $this->checkData($order, $retries); if (false !== $data) { $caseData = $this->helper->processOrderData($order); $this->addData($caseData, $data); @@ -118,11 +126,16 @@ public function getAsyncWaitingCases($status) return $casesToRetry; } - public function checkData($order) + public function checkData($order, $retries) { $orderPayment = $order->getPayment(); $paymentMethod = $orderPayment->getMethod(); - $data = $this->{strtolower($paymentMethod)}->getData($order); + try { + $data = $this->{strtolower($paymentMethod)}->getData($order, $retries); + } catch (Exception $ex) { + $this->logger->error($ex->__toString()); + $data = false; + } return $data; } @@ -134,6 +147,8 @@ public function addData($case, $data) $case->card->expiryMonth = $data['cc_exp_month']; $case->card->expiryYear = $data['cc_exp_year']; $case->card->hash = $data['cc_trans_id']; + $case->purchase->avsResponseCode = $data['cc_avs_status']; + $case->purchase->cvvResponseCode = $data['cc_cvv_status']; return true; } diff --git a/Model/Payment/Adyen/Mapper.php b/Model/Payment/Adyen/Mapper.php new file mode 100644 index 00000000..9a6d7e0b --- /dev/null +++ b/Model/Payment/Adyen/Mapper.php @@ -0,0 +1,120 @@ +getPayment()->getAdditionalInformation(); + if (empty($additionalInfo)) { + return false; + } + + if (!is_array($additionalInfo)) { + return false; + } + + if (!array_key_exists('adyen_avs_result', $additionalInfo) || !array_key_exists('adyen_cvc_result', $additionalInfo)) { + if ($retries < 5) { + return false; + } + } + + $expireDate = isset($additionalInfo['adyen_expiry_date']) ? explode("/", $additionalInfo['adyen_expiry_date']) : []; + $data = [ + 'card_type' => isset($additionalInfo['cc_type']) ? $additionalInfo['cc_type'] : $order->getPayment()->getCcType(), + 'cc_trans_id' => isset($additionalInfo['pspReference']) ? $additionalInfo['pspReference'] : $order->getPayment()->getLastTransId(), + 'cc_last_4' => isset($additionalInfo['cardSummary']) ? $additionalInfo['cardSummary'] : $order->getPayment()->getCcLast4(), + 'cc_number' => isset($additionalInfo['adyen_card_bin']) ? $additionalInfo['adyen_card_bin'] : null, + 'cc_avs_status' => isset($additionalInfo['adyen_avs_result']) ? $this->processAvs($additionalInfo['adyen_avs_result']) : $order->getPayment()->getAvsStatus(), + 'cc_cvv_status' => isset($additionalInfo['adyen_cvc_result']) ? $this->processCvc($additionalInfo['adyen_cvc_result']) : null, + 'cc_exp_month' => isset($expireDate[0]) ? $expireDate[0] : null, + 'cc_exp_year' => isset($expireDate[1]) ? $expireDate[1] : null, + ]; + + return $data; + } + + /** + * @param $avs + * @return mixed|null + */ + public function processAvs($avs) + { + $validCodes = [ + -1 => null, + 0 => null, + 1 => "A", + 2 => "N", + 3 => "U", + 4 => "S", + 5 => "U", + 6 => "Z", + 7 => "Y", + 8 => null, + 9 => "A", + 10 => "N", + 11 => null, + 12 => "A", + 13 => "N", + 14 => "Z", + 15 => "Z", + 16 => "N", + 17 => "N", + 18 => "U", + 19 => "Z", + 20 => "Y", + 21 => "A", + 22 => "N", + 23 => "Z", + 24 => "Y", + 25 => "A", + 26 => "N" + ]; + $avsArr = explode(" ", $avs); + return (array_key_exists($avsArr[0], $validCodes)) ? $validCodes[$avsArr[0]] : null; + } + + /** + * @param $cvc + * @return string|null + */ + public function processCvc($cvc) + { + $validCode = null; + $avsArr = explode(" ", $cvc); + switch ($avsArr[0]) { + case '0' : + $validCode = null; + break; + case '1': + $validCode = 'M'; + break; + case '2': + $validCode = 'N'; + break; + case '3': + $validCode = 'P'; + break; + case '4': + $validCode = 'S'; + break; + case '5': + $validCode = 'U'; + break; + case '6': + $validCode = null; + break; + default: + $validCode = null; + break; + } + + return $validCode; + } +} diff --git a/Model/Payment/Cybersorurce/Mapper.php b/Model/Payment/Cybersorurce/Mapper.php index 745a63d5..5e95bb9b 100644 --- a/Model/Payment/Cybersorurce/Mapper.php +++ b/Model/Payment/Cybersorurce/Mapper.php @@ -8,7 +8,7 @@ class Mapper * @param $order * @return array|bool */ - public function getData($order) + public function getData($order, $retries) { $additionalInfo = $order->getPayment()->getAdditionalInformation(); if (empty($additionalInfo)) { @@ -19,14 +19,20 @@ public function getData($order) return false; } + if (!array_key_exists('auth_avs_code', $additionalInfo) || !array_key_exists('auth_avs_code', $additionalInfo)) { + if ($retries < 5) { + return false; + } + } + $expireDate = isset($additionalInfo['card_expiry_date']) ? explode("-", $additionalInfo['card_expiry_date']) : []; $data = [ 'card_type' => $this->getCybersourceCardTypeByCode($additionalInfo['card_type']), - 'cc_trans_id' => isset($additionalInfo['transaction_id']) ? $additionalInfo['transaction_id'] : null, - 'cc_last_4' => isset($additionalInfo['card_number']) ? $additionalInfo['card_number'] : null, + 'cc_trans_id' => isset($additionalInfo['transaction_id']) ? $additionalInfo['transaction_id'] : $order->getPayment()->getLastTransId(), + 'cc_last_4' => isset($additionalInfo['card_number']) ? $additionalInfo['card_number'] : $order->getPayment()->getCcLast4(), 'cc_number' => isset($additionalInfo['card_bin']) ? $additionalInfo['card_bin'] : null, - 'cc_avs_status' => isset($additionalInfo['auth_avs_code']) ? $additionalInfo['auth_avs_code'] : null, - 'cc_cid_status' => isset($additionalInfo['auth_cv_result']) ? $additionalInfo['auth_cv_result'] : null, + 'cc_avs_status' => isset($additionalInfo['auth_avs_code']) ? $this->processAvs($additionalInfo['auth_avs_code']) : $order->getPayment()->getAvsStatus(), + 'cc_cvv_status' => isset($additionalInfo['auth_cv_result']) ? $this->processCvc($additionalInfo['auth_cv_result']) : null, 'cc_exp_month' => isset($expireDate[0]) ? $expireDate[0] : null, 'cc_exp_year' => isset($expireDate[1]) ? $expireDate[1] : null, ]; @@ -34,6 +40,10 @@ public function getData($order) return $data; } + /** + * @param $code + * @return mixed|string + */ public function getCybersourceCardTypeByCode($code) { $cardTypes = [ @@ -49,4 +59,41 @@ public function getCybersourceCardTypeByCode($code) return $cardTypes[$code]; } } + + /** + * @param $avs + * @return mixed|null + */ + public function processAvs($avs) + { + $validCodes = [ + "F" => "Z", + "H" => "Y", + "T" => "A", + "1" => "S", + "2" => "E", + "K" => "N", + "L" => "Z", + "O" => "A" + ]; + + return (array_key_exists($avs, $validCodes)) ? $validCodes[$avs] : $avs; + } + + /** + * @param $cvc + * @return string|null + */ + public function processCvc($cvc) + { + $validCodes = [ + "D" => "U", + "I" => "N", + "X" => "U", + "1" => "U", + "2" => "N", + "3" => "P" + ]; + return (array_key_exists($cvc, $validCodes)) ? $validCodes[$cvc] : $cvc; + } } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 5c97197e..f8d00890 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -233,6 +233,16 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con } } + if (version_compare($context->getVersion(), '3.6.0') < 0) { + $data = [ + 'scope' => 'default', + 'scope_id' => 0, + 'path' => 'signifyd/general/async_payment_methods', + 'value' => 'cybersource,adyen_cc', + ]; + $setup->getConnection()->insertOnDuplicate($setup->getTable('core_config_data'), $data, ['value']); + } + $setup->endSetup(); } } diff --git a/composer.json b/composer.json index 04819591..22bc7f0f 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "php": ">=5.5.22" }, "type": "magento2-module", - "version": "3.5.2", + "version": "3.6.0", "autoload": { "files": [ "registration.php" diff --git a/etc/config.xml b/etc/config.xml index 5a547a93..95344cb9 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -14,7 +14,7 @@ pending_payment,payment_review,canceled,closed,complete holded,pending_payment,payment_review,canceled,closed,complete checkmo,cashondelivery,banktransfer,purchaseorder - cybersource + cybersource,adyen_cc nothing diff --git a/etc/module.xml b/etc/module.xml index 3772c4b1..3e9eac3c 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -5,7 +5,7 @@ */ --> - +