From 38595ee3624f8cee96c8ed91ef14bf7a402c5d07 Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Wed, 18 Oct 2023 10:51:42 +0200 Subject: [PATCH] [ECP-8634] Use eventDate of the webhook --- Controller/Process/Json.php | 10 +++++++++- Gateway/Request/RefundDataBuilder.php | 2 +- Helper/AdyenOrderPayment.php | 11 +++++++---- Model/ResourceModel/Order/Payment/Collection.php | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Controller/Process/Json.php b/Controller/Process/Json.php index 316b6c23c..c50c1d2ba 100755 --- a/Controller/Process/Json.php +++ b/Controller/Process/Json.php @@ -341,7 +341,15 @@ private function loadNotificationFromRequest(Notification $notification, array $ // do this to set both fields in the correct timezone $date = new DateTime(); - $notification->setCreatedAt($date); + if (isset($requestItem['eventDate'])) { + $eventDate = DateTime::createFromFormat(DATE_ATOM, $requestItem['eventDate']); + // Change webhook's timezone to server's timezone + if ($eventDate) { + $formattedEventDate = $eventDate->setTimezone($date->getTimezone()); + } + } + + $notification->setCreatedAt($formattedEventDate ?? $date); $notification->setUpdatedAt($date); } diff --git a/Gateway/Request/RefundDataBuilder.php b/Gateway/Request/RefundDataBuilder.php index bdded190b..1e13522f0 100644 --- a/Gateway/Request/RefundDataBuilder.php +++ b/Gateway/Request/RefundDataBuilder.php @@ -112,7 +112,7 @@ public function build(array $buildSubject): array ->addFieldToFilter('payment_id', $payment->getId()); // partial refund if multiple payments check refund strategy - if ($orderPaymentCollection->getSize() > self::REFUND_STRATEGY_ASCENDING_ORDER) { + if ($orderPaymentCollection->getSize() > 1) { $refundStrategy = $this->adyenHelper->getAdyenAbstractConfigData( 'partial_payments_refund_strategy', $storeId diff --git a/Helper/AdyenOrderPayment.php b/Helper/AdyenOrderPayment.php index d8c2336d6..132d1af48 100644 --- a/Helper/AdyenOrderPayment.php +++ b/Helper/AdyenOrderPayment.php @@ -18,6 +18,7 @@ use Adyen\Payment\Model\Order\PaymentFactory; use Adyen\Payment\Model\ResourceModel\Order\Payment as OrderPaymentResourceModel; use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory as AdyenOrderPaymentCollection; +use DateTime; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Exception\AlreadyExistsException; @@ -210,7 +211,9 @@ public function createAdyenOrderPayment(Order $order, Notification $notification $pspReference = $notification->getPspreference(); try { - $date = new \DateTime(); + $date = new DateTime(); + $eventDate = DateTime::createFromFormat('Y-m-d H:i:s', $notification->getCreatedAt()); + $adyenOrderPayment = $this->adyenOrderPaymentFactory->create(); $adyenOrderPayment->setPspreference($pspReference); $adyenOrderPayment->setMerchantReference($merchantReference); @@ -219,7 +222,7 @@ public function createAdyenOrderPayment(Order $order, Notification $notification $adyenOrderPayment->setCaptureStatus($captureStatus); $adyenOrderPayment->setAmount($amount); $adyenOrderPayment->setTotalRefunded(0); - $adyenOrderPayment->setCreatedAt($date); + $adyenOrderPayment->setCreatedAt($eventDate); $adyenOrderPayment->setUpdatedAt($date); $this->orderPaymentResourceModel->save($adyenOrderPayment); } catch (\Exception $e) { @@ -249,7 +252,7 @@ public function refundAdyenOrderPayment(OrderPaymentInterface $adyenOrderPayment { $amountRefunded = $adyenOrderPayment->getTotalRefunded() + $this->adyenDataHelper->originalAmount($notification->getAmountValue(), $notification->getAmountCurrency()); - $adyenOrderPayment->setUpdatedAt(new \DateTime()); + $adyenOrderPayment->setUpdatedAt(new DateTime()); $adyenOrderPayment->setTotalRefunded($amountRefunded); $adyenOrderPayment->save(); @@ -266,7 +269,7 @@ public function refundAdyenOrderPayment(OrderPaymentInterface $adyenOrderPayment public function refundFullyAdyenOrderPayment(OrderPaymentInterface $adyenOrderPayment): OrderPaymentInterface { $amountRefunded = $adyenOrderPayment->getAmount(); - $adyenOrderPayment->setUpdatedAt(new \DateTime()); + $adyenOrderPayment->setUpdatedAt(new DateTime()); $adyenOrderPayment->setTotalRefunded($amountRefunded); $adyenOrderPayment->save(); diff --git a/Model/ResourceModel/Order/Payment/Collection.php b/Model/ResourceModel/Order/Payment/Collection.php index 035999ca4..3e6c8dbc7 100644 --- a/Model/ResourceModel/Order/Payment/Collection.php +++ b/Model/ResourceModel/Order/Payment/Collection.php @@ -83,7 +83,7 @@ public function addPaymentFilterAscending($paymentId) public function addPaymentFilterDescending($paymentId) { $this->addFieldToFilter('payment_id', $paymentId); - $this->getSelect()->order(['created_at DESC', 'entity_id DESC']); + $this->getSelect()->order(['created_at DESC']); return $this; } }