From ecb0f9c5c32c34d4c7c402f6577af079f900d146 Mon Sep 17 00:00:00 2001 From: Reinder Date: Thu, 13 Feb 2020 14:27:48 +0100 Subject: [PATCH 1/3] Added files to .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fee391b3..fcb2d478 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ logs .idea -releases \ No newline at end of file +releases +/*.sh +/scoper.inc.php +/UpdateVersionNumber.php \ No newline at end of file From 590f2ecc92fe231df3ef61411610613c3d41e180 Mon Sep 17 00:00:00 2001 From: Reinder Date: Thu, 13 Feb 2020 14:29:32 +0100 Subject: [PATCH 2/3] Created version 1.5.6 of the Mollie Shopware 5 plugin --- .gitignore | 3 +- Command/KlarnaShippingCommand.php | 125 ++++++ Components/Base/AbstractPaymentController.php | 19 +- Components/Config.php | 20 + Components/Helpers/LogHelper.php | 76 ++++ Components/MollieApiFactory.php | 34 +- Components/Notifier.php | 5 +- Components/Services/BasketService.php | 2 +- Components/Services/IdealService.php | 2 +- Components/Services/PaymentMethodService.php | 262 +++++++++++++ Components/Services/PaymentService.php | 11 + Controllers/Frontend/Mollie.php | 368 ++++++++++-------- Models/Transaction.php | 50 +++ MollieShopware.php | 265 ++++++------- Resources/config.xml | 53 ++- Resources/services.xml | 14 + .../frontend/_public/src/less/checkout.less | 4 + .../plugins/payment/mollie_creditcard.tpl | 3 +- Subscriber/IdealIssuersSubscriber.php | 23 +- Subscriber/OrderBackendSubscriber.php | 61 +-- plugin.xml | 8 +- 21 files changed, 1031 insertions(+), 377 deletions(-) create mode 100644 Command/KlarnaShippingCommand.php create mode 100644 Components/Helpers/LogHelper.php create mode 100644 Components/Services/PaymentMethodService.php create mode 100644 Resources/views/frontend/_public/src/less/checkout.less diff --git a/.gitignore b/.gitignore index fcb2d478..190ff0f1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ logs releases /*.sh /scoper.inc.php -/UpdateVersionNumber.php \ No newline at end of file +/UpdateVersionNumber.php +/composer.json \ No newline at end of file diff --git a/Command/KlarnaShippingCommand.php b/Command/KlarnaShippingCommand.php new file mode 100644 index 00000000..aa557191 --- /dev/null +++ b/Command/KlarnaShippingCommand.php @@ -0,0 +1,125 @@ +config = $config; + $this->modelManager = $modelManager; + $this->apiClient = $apiClient; + } + + public function configure() + { + $this + ->setName('mollie:ship:klarna') + ->setDescription('Ship completed Klarna orders'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + /** @var Transaction[] $transactions */ + $transactions = null; + + /** @var TransactionRepository $transactionRepository */ + $transactionRepository = $this->modelManager + ->getRepository(Transaction::class); + + if ($transactionRepository !== null) { + $transactions = $transactionRepository->findBy([ + 'isShipped' => false, + 'paymentMethod' => 'mollie_klarna' + ]); + } + + if ( + $transactions !== null + && is_array($transactions) + ) { + $output->writeln(count($transactions) . ' orders to update.'); + + foreach ($transactions as $transaction) { + $output->writeln('Updating order ' . $transaction->getOrderNumber()); + + /** @var Order $order */ + $order = null; + + if ($transaction->getOrderId() === null) { + continue; + } + + /** @var Repository $orderRepository */ + $orderRepository = $this->modelManager->getRepository(Order::class); + + if ($orderRepository === null) { + continue; + } + + /** @var Order $order */ + $order = $orderRepository->find($transaction->getOrderId()); + + if ($order === null) { + continue; + } + + // Ship order + try { + $mollieOrder = $this->apiClient->orders->get($transaction->getMollieId()); + + if ( + $mollieOrder !== null + && $order->getOrderStatus()->getId() === $this->config->getKlarnaShipOnStatus() + && !in_array($order->getPaymentStatus()->getId(), [ + Status::PAYMENT_STATE_OPEN, + Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED + ], true) + ) { + $mollieOrder->shipAll(); + $transaction->setIsShipped(true); + } + } catch (\Exception $e) { + // + } + + // Save order + try { + $transactionRepository->save($transaction); + } catch (\Exception $e) { + // + } + } + + $output->writeln('Done.'); + } + } +} \ No newline at end of file diff --git a/Components/Base/AbstractPaymentController.php b/Components/Base/AbstractPaymentController.php index cdb65812..a0613046 100644 --- a/Components/Base/AbstractPaymentController.php +++ b/Components/Base/AbstractPaymentController.php @@ -176,24 +176,27 @@ protected function getUserId() */ protected function getPaymentId() { + $paymentId = null; $user = $this->getUser(); + $userId = $this->getUserId(); if (!empty($user['additional']['payment']['id'])) { - return $user['additional']['payment']['id']; + $paymentId = $user['additional']['payment']['id']; } if (!empty($user['additional']['user']['paymentID'])) { - return $user['additional']['user']['paymentID']; + $paymentId = $user['additional']['user']['paymentID']; } - $userId = $this->getUserId(); - - if (empty($userId)) { - return null; + if ($paymentId === null && $userId !== null) { + $connection = $this->container->get('models')->getConnection(); + $paymentId = $connection->fetchColumn('SELECT paymentID FROM s_user WHERE id = :userId', ['userId' => $userId]); } - $connection = $this->container->get('models')->getConnection(); - return $connection->fetchColumn('SELECT paymentID FROM s_user WHERE id = :userId', [ 'userId' => $userId ]); + $user['additional']['payment']['id'] = $paymentId; + $user['additional']['user']['paymentID'] = $paymentId; + + return $paymentId; } /** diff --git a/Components/Config.php b/Components/Config.php index e506ce6f..bdd09588 100644 --- a/Components/Config.php +++ b/Components/Config.php @@ -6,6 +6,9 @@ class Config { + const TRANSACTION_NUMBER_TYPE_MOLLIE = 'mollie'; + const TRANSACTION_NUMBER_TYPE_PAYMENT_METHOD = 'payment_method'; + /** @var \Shopware\Components\Plugin\ConfigReader */ protected $configReader; @@ -104,6 +107,14 @@ public function useOrdersApiOnlyWhereMandatory() return ($this->get('orders_api_only_where_mandatory', 'yes') == 'yes'); } + /** + * @return string + */ + public function getTransactionNumberType() + { + return (string) $this->get('transaction_number_type', self::TRANSACTION_NUMBER_TYPE_MOLLIE); + } + /** * @return int */ @@ -144,6 +155,14 @@ public function cancelFailedOrders() return ($this->get('auto_cancel_failed_orders', 'yes') === 'yes'); } + /** + * @return int + */ + public function getKlarnaShipOnStatus() + { + return (int) $this->get('klarna_ship_on_status', \Shopware\Models\Order\Status::ORDER_STATE_COMPLETELY_DELIVERED); + } + /** * @return int */ @@ -151,6 +170,7 @@ public function getShippedStatus() { return (int) $this->get('klarna_shipped_status', -1); } + /** * @return string */ diff --git a/Components/Helpers/LogHelper.php b/Components/Helpers/LogHelper.php new file mode 100644 index 00000000..9a905300 --- /dev/null +++ b/Components/Helpers/LogHelper.php @@ -0,0 +1,76 @@ +$logMethod($message, [$exception]); + } + + /** + * Returns the plugin logger. + * + * @param ContainerInterface|null $container + * @return Logger + */ + private static function pluginLogger(ContainerInterface $container = null) + { + // Set the container + static::$container = ($container === null ? static::$container : $container); + + // Get the plugin logger from the container + if (static::$pluginLogger === null && static::$container !== null) { + static::$pluginLogger = static::$container->get('pluginlogger'); + } + + return static::$pluginLogger; + } +} \ No newline at end of file diff --git a/Components/MollieApiFactory.php b/Components/MollieApiFactory.php index 1b92eed8..fce06874 100644 --- a/Components/MollieApiFactory.php +++ b/Components/MollieApiFactory.php @@ -2,6 +2,8 @@ namespace MollieShopware\Components; +require_once __DIR__ . '/../Client/vendor/autoload.php'; + use Mollie\Api\MollieApiClient; class MollieApiFactory @@ -32,6 +34,8 @@ public function __construct(Config $config) */ public function create() { + $this->requireDependencies(); + if (empty($this->apiClient)) { $this->apiClient = new MollieApiClient(); $this->apiClient->setApiKey($this->config->apikey()); @@ -45,7 +49,7 @@ public function create() // add plugin name and version $this->apiClient->addVersionString( - 'MollieShopware/1.5.5' + 'MollieShopware/1.5.6' ); } catch (\Exception $ex) { @@ -55,4 +59,32 @@ public function create() return $this->apiClient; } + + public function requireDependencies() + { + // Load composer libraries + if (file_exists(__DIR__ . '/../Client/vendor/scoper-autoload.php')) { + require_once __DIR__ . '/../Client/vendor/scoper-autoload.php'; + } + + // Load guzzle functions + if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/guzzle/src/functions_include.php')) { + require_once __DIR__ . '/../Client/vendor/guzzlehttp/guzzle/src/functions_include.php'; + } + + // Load promises functions + if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/promises/src/functions_include.php')) { + require_once __DIR__ . '/../Client/vendor/guzzlehttp/promises/src/functions_include.php'; + } + + // Load psr7 functions + if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/psr7/src/functions_include.php')) { + require_once __DIR__ . '/../Client/vendor/guzzlehttp/psr7/src/functions_include.php'; + } + + // Load client + if (file_exists(__DIR__ . '/../Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php')) { + require_once __DIR__ . '/../Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php'; + } + } } diff --git a/Components/Notifier.php b/Components/Notifier.php index 4cf903de..33a6d037 100644 --- a/Components/Notifier.php +++ b/Components/Notifier.php @@ -11,11 +11,12 @@ class Notifier * @param $error * @throws \Exception */ - public static function notifyException($error) { + public static function notifyException($error, $exception = null) { // log the error Logger::log( 'error', - $error + $error, + $exception ); // return the error json diff --git a/Components/Services/BasketService.php b/Components/Services/BasketService.php index 6e46e915..f6a1b0aa 100644 --- a/Components/Services/BasketService.php +++ b/Components/Services/BasketService.php @@ -122,7 +122,7 @@ public function restoreBasket($orderId) // save order $this->modelManager->persist($order); - $this->modelManager->flush($order); + $this->modelManager->flush(); } } diff --git a/Components/Services/IdealService.php b/Components/Services/IdealService.php index e766c042..ecada920 100644 --- a/Components/Services/IdealService.php +++ b/Components/Services/IdealService.php @@ -84,7 +84,7 @@ public function setSelectedIssuer($issuer) $attributes->setMollieShopwareIdealIssuer($issuer); $this->modelManager->persist($attributes); - $this->modelManager->flush(); + $this->modelManager->flush($attributes); return $issuer; } diff --git a/Components/Services/PaymentMethodService.php b/Components/Services/PaymentMethodService.php new file mode 100644 index 00000000..de9c72ce --- /dev/null +++ b/Components/Services/PaymentMethodService.php @@ -0,0 +1,262 @@ +modelManager = $modelManager; + $this->mollieApiClient = $mollieApiClient; + $this->paymentInstaller = $paymentInstaller; + $this->templateManager = $templateManager; + } + + /** + * Returns a payment method by an array of params, or returns null + * if no payment method is found. + * + * @param array $params + * @return object|Payment|null + */ + public function getPaymentMethod(array $params) + { + /** @var null|Payment $paymentMethod */ + $paymentMethod = null; + + /** @var PaymentRepository $paymentMethodRepository */ + $paymentRepository = $this->modelManager + ->getRepository(Payment::class); + + // Find the payment method by the given params + if ($paymentRepository !== null) { + $paymentMethod = $paymentRepository->findOneBy($params); + } + + return $paymentMethod; + } + + /** + * Returns an array of payment methods from the Mollie API. + * + * @todo Get methods in the correct locale (de_DE en_US es_ES fr_FR nl_BE fr_BE nl_NL) + * + * @return array + */ + public function getPaymentMethodsFromMollie() + { + // Variables + $options = []; + $position = 0; + + /** @var null|MethodCollection $methods */ + $methods = $this->getActivePaymentMethodsFromMollie(); + + // Add the template directory to the template manager + $this->templateManager->addTemplateDir(__DIR__ . '/Resources/views'); + + // Assign the Shopware router to the template manager + if (Shopware()->Front() !== null) { + $this->templateManager->assign('router', Shopware()->Front()->Router()); + } + + // Add options to the array + foreach ($methods as $method) { + $option = $this->getPreparedPaymentOption($method, $position); + + if (is_array($option)) { + $options[] = $option; + $position++; + } + } + + return $options; + } + + /** + * Installs an array of payment methods. + * + * @param string $pluginName + * @param array $methods + */ + public function installPaymentMethod($pluginName, array $methods) + { + foreach ($methods as $method) { + /** @var Payment $existingMethod */ + $existingMethod = null; + + // Retrieve existing information so it doesn't get overwritten + if (isset($method[self::PAYMENT_METHOD_NAME], $method[self::PAYMENT_METHOD_ACTION])) { + $existingMethod = $this->getPaymentMethod([ + 'name' => $method[self::PAYMENT_METHOD_NAME], + 'action' => $method[self::PAYMENT_METHOD_ACTION] + ]); + } + + // Set existing data on method + if ($existingMethod !== null) { + $method[self::PAYMENT_METHOD_ADDITIONAL_DESCRIPTION] = (string) $existingMethod->getAdditionalDescription(); + $method[self::PAYMENT_METHOD_DESCRIPTION] = (string) $existingMethod->getDescription(); + $method[self::PAYMENT_METHOD_POSITION] = $existingMethod->getPosition(); + } + + // Install the payment method in Shopware + $this->paymentInstaller->createOrUpdate($pluginName, $method); + } + } + + /** + * Deactivates all payment methods created by the Mollie plugin. + */ + public function deactivatePaymentMethods() + { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = $this->modelManager->createQueryBuilder(); + + /** @var Query $query */ + $query = $queryBuilder->update(Payment::class, 'paymentMethod') + ->set('paymentMethod.active', '?1') + ->where($queryBuilder->expr()->like('paymentMethod.name', '?2')) + ->setParameter(1, false) + ->setParameter(2, 'mollie_%') + ->getQuery(); + + // Execute the query + $query->execute(); + } + + /** + * Returns a collection of active payment methods from the Mollie API. + * + * @return null|BaseCollection|MethodCollection + */ + public function getActivePaymentMethodsFromMollie() + { + /** @var MethodCollection $methods */ + $methods = null; + + try { + $methods = $this->mollieApiClient->methods->allActive([ + 'resource' => 'orders', + 'includeWallets' => 'applepay' + ]); + } catch (ApiException $e) { + LogHelper::logMessage($e->getMessage(), LogHelper::LOG_ERROR, $e); + } + + return $methods; + } + + /** + * Returns an option array of a payment method. + * + * @param $method + * @param $position + * @return array + */ + private function getPreparedPaymentOption($method, $position = 0) + { + // Get the name of the payment method + $paymentMethodName = 'mollie_' . strtolower($method->id); + + // Get the additional description of a payment method + $additionalDescription = $this->getAdditionalDescription($method, $paymentMethodName); + + // Build the option array of the payment method + $option = [ + self::PAYMENT_METHOD_ACTION => 'frontend/Mollie', + self::PAYMENT_METHOD_ACTIVE => 1, + self::PAYMENT_METHOD_NAME => $paymentMethodName, + self::PAYMENT_METHOD_ADDITIONAL_DESCRIPTION => $additionalDescription, + self::PAYMENT_METHOD_DESCRIPTION => (string) $method->description, + self::PAYMENT_METHOD_POSITION => $position, + ]; + + // Add the template to the payment method + if (file_exists(self::PAYMENT_METHOD_TEMPLATE_DIR . '/' . $paymentMethodName . '.tpl')) { + $option['template'] = $paymentMethodName . '.tpl'; + } + + return $option; + } + + /** + * Returns the additional description of a payment method. + * + * @param $method + * @param $paymentMethodName + * @return string + */ + private function getAdditionalDescription($method, $paymentMethodName) + { + /** @var null|string $additionalDescription */ + $additionalDescription = null; + + // Assign the method to the template manager + $this->templateManager->assign('method', $method); + + // Get the path of the template file + $templatePath = self::PAYMENT_METHOD_TEMPLATE_DIR . '/methods/' . $paymentMethodName . '.tpl'; + + // If the template doesn't exist, fallback to the default template + if (!file_exists($templatePath)) { + $templatePath = self::PAYMENT_METHOD_TEMPLATE_DIR . '/methods/main.tpl'; + } + + // Fetch the additional description from the template + try { + $additionalDescription = $this->templateManager->fetch('file:' . $templatePath); + } catch (Exception $e) { + // No need to handle this exception, the additional description is simply left null + } + + return (string) $additionalDescription; + } +} \ No newline at end of file diff --git a/Components/Services/PaymentService.php b/Components/Services/PaymentService.php index a155b5f7..1382a215 100644 --- a/Components/Services/PaymentService.php +++ b/Components/Services/PaymentService.php @@ -181,6 +181,17 @@ public function startTransaction( } } + try { + // Store payment method in the transaction + $transaction->setPaymentMethod($paymentMethod); + + // Set shipped to false + $transaction->setIsShipped(false); + } catch (\Exception $e) { + // + } + + // Store transaction $transactionRepo->save($transaction); if ((string) $errorMessage !== '') { diff --git a/Controllers/Frontend/Mollie.php b/Controllers/Frontend/Mollie.php index 05faf91a..5a687f28 100644 --- a/Controllers/Frontend/Mollie.php +++ b/Controllers/Frontend/Mollie.php @@ -55,6 +55,9 @@ public function directAction() /** @var \MollieShopware\Components\Config $config */ $config = $this->container->get('mollie_shopware.config'); + /** @var int $paymentId */ + $paymentId = $this->getPaymentId(); + /** * Create an instance of the PaymentService. The PaymentService is used * to handle transactions. @@ -158,6 +161,8 @@ public function directAction() */ public function returnAction() { + $transaction = null; + /** @var string $transactionNumber */ $transactionNumber = $this->Request()->getParam('transactionNumber'); @@ -180,14 +185,7 @@ public function returnAction() try { /** @var \Shopware\Models\Order\Order $order */ $order = $this->getOrder(); - - if ($order === null || !$order instanceof \Shopware\Models\Order\Order) { - if (!empty($transactionNumber)) { - $order = $this->getOrderFromTransaction($transactionNumber); - } - } - } - catch (\Exception $e) { + } catch (\Exception $e) { Logger::log( 'error', $e->getMessage(), @@ -195,6 +193,13 @@ public function returnAction() ); } + if ( + $transactionNumber !== '' + && ($order === null || !$order instanceof \Shopware\Models\Order\Order) + ) { + $order = $this->getOrderFromTransaction($transactionNumber); + } + if ($order === null) { return $this->redirectBack('Payment failed'); } @@ -208,10 +213,14 @@ public function returnAction() $this->view->assign('orderNumber', $order->getNumber()); // Update the transaction ID - $this->updateTransactionId($order); + $this->updateTransactionId($order, $transaction); } - if ($transaction !== null && (string) $transaction->getOrderNumber() !== '') { + if ( + $transaction !== null + && (string) $transaction->getOrderNumber() !== '' + && (string) $transaction->getMolliePaymentId() === '') + { $result = $this->processOrderReturn($order, $paymentService); if ($result !== false) { @@ -219,7 +228,10 @@ public function returnAction() } } - if ($transaction !== null && (string) $transaction->getMolliePaymentId() !== '') { + if ( + $transaction !== null + && (string) $transaction->getMolliePaymentId() !== '' + ) { $result = $this->processPaymentReturn($order, $paymentService); if ($result !== false) { @@ -230,7 +242,7 @@ public function returnAction() // something went wrong because nothing is returned until now Logger::log( 'error', - 'The order couldn\'t be retrieved.', + 'Return action: The order couldn\'t be retrieved.', null ); @@ -254,19 +266,21 @@ public function notifyAction() try { $order = $this->getOrder(); - - if (empty($order) || $order == false || !$order instanceof \Shopware\Models\Order\Order) { - if (!empty($transactionNumber)) { - $order = $this->getOrderFromTransaction($transactionNumber); - } - } } - catch(\Exception $ex) { + catch(\Exception $e) { Notifier::notifyException( - $ex->getMessage() + $e->getMessage(), + $e ); } + if ( + $transactionNumber !== '' + && ($order === null || !$order instanceof \Shopware\Models\Order\Order) + ) { + $order = $this->getOrderFromTransaction($transactionNumber); + } + // check the payment status for the order and notify the user. try { $result = null; @@ -296,12 +310,15 @@ public function notifyAction() ); } } else { - return $this->redirectBack('Payment failed'); + Notifier::notifyException( + 'Order not found' + ); } } - catch (\Exception $ex) { + catch (\Exception $e) { Notifier::notifyException( - $ex->getMessage() + $e->getMessage(), + $e ); } } @@ -460,27 +477,25 @@ private function prepareTransaction(\MollieShopware\Models\Transaction $transact return $transaction; } - private function updateTransactionId(Order $order) + private function updateTransactionId(Order $order, $transaction = null) { - // Variables - $transaction = null; - - /** - * Get the transaction from the TransactionRepository, or log an error - * when the transaction can't be retrieved. - */ - try { - /** @var \MollieShopware\Models\Transaction $transaction */ - $transaction = $this->getTransactionRepository()->findOneBy([ - 'transactionId' => $order->getTransactionId() - ]); - } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); + if ($transaction === null) { + /** + * Get the transaction from the TransactionRepository, or log an error + * when the transaction can't be retrieved. + */ + try { + /** @var \MollieShopware\Models\Transaction $transaction */ + $transaction = $this->getTransactionRepository()->findOneBy([ + 'transactionId' => $order->getTransactionId() + ]); + } catch (\Exception $ex) { + Logger::log( + 'error', + $ex->getMessage(), + $ex + ); + } } if ($transaction !== null) { @@ -488,6 +503,30 @@ private function updateTransactionId(Order $order) if ((string) $transaction->getMolliePaymentId() !== '') { $order->setTransactionId($transaction->getMolliePaymentId()); + + $config = $this->getConfig(); + + if ( + $config !== null + && $config->getTransactionNumberType() === $config::TRANSACTION_NUMBER_TYPE_PAYMENT_METHOD + ) { + $mollieApi = $this->getMollieApi(); + + if ($mollieApi !== null) { + $molliePayment = $mollieApi->payments->get($transaction->getMolliePaymentId()); + $transactionNumber = $transaction->getMolliePaymentId(); + + if (isset($molliePayment->details->paypalReference)) { + $transactionNumber = $molliePayment->details->paypalReference; + } + + if (isset($molliePayment->details->transferReference)) { + $transactionNumber = $molliePayment->details->transferReference; + } + + $order->setTransactionId($transactionNumber); + } + } } if ((string) $transaction->getMollieId() !== '') { @@ -522,23 +561,15 @@ private function getOrder() $order = null; $orderNumber = $this->Request()->getParam('orderNumber'); - /** - * Get the order from the OrderRepository, or log an error - * when the order can't be retrieved. - */ - try { + if ( + (string) $orderNumber !== '' + && $this->getOrderRepository() !== null + ) { /** @var \Shopware\Models\Order\Order $order */ $order = $this->getOrderRepository()->findOneBy([ 'number' => $orderNumber, ]); } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); - } return $order; } @@ -546,162 +577,173 @@ private function getOrder() private function getOrderFromTransaction($transactionNumber) { $order = null; + $transaction = null; + $transactionRepo = null; - try { - /** @var \MollieShopware\Components\Services\OrderService $orderService */ - $orderService = $this->container - ->get('mollie_shopware.order_service'); + /** @var \Shopware\Components\Model\ModelManager $modelManager */ + $modelManager = $this->container + ->get('models'); - /** @var \MollieShopware\Components\Services\PaymentService $paymentService */ - $paymentService = $this->container->get('mollie_shopware.payment_service'); + /** @var \MollieShopware\Components\Services\OrderService $orderService */ + $orderService = $this->container + ->get('mollie_shopware.order_service'); + /** @var \MollieShopware\Components\Services\PaymentService $paymentService */ + $paymentService = $this->container + ->get('mollie_shopware.payment_service'); + + if ($modelManager !== null) { /** @var \MollieShopware\Models\TransactionRepository $transactionRepo */ - $transactionRepo = Shopware()->Models()->getRepository( + $transactionRepo = $modelManager->getRepository( \MollieShopware\Models\Transaction::class ); + } + if ($transactionRepo !== null) { /** @var \MollieShopware\Models\Transaction $transaction */ $transaction = $transactionRepo->find($transactionNumber); + } - if (!empty($transaction)) { - // check whether the payment was canceled - if ($this->getConfig() !== null && - $this->getOrderCanceledOrFailed($transaction) === true) { + if ($transaction !== null) { - if ($this->getConfig()->createOrderBeforePayment() === false) { - return null; - } + // check whether the payment was canceled + if ($this->getConfig() !== null && + $this->getOrderCanceledOrFailed($transaction) === true) { + + if ($this->getConfig()->createOrderBeforePayment() === false) { + return null; + } + + if ($this->getConfig()->createOrderBeforePayment() === true) { + if ($transaction->getOrderId() > 0) { + $modules = Shopware()->Modules(); - if ($this->getConfig()->createOrderBeforePayment() === true) { - if ($transaction->getOrderId() > 0) { - try { - // Cancel payment - Shopware()->Modules()->Order()->setPaymentStatus( + if ($modules !== null) { + // Cancel payment + $modules->Order()->setPaymentStatus( + $transaction->getOrderId(), + Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED + ); + + if ($this->getConfig()->cancelFailedOrders() === true) { + // Cancel order + $modules->Order()->setOrderStatus( $transaction->getOrderId(), - Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED + Status::ORDER_STATE_CANCELLED_REJECTED ); - if ($this->getConfig()->cancelFailedOrders() === true) { - // Cancel order - Shopware()->Modules()->Order()->setOrderStatus( - $transaction->getOrderId(), - Status::ORDER_STATE_CANCELLED_REJECTED - ); - - if ($paymentService !== null) { + if ($paymentService !== null) { + try { $paymentService->resetStock( $orderService->getOrderById($transaction->getOrderId()) ); + } catch (\Exception $e) { + // } } - } catch (\Exception $e) { - // - } - - try { - // Restore order - $this->retryOrderRestore( - $orderService->getOrderById($transaction->getOrderId()) - ); - } catch (\Exception $e) { - // } } - return null; + try { + // Restore order + $this->retryOrderRestore( + $orderService->getOrderById($transaction->getOrderId()) + ); + } catch (Exception $e) { + // + } } + + return null; } + } - // get the order number - $orderNumber = $transaction->getOrderNumber(); + // get the order number + $orderNumber = $transaction->getOrderNumber(); - // get the transaction ID - $transactionId = $transaction->getMolliePaymentId(); + // get the transaction ID + $transactionId = $transaction->getMolliePaymentId(); - if (empty($transactionId)) - $transactionId = $transaction->getMollieId(); + if (empty($transactionId)) + $transactionId = $transaction->getMollieId(); - if (empty($transactionId)) - $transactionId = $transaction->getTransactionId(); + if (empty($transactionId)) + $transactionId = $transaction->getTransactionId(); - $createOrder = false; + $createOrder = false; - /** @var \Mollie\Api\MollieApiClient $mollieApi */ - $mollieApi = $this->container->get('mollie_shopware.api'); + /** @var \Mollie\Api\MollieApiClient $mollieApi */ + $mollieApi = $this->container->get('mollie_shopware.api'); - if ($mollieApi !== null) { - if ((string)$transaction->getMollieId() !== '') { - /** @var \Mollie\Api\Resources\Order $mollieOrder */ - $mollieOrder = $mollieApi->orders->get($transaction->getMollieId()); + if ($mollieApi !== null) { + if ((string) $transaction->getMollieId() !== '') { + /** @var \Mollie\Api\Resources\Order $mollieOrder */ + $mollieOrder = $mollieApi->orders->get($transaction->getMollieId()); - if ($mollieOrder !== null && - $mollieOrder->isCanceled() === false) { - $createOrder = true; - } + if ($mollieOrder !== null && + $mollieOrder->isCanceled() === false) { + $createOrder = true; } + } - if ((string)$transaction->getMolliePaymentId() !== '') { - /** @var \Mollie\Api\Resources\Payment $molliePayment */ - $molliePayment = $mollieApi->payments->get($transaction->getMolliePaymentId()); + if ((string)$transaction->getMolliePaymentId() !== '') { + /** @var \Mollie\Api\Resources\Payment $molliePayment */ + $molliePayment = $mollieApi->payments->get($transaction->getMolliePaymentId()); - if ($molliePayment !== null && - $molliePayment->isCanceled() === false && - $molliePayment->isFailed() === false) { - $createOrder = true; - } + if ($molliePayment !== null && + $molliePayment->isCanceled() === false && + $molliePayment->isFailed() === false) { + $createOrder = true; } } + } - // if order doesn't exist, save the order and retrieve an order number - if (empty($orderNumber) && $createOrder === true) { - $sendStatusMail = false; + // if order doesn't exist, save the order and retrieve an order number + if (empty($orderNumber) && $createOrder === true) { + $sendStatusMail = false; - if ($this->getConfig() !== null) { - $sendStatusMail = $this->getConfig()->sendStatusMail(); - } + if ($this->getConfig() !== null) { + $sendStatusMail = $this->getConfig()->sendStatusMail(); + } - $orderNumber = $this->saveOrder( - $transactionId, - $transaction->getBasketSignature(), - Status::PAYMENT_STATE_OPEN, - $sendStatusMail - ); + $orderNumber = $this->saveOrder( + $transactionId, + $transaction->getBasketSignature(), + Status::PAYMENT_STATE_OPEN, + $sendStatusMail + ); - // update the order number at Mollie - $this->updateMollieOrderNumber($transaction, $orderNumber); - } + // update the order number at Mollie + $this->updateMollieOrderNumber($transaction, $orderNumber); + } + try { /** @var \Shopware\Models\Order\Order $order */ $order = $orderService->getOrderByNumber($orderNumber); + } catch (\Exception $e) { + // + } - if (!empty($order)) { - $this->updateTransactionId($order); - - /** @var \Shopware\Components\Model\ModelManager $modelManager */ - $modelManager = Shopware()->Models(); + if ($order !== null) { + $this->updateTransactionId($order, $transaction); - if ($modelManager !== null) { - // Store order number and ID on transaction - $transaction->setOrderNumber($order->getNumber()); - $transaction->setOrderId($order->getId()); + /** @var \Shopware\Components\Model\ModelManager $modelManager */ + $modelManager = Shopware()->Models(); - try { - $modelManager->persist($transaction); - $modelManager->flush($transaction); - } catch (\Exception $e) { - // - } + if ($modelManager !== null) { + // Store order number and ID on transaction + $transaction->setOrderNumber($order->getNumber()); + $transaction->setOrderId($order->getId()); + + try { + $modelManager->persist($transaction); + $modelManager->flush($transaction); + } catch (\Exception $e) { + // } } } } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); - } return $order; } @@ -855,7 +897,7 @@ private function updateMollieOrderNumber( $orderNumber ) { - if (strlen($transaction->getMollieId())) { + if ((string) $transaction->getMollieId() !== null) { /** @var \Mollie\Api\MollieApiClient $mollieApi */ $mollieApi = $this->container->get('mollie_shopware.api'); @@ -864,7 +906,7 @@ private function updateMollieOrderNumber( $mollieOrder = $mollieApi->orders->get($transaction->getMollieId()); // set the new order number - $mollieOrder->orderNumber = $orderNumber; + $mollieOrder->orderNumber = (string) $orderNumber; // store the new order number $mollieOrder->update(); @@ -900,7 +942,7 @@ private function processOrderReturn( } catch (\Exception $e) { Logger::log( 'error', - 'The order couldn\'t be retrieved.', + 'Process order return: The order couldn\'t be retrieved.', $e ); } @@ -997,7 +1039,7 @@ private function processPaymentReturn($order, $paymentService) catch (\Exception $e) { Logger::log( 'error', - 'The payment couldn\'t be retrieved.', + 'Process payment return: The payment couldn\'t be retrieved.', $e ); } diff --git a/Models/Transaction.php b/Models/Transaction.php index d20ccc4c..0024ea2c 100644 --- a/Models/Transaction.php +++ b/Models/Transaction.php @@ -134,6 +134,20 @@ class Transaction */ private $totalAmount; + /** + * @return string + * + * @ORM\Column(name="payment_method", type="string", nullable=true) + */ + private $paymentMethod; + + /** + * @return bool + * + * @ORM\Column(name="is_shipped", type="boolean", nullable=true) + */ + private $isShipped; + public function getId() { return $this->id; @@ -367,4 +381,40 @@ public function setTotalAmount($totalAmount) $this->totalAmount = $totalAmount; return $this; } + + /** + * @return string + */ + public function getPaymentMethod() + { + return (string) $this->paymentMethod; + } + + /** + * @param $paymentMethod + * @return $this + */ + public function setPaymentMethod($paymentMethod) + { + $this->paymentMethod = $paymentMethod; + return $this; + } + + /** + * @return bool + */ + public function getIsShipped() + { + return (bool) $this->isShipped; + } + + /** + * @param $isShipped + * @return Transaction + */ + public function setIsShipped($isShipped) + { + $this->isShipped = $isShipped; + return $this; + } } \ No newline at end of file diff --git a/MollieShopware.php b/MollieShopware.php index f99416bb..2a0ad77a 100644 --- a/MollieShopware.php +++ b/MollieShopware.php @@ -2,15 +2,19 @@ namespace MollieShopware; +use Enlight_Template_Manager; +use Exception; use Mollie\Api\MollieApiClient; +use MollieShopware\Components\Config; +use MollieShopware\Components\MollieApiFactory; +use MollieShopware\Components\Services\PaymentMethodService; use MollieShopware\Models\TransactionItem; use MollieShopware\Models\Transaction; use MollieShopware\Models\OrderLines; use MollieShopware\Components\Schema; use MollieShopware\Components\Attributes; -use MollieShopware\Components\Config; -use MollieShopware\Components\MollieApiFactory; use MollieShopware\Components\Logger; +use Shopware\Components\Model\ModelManager; use Shopware\Components\Plugin; use Shopware\Components\Plugin\Context\ActivateContext; use Shopware\Components\Plugin\Context\DeactivateContext; @@ -20,9 +24,6 @@ class MollieShopware extends Plugin { - /** @var \MollieShopware\Components\Config */ - protected $config; - /** * Return Shopware events subscribed to */ @@ -61,8 +62,8 @@ public function requireDependencies() } // Load client - if (file_exists($this->getPath() . '/Client/src/MollieApiClient.php')) { - require_once $this->getPath() . '/Client/src/MollieApiClient.php'; + if (file_exists($this->getPath() . '/Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php')) { + require_once $this->getPath() . '/Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php'; } } @@ -175,7 +176,14 @@ public function uninstall(UninstallContext $context) { // Don't remove payment methods but set them to inactive. // So orders paid still reference an existing payment method - $this->deactivatePayments(); + + /** @var PaymentMethodService $paymentMethodService */ + $paymentMethodService = $this->getPaymentMethodService(); + + if ($paymentMethodService !== null) { + // Deactivate all Mollie payment methods + $paymentMethodService->deactivatePaymentMethods(); + } // remove extra attributes $this->removeAttributes(); @@ -188,7 +196,13 @@ public function uninstall(UninstallContext $context) */ public function deactivate(DeactivateContext $context) { - $this->deactivatePayments(); + /** @var PaymentMethodService $paymentMethodService */ + $paymentMethodService = $this->getPaymentMethodService(); + + if ($paymentMethodService !== null) { + // Deactivate all Mollie payment methods + $paymentMethodService->deactivatePaymentMethods(); + } parent::deactivate($context); } @@ -204,136 +218,26 @@ public function activate(ActivateContext $context) // update db tables $this->updateDbTables(); - // first set all payment methods to inactive - // $this->setActiveFlag($context->getPlugin()->getPayments(), false); - $this->deactivatePayments(); - - /** @var \Shopware\Components\Plugin\PaymentInstaller $installer */ - $installer = $this->container->get('shopware.plugin_payment_installer'); - - try { - $paymentOptions = $this->getPaymentOptions(); - } catch (\Exception $e) { - throw $e; - } - - foreach ($paymentOptions as $key => $options) { - $installer->createOrUpdate($context->getPlugin(), $options); - } - - parent::activate($context); - } - - /** - * Deactivate all Mollie payment methods - */ - protected function deactivatePayments() - { - $em = $this->container->get('models'); - - $qb = $em->createQueryBuilder(); + /** @var null|array $methods */ + $methods = null; - $query = $qb->update('Shopware\Models\Payment\Payment', 'p') - ->set('p.active', '?1') - ->where($qb->expr()->like('p.name', '?2')) - ->setParameter(1, false) - ->setParameter(2, 'mollie_%') - ->getQuery(); + /** @var PaymentMethodService $paymentMethodService */ + $paymentMethodService = $this->getPaymentMethodService(); - $query->execute(); - } - - /** - * Get the current payment methods via the Mollie API - * @return array[] $options - * - * @throws \Exception - */ - protected function getPaymentOptions() - { - $mollie = $this->getMollieClient(); - - // TODO: get methods in the correct locale (de_DE en_US es_ES fr_FR nl_BE fr_BE nl_NL) - $methods = $mollie->methods->allActive([ - 'resource' => 'orders', - 'includeWallets' => 'applepay' - ]); - - $options = []; - $position = 0; - - // path to template dir for extra payment-mean options - $paymentTemplateDir = __DIR__ . '/Resources/views/frontend/plugins/payment'; - - /** @var \Enlight_Template_Manager $templateManager */ - $templateManager = $this->container->get('template'); - $templateManager->addTemplateDir(__DIR__ . '/Resources/views'); - - foreach ($methods as $key => $method) { - $name = 'mollie_' . $method->id; + if ($paymentMethodService !== null) { + // Deactivate all Mollie payment methods + $paymentMethodService->deactivatePaymentMethods(); - $templateManager->assign('method', $method); - $templateManager->assign('router', Shopware()->Router()); - - // template path - $adTemplate = $paymentTemplateDir . '/methods/' . strtolower($method->id) . '.tpl'; - - // set default template if no specific template exists - if (!file_exists($adTemplate)) { - $adTemplate = $paymentTemplateDir . '/methods/main.tpl'; - } - - $additionalDescription = $templateManager->fetch('file:' . $adTemplate); - - $option = [ - 'name' => $name, - 'description' => $method->description, - 'action' => 'frontend/Mollie', - 'active' => 1, - 'position' => $position, - 'additionalDescription' => $additionalDescription - ]; - - // check template exist - if (file_exists($paymentTemplateDir . '/' . $name . '.tpl')) { - $option['template'] = $name . '.tpl'; - } - - $options[] = $option; + // Get all active payment methods from Mollie + $methods = $paymentMethodService->getPaymentMethodsFromMollie(); } - return $options; - } - - /** - * @return \Mollie\Api\MollieApiClient - */ - protected function getMollieClient() - { - // Variables - $client = null; - - // Require dependencies - $this->requireDependencies(); - - /** @var Plugin\ConfigReader $configReader */ - $configReader = $this->container - ->get('shopware.plugin.cached_config_reader'); - - /** @var Config $config */ - $config = new Config($configReader); - - /** @var MollieApiFactory $factory */ - $factory = new MollieApiFactory($config); - - /** @var MollieApiClient $client */ - try { - $client = $factory->create(); - } catch (\Exception $e) { - // + // Install the payment methods from Mollie + if ($methods !== null) { + $paymentMethodService->installPaymentMethod($context->getPlugin()->getName(), $methods); } - return $client; + parent::activate($context); } /** @@ -349,7 +253,7 @@ protected function updateDbTables() OrderLines::class ]); } - catch (\Exception $ex) { + catch (Exception $ex) { Logger::log( 'error', $ex->getMessage(), @@ -369,7 +273,7 @@ protected function removeDBTables() $schema->remove(TransactionItem::class); $schema->remove(OrderLines::class); } - catch (\Exception $ex) { + catch (Exception $ex) { Logger::log( 'error', $ex->getMessage(), @@ -397,14 +301,14 @@ protected function updateAttributes() try { $this->makeAttributes()->create([['s_user_attributes', 'mollie_shopware_ideal_issuer', 'string', []]]); } - catch (\Exception $ex) { + catch (Exception $ex) { // } try { $this->makeAttributes()->create([['s_user_attributes', 'mollie_shopware_credit_card_token', 'string', []]]); } - catch (\Exception $ex) { + catch (Exception $ex) { // } } @@ -417,14 +321,14 @@ protected function removeAttributes() try { $this->makeAttributes()->remove([['s_user_attributes', 'mollie_shopware_ideal_issuer']]); } - catch (\Exception $ex) { + catch (Exception $ex) { // } try { $this->makeAttributes()->remove([['s_user_attributes', 'mollie_shopware_credit_card_token']]); } - catch (\Exception $ex) { + catch (Exception $ex) { // } } @@ -435,7 +339,7 @@ protected function removeAttributes() * @param \Shopware\Models\Plugin\Plugin $plugin * @param $key * @param $value - * @throws \Exception + * @throws Exception */ protected function writeConfig(\Shopware\Models\Plugin\Plugin $plugin, $key, $value) { @@ -458,8 +362,89 @@ protected function writeConfig(\Shopware\Models\Plugin\Plugin $plugin, $key, $va ); } } - catch (\Exception $ex) { + catch (Exception $ex) { // } } + + /** + * Returns an instance of the Mollie API client. + * + * @return MollieApiClient + */ + protected function getMollieApiClient() + { + /** @var Config $config */ + $config = null; + + /** @var Plugin\ConfigReader $configReader */ + $configReader = $this->container->get('shopware.plugin.cached_config_reader'); + + /** @var MollieApiFactory $factory */ + $factory = null; + + /** @var MollieApiClient $mollieApiClient */ + $mollieApiClient = null; + + // Get the config + if ($configReader !== null) { + $config = new Config($configReader); + } + + // Get the Mollie API factory service + if ($config !== null) { + $factory = new MollieApiFactory($config); + } + + // Create the Mollie API client + if ($factory !== null) { + try { + $mollieApiClient = $factory->create(); + } catch (Exception $e) { + // + } + } + + return $mollieApiClient; + } + + /** + * Returns an instance of the payment method service. + * + * @return PaymentMethodService + */ + protected function getPaymentMethodService() + { + /** @var ModelManager $modelManager */ + $modelManager = $this->container->get('models'); + + /** @var MollieApiClient $mollieApiClient */ + $mollieApiClient = $this->getMollieApiClient(); + + /** @var Plugin\PaymentInstaller $paymentInstaller */ + $paymentInstaller = $this->container->get('shopware.plugin_payment_installer'); + + /** @var PaymentMethodService $paymentMethodService */ + $paymentMethodService = null; + + /** @var Enlight_Template_Manager $templateManager */ + $templateManager = $this->container->get('template'); + + // Create an instance of the payment method service + if ( + $modelManager !== null + && $mollieApiClient !== null + && $paymentInstaller !== null + && $templateManager !== null + ) { + $paymentMethodService = new PaymentMethodService( + $modelManager, + $mollieApiClient, + $paymentInstaller, + $templateManager + ); + } + + return $paymentMethodService; + } } diff --git a/Resources/config.xml b/Resources/config.xml index fb76dd3a..1f9a3aeb 100644 --- a/Resources/config.xml +++ b/Resources/config.xml @@ -125,14 +125,44 @@ + + klarna_ship_on_status + + + + Shopware.apps.Base.store.OrderStatus + + klarna_shipped_status - - - + + + Shopware.apps.Base.store.OrderStatus + + transaction_number_type + + + + mollie + + + + + + orders_api_update_order_status @@ -243,22 +273,5 @@ true - - diff --git a/Resources/services.xml b/Resources/services.xml index dd9bcb7d..38a1e7da 100644 --- a/Resources/services.xml +++ b/Resources/services.xml @@ -6,12 +6,26 @@ + + + + + + + %shopware.custom% + + + + + + + diff --git a/Resources/views/frontend/_public/src/less/checkout.less b/Resources/views/frontend/_public/src/less/checkout.less new file mode 100644 index 00000000..6c7a13b5 --- /dev/null +++ b/Resources/views/frontend/_public/src/less/checkout.less @@ -0,0 +1,4 @@ +img.mollie-payment-icon { + float: right; + .unitize(margin-top, -25); +} \ No newline at end of file diff --git a/Resources/views/frontend/plugins/payment/mollie_creditcard.tpl b/Resources/views/frontend/plugins/payment/mollie_creditcard.tpl index 70272859..9552c9ca 100644 --- a/Resources/views/frontend/plugins/payment/mollie_creditcard.tpl +++ b/Resources/views/frontend/plugins/payment/mollie_creditcard.tpl @@ -1,11 +1,12 @@ {namespace name='frontend/plugins/payment/mollie_creditcard'} {if $payment_mean.name|lower === 'mollie_creditcard' && $payment_mean.id eq $sFormData.payment && $sMollieEnableComponent} +
- {$sPayment}{s namespace="frontend/mollie/plugins" name="CreditCardHeadLine"}Enter your card information{/s} + {s namespace="frontend/mollie/plugins" name="CreditCardHeadLine"}Enter your card information{/s}
diff --git a/Subscriber/IdealIssuersSubscriber.php b/Subscriber/IdealIssuersSubscriber.php index b88e24e3..8e6fbdc3 100644 --- a/Subscriber/IdealIssuersSubscriber.php +++ b/Subscriber/IdealIssuersSubscriber.php @@ -47,6 +47,8 @@ public function onChoosePaymentDispatch(\Enlight_Event_EventArgs $args) $view->assign('mollieIssues', true); } + $this->updateIssuer(); + $view->addTemplateDir(__DIR__ . '/../Resources/views'); } @@ -62,12 +64,23 @@ public function onUpdatePaymentForUser(\Enlight_Event_EventArgs $args) // get query $query = $args->getReturn(); - // get issuer - $issuer = Shopware()->Front()->Request()->getPost('mollie-ideal-issuer'); - - // write issuer id to database - $this->idealService->setSelectedIssuer($issuer); + $this->updateIssuer(); return $query; } + + private function updateIssuer() + { + try { + // get issuer + $issuer = Shopware()->Front()->Request()->getPost('mollie-ideal-issuer'); + + // write issuer id to database + if ((string)$issuer !== '') { + $this->idealService->setSelectedIssuer($issuer); + } + } catch (\Exception $e) { + // + } + } } diff --git a/Subscriber/OrderBackendSubscriber.php b/Subscriber/OrderBackendSubscriber.php index 887f0684..46af5b6d 100644 --- a/Subscriber/OrderBackendSubscriber.php +++ b/Subscriber/OrderBackendSubscriber.php @@ -3,6 +3,8 @@ namespace MollieShopware\Subscriber; use Enlight\Event\SubscriberInterface; +use MollieShopware\Components\Config; +use MollieShopware\Components\Helpers\LogHelper; use MollieShopware\Components\Logger; class OrderBackendSubscriber implements SubscriberInterface @@ -56,9 +58,8 @@ public function onSendMail(\Enlight_Event_EventArgs $args) try { $transaction->setOrdermailVariables(json_encode($variables)); $transactionRepo->save($transaction); - } - catch (\Exception $ex) { - Logger::log('error', $ex->getMessage(), $ex); + } catch (\Exception $e) { + LogHelper::logMessage($e->getMessage(), LogHelper::LOG_ERROR, $e); } return false; @@ -72,16 +73,19 @@ public function onOrderPostDispatch(\Enlight_Controller_ActionEventArgs $args) /** @var \Enlight_Controller_Request_Request $request */ $request = $args->getRequest(); - if ($request == null) + if ($request === null) { return true; + } - if ($request->getActionName() != 'save') + if ($request->getActionName() !== 'save') { return true; + } $orderId = $request->getParam('id'); - if (empty($orderId)) + if (empty($orderId)) { return true; + } $order = null; @@ -92,36 +96,38 @@ public function onOrderPostDispatch(\Enlight_Controller_ActionEventArgs $args) /** @var \Shopware\Models\Order\Order $order */ $order = $orderService->getOrderById($orderId); - } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); + } catch (\Exception $e) { + LogHelper::logMessage($e->getMessage(), LogHelper::LOG_ERROR, $e); } - if (empty($order)) + if ($order === null) { return true; + } $mollieId = null; try { $mollieId = $orderService->getMollieOrderId($order); - } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); + } catch (\Exception $e) { + LogHelper::logMessage($e->getMessage(), LogHelper::LOG_ERROR, $e); } - if (empty($mollieId)) + if (empty($mollieId)) { return true; + } + + /** @var Config $config */ + $config = Shopware()->Container()->get('mollie_shopware.config'); - if ($order->getOrderStatus()->getId() != \Shopware\Models\Order\Status::ORDER_STATE_COMPLETELY_DELIVERED) + $orderStatusId = \Shopware\Models\Order\Status::ORDER_STATE_COMPLETELY_DELIVERED; + + if ($config !== null) { + $orderStatusId = $config->getKlarnaShipOnStatus(); + } + + if ($order->getOrderStatus()->getId() !== $orderStatusId) { return true; + } try { /** @var \MollieShopware\Components\Services\PaymentService $paymentService */ @@ -129,13 +135,8 @@ public function onOrderPostDispatch(\Enlight_Controller_ActionEventArgs $args) ->get('mollie_shopware.payment_service'); $paymentService->sendOrder($mollieId); - } - catch (\Exception $ex) { - Logger::log( - 'error', - $ex->getMessage(), - $ex - ); + } catch (\Exception $e) { + LogHelper::logMessage($e->getMessage(), LogHelper::LOG_ERROR, $e); } } } \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 6fe04cd4..a61a9a5a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -6,14 +6,14 @@ - 1.5.5 + 1.5.6 (c) Mollie B.V. Kiener E-commerce https://www.kiener.nl - Plugin to allow iDeal payments using the Mollie API (1.5.5) - Plugin die iDealbetalingen via de Mollie API doorgeeft (1.5.5) - Plugin um Zahlungen mit der Mollie API zu ermöglichen (1.5.5) + Plugin to allow iDeal payments using the Mollie API (1.5.6) + Plugin die iDealbetalingen via de Mollie API doorgeeft (1.5.6) + Plugin um Zahlungen mit der Mollie API zu ermöglichen (1.5.6) \ No newline at end of file From 18a4ad4081a16df29c412fd9fb86a8d881c114fd Mon Sep 17 00:00:00 2001 From: Reinder Date: Thu, 13 Feb 2020 14:35:46 +0100 Subject: [PATCH 3/3] Fixed a type compare issue --- Controllers/Frontend/Mollie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/Frontend/Mollie.php b/Controllers/Frontend/Mollie.php index 5a687f28..1b06adfd 100644 --- a/Controllers/Frontend/Mollie.php +++ b/Controllers/Frontend/Mollie.php @@ -897,7 +897,7 @@ private function updateMollieOrderNumber( $orderNumber ) { - if ((string) $transaction->getMollieId() !== null) { + if ((string) $transaction->getMollieId() !== '') { /** @var \Mollie\Api\MollieApiClient $mollieApi */ $mollieApi = $this->container->get('mollie_shopware.api');