From 90040d0a240b455f67e43d155e9449be5e28b378 Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Tue, 2 Mar 2021 08:46:54 +0100 Subject: [PATCH] RATEPLUG-176: bidirectionality: fix order-fetch issues --- Exception/RatepayException.php | 28 ++++++++++ .../RatepayPositionNotFoundException.php | 28 ++++++++++ Helper/PositionHelper.php | 10 ++-- README.md | 2 +- .../Cron/UpdateTransactionsSubscriber.php | 52 +++++++++++-------- plugin.xml | 21 +++++++- 6 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 Exception/RatepayException.php create mode 100644 Exception/RatepayPositionNotFoundException.php diff --git a/Exception/RatepayException.php b/Exception/RatepayException.php new file mode 100644 index 00000000..b84e74e7 --- /dev/null +++ b/Exception/RatepayException.php @@ -0,0 +1,28 @@ +context = $context; + } + + /** + * @return array + */ + public function getContext() + { + return $this->context; + } + +} \ No newline at end of file diff --git a/Exception/RatepayPositionNotFoundException.php b/Exception/RatepayPositionNotFoundException.php new file mode 100644 index 00000000..d7fded9e --- /dev/null +++ b/Exception/RatepayPositionNotFoundException.php @@ -0,0 +1,28 @@ + $order->getId(), + 'order_number' => $order->getNumber(), + 'order_detail_id' => $detail->getId(), + 'expected_model' => $expectedClass + ]); + } + +} \ No newline at end of file diff --git a/Helper/PositionHelper.php b/Helper/PositionHelper.php index f7b2e33c..d958938d 100644 --- a/Helper/PositionHelper.php +++ b/Helper/PositionHelper.php @@ -10,6 +10,7 @@ use RpayRatePay\DTO\BasketPosition; +use RpayRatePay\Exception\RatepayPositionNotFoundException; use RpayRatePay\Models\Position\AbstractPosition; use RpayRatePay\Models\Position\Discount; use RpayRatePay\Models\Position\Product; @@ -94,10 +95,15 @@ public function doesOrderHasOpenPositions(Order $_order, $items = []) /** * @param Detail $detail * @return AbstractPosition + * @throws RatepayPositionNotFoundException */ public function getPositionForDetail(Detail $detail) { - return $this->modelManager->find(self::getPositionClass($detail), $detail->getId()); + $entity = $this->modelManager->find(self::getPositionClass($detail), $detail->getId()); + if ($entity === null) { + throw new RatepayPositionNotFoundException($detail->getOrder(), $detail, self::getPositionClass($detail)); + } + return $entity; } public static function getPositionClass(Detail $detail) @@ -107,12 +113,10 @@ public static function getPositionClass(Detail $detail) case self::MODE_SW_PRODUCT[0]: //product case self::MODE_SW_PRODUCT[1]: //premium product return Product::class; - break; case self::MODE_SW_DISCOUNT[0]: //voucher case self::MODE_SW_DISCOUNT[1]: //IS_REBATE = rabatt case self::MODE_SW_DISCOUNT[2]: //IS_SURCHARGE_DISCOUNT //IST ZUSCHLAGSRABATT return Discount::class; - break; case self::MODE_RP_SHIPPING: return ShippingPosition::class; default: diff --git a/README.md b/README.md index 60030aa2..2c72a2f5 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ |Module | Ratepay Module for Shopware |------|---------- |Shop Version | `5.5.0` - `5.7.x` -|Version | `6.0.0` +|Version | `6.0.1` |Link | http://www.ratepay.com |Mail | integration@ratepay.com |Full Documentation | https://ratepay.gitbook.io/shopware5/ diff --git a/Subscriber/Cron/UpdateTransactionsSubscriber.php b/Subscriber/Cron/UpdateTransactionsSubscriber.php index 3597fd3b..0284e02d 100644 --- a/Subscriber/Cron/UpdateTransactionsSubscriber.php +++ b/Subscriber/Cron/UpdateTransactionsSubscriber.php @@ -16,6 +16,7 @@ use Exception; use Monolog\Logger; use RpayRatePay\Enum\PaymentMethods; +use RpayRatePay\Exception\RatepayException; use RpayRatePay\Services\Config\ConfigService; use RpayRatePay\Services\OrderStatusChangeService; use Shopware\Components\Model\ModelManager; @@ -25,8 +26,6 @@ class UpdateTransactionsSubscriber implements SubscriberInterface { - const MSG_NOTIFY_UPDATES_TO_RATEPAY = '[%d/%d] Processing order %d ...notify needed updates to Ratepay'; - /** * @var string */ @@ -131,24 +130,37 @@ public function updateRatepayTransactions(Shopware_Components_Cron_CronJob $job) $this->logger->info('Ratepay bidirectionality is turned off.'); return 'Ratepay bidirectionality is turned off.'; } - try { $orderIds = $this->findCandidateOrdersForUpdate(); $totalOrders = count($orderIds); foreach ($orderIds as $key => $orderId) { $order = $this->modelManager->find(Order::class, $orderId); $this->logger->info( - sprintf(self::MSG_NOTIFY_UPDATES_TO_RATEPAY, ($key + 1), $totalOrders, $orderId) + sprintf('Bidirectionality: Processing %d/%d order-id %d ...', ($key + 1), $totalOrders, $orderId), + [ + 'order_id' => $order->getId(), + 'order_number' => $order->getNumber() + ] ); - $this->orderStatusChangeService->informRatepayOfOrderStatusChange($order); + try { + $this->orderStatusChangeService->informRatepayOfOrderStatusChange($order); + } catch (Exception $e) { + $context = [ + 'order_id' => $order->getId(), + 'order_number' => $order->getNumber(), + 'trace' => $e->getTraceAsString() + ]; + if ($e instanceof RatepayException) { + /** @noinspection SlowArrayOperationsInLoopInspection */ + $context = array_merge($context, $e->getContext()); + } + $this->logger->error('Bidirectionality: ' . $e->getMessage(), $context); + } } } catch (Exception $e) { - $this->logger->error( - sprintf('Fehler UpdateTransactionsSubscriber: %s %s', $e->getMessage(), $e->getTraceAsString()) - ); + $this->logger->error('bidirectionality: ' .$e->getMessage(), [$e->getTraceAsString()]); return $e->getMessage(); } - return 'Success'; } @@ -173,24 +185,21 @@ private function findCandidateOrdersForUpdate() } $changeDate = $this->getChangeDateLimit(); + $paymentMethods = PaymentMethods::getNames(); $query = $this->db->select() + ->distinct(true) ->from(['history' => 's_order_history'], null) - ->joinLeft(['order' => 's_order'], 'history.orderID = order.id', ['id']) - ->joinLeft(['payment' => 's_core_paymentmeans'], 'order.paymentID = payment.id', null) + ->joinLeft(['s_order' => 's_order'], 'history.orderID = s_order.id', ['id']) + ->joinLeft(['payment' => 's_core_paymentmeans'], 's_order.paymentID = payment.id', null) ->where('history.change_date >= :changeDate') - ->where('order.status IN (:allowed_orderstatus)') - ->where('payment.name IN (:payment_methods)') - ->distinct(true); - - $rows = $this->db->fetchAll( - $query, - [ - ':changeDate' => $changeDate, - ':allowed_orderstatus' => $allowedOrderStates, - ':payment_methods' => PaymentMethods::getNames() + ->where("s_order.status IN (" . implode(",", $allowedOrderStates) . ")") + ->where("payment.name IN ('" . implode("','", $paymentMethods) . "')") + ->bind([ + 'changeDate' => $changeDate ]); + $rows = $this->db->fetchAll($query); return array_column($rows, 'id'); } @@ -230,7 +239,6 @@ private function getLastUpdateDate() $this->_cronjobLastExecutionDate = $date; } - return $this->_cronjobLastExecutionDate; } diff --git a/plugin.xml b/plugin.xml index 754db832..99c2e1eb 100644 --- a/plugin.xml +++ b/plugin.xml @@ -12,7 +12,7 @@ - 6.0.0 + 6.0.1 Copyright (c) 2020, Ratepay GmbH MIT https://www.ratepay.com @@ -20,7 +20,24 @@ Ratepay enables you to accept online payments wrten zur Verfügung, ohne dass Sie sich mit Risikoprüfungen beschäftigen müssen.ithout any risk of fraud and without the hassle of debtors management. - Ratepay stellt verschiedene Zahlungsa + Ratepay stellt verschiedene Zahlungsarten für Ihren Onlineshop zur Verfügung, ohne dass Sie sich mit der Risikoprüfung oder dem Debitorenmanagement beschäftigen müssen. + + + + +
  • Fixes some issues with the automatic bidirectionality
  • + + ]]> +
    + + +
  • Behebt einen Fehler mit der automatischen Bidirektionalität
  • + + ]]> +
    +