Skip to content

Commit

Permalink
Merge branch '4.3.5' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ebanolopes committed Mar 15, 2022
2 parents eb891e6 + 3b3827d commit fde93ef
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 77 deletions.
76 changes: 67 additions & 9 deletions Helper/PurchaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,18 @@ public function getProductImage($product)
*
* The decision request.
*/
public function getDecisionRequest()
public function getDecisionRequest($paymentMethod = null)
{
$decisionRequest = [];

if ($this->configHelper->isScoreOnly()) {
$decisionRequest['paymentFraud'] = 'SCORE';
} else {
$configDecision = $this->configHelper->getDecisionRequest();
$allowedDecisions = ['GUARANTEE', 'SCORE', 'DECISION'];
$decisionRequest['paymentFraud'] = in_array($configDecision, $allowedDecisions) ?
$configDecision : 'GUARANTEE';
return $decisionRequest;
}

$configDecision = $this->configHelper->getDecisionRequest();
$decisionRequest['paymentFraud'] = $this->getDecisionForMethod($configDecision, $paymentMethod);

return $decisionRequest;
}

Expand Down Expand Up @@ -794,6 +793,26 @@ public function makeShipments(Order $order)
return $shipments;
}

/**
* @param Quote $quote
* @return array
*/
public function makeShipmentsFromQuote(Quote $quote)
{
$shipments = [];
$shipment = [];
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
$shippingAmount = $quote->getShippingAddress()->getShippingAmount();

$shipment['shippingMethod'] = $shippingMethod ? $this->makeshippingMethod($shippingMethod) : null;
$shipment['shippingPrice'] = is_numeric($shippingAmount) ? floatval($shippingAmount) : null;
$shipment['shipper'] = $shippingMethod ? $this->makeShipper($shippingMethod) : null;

$shipments[] = $shipment;

return $shipments;
}

public function makeShipper($shippingMethod)
{
if (is_string($shippingMethod)) {
Expand Down Expand Up @@ -1131,7 +1150,7 @@ public function processOrderData($order)
$case['customerOrderRecommendation'] = $this->getCustomerOrderRecommendation();
$case['deviceFingerprints'] = $this->getDeviceFingerprints();
$case['policy'] = $this->makePolicy($order, ScopeInterface::SCOPE_STORES, $order->getStoreId());
$case['decisionRequest'] = $this->getDecisionRequest();
$case['decisionRequest'] = $this->getDecisionRequest($order->getPayment()->getMethod());
$case['sellers'] = $this->getSellers();
$case['tags'] = $this->getTags();
$case['purchase']['checkoutToken'] = uniqid();
Expand Down Expand Up @@ -1532,8 +1551,8 @@ public function processQuoteData(Quote $quote, $checkoutPaymentDetails = null, $
$case['customerOrderRecommendation'] = $this->getCustomerOrderRecommendation();
$case['deviceFingerprints'] = $this->getDeviceFingerprints();
$case['policy'] = $this->makePolicy($quote, ScopeInterface::SCOPE_STORES, $quote->getStoreId());
$case['decisionRequest'] = $this->getDecisionRequest();
$case['purchase']['checkoutToken'] = uniqid();
$case['decisionRequest'] = $this->getDecisionRequest($paymentMethod);
$case['purchase']['checkoutToken'] = sha1($this->jsonSerializer->serialize($case));

$positiveAction = $this->configHelper->getConfigData('signifyd/advanced/guarantee_positive_action');
$transactionType = $positiveAction == 'capture' ? 'SALE' : 'AUTHORIZATION';
Expand Down Expand Up @@ -1630,6 +1649,7 @@ public function makePurchaseFromQuote(Quote $quote)
}
}

$purchase['shipments'] = $this->makeShipmentsFromQuote($quote);
$purchase['orderChannel'] = "WEB";
$purchase['totalPrice'] = $quote->getGrandTotal();
$purchase['currency'] = $quote->getQuoteCurrencyCode();
Expand Down Expand Up @@ -1865,6 +1885,44 @@ public function getIsPreAuth($policyName, $paymentMethod)
}
}

public function getDecisionForMethod($decision, $paymentMethod)
{
$isJson = $this->isJson($decision);

if ($isJson) {
if (isset($paymentMethod) === false) {
return "GUARANTEE";
}

$configDecisions = $this->jsonSerializer->unserialize($decision);

foreach ($configDecisions as $configDecision => $method) {
if ($this->isDecisionValid($configDecision) === false) {
continue;
}

if (in_array($paymentMethod, $method)) {
return $configDecision;
}
}

return "GUARANTEE";
} else {
if ($this->isDecisionValid($decision) === false) {
return "GUARANTEE";
}

return $decision;
}
}

public function isDecisionValid($decision)
{
$allowedDecisions = ['GUARANTEE', 'SCORE', 'DECISION'];

return in_array($decision, $allowedDecisions);
}

public function isJson($string)
{
json_decode($string);
Expand Down
56 changes: 33 additions & 23 deletions Model/Casedata.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,7 @@ public function updateOrder()
$order->setCustomerNoteNotify(true);
$order->setIsInProcess(true);

if ($enableTransaction) {
$this->orderResourceModel->save($order);
$this->invoiceResourceModel->save($invoice);
} else {
/** @var \Magento\Framework\DB\Transaction $transactionSave */
$transactionSave = $this->transactionFactory->create();
$transactionSave->addObject(
$invoice
)->addObject(
$invoice->getOrder()
);

$transactionSave->save();
}
$this->handleTransaction($enableTransaction, $order, $invoice);

$this->orderHelper->addCommentToStatusHistory(
$order,
Expand Down Expand Up @@ -587,15 +574,7 @@ public function updateOrder()
$invoices = $order->getInvoiceCollection();

if ($invoices->getTotalCount() > 0) {
foreach ($invoices as $invoice) {
$creditmemo = $this->creditmemoFactory->createByOrder($order);
$creditmemo->setInvoice($invoice);
$this->creditmemoService->refund($creditmemo);
$this->logger->debug(
'Credit memo was created for order: ' . $order->getIncrementId(),
['entity' => $order]
);
}
$this->createInvoicesCreditMemo($invoices, $order);
} else {
$this->holdOrder($order);
$message = "Signifyd: tried to refund, but there is no invoice to add credit memo";
Expand Down Expand Up @@ -867,4 +846,35 @@ public function holdOrder($order)
$this->orderResourceModel->save($order);
}
}

public function handleTransaction($enableTransaction, $order, $invoice)
{
if ($enableTransaction) {
$this->orderResourceModel->save($order);
$this->invoiceResourceModel->save($invoice);
} else {
/** @var \Magento\Framework\DB\Transaction $transactionSave */
$transactionSave = $this->transactionFactory->create();
$transactionSave->addObject(
$invoice
)->addObject(
$invoice->getOrder()
);

$transactionSave->save();
}
}

public function createInvoicesCreditMemo($invoices, $order)
{
foreach ($invoices as $invoice) {
$creditmemo = $this->creditmemoFactory->createByOrder($order);
$creditmemo->setInvoice($invoice);
$this->creditmemoService->refund($creditmemo);
$this->logger->debug(
'Credit memo was created for order: ' . $order->getIncrementId(),
['entity' => $order]
);
}
}
}
62 changes: 58 additions & 4 deletions Observer/PreAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Signifyd\Connect\Model\CasedataFactory;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magento\Framework\ObjectManagerInterface;

class PreAuth implements ObserverInterface
{
Expand Down Expand Up @@ -88,6 +89,11 @@ class PreAuth implements ObserverInterface
*/
protected $configHelper;

/**
* @var ObjectManagerInterface
*/
protected $objectManagerInterface;

/**
* PreAuth constructor.
* @param Logger $logger
Expand All @@ -103,6 +109,7 @@ class PreAuth implements ObserverInterface
* @param RequestHttp $requestHttp
* @param JsonSerializer $jsonSerializer
* @param ConfigHelper $configHelper
* @param ObjectManagerInterface $objectManagerInterface
*/
public function __construct(
Logger $logger,
Expand All @@ -117,7 +124,8 @@ public function __construct(
CasedataResourceModel $casedataResourceModel,
RequestHttp $requestHttp,
JsonSerializer $jsonSerializer,
ConfigHelper $configHelper
ConfigHelper $configHelper,
ObjectManagerInterface $objectManagerInterface
) {
$this->logger = $logger;
$this->purchaseHelper = $purchaseHelper;
Expand All @@ -132,6 +140,7 @@ public function __construct(
$this->requestHttp = $requestHttp;
$this->jsonSerializer = $jsonSerializer;
$this->configHelper = $configHelper;
$this->objectManagerInterface = $objectManagerInterface;
}

public function execute(Observer $observer)
Expand All @@ -153,7 +162,12 @@ public function execute(Observer $observer)

$paymentMethod = null;
$data = $this->requestHttp->getContent();
$dataArray = $this->jsonSerializer->unserialize($data);

if (empty($data) === false) {
$dataArray = $this->jsonSerializer->unserialize($data);
} else {
$dataArray = [];
}

if (isset($dataArray['paymentMethod']) &&
isset($dataArray['paymentMethod']['method'])
Expand Down Expand Up @@ -185,9 +199,49 @@ public function execute(Observer $observer)

if (isset($dataArray['paymentMethod']) &&
isset($dataArray['paymentMethod']['additional_data'])
) {
if ($paymentMethod == 'adyen_oneclick' &&
isset($dataArray['paymentMethod']['additional_data']['stateData'])
) {
$checkoutPaymentDetails['cardBin'] =
$dataArray['paymentMethod']['additional_data']['cardBin'] ?? null;
try {
$stateData = $this->jsonSerializer
->unserialize($dataArray['paymentMethod']['additional_data']['stateData']);

/** @var \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest */
$paymentRequest = $this->objectManagerInterface->create(
\Adyen\Payment\Model\Api\PaymentRequest::class
);

if ($quote->getCustomer()->getId() < 10) {
$shopperReference = '00' . $quote->getCustomer()->getId();
} elseif ($quote->getCustomer()->getId() >= 10 && $quote->getCustomer()->getId() <= 100) {
$shopperReference = '0' . $quote->getCustomer()->getId();
} else {
$shopperReference = $quote->getCustomer()->getId();
}

$contracts = $paymentRequest->getRecurringContractsForShopper(
$shopperReference,
$quote->getStoreId()
);

if (isset($stateData['paymentMethod']) &&
isset($stateData['paymentMethod']['storedPaymentMethodId'])
) {
$storedPaymentMethodId = $stateData['paymentMethod']['storedPaymentMethodId'];

$checkoutPaymentDetails['cardBin'] =
$contracts[$storedPaymentMethodId]['additionalData']['cardBin'] ?? null;
} else {
$checkoutPaymentDetails['cardBin'] = null;
}
} catch (\Exception $e) {
$checkoutPaymentDetails['cardBin'] = null;
}
} else {
$checkoutPaymentDetails['cardBin'] =
$dataArray['paymentMethod']['additional_data']['cardBin'] ?? null;
}

$checkoutPaymentDetails['holderName'] =
$dataArray['paymentMethod']['additional_data']['holderName'] ?? null;
Expand Down
6 changes: 3 additions & 3 deletions Observer/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,6 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)
$checkoutToken = $orderData['purchase']['checkoutToken'];
$caseResponse = $this->purchaseHelper->postCaseToSignifyd($orderData, $order);

// Initial hold order
$this->holdOrder($order, $case, $isPassive);

if (is_object($caseResponse)) {
$case->setCode($caseResponse->getCaseId());
$case->setMagentoStatus(Casedata::IN_REVIEW_STATUS);
Expand All @@ -323,6 +320,9 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)

$this->casedataResourceModel->save($case);

// Initial hold order
$this->holdOrder($order, $case, $isPassive);

if ($isPassive === false) {
$this->orderResourceModel->save($order);
}
Expand Down
Loading

0 comments on commit fde93ef

Please sign in to comment.