Skip to content

Commit fde93ef

Browse files
committed
Merge branch '4.3.5' into main
2 parents eb891e6 + 3b3827d commit fde93ef

File tree

18 files changed

+296
-77
lines changed

18 files changed

+296
-77
lines changed

Helper/PurchaseHelper.php

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,18 @@ public function getProductImage($product)
409409
*
410410
* The decision request.
411411
*/
412-
public function getDecisionRequest()
412+
public function getDecisionRequest($paymentMethod = null)
413413
{
414414
$decisionRequest = [];
415415

416416
if ($this->configHelper->isScoreOnly()) {
417417
$decisionRequest['paymentFraud'] = 'SCORE';
418-
} else {
419-
$configDecision = $this->configHelper->getDecisionRequest();
420-
$allowedDecisions = ['GUARANTEE', 'SCORE', 'DECISION'];
421-
$decisionRequest['paymentFraud'] = in_array($configDecision, $allowedDecisions) ?
422-
$configDecision : 'GUARANTEE';
418+
return $decisionRequest;
423419
}
424420

421+
$configDecision = $this->configHelper->getDecisionRequest();
422+
$decisionRequest['paymentFraud'] = $this->getDecisionForMethod($configDecision, $paymentMethod);
423+
425424
return $decisionRequest;
426425
}
427426

@@ -794,6 +793,26 @@ public function makeShipments(Order $order)
794793
return $shipments;
795794
}
796795

796+
/**
797+
* @param Quote $quote
798+
* @return array
799+
*/
800+
public function makeShipmentsFromQuote(Quote $quote)
801+
{
802+
$shipments = [];
803+
$shipment = [];
804+
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
805+
$shippingAmount = $quote->getShippingAddress()->getShippingAmount();
806+
807+
$shipment['shippingMethod'] = $shippingMethod ? $this->makeshippingMethod($shippingMethod) : null;
808+
$shipment['shippingPrice'] = is_numeric($shippingAmount) ? floatval($shippingAmount) : null;
809+
$shipment['shipper'] = $shippingMethod ? $this->makeShipper($shippingMethod) : null;
810+
811+
$shipments[] = $shipment;
812+
813+
return $shipments;
814+
}
815+
797816
public function makeShipper($shippingMethod)
798817
{
799818
if (is_string($shippingMethod)) {
@@ -1131,7 +1150,7 @@ public function processOrderData($order)
11311150
$case['customerOrderRecommendation'] = $this->getCustomerOrderRecommendation();
11321151
$case['deviceFingerprints'] = $this->getDeviceFingerprints();
11331152
$case['policy'] = $this->makePolicy($order, ScopeInterface::SCOPE_STORES, $order->getStoreId());
1134-
$case['decisionRequest'] = $this->getDecisionRequest();
1153+
$case['decisionRequest'] = $this->getDecisionRequest($order->getPayment()->getMethod());
11351154
$case['sellers'] = $this->getSellers();
11361155
$case['tags'] = $this->getTags();
11371156
$case['purchase']['checkoutToken'] = uniqid();
@@ -1532,8 +1551,8 @@ public function processQuoteData(Quote $quote, $checkoutPaymentDetails = null, $
15321551
$case['customerOrderRecommendation'] = $this->getCustomerOrderRecommendation();
15331552
$case['deviceFingerprints'] = $this->getDeviceFingerprints();
15341553
$case['policy'] = $this->makePolicy($quote, ScopeInterface::SCOPE_STORES, $quote->getStoreId());
1535-
$case['decisionRequest'] = $this->getDecisionRequest();
1536-
$case['purchase']['checkoutToken'] = uniqid();
1554+
$case['decisionRequest'] = $this->getDecisionRequest($paymentMethod);
1555+
$case['purchase']['checkoutToken'] = sha1($this->jsonSerializer->serialize($case));
15371556

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

1652+
$purchase['shipments'] = $this->makeShipmentsFromQuote($quote);
16331653
$purchase['orderChannel'] = "WEB";
16341654
$purchase['totalPrice'] = $quote->getGrandTotal();
16351655
$purchase['currency'] = $quote->getQuoteCurrencyCode();
@@ -1865,6 +1885,44 @@ public function getIsPreAuth($policyName, $paymentMethod)
18651885
}
18661886
}
18671887

1888+
public function getDecisionForMethod($decision, $paymentMethod)
1889+
{
1890+
$isJson = $this->isJson($decision);
1891+
1892+
if ($isJson) {
1893+
if (isset($paymentMethod) === false) {
1894+
return "GUARANTEE";
1895+
}
1896+
1897+
$configDecisions = $this->jsonSerializer->unserialize($decision);
1898+
1899+
foreach ($configDecisions as $configDecision => $method) {
1900+
if ($this->isDecisionValid($configDecision) === false) {
1901+
continue;
1902+
}
1903+
1904+
if (in_array($paymentMethod, $method)) {
1905+
return $configDecision;
1906+
}
1907+
}
1908+
1909+
return "GUARANTEE";
1910+
} else {
1911+
if ($this->isDecisionValid($decision) === false) {
1912+
return "GUARANTEE";
1913+
}
1914+
1915+
return $decision;
1916+
}
1917+
}
1918+
1919+
public function isDecisionValid($decision)
1920+
{
1921+
$allowedDecisions = ['GUARANTEE', 'SCORE', 'DECISION'];
1922+
1923+
return in_array($decision, $allowedDecisions);
1924+
}
1925+
18681926
public function isJson($string)
18691927
{
18701928
json_decode($string);

Model/Casedata.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -512,20 +512,7 @@ public function updateOrder()
512512
$order->setCustomerNoteNotify(true);
513513
$order->setIsInProcess(true);
514514

515-
if ($enableTransaction) {
516-
$this->orderResourceModel->save($order);
517-
$this->invoiceResourceModel->save($invoice);
518-
} else {
519-
/** @var \Magento\Framework\DB\Transaction $transactionSave */
520-
$transactionSave = $this->transactionFactory->create();
521-
$transactionSave->addObject(
522-
$invoice
523-
)->addObject(
524-
$invoice->getOrder()
525-
);
526-
527-
$transactionSave->save();
528-
}
515+
$this->handleTransaction($enableTransaction, $order, $invoice);
529516

530517
$this->orderHelper->addCommentToStatusHistory(
531518
$order,
@@ -587,15 +574,7 @@ public function updateOrder()
587574
$invoices = $order->getInvoiceCollection();
588575

589576
if ($invoices->getTotalCount() > 0) {
590-
foreach ($invoices as $invoice) {
591-
$creditmemo = $this->creditmemoFactory->createByOrder($order);
592-
$creditmemo->setInvoice($invoice);
593-
$this->creditmemoService->refund($creditmemo);
594-
$this->logger->debug(
595-
'Credit memo was created for order: ' . $order->getIncrementId(),
596-
['entity' => $order]
597-
);
598-
}
577+
$this->createInvoicesCreditMemo($invoices, $order);
599578
} else {
600579
$this->holdOrder($order);
601580
$message = "Signifyd: tried to refund, but there is no invoice to add credit memo";
@@ -867,4 +846,35 @@ public function holdOrder($order)
867846
$this->orderResourceModel->save($order);
868847
}
869848
}
849+
850+
public function handleTransaction($enableTransaction, $order, $invoice)
851+
{
852+
if ($enableTransaction) {
853+
$this->orderResourceModel->save($order);
854+
$this->invoiceResourceModel->save($invoice);
855+
} else {
856+
/** @var \Magento\Framework\DB\Transaction $transactionSave */
857+
$transactionSave = $this->transactionFactory->create();
858+
$transactionSave->addObject(
859+
$invoice
860+
)->addObject(
861+
$invoice->getOrder()
862+
);
863+
864+
$transactionSave->save();
865+
}
866+
}
867+
868+
public function createInvoicesCreditMemo($invoices, $order)
869+
{
870+
foreach ($invoices as $invoice) {
871+
$creditmemo = $this->creditmemoFactory->createByOrder($order);
872+
$creditmemo->setInvoice($invoice);
873+
$this->creditmemoService->refund($creditmemo);
874+
$this->logger->debug(
875+
'Credit memo was created for order: ' . $order->getIncrementId(),
876+
['entity' => $order]
877+
);
878+
}
879+
}
870880
}

Observer/PreAuth.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Signifyd\Connect\Model\CasedataFactory;
2121
use Magento\Framework\App\Request\Http as RequestHttp;
2222
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
23+
use Magento\Framework\ObjectManagerInterface;
2324

2425
class PreAuth implements ObserverInterface
2526
{
@@ -88,6 +89,11 @@ class PreAuth implements ObserverInterface
8889
*/
8990
protected $configHelper;
9091

92+
/**
93+
* @var ObjectManagerInterface
94+
*/
95+
protected $objectManagerInterface;
96+
9197
/**
9298
* PreAuth constructor.
9399
* @param Logger $logger
@@ -103,6 +109,7 @@ class PreAuth implements ObserverInterface
103109
* @param RequestHttp $requestHttp
104110
* @param JsonSerializer $jsonSerializer
105111
* @param ConfigHelper $configHelper
112+
* @param ObjectManagerInterface $objectManagerInterface
106113
*/
107114
public function __construct(
108115
Logger $logger,
@@ -117,7 +124,8 @@ public function __construct(
117124
CasedataResourceModel $casedataResourceModel,
118125
RequestHttp $requestHttp,
119126
JsonSerializer $jsonSerializer,
120-
ConfigHelper $configHelper
127+
ConfigHelper $configHelper,
128+
ObjectManagerInterface $objectManagerInterface
121129
) {
122130
$this->logger = $logger;
123131
$this->purchaseHelper = $purchaseHelper;
@@ -132,6 +140,7 @@ public function __construct(
132140
$this->requestHttp = $requestHttp;
133141
$this->jsonSerializer = $jsonSerializer;
134142
$this->configHelper = $configHelper;
143+
$this->objectManagerInterface = $objectManagerInterface;
135144
}
136145

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

154163
$paymentMethod = null;
155164
$data = $this->requestHttp->getContent();
156-
$dataArray = $this->jsonSerializer->unserialize($data);
165+
166+
if (empty($data) === false) {
167+
$dataArray = $this->jsonSerializer->unserialize($data);
168+
} else {
169+
$dataArray = [];
170+
}
157171

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

186200
if (isset($dataArray['paymentMethod']) &&
187201
isset($dataArray['paymentMethod']['additional_data'])
202+
) {
203+
if ($paymentMethod == 'adyen_oneclick' &&
204+
isset($dataArray['paymentMethod']['additional_data']['stateData'])
188205
) {
189-
$checkoutPaymentDetails['cardBin'] =
190-
$dataArray['paymentMethod']['additional_data']['cardBin'] ?? null;
206+
try {
207+
$stateData = $this->jsonSerializer
208+
->unserialize($dataArray['paymentMethod']['additional_data']['stateData']);
209+
210+
/** @var \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest */
211+
$paymentRequest = $this->objectManagerInterface->create(
212+
\Adyen\Payment\Model\Api\PaymentRequest::class
213+
);
214+
215+
if ($quote->getCustomer()->getId() < 10) {
216+
$shopperReference = '00' . $quote->getCustomer()->getId();
217+
} elseif ($quote->getCustomer()->getId() >= 10 && $quote->getCustomer()->getId() <= 100) {
218+
$shopperReference = '0' . $quote->getCustomer()->getId();
219+
} else {
220+
$shopperReference = $quote->getCustomer()->getId();
221+
}
222+
223+
$contracts = $paymentRequest->getRecurringContractsForShopper(
224+
$shopperReference,
225+
$quote->getStoreId()
226+
);
227+
228+
if (isset($stateData['paymentMethod']) &&
229+
isset($stateData['paymentMethod']['storedPaymentMethodId'])
230+
) {
231+
$storedPaymentMethodId = $stateData['paymentMethod']['storedPaymentMethodId'];
232+
233+
$checkoutPaymentDetails['cardBin'] =
234+
$contracts[$storedPaymentMethodId]['additionalData']['cardBin'] ?? null;
235+
} else {
236+
$checkoutPaymentDetails['cardBin'] = null;
237+
}
238+
} catch (\Exception $e) {
239+
$checkoutPaymentDetails['cardBin'] = null;
240+
}
241+
} else {
242+
$checkoutPaymentDetails['cardBin'] =
243+
$dataArray['paymentMethod']['additional_data']['cardBin'] ?? null;
244+
}
191245

192246
$checkoutPaymentDetails['holderName'] =
193247
$dataArray['paymentMethod']['additional_data']['holderName'] ?? null;

Observer/Purchase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,6 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)
311311
$checkoutToken = $orderData['purchase']['checkoutToken'];
312312
$caseResponse = $this->purchaseHelper->postCaseToSignifyd($orderData, $order);
313313

314-
// Initial hold order
315-
$this->holdOrder($order, $case, $isPassive);
316-
317314
if (is_object($caseResponse)) {
318315
$case->setCode($caseResponse->getCaseId());
319316
$case->setMagentoStatus(Casedata::IN_REVIEW_STATUS);
@@ -323,6 +320,9 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)
323320

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

323+
// Initial hold order
324+
$this->holdOrder($order, $case, $isPassive);
325+
326326
if ($isPassive === false) {
327327
$this->orderResourceModel->save($order);
328328
}

0 commit comments

Comments
 (0)