From d09069d209b929ada211a0067fa15d56b64b16b0 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 3 Jul 2023 14:58:46 +0200 Subject: [PATCH 1/4] Bugfix: Limit the pagination by the API limit of 250 #59 --- Model/MollieSubscriptionsListing.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Model/MollieSubscriptionsListing.php b/Model/MollieSubscriptionsListing.php index 8f87d5f..740313d 100644 --- a/Model/MollieSubscriptionsListing.php +++ b/Model/MollieSubscriptionsListing.php @@ -93,9 +93,14 @@ public function getDataSourceData() $api = $this->mollieModel->getMollieApi($storeId); $paging = $this->getContext()->getRequestParam('paging'); + $pageSize = $paging['pageSize'] ?? 20; + if ($pageSize > 250) { + $pageSize = 250; + } + $result = $api->subscriptions->page( $this->getContext()->getRequestParam('offsetID'), - $paging['pageSize'] ?? 20 + $pageSize ); $this->preloadCustomers((array)$result); From c041532a8dc0341499425510151504579c5e043c Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 10 Jul 2023 12:11:08 +0200 Subject: [PATCH 2/4] Feature: Send email on webhook error --- Config.php | 44 +++++++++++ Controller/Api/Webhook.php | 16 +++- Service/Mollie/SendAdminNotification.php | 78 +++++++++++++++++++ etc/adminhtml/system/debug.xml | 42 +++++++++- etc/config.xml | 8 +- etc/email_templates.xml | 1 + ...subscription_error_admin_notification.html | 31 ++++++++ 7 files changed, 212 insertions(+), 8 deletions(-) create mode 100644 Service/Mollie/SendAdminNotification.php create mode 100644 view/frontend/email/subscription_error_admin_notification.html diff --git a/Config.php b/Config.php index 6f5bdde..d62347d 100755 --- a/Config.php +++ b/Config.php @@ -20,6 +20,10 @@ class Config { const EXTENSION_CODE = 'Mollie_Subscriptions'; + const XML_PATH_DEBUG_ERROR_EMAIL_TEMPLATE = 'mollie_subscriptions/debug/error_email_template'; + const XML_PATH_DEBUG_ENABLE_ERROR_EMAILS = 'mollie_subscriptions/debug/enable_error_emails'; + const XML_PATH_DEBUG_ERROR_SENDER_EMAIL = 'mollie_subscriptions/debug/error_sender_email'; + const XML_PATH_DEBUG_ERROR_RECEIVER_EMAIL = 'mollie_subscriptions/debug/error_receiver_email'; const XML_PATH_EXTENSION_VERSION = 'mollie_subscriptions/general/version'; const XML_PATH_EXTENSION_ENABLE = 'mollie_subscriptions/general/enable'; const XML_PATH_EXTENSION_SHIPPING_METHOD = 'mollie_subscriptions/general/shipping_method'; @@ -157,6 +161,36 @@ public function isEnabled(int $storeId = null): bool return $this->getFlag(self::XML_PATH_EXTENSION_ENABLE, $storeId); } + /** + * @param int $storeId + * @param string $scope + * @return bool + */ + public function isErrorEmailEnabled(int $storeId = null, string $scope = ScopeInterface::SCOPE_STORE): bool + { + return $this->getFlag(self::XML_PATH_DEBUG_ENABLE_ERROR_EMAILS, $storeId, $scope); + } + + /** + * @param int|null $storeId + * @param string $scope + * @return string + */ + public function errorEmailSender(int $storeId = null, string $scope = ScopeInterface::SCOPE_STORE): string + { + return $this->getStoreValue(self::XML_PATH_DEBUG_ERROR_SENDER_EMAIL, $storeId, $scope); + } + + /** + * @param int|null $storeId + * @param string $scope + * @return string + */ + public function errorEmailReceiver(int $storeId = null, string $scope = ScopeInterface::SCOPE_STORE): string + { + return $this->getStoreValue(self::XML_PATH_DEBUG_ERROR_RECEIVER_EMAIL, $storeId, $scope); + } + /** * @param int|null $storeId * @return string @@ -229,6 +263,16 @@ public function allowOneTimePurchase($storeId = null, $scope = ScopeInterface::S return $this->getFlag(static::XML_PATH_ALLOW_ONE_TIME_PURCHASE, $storeId, $scope); } + /** + * @param int $storeId + * @param string $scope + * @return null|string + */ + public function subscriptionErrorAdminNotificationTemplate(int $storeId = null, string $scope = ScopeInterface::SCOPE_STORE): ?string + { + return $this->getStoreValue(static::XML_PATH_DEBUG_ERROR_EMAIL_TEMPLATE, $storeId, $scope); + } + /** * @param null|int|string $storeId * @param string $scope diff --git a/Controller/Api/Webhook.php b/Controller/Api/Webhook.php index aa8ba58..6dfea36 100644 --- a/Controller/Api/Webhook.php +++ b/Controller/Api/Webhook.php @@ -34,6 +34,7 @@ use Mollie\Payment\Service\Order\SendOrderEmails; use Mollie\Subscriptions\Config; use Mollie\Subscriptions\Service\Mollie\RetryUsingOtherStoreViews; +use Mollie\Subscriptions\Service\Mollie\SendAdminNotification; class Webhook extends Action implements CsrfAwareActionInterface { @@ -110,6 +111,10 @@ class Webhook extends Action implements CsrfAwareActionInterface * @var ValidateMetadata */ private $validateMetadata; + /** + * @var SendAdminNotification + */ + private $sendAdminNotification; public function __construct( Context $context, @@ -126,7 +131,8 @@ public function __construct( MollieLogger $mollieLogger, SendOrderEmails $sendOrderEmails, RetryUsingOtherStoreViews $retryUsingOtherStoreViews, - ValidateMetadata $validateMetadata + ValidateMetadata $validateMetadata, + SendAdminNotification $sendAdminNotification ) { parent::__construct($context); @@ -144,6 +150,7 @@ public function __construct( $this->sendOrderEmails = $sendOrderEmails; $this->retryUsingOtherStoreViews = $retryUsingOtherStoreViews; $this->validateMetadata = $validateMetadata; + $this->sendAdminNotification = $sendAdminNotification; } public function execute() @@ -193,9 +200,12 @@ public function execute() $this->orderRepository->save($order); $this->mollie->processTransactionForOrder($order, 'webhook'); + return $this->returnOkResponse(); - } catch (ApiException $exception) { - $this->mollieLogger->addInfoLog('ApiException occured while checking transaction', [ + } catch (\Throwable $exception) { + $this->sendAdminNotification->send($id, $exception); + + $this->mollieLogger->addInfoLog('Error occurred while processing subscription', [ 'id' => $id, 'exception' => $exception->__toString() ]); diff --git a/Service/Mollie/SendAdminNotification.php b/Service/Mollie/SendAdminNotification.php new file mode 100644 index 0000000..aa8c7a0 --- /dev/null +++ b/Service/Mollie/SendAdminNotification.php @@ -0,0 +1,78 @@ +config = $config; + $this->transportBuilder = $transportBuilder; + $this->senderResolver = $senderResolver; + $this->urlInterface = $urlInterface; + } + + public function send(string $id, \Throwable $exception): void + { + if (!$this->config->isErrorEmailEnabled()) { + return; + } + + $url = $this->urlInterface->getCurrentUrl(); + $sender = $this->senderResolver->resolve($this->config->errorEmailSender()); + $receiver = $this->senderResolver->resolve($this->config->errorEmailReceiver()); + + $templateId = $this->config->subscriptionErrorAdminNotificationTemplate(); + $builder = $this->transportBuilder->setTemplateIdentifier($templateId); + $builder->setTemplateOptions(['area' => 'frontend', 'store' => $this->config->getStore()->getId()]); + + $builder->setFromByScope($sender); + $builder->setTemplateVars([ + 'id' => $id, + 'url' => $url, + 'error' => $exception->__toString(), + ]); + + $builder->addTo($receiver['email'], $receiver['name']); + + $transport = $builder->getTransport(); + $transport->sendMessage(); + } +} diff --git a/etc/adminhtml/system/debug.xml b/etc/adminhtml/system/debug.xml index 2cacc23..84d7b50 100755 --- a/etc/adminhtml/system/debug.xml +++ b/etc/adminhtml/system/debug.xml @@ -16,26 +16,60 @@ Errors are always logged and written into the error.log file.]]> Mollie\Subscriptions\Block\Adminhtml\Magmodules\Heading - Magento\Config\Model\Config\Source\Yesno mollie_subscriptions/general/debug - - - + + + Magento\Config\Model\Config\Source\Yesno + + mollie_subscriptions/debug/enable_error_emails + + + + Magento\Config\Model\Config\Source\Email\Identity + mollie_subscriptions/debug/error_sender_email + + 1 + + + + + Magento\Config\Model\Config\Source\Email\Identity + mollie_subscriptions/debug/error_receiver_email + + 1 + + + + + Magento\Config\Model\Config\Source\Email\Template + mollie_subscriptions/debug/error_email_template + + 1 + + diff --git a/etc/config.xml b/etc/config.xml index af0e0cb..4be211c 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -12,8 +12,14 @@ v1.10.3 0 - 0 + 1 + + 1 + general + general + Mollie_Subscriptions_debug_error_email_template + 0 diff --git a/etc/email_templates.xml b/etc/email_templates.xml index 4a52a9e..a2777fb 100644 --- a/etc/email_templates.xml +++ b/etc/email_templates.xml @@ -7,4 +7,5 @@