From 4d8593b31663639a82a754d3fab0aaf6ef68d101 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 8 Aug 2018 17:00:19 +0200 Subject: [PATCH 1/3] [bugfix] (MAGE2-85) Avoid sending order mail twice. --- PaymentMethods/HeidelpayAbstractPaymentMethod.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PaymentMethods/HeidelpayAbstractPaymentMethod.php b/PaymentMethods/HeidelpayAbstractPaymentMethod.php index 32fbcbd47ad..496267ce812 100755 --- a/PaymentMethods/HeidelpayAbstractPaymentMethod.php +++ b/PaymentMethods/HeidelpayAbstractPaymentMethod.php @@ -281,6 +281,12 @@ public function activeRedirect() */ public function getConfigData($field, $storeId = null) { + // in order to avoid the order mail to be sent twice, + // once by us and once by the SubmitObserver. + if ($field === 'order_place_redirect_url') { + return 'dummy_redirect_url'; + } + $path = 'payment/' . $this->getCode() . '/' . $field; return $this->_scopeConfig->getValue($path, StoreScopeInterface::SCOPE_STORE, $storeId); From 6e3702ea9b3b1cdb107dd6cef496815fc18075de Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 8 Aug 2018 17:52:50 +0200 Subject: [PATCH 2/3] [cleanup] Fix several code style issues and typos and removed dead code. --- Controller/Index/Index.php | 28 +++++---- Controller/Index/Push.php | 60 +++++++------------ Controller/Index/Redirect.php | 46 +++++++------- Controller/Index/Response.php | 60 +++++++++---------- .../HeidelpayAbstractPaymentMethod.php | 13 ++-- 5 files changed, 94 insertions(+), 113 deletions(-) diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index 17867fe4736..afbb2314469 100755 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -4,6 +4,8 @@ use Heidelpay\Gateway\Helper\Payment as HeidelpayHelper; use Heidelpay\Gateway\PaymentMethods\HeidelpayAbstractPaymentMethod; +use Magento\Framework\App\Action\Context; +use Magento\Framework\Escaper; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; use Magento\Sales\Model\Order\Email\Sender\OrderSender; @@ -16,21 +18,15 @@ * @link http://dev.heidelpay.com/magento2 * @author Jens Richter * - * @package heidelpay - * @subpackage magento2 - * @category magento2 + * @package heidelpay\magento2\controllers */ class Index extends \Heidelpay\Gateway\Controller\HgwAbstract { - protected $resultPageFactory; - protected $logger; - - /** @var \Magento\Framework\Escaper */ - protected $escaper; - + /** @var Escaper */ + private $escaper; public function __construct( - \Magento\Framework\App\Action\Context $context, + Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, @@ -45,7 +41,7 @@ public function __construct( OrderCommentSender $orderCommentSender, \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Customer\Model\Url $customerUrl, - \Magento\Framework\Escaper $escaper + Escaper $escaper ) { parent::__construct( $context, @@ -68,13 +64,19 @@ public function __construct( $this->escaper = $escaper; } + /** + * {@inheritDoc} + * @throws \Heidelpay\PhpBasketApi\Exception\InvalidBasketitemPositionException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Exception + */ public function execute() { $session = $this->getCheckout(); $quote = $session->getQuote(); if (!$quote->getId()) { - $message = __("An unexpected error occurred. Please contact us to get further information."); + $message = __('An unexpected error occurred. Please contact us to get further information.'); $this->messageManager->addErrorMessage($this->escaper->escapeHtml($message)); return $this->_redirect('checkout/cart/', ['_secure' => true]); @@ -87,7 +89,7 @@ public function execute() /** @var \Heidelpay\PhpPaymentApi\Response $response */ $response = $payment->getHeidelpayUrl($quote); - $this->_logger->debug('Heidelpay init respose : ' . print_r($response, 1)); + $this->_logger->debug('Heidelpay init response : ' . print_r($response, 1)); if ($response->isSuccess()) { // redirect to payment url, if it uses redirecting diff --git a/Controller/Index/Push.php b/Controller/Index/Push.php index a15993bc02e..875ac3d0a9c 100755 --- a/Controller/Index/Push.php +++ b/Controller/Index/Push.php @@ -2,8 +2,10 @@ namespace Heidelpay\Gateway\Controller\Index; -use Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory as HeidelpayTransactionCollectionFactory; use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; +use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; +use Magento\Sales\Model\Order\Email\Sender\OrderSender; use Magento\Sales\Model\Order\Payment\Transaction; use Magento\Sales\Model\Order\Invoice; use Magento\Sales\Model\OrderRepository; @@ -21,39 +23,20 @@ * * @author Stephano Vogel * - * @package heidelpay - * @subpackage magento2 - * @category magento2 + * @package heidelpay\magento2\controllers */ class Push extends \Heidelpay\Gateway\Controller\HgwAbstract { - /** - * @var OrderRepository $orderRepository - */ - protected $orderRepository; + /** @var OrderRepository $orderRepository */ + private $orderRepository; - /** - * @var \Magento\Sales\Api\Data\OrderInterface - */ - protected $order; - - /** - * @var \Heidelpay\PhpPaymentApi\Push - */ - protected $heidelpayPush; + /** @var \Heidelpay\PhpPaymentApi\Push */ + private $heidelpayPush; - /** - * @var HeidelpayTransactionCollectionFactory - */ - protected $transactionCollectionFactory; - /** - * @var SearchCriteriaBuilder - */ + /** @var SearchCriteriaBuilder */ private $searchCriteriaBuilder; /** - * Push constructor. - * * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Checkout\Model\Session $checkoutSession @@ -64,15 +47,13 @@ class Push extends \Heidelpay\Gateway\Controller\HgwAbstract * @param \Magento\Quote\Api\CartRepositoryInterface $quoteObject * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory * @param \Heidelpay\Gateway\Helper\Payment $paymentHelper - * @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender - * @param \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender - * @param \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender + * @param OrderSender $orderSender + * @param InvoiceSender $invoiceSender + * @param OrderCommentSender $orderCommentSender * @param \Magento\Framework\Encryption\Encryptor $encryptor * @param \Magento\Customer\Model\Url $customerUrl * @param OrderRepository $orderRepository - * @param \Magento\Sales\Api\Data\OrderInterface $order * @param \Heidelpay\PhpPaymentApi\Push $heidelpayPush - * @param HeidelpayTransactionCollectionFactory $collectionFactory * @param SearchCriteriaBuilder $searchCriteriaBuilder */ public function __construct( @@ -86,15 +67,13 @@ public function __construct( \Magento\Quote\Api\CartRepositoryInterface $quoteObject, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Heidelpay\Gateway\Helper\Payment $paymentHelper, - \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender, - \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, - \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender, + OrderSender $orderSender, + InvoiceSender $invoiceSender, + OrderCommentSender $orderCommentSender, \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Customer\Model\Url $customerUrl, OrderRepository $orderRepository, - \Magento\Sales\Api\Data\OrderInterface $order, \Heidelpay\PhpPaymentApi\Push $heidelpayPush, - HeidelpayTransactionCollectionFactory $collectionFactory, SearchCriteriaBuilder $searchCriteriaBuilder ) { parent::__construct( @@ -116,13 +95,14 @@ public function __construct( ); $this->orderRepository = $orderRepository; - $this->order = $order; - $this->heidelpayPush = $heidelpayPush; - $this->transactionCollectionFactory = $collectionFactory; $this->searchCriteriaBuilder = $searchCriteriaBuilder; } + /** + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void + * @throws \Heidelpay\PhpPaymentApi\Exceptions\XmlResponseParserException + */ public function execute() { /** @var \Magento\Framework\App\Request\Http $request */ @@ -176,7 +156,7 @@ public function execute() $order = $orderList->getFirstItem(); $paidAmount = (float)$this->heidelpayPush->getResponse()->getPresentation()->getAmount(); - $dueLeft = (float)($order->getTotalDue() - $paidAmount); + $dueLeft = $order->getTotalDue() - $paidAmount; $state = Order::STATE_PROCESSING; $comment = 'heidelpay - Purchase Complete'; diff --git a/Controller/Index/Redirect.php b/Controller/Index/Redirect.php index ffe7285584b..890eb516a72 100755 --- a/Controller/Index/Redirect.php +++ b/Controller/Index/Redirect.php @@ -3,6 +3,10 @@ namespace Heidelpay\Gateway\Controller\Index; use Heidelpay\Gateway\Helper\Payment as HeidelpayHelper; +use Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory; +use Heidelpay\PhpPaymentApi\Response; +use Magento\Sales\Helper\Data; +use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; use Magento\Sales\Model\Order\Email\Sender\OrderSender; @@ -20,23 +24,18 @@ * @link http://dev.heidelpay.com/magento2 * @author Jens Richter * - * @package heidelpay - * @subpackage magento2 - * @category magento2 + * @package heidelpay\magento2\controllers */ class Redirect extends \Heidelpay\Gateway\Controller\HgwAbstract { - protected $resultPageFactory; - protected $logger; + /** @var Response The heidelpay response class */ + private $heidelpayResponse; - /** @var \Heidelpay\PhpPaymentApi\Response The heidelpay response class */ - protected $heidelpayResponse; + /** @var CollectionFactory */ + private $transactionCollectionFactory; - /** @var \Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory */ - protected $transactionCollectionFactory; - - /** @var \Magento\Sales\Helper\Data */ - protected $salesHelper; + /** @var Data */ + private $salesHelper; /** * heidelpay Redirect constructor. @@ -51,14 +50,13 @@ class Redirect extends \Heidelpay\Gateway\Controller\HgwAbstract * @param \Magento\Quote\Api\CartRepositoryInterface $quoteObject * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory * @param HeidelpayHelper $paymentHelper - * @param \Magento\Sales\Helper\Data $salesHelper + * @param Data $salesHelper * @param OrderSender $orderSender * @param InvoiceSender $invoiceSender * @param OrderCommentSender $orderCommentSender * @param \Magento\Framework\Encryption\Encryptor $encryptor * @param \Magento\Customer\Model\Url $customerUrl - * @param \Heidelpay\PhpPaymentApi\Response $heidelpayResponse - * @param \Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory $transactionCollectionFactory + * @param CollectionFactory $transactionCollectionFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, @@ -71,14 +69,13 @@ public function __construct( \Magento\Quote\Api\CartRepositoryInterface $quoteObject, \Magento\Framework\View\Result\PageFactory $resultPageFactory, HeidelpayHelper $paymentHelper, - \Magento\Sales\Helper\Data $salesHelper, + Data $salesHelper, OrderSender $orderSender, InvoiceSender $invoiceSender, OrderCommentSender $orderCommentSender, \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Customer\Model\Url $customerUrl, - \Heidelpay\PhpPaymentApi\Response $heidelpayResponse, - \Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory $transactionCollectionFactory + CollectionFactory $transactionCollectionFactory ) { parent::__construct( $context, @@ -98,11 +95,14 @@ public function __construct( $customerUrl ); - $this->heidelpayResponse = $heidelpayResponse; $this->transactionCollectionFactory = $transactionCollectionFactory; $this->salesHelper = $salesHelper; } + /** + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface + * @throws \Heidelpay\MessageCodeMapper\Exceptions\MissingLocaleFileException + */ public function execute() { $session = $this->getCheckout(); @@ -133,20 +133,21 @@ public function execute() // display the customer-friendly message for the customer $this->messageManager->addErrorMessage( - __("An unexpected error occurred. Please contact us to get further information.") + __('An unexpected error occurred. Please contact us to get further information.') ); return $this->_redirect('checkout/cart/', ['_secure' => true]); } // initialize the Response object with data from the transaction. - $this->heidelpayResponse = $this->heidelpayResponse->splitArray($data); + $this->heidelpayResponse = Response::fromPost($data); // set Parameters for success page if ($this->heidelpayResponse->isSuccess()) { // lock the quote $session->getQuote()->setIsActive(false)->save(); + /** @var Order $order */ $order = null; try { @@ -172,10 +173,9 @@ public function execute() } } - $session->clearHelperData(); - // set QouteIds + // set QuoteIds $session->setLastQuoteId($quoteId) ->setLastSuccessQuoteId($quoteId); diff --git a/Controller/Index/Response.php b/Controller/Index/Response.php index 112837853e5..b38c1f21e1e 100755 --- a/Controller/Index/Response.php +++ b/Controller/Index/Response.php @@ -4,7 +4,13 @@ use Heidelpay\Gateway\Helper\Payment as HeidelpayHelper; use Heidelpay\Gateway\Model\ResourceModel\PaymentInformation\CollectionFactory as PaymentInformationCollectionFactory; +use Heidelpay\Gateway\Model\ResourceModel\PaymentInformation\CollectionFactory; +use Heidelpay\Gateway\Model\TransactionFactory; use Heidelpay\PhpPaymentApi\Exceptions\HashVerificationException; +use Heidelpay\PhpPaymentApi\Response as HeidelpayResponse; +use Magento\Quote\Model\Quote; +use Magento\Quote\Model\QuoteRepository; +use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; use Magento\Sales\Model\Order\Email\Sender\OrderSender; @@ -27,29 +33,21 @@ * * @author Jens Richter * - * @package heidelpay - * @subpackage magento2 - * @category magento2 + * @package heidelpay\magento2\controllers */ class Response extends \Heidelpay\Gateway\Controller\HgwAbstract { - protected $resultPageFactory; - protected $logger; + /** @var QuoteRepository */ + private $quoteRepository; - /** @var \Magento\Quote\Model\QuoteRepository */ - protected $quoteRepository; + /** @var HeidelpayResponse The heidelpay response object */ + private $heidelpayResponse; - /** @var \Magento\Framework\Controller\Result\RawFactory */ - protected $resultFactory; + /** @var TransactionFactory */ + private $transactionFactory; - /** @var \Heidelpay\PhpPaymentApi\Response The heidelpay response object */ - protected $heidelpayResponse; - - /** @var \Heidelpay\Gateway\Model\TransactionFactory */ - protected $transactionFactory; - - /** @var \Heidelpay\Gateway\Model\ResourceModel\PaymentInformation\CollectionFactory */ - protected $paymentInformationCollectionFactory; + /** @var CollectionFactory */ + private $paymentInformationCollectionFactory; /** * heidelpay Response constructor. @@ -70,10 +68,9 @@ class Response extends \Heidelpay\Gateway\Controller\HgwAbstract * @param \Magento\Framework\Encryption\Encryptor $encryptor * @param \Magento\Customer\Model\Url $customerUrl * @param \Magento\Framework\Controller\Result\RawFactory $rawResultFactory - * @param \Magento\Quote\Model\QuoteRepository $quoteRepository - * @param \Heidelpay\PhpPaymentApi\Response $heidelpayResponse + * @param QuoteRepository $quoteRepository * @param PaymentInformationCollectionFactory $paymentInformationCollectionFactory, - * @param \Heidelpay\Gateway\Model\TransactionFactory $transactionFactory + * @param TransactionFactory $transactionFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, @@ -92,10 +89,9 @@ public function __construct( \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Customer\Model\Url $customerUrl, \Magento\Framework\Controller\Result\RawFactory $rawResultFactory, - \Magento\Quote\Model\QuoteRepository $quoteRepository, - \Heidelpay\PhpPaymentApi\Response $heidelpayResponse, + QuoteRepository $quoteRepository, PaymentInformationCollectionFactory $paymentInformationCollectionFactory, - \Heidelpay\Gateway\Model\TransactionFactory $transactionFactory + TransactionFactory $transactionFactory ) { parent::__construct( $context, @@ -117,13 +113,13 @@ public function __construct( $this->resultFactory = $rawResultFactory; $this->quoteRepository = $quoteRepository; - $this->heidelpayResponse = $heidelpayResponse; $this->paymentInformationCollectionFactory = $paymentInformationCollectionFactory; $this->transactionFactory = $transactionFactory; } /** * @inheritdoc + * @throws \Exception */ public function execute() { @@ -148,13 +144,13 @@ public function execute() if (!$this->getRequest()->isPost()) { $this->_logger->warning('Heidelpay - Response: Request is not POST.'); - // return the result now, no further processing. + // no further processing. return $result; } // initialize the Response object with data from the request. try { - $this->heidelpayResponse->splitArray($this->getRequest()->getParams()); + $this->heidelpayResponse = HeidelpayResponse::fromPost($this->getRequest()->getParams()); } catch (\Exception $e) { $this->_logger->error( 'Heidelpay - Response: Cannot initialize response object from Post Request. ' . $e->getMessage() @@ -174,11 +170,11 @@ public function execute() try { $this->heidelpayResponse->verifySecurityHash($secret, $identificationTransactionId); } catch (HashVerificationException $e) { - $this->_logger->critical("Heidelpay Response - HashVerification Exception: " . $e->getMessage()); + $this->_logger->critical('Heidelpay Response - HashVerification Exception: ' . $e->getMessage()); $this->_logger->critical( - "Heidelpay Response - Received request form server " + 'Heidelpay Response - Received request form server ' . $this->getRequest()->getServer('REMOTE_ADDR') - . " with an invalid hash. This could be some kind of manipulation." + . ' with an invalid hash. This could be some kind of manipulation.' ); $this->_logger->critical( 'Heidelpay Response - Reference secret hash: ' @@ -193,10 +189,10 @@ public function execute() . print_r($this->heidelpayResponse, true) ); - /** @var \Magento\Sales\Model\Order $order */ + /** @var Order $order */ $order = null; - /** @var \Magento\Quote\Model\Quote $quote */ + /** @var Quote $quote */ $quote = null; $data = $this->getRequest()->getParams(); @@ -247,7 +243,7 @@ public function execute() if ($this->heidelpayResponse->isSuccess()) { try { // get the quote by transactionid from the heidelpay response - /** @var \Magento\Quote\Model\Quote $quote */ + /** @var Quote $quote */ $quote = $this->quoteRepository->get($this->heidelpayResponse->getIdentification()->getTransactionId()); $quote->collectTotals(); diff --git a/PaymentMethods/HeidelpayAbstractPaymentMethod.php b/PaymentMethods/HeidelpayAbstractPaymentMethod.php index 496267ce812..b0a3883bc85 100755 --- a/PaymentMethods/HeidelpayAbstractPaymentMethod.php +++ b/PaymentMethods/HeidelpayAbstractPaymentMethod.php @@ -8,6 +8,7 @@ use Heidelpay\Gateway\Model\ResourceModel\PaymentInformation\CollectionFactory as PaymentInformationCollectionFactory; use Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory as HeidelpayTransactionCollectionFactory; use Heidelpay\PhpPaymentApi\ParameterGroups\BasketParameterGroup; +use Heidelpay\PhpPaymentApi\PaymentMethods\AbstractPaymentMethod; use Heidelpay\PhpPaymentApi\Response; use Magento\Sales\Api\Data\TransactionInterface; use Magento\Sales\Model\Order; @@ -15,6 +16,7 @@ use Magento\Store\Model\ScopeInterface as StoreScopeInterface; use Heidelpay\Gateway\Block\Payment\HgwAbstract; use Heidelpay\PhpPaymentApi\PaymentMethods\PaymentMethodInterface; +use Magento\Store\Model\ScopeInterface; /** * Heidelpay abstract payment method @@ -278,6 +280,7 @@ public function activeRedirect() * @param integer $storeId * * @return string config value + * @throws \Magento\Framework\Exception\LocalizedException */ public function getConfigData($field, $storeId = null) { @@ -407,7 +410,7 @@ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) ); } - // set authentification data + // set authentication data $this->performAuthentication(); // set basket data @@ -568,7 +571,7 @@ public function saveHeidelpayTransaction(Response $response, $paymentMethod, $pa /** * Returns the heidelpay PhpPaymentApi Paymentmethod Instance. * - * @return \Heidelpay\PhpPaymentApi\PaymentMethods\AbstractPaymentMethod + * @return AbstractPaymentMethod|PaymentMethodInterface */ public function getHeidelpayPaymentMethodInstance() { @@ -661,9 +664,9 @@ public function pendingTransactionProcessing($data, &$order, $message = null) } /** - * - * @param array $data + * @param array $data * @param \Magento\Sales\Model\Order $order + * @throws \Magento\Framework\Exception\LocalizedException */ public function processingTransactionProcessing($data, &$order) { @@ -723,7 +726,7 @@ public function getBookingMode($storeId = null) return $this->_scopeConfig->getValue( 'payment/' . $this->getCode() . '/bookingmode', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $store ); } From 4677431916e1df6d8cc043c100d910d9468feee9 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 9 Aug 2018 12:03:16 +0200 Subject: [PATCH 3/3] [change] (MAGE2-85) Update changelog and version --- CHANGELOG.md | 5 +++++ etc/module.xml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae192d58e34..591badd6fde 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ This project does not follow a versioning standard. Versions are crafted after the dates; for example, the version 17.7.25 was released on July, 25th in 2017 +## 18.8.9 + +### Fixed +- An issue which resulted in the order mail being sent twice. + ## 18.6.11 ### Fixed diff --git a/etc/module.xml b/etc/module.xml index 8d8f35086b0..e257f6392e6 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - +