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 f2d13c25 authored by cyattilakiss's avatar cyattilakiss Committed by GitHub

[PW-1993] Fix adyen_order_payment new entry creation issue during cron (#614)

Add try catch into cron process when new adyen_order_payment entity is added

Turned out that other plugins can intercept into invoice creation and
these are potential sources of exceptions. After inserting a new
adyen_order_payment entity we start the invoice creation process and if
something goes wrong during that process we reprocess the notification in
the next batch. Therefore the adyen_order_payment is already saved and the
next run will violate the unique constraint setting of the table.

This is a quick solution to prevent the process to stop and the order to
get stuck because of this exception.

Resolves GitHub issues: #603, #610
parent d3a41c3b
...@@ -38,6 +38,7 @@ class Cron ...@@ -38,6 +38,7 @@ class Cron
/** /**
* Logging instance * Logging instance
*
* @var \Adyen\Payment\Logger\AdyenLogger * @var \Adyen\Payment\Logger\AdyenLogger
*/ */
protected $_logger; protected $_logger;
...@@ -296,6 +297,7 @@ class Cron ...@@ -296,6 +297,7 @@ class Cron
/** /**
* Process the notification * Process the notification
*
* @return void * @return void
*/ */
public function processNotification() public function processNotification()
...@@ -452,7 +454,8 @@ class Cron ...@@ -452,7 +454,8 @@ class Cron
} catch (\Exception $e) { } catch (\Exception $e) {
$this->_updateNotification($notification, false, false); $this->_updateNotification($notification, false, false);
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
sprintf("Notification %s had an error: %s \n %s", $notification->getEntityId(), $e->getMessage(), $e->getTraceAsString()) sprintf("Notification %s had an error: %s \n %s", $notification->getEntityId(), $e->getMessage(),
$e->getTraceAsString())
); );
} }
} }
...@@ -780,6 +783,7 @@ class Cron ...@@ -780,6 +783,7 @@ class Cron
/** /**
* retrieve last 4 digits of card from the reason field * retrieve last 4 digits of card from the reason field
*
* @param $reason * @param $reason
* @return string * @return string
*/ */
...@@ -934,7 +938,7 @@ class Cron ...@@ -934,7 +938,7 @@ class Cron
} }
// Order is already Cancelled // Order is already Cancelled
if ($this->_order->isCanceled()){ if ($this->_order->isCanceled()) {
$this->_adyenLogger->addAdyenNotificationCronjob("Order is already cancelled, skipping OFFER_CLOSED"); $this->_adyenLogger->addAdyenNotificationCronjob("Order is already cancelled, skipping OFFER_CLOSED");
break; break;
} }
...@@ -1138,6 +1142,7 @@ class Cron ...@@ -1138,6 +1142,7 @@ class Cron
/** /**
* Not implemented * Not implemented
*
* @return bool * @return bool
*/ */
protected function _refundOrder() protected function _refundOrder()
...@@ -1287,8 +1292,8 @@ class Cron ...@@ -1287,8 +1292,8 @@ class Cron
} }
/** /**
* @throws Exception
* @return void * @return void
* @throws Exception
*/ */
protected function _prepareInvoice() protected function _prepareInvoice()
{ {
...@@ -1352,19 +1357,27 @@ class Cron ...@@ -1352,19 +1357,27 @@ class Cron
$orderCurrencyCode = $this->_order->getOrderCurrencyCode(); $orderCurrencyCode = $this->_order->getOrderCurrencyCode();
$amount = $this->_adyenHelper->originalAmount($this->_value, $orderCurrencyCode); $amount = $this->_adyenHelper->originalAmount($this->_value, $orderCurrencyCode);
// add to order payment try {
$date = new \DateTime(); // add to order payment
$this->_adyenOrderPaymentFactory->create() $date = new \DateTime();
->setPspreference($this->_pspReference) $this->_adyenOrderPaymentFactory->create()
->setMerchantReference($this->_merchantReference) ->setPspreference($this->_pspReference)
->setPaymentId($paymentObj->getId()) ->setMerchantReference($this->_merchantReference)
->setPaymentMethod($this->_paymentMethod) ->setPaymentId($paymentObj->getId())
->setAmount($amount) ->setPaymentMethod($this->_paymentMethod)
->setTotalRefunded(0) ->setAmount($amount)
->setCreatedAt($date) ->setTotalRefunded(0)
->setUpdatedAt($date) ->setCreatedAt($date)
->save(); ->setUpdatedAt($date)
->save();
} catch (\Exception $e) {
$this->_adyenLogger->addError(
'While processing a notification an exception occured. The payment has already been saved in the ' .
'adyen_order_payment table but something went wrong later. Please check your logs for potential ' .
'error messages regarding the merchant reference (order id): "' . $this->_merchantReference .
'" and PSP reference: "' . $this->_pspReference . '"'
);
}
if ($this->_isTotalAmount($paymentObj->getEntityId(), $orderCurrencyCode)) { if ($this->_isTotalAmount($paymentObj->getEntityId(), $orderCurrencyCode)) {
$this->_createInvoice(); $this->_createInvoice();
...@@ -1618,9 +1631,9 @@ class Cron ...@@ -1618,9 +1631,9 @@ class Cron
} }
/** /**
* @throws Exception
* @throws \Magento\Framework\Exception\LocalizedException
* @return void * @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws Exception
*/ */
protected function _createInvoice() protected function _createInvoice()
{ {
......
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