-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from signifyd/stage
Stage
- Loading branch information
Showing
10 changed files
with
420 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
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 | ||
{ | ||
/** | ||
* @var Retry | ||
*/ | ||
public $caseRetryObj; | ||
/** | ||
* @var Logger | ||
*/ | ||
private $logger; | ||
/** | ||
* @var PurchaseHelper | ||
*/ | ||
private $helper; | ||
/** | ||
* @var OrderFactory | ||
*/ | ||
private $orderFactory; | ||
/** | ||
* @var Casedata | ||
*/ | ||
private $caseData; | ||
/** | ||
* @var Mapper | ||
*/ | ||
private $cybersource; | ||
/** | ||
* @var AdyenMapper | ||
*/ | ||
private $adyen_cc; | ||
|
||
public function __construct( | ||
PurchaseHelper $helper, | ||
Logger $logger, | ||
Retry $caseRetryObj, | ||
Casedata $caseData, | ||
OrderFactory $orderFactory, | ||
Mapper $cybersource, | ||
AdyenMapper $adyen | ||
) { | ||
$this->caseRetryObj = $caseRetryObj; | ||
$this->logger = $logger; | ||
$this->helper = $helper; | ||
$this->orderFactory = $orderFactory; | ||
$this->caseData = $caseData; | ||
$this->cybersource = $cybersource; | ||
$this->adyen_cc = $adyen; | ||
} | ||
|
||
public function execute() | ||
{ | ||
/** | ||
* Getting all the cases that were not submitted to Signifyd | ||
*/ | ||
$waitingCases = $this->getAsyncWaitingCases(Casedata::ASYNC_WAIT); | ||
foreach ($waitingCases as $case) { | ||
$message = "Signifyd: preparing for send case no: {$case['order_increment']}"; | ||
$this->logger->debug($message, ['entity' => $case]); | ||
$caseObj = $this->caseData->load($case->getOrderIncrement()); | ||
$order = $this->getOrder($case['order_increment']); | ||
$retries = (int)$caseObj->getData('retries') + 1; | ||
$data = $this->checkData($order, $retries); | ||
if (false !== $data) { | ||
$caseData = $this->helper->processOrderData($order); | ||
$this->addData($caseData, $data); | ||
$result = $this->helper->postCaseToSignifyd($caseData, $order); | ||
$caseObj->setMagentoStatus(Casedata::IN_REVIEW_STATUS); | ||
$retries = 0; | ||
} else { | ||
if ($case->getRetries() == 5) { | ||
$caseData = $this->helper->processOrderData($order); | ||
$result = $this->helper->postCaseToSignifyd($caseData, $order); | ||
$caseObj->setMagentoStatus(Casedata::IN_REVIEW_STATUS); | ||
$retries = 0; | ||
} else { | ||
$this->logger->debug("Case not sent because no data yet"); | ||
} | ||
} | ||
|
||
if (isset($result) && $result) { | ||
$caseObj->setCode($result); | ||
} | ||
|
||
$caseObj->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time())); | ||
$caseObj->setRetries($retries); | ||
$caseObj->save(); | ||
} | ||
} | ||
|
||
/** | ||
* @param $incrementId | ||
* @return \Magento\Sales\Model\Order | ||
*/ | ||
public function getOrder($incrementId) | ||
{ | ||
return $this->orderFactory->create()->loadByIncrementId($incrementId); | ||
} | ||
|
||
/** | ||
* @param $status | ||
* @return array | ||
*/ | ||
public function getAsyncWaitingCases($status) | ||
{ | ||
$casesCollection = $this->caseData->getCollection(); | ||
$casesCollection->addFieldToFilter('magento_status', ['eq' => $status]); | ||
$casesCollection->addFieldToFilter('retries', ['lt' => 6]); | ||
$casesToRetry = []; | ||
foreach ($casesCollection->getItems() as $case) { | ||
$casesToRetry[$case->getId()] = $case; | ||
} | ||
|
||
return $casesToRetry; | ||
} | ||
|
||
public function checkData($order, $retries) | ||
{ | ||
$orderPayment = $order->getPayment(); | ||
$paymentMethod = $orderPayment->getMethod(); | ||
try { | ||
$data = $this->{strtolower($paymentMethod)}->getData($order, $retries); | ||
} catch (Exception $ex) { | ||
$this->logger->error($ex->__toString()); | ||
$data = false; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
public function addData($case, $data) | ||
{ | ||
$case->card->bin = $data['cc_number']; | ||
$case->card->last4 = $data['cc_last_4']; | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Signifyd\Connect\Model\Payment\Adyen; | ||
|
||
class Mapper | ||
{ | ||
/** | ||
* @param $order | ||
* @return array|bool | ||
*/ | ||
public function getData($order, $retries) | ||
{ | ||
$additionalInfo = $order->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Signifyd\Connect\Model\Payment\Cybersorurce; | ||
|
||
class Mapper | ||
{ | ||
/** | ||
* @param $order | ||
* @return array|bool | ||
*/ | ||
public function getData($order, $retries) | ||
{ | ||
$additionalInfo = $order->getPayment()->getAdditionalInformation(); | ||
if (empty($additionalInfo)) { | ||
return false; | ||
} | ||
|
||
if (!is_array($additionalInfo)) { | ||
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'] : $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']) ? $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, | ||
]; | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param $code | ||
* @return mixed|string | ||
*/ | ||
public function getCybersourceCardTypeByCode($code) | ||
{ | ||
$cardTypes = [ | ||
'001' => 'Visa', | ||
'002' => 'Master Card', | ||
'003' => 'American Express', | ||
'004' => 'Discover', | ||
]; | ||
|
||
if (!isset($cardTypes[$code])) { | ||
return '***Unknown***'; | ||
} else { | ||
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; | ||
} | ||
} |
Oops, something went wrong.