From 42c74a9cebbaa2c65720e673387c9732a03b71f4 Mon Sep 17 00:00:00 2001 From: Florian Seeger Date: Tue, 13 Oct 2020 09:31:46 +0200 Subject: [PATCH] added functionality to mark payment methods as harmless --- Attribute/AbstractAttribute.php | 113 ++++++++++++++++++++++++++++++ Attribute/Payment.php | 50 ++++++++++++++ LxRmp.php | 39 +++++++++++ Resources/services.xml | 8 +++ Services/PaymentService.php | 14 ++++ Subscriber/RiskManagement.php | 117 +++++++++++++++++--------------- Util/AbstractHandler.php | 71 +++++++++++++++++++ plugin.xml | 6 +- 8 files changed, 363 insertions(+), 55 deletions(-) create mode 100755 Attribute/AbstractAttribute.php create mode 100755 Attribute/Payment.php create mode 100755 Util/AbstractHandler.php diff --git a/Attribute/AbstractAttribute.php b/Attribute/AbstractAttribute.php new file mode 100755 index 0000000..82ad375 --- /dev/null +++ b/Attribute/AbstractAttribute.php @@ -0,0 +1,113 @@ +get('oldVersion'); + $this->doUpdate($oldVersion); + } + + /** + * @param \Enlight_Event_EventArgs $args + * @return mixed|void + */ + public function onPluginUninstall(\Enlight_Event_EventArgs $args){} + + /** + * @param $name + * @throws \Exception + */ + protected function addField($name):void + { + $this->updateField($name); + } + + /** + * @param $name + * @throws \Exception + */ + protected function updateField($name):void + { + $def = $this->getDefinition($name); + $service = $this->getCrudService(); + $def[2]['displayInBackend'] = true; + $service->update($this->getTableName(), $def[0], $def[1], $def[2]); + } + + /** + * @param $name + * @throws \Exception + */ + protected function deleteField($name):void + { + $service = $this->getCrudService(); + if ($service->get($this->getTableName(), $name)) { + $service->delete($this->getTableName(), $name); + } + } + + /** + * @param $name + * @return mixed + * @throws NoResultException + */ + protected function getDefinition($name) + { + foreach ($this->getFields() as $def) { + if ($def[0] == $name) { + return $def; + } + } + throw new NoResultException(); + } + + /** + * @return CrudService + */ + protected function getCrudService():CrudService + { + if (!$this->crudService instanceof CrudService) { + $this->crudService = Shopware()->Container()->get('shopware_attribute.crud_service'); + } + return $this->crudService; + } +} diff --git a/Attribute/Payment.php b/Attribute/Payment.php new file mode 100755 index 0000000..dc6f17e --- /dev/null +++ b/Attribute/Payment.php @@ -0,0 +1,50 @@ + 'Unbedenklich', + 'position' => 10 + ] + ]; + return $fields; + } + + /** + * @param string $oldVersion + */ + protected function doUpdate($oldVersion):void + { + if(version_compare('1.0.3', $oldVersion, '>')){ + try{ + $this->addField('harmless'); + }catch (\Exception $exception){ + } + } + } +} diff --git a/LxRmp.php b/LxRmp.php index a5bb724..a009b03 100755 --- a/LxRmp.php +++ b/LxRmp.php @@ -2,6 +2,9 @@ namespace LxRmp; use Shopware\Components\Plugin; +use Shopware\Components\Plugin\Context\InstallContext; +use Shopware\Components\Plugin\Context\UninstallContext; +use Shopware\Components\Plugin\Context\UpdateContext; use Symfony\Component\DependencyInjection\ContainerBuilder; /** @@ -14,6 +17,8 @@ class LxRmp extends Plugin { public const PLUGIN_NAMESPACE = 'LxRmp'; + public const PLUGIN_UPDATE = self::PLUGIN_NAMESPACE.'_Update'; + public const PLUGIN_UNINSTALL = self::PLUGIN_NAMESPACE.'_Uninstall'; /** * @return array @@ -42,4 +47,38 @@ public function build(ContainerBuilder $container) $container->setParameter('index.plugin_dir', $this->getPath()); parent::build($container); } + + /** + * @param InstallContext $context + */ + public function install(InstallContext $context) + { + $context->getPlugin()->setVersion('1.0.0'); + } + + /** + * @param UpdateContext $context + */ + public function update(UpdateContext $context) + { + $eventManager = $this->getEventManager(); + $eventManager->notify(self::PLUGIN_UPDATE, ['oldVersion' => $context->getCurrentVersion()]); + } + + /** + * @param UninstallContext $context + */ + public function uninstall(UninstallContext $context) + { + $eventManager = $this->getEventManager(); + $eventManager->notify(self::PLUGIN_UNINSTALL); + } + + /** + * @return object + */ + private function getEventManager() + { + return $this->container->get('events'); + } } \ No newline at end of file diff --git a/Resources/services.xml b/Resources/services.xml index 18296f3..68e77a7 100755 --- a/Resources/services.xml +++ b/Resources/services.xml @@ -19,5 +19,13 @@ + + + + + + + + \ No newline at end of file diff --git a/Services/PaymentService.php b/Services/PaymentService.php index 1bad424..e662947 100755 --- a/Services/PaymentService.php +++ b/Services/PaymentService.php @@ -28,4 +28,18 @@ public function getPaymentByID(int $paymentId):?string return null; } + public function isHarmless(int $paymentId):bool + { + $queryBuilder = $this->entityManager->getDBALQueryBuilder(); + $statement = $queryBuilder->select('harmless') + ->from('s_core_paymentmeans_attributes') + ->where('paymentmeanID = :id') + ->setParameter('id', $paymentId) + ->execute(); + $payment = $statement->fetch(); + if($payment && array_key_exists('harmless', $payment)){ + return (bool)$payment['harmless']; + } + return false; + } } \ No newline at end of file diff --git a/Subscriber/RiskManagement.php b/Subscriber/RiskManagement.php index bf2e2b4..a54d9f2 100755 --- a/Subscriber/RiskManagement.php +++ b/Subscriber/RiskManagement.php @@ -46,10 +46,8 @@ public static function getSubscribedEvents():array /** * @param \Enlight_Event_EventArgs $args - * @return bool - * @throws \Exception */ - public function onFilterPaymentMeans(\Enlight_Event_EventArgs $args) + public function onFilterPaymentMeans(\Enlight_Event_EventArgs $args):void { $timeOfVerification = $this->getTimeOfVerification(); if($timeOfVerification === 0){ @@ -74,7 +72,7 @@ public function onFilterPaymentMeans(\Enlight_Event_EventArgs $args) } } - public function onSavePayment(\Enlight_Event_EventArgs $eventArgs) + public function onSavePayment(\Enlight_Event_EventArgs $eventArgs):void { $timeOfVerification = $this->getTimeOfVerification(); /** @var \Enlight_Controller_Action $controller */ @@ -85,22 +83,24 @@ public function onSavePayment(\Enlight_Event_EventArgs $eventArgs) $timeOfVerification === 1 ){ $paymentId = $controller->Request()->getPost('payment'); - $paymentName = $this->paymentService->getPaymentByID($paymentId); - if($paymentName !== null){ - /** @var Response $response */ - try{ - $response = $this->connector->getRating(); - }catch (\Exception $exception){ - $response = null; - } - if($response !== null && $response->wasSuccessful()){ - if(!$response->filterPayments($paymentName)){ - Shopware()->Session()->showError = true; - $controller->redirect([ - 'controller' => 'checkout', - 'action' => 'shippingPayment', - ]); - return; + if(!$this->paymentService->isHarmless($paymentId)){ + $paymentName = $this->paymentService->getPaymentByID($paymentId); + if($paymentName !== null){ + /** @var Response $response */ + try{ + $response = $this->connector->getRating(); + }catch (\Exception $exception){ + $response = null; + } + if($response !== null && $response->wasSuccessful()){ + if(!$response->filterPayments($paymentName)){ + Shopware()->Session()->showError = true; + $controller->redirect([ + 'controller' => 'checkout', + 'action' => 'shippingPayment', + ]); + return; + } } } } @@ -113,29 +113,31 @@ public function onSavePayment(\Enlight_Event_EventArgs $eventArgs) } if( $controller->Request()->getActionName() === 'finish' ){ $userData = Shopware()->Modules()->Admin()->sGetUserData(); - $paymentName = $userData['additional']['payment']['name']; - if($paymentName !== null){ - /** @var Response $response */ - try{ - $response = $this->connector->getRating(); - }catch (\Exception $exception){ - $response = null; - } - if($response !== null && $response->wasSuccessful()){ - if(!$response->filterPayments($paymentName)){ - Shopware()->Session()->showError = true; - $controller->redirect([ - 'controller' => 'checkout', - 'action' => 'shippingPayment', - ]); - return; + if(!$this->paymentService->isHarmless($userData['additional']['payment']['id'])){ + $paymentName = $userData['additional']['payment']['name']; + if($paymentName !== null){ + /** @var Response $response */ + try{ + $response = $this->connector->getRating(); + }catch (\Exception $exception){ + $response = null; + } + if($response !== null && $response->wasSuccessful()){ + if(!$response->filterPayments($paymentName)){ + Shopware()->Session()->showError = true; + $controller->redirect([ + 'controller' => 'checkout', + 'action' => 'shippingPayment', + ]); + return; + } } } } } } - public function onSavePaymentAccount(\Enlight_Event_EventArgs $eventArgs) + public function onSavePaymentAccount(\Enlight_Event_EventArgs $eventArgs):void { $timeOfVerification = $this->getTimeOfVerification(); /** @var \Enlight_Controller_Action $controller */ @@ -145,24 +147,31 @@ public function onSavePaymentAccount(\Enlight_Event_EventArgs $eventArgs) $controller->Request()->getActionName() === 'savePayment' && $timeOfVerification === 1 ){ - $paymentId = $controller->Request()->getPost('sPayment'); - $paymentName = $this->paymentService->getPaymentByID($paymentId); - if($paymentName !== null){ - /** @var Response $response */ - try{ - $response = $this->connector->getRating(); - }catch (\Exception $exception){ - $response = null; - } - if($response !== null && $response->wasSuccessful()){ - if(!$response->filterPayments($paymentName)){ - Shopware()->Session()->showError = true; + $post = $controller->Request()->getPost('register'); + if(is_array($post) && array_key_exists('payment', $post)){ + $paymentId = (int)$post['payment']; + }else{ + $paymentId = null; + } + if(!$this->paymentService->isHarmless($paymentId)){ + $paymentName = $this->paymentService->getPaymentByID($paymentId); + if($paymentName !== null){ + /** @var Response $response */ + try{ + $response = $this->connector->getRating(); + }catch (\Exception $exception){ + $response = null; + } + if($response !== null && $response->wasSuccessful()){ + if(!$response->filterPayments($paymentName)){ + Shopware()->Session()->showError = true; - $controller->redirect([ - 'controller' => 'account', - 'action' => 'payment', - ]); - return; + $controller->redirect([ + 'controller' => 'account', + 'action' => 'payment', + ]); + return; + } } } } diff --git a/Util/AbstractHandler.php b/Util/AbstractHandler.php new file mode 100755 index 0000000..0c326ef --- /dev/null +++ b/Util/AbstractHandler.php @@ -0,0 +1,71 @@ +entityManager = $entityManager; + $this->connection = $connection; + } + + /** + * @return array + */ + public static function getSubscribedEvents():array + { + return [ + LxRmp::PLUGIN_UPDATE => 'onPluginUpdate', + LxRmp::PLUGIN_UNINSTALL => 'onPluginUninstall' + ]; + } + + /** + * @param \Enlight_Event_EventArgs $args + * @return mixed + */ + abstract public function onPluginUpdate(\Enlight_Event_EventArgs $args); + + /** + * @param \Enlight_Event_EventArgs $args + * @return mixed + */ + abstract public function onPluginUninstall(\Enlight_Event_EventArgs $args); + + /** + * @return SchemaTool + */ + protected function getNewSchema():SchemaTool + { + return new SchemaTool($this->entityManager); + } + + /** + * @param $class + * @return string + */ + protected function getEntityClassTable($class):string + { + return $this->entityManager->getClassMetadata($class)->getTableName(); + } +} diff --git a/plugin.xml b/plugin.xml index 98fda99..16be9b6 100755 --- a/plugin.xml +++ b/plugin.xml @@ -3,10 +3,14 @@ - 1.0.2 + 1.0.3 https://www.leonex.de/ LEONEX Internet GmbH + + Funktionalität hinzugefügt um Bezahlmethoden als "Harmlos" zu markieren + Added functionality to mark payment methods as "harmless + Bugfix für bestimmte Fehlercodes aus RMP Software Bugfix for specific Error-Codes from the RMP Software