We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 9fa37954 authored by Attila Kiss's avatar Attila Kiss Committed by GitHub

Merge pull request #732 from Adyen/PW-2332

[PW-2332] Delay OFFER_CLOSED notifications processing by 10 minutes
parents 7025e6c6 da417bf3
...@@ -331,6 +331,34 @@ class Cron ...@@ -331,6 +331,34 @@ class Cron
} }
} }
/**
* @param $notification
* @return bool
*/
private function shouldSkipProcessingNotification($notification)
{
// OFFER_CLOSED notifications needs to be at least 10 minutes old to be processed
$offerClosedMinDate = new \DateTime();
$offerClosedMinDate->modify('-10 minutes');
// Remove OFFER_CLOSED notifications arrived in the last 10 minutes from the list to process to ensure it
// won't close any order which has an AUTHORISED notification arrived a bit later than the OFFER_CLOSED one.
$createdAt = \DateTime::createFromFormat('Y-m-d H:i:s', $notification['created_at']);
// To get the difference between $offerClosedMinDate and $createdAt, $offerClosedMinDate time in seconds is
// deducted from $createdAt time in seconds, divided by 60 and rounded down to integer
$minutesUntilProcessing = floor(($createdAt->getTimestamp() - $offerClosedMinDate->getTimestamp()) / 60);
if ($notification['event_code'] == Notification::OFFER_CLOSED && $minutesUntilProcessing > 0) {
$this->_adyenLogger->addAdyenNotificationCronjob(
sprintf('OFFER_CLOSED notification %s skipped! Wait %s minute(s) before processing.',
$notification->getEntityId(), $minutesUntilProcessing)
);
return true;
}
return false;
}
public function execute() public function execute()
{ {
// needed for Magento < 2.2.0 https://github.com/magento/magento2/pull/8413 // needed for Magento < 2.2.0 https://github.com/magento/magento2/pull/8413
...@@ -341,22 +369,19 @@ class Cron ...@@ -341,22 +369,19 @@ class Cron
$this->_order = null; $this->_order = null;
// execute notifications from 2 minute or earlier because order could not yet been created by magento
$dateStart = new \DateTime();
$dateStart->modify('-5 day');
$dateEnd = new \DateTime();
$dateEnd->modify('-1 minute');
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
// create collection
$notifications = $this->_notificationFactory->create(); $notifications = $this->_notificationFactory->create();
$notifications->addFieldToFilter('done', 0); $notifications->notificationsToProcessFilter();
$notifications->addFieldToFilter('processing', 0);
$notifications->addFieldToFilter('created_at', $dateRange);
$notifications->addFieldToFilter('error_count', ['lt' => Notification::MAX_ERROR_COUNT]);
// Loop thorugh notifications to set processing to true if notifiaction should not be skipped
foreach ($notifications as $notification) { foreach ($notifications as $notification) {
// set Cron processing to true // Check if notification should be processed or not
if ($this->shouldSkipProcessingNotification($notification)) {
// Remove notification from collection which will be processed
$notifications->removeItemByKey($notification->getId());
continue;
}
// set notification processing to true
$this->_updateNotification($notification, true, false); $this->_updateNotification($notification, true, false);
} }
...@@ -459,7 +484,7 @@ class Cron ...@@ -459,7 +484,7 @@ class Cron
); );
} }
//Trigger admin notice for unsuccessful REFUND notifications //Trigger admin notice for unsuccessful REFUND notifications
if ($this->_eventCode == Notification::REFUND){ if ($this->_eventCode == Notification::REFUND) {
$this->addRefundFailedNotice(); $this->addRefundFailedNotice();
} }
} else { } else {
...@@ -882,7 +907,6 @@ class Cron ...@@ -882,7 +907,6 @@ class Cron
*/ */
protected function _processNotification() protected function _processNotification()
{ {
$this->_adyenLogger->addAdyenNotificationCronjob('Processing the notification'); $this->_adyenLogger->addAdyenNotificationCronjob('Processing the notification');
$_paymentCode = $this->_paymentMethodCode(); $_paymentCode = $this->_paymentMethodCode();
...@@ -1143,10 +1167,8 @@ class Cron ...@@ -1143,10 +1167,8 @@ class Cron
// Populate billing agreement data // Populate billing agreement data
$billingAgreement->parseRecurringContractData($contractDetail); $billingAgreement->parseRecurringContractData($contractDetail);
if ($billingAgreement->isValid()) { if ($billingAgreement->isValid()) {
if (!$this->agreementResourceModel->getOrderRelation($billingAgreement->getAgreementId(), if (!$this->agreementResourceModel->getOrderRelation($billingAgreement->getAgreementId(),
$this->_order->getId())) { $this->_order->getId())) {
// save into sales_billing_agreement_order // save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($this->_order); $billingAgreement->addOrderRelation($this->_order);
......
...@@ -46,4 +46,30 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab ...@@ -46,4 +46,30 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
$this->addFieldToFilter('created_at', $dateRange); $this->addFieldToFilter('created_at', $dateRange);
return $this; return $this;
} }
/**
* Filter the notifications table to get non processed or done notifications without 5 or more errors older than
* 2 minutes but not older than 5 days, ordered by created_at and event_code columns
*
* @return $this
*/
public function notificationsToProcessFilter()
{
// execute notifications from 2 minute or earlier because order could not yet been created by magento
$dateStart = new \DateTime();
$dateStart->modify('-5 day');
$dateEnd = new \DateTime();
$dateEnd->modify('-1 minute');
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
$this->addFieldToFilter('done', 0);
$this->addFieldToFilter('processing', 0);
$this->addFieldToFilter('created_at', $dateRange);
$this->addFieldToFilter('error_count', ['lt' => \Adyen\Payment\Model\Notification::MAX_ERROR_COUNT]);
// Process the notifications in ascending order by creation date and event_code
$this->getSelect()->order('created_at ASC')->order('event_code ASC');
return $this;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment