-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from mollie/1.9.0
1.9.0
- Loading branch information
Showing
15 changed files
with
663 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
/** | ||
* Copyright Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Mollie\Payment\Controller\Adminhtml\Action; | ||
|
||
use Magento\Backend\App\Action; | ||
use Magento\Backend\Model\Session\Quote; | ||
use Magento\Framework\DB\Transaction; | ||
use Magento\Framework\DB\TransactionFactory; | ||
use Magento\Sales\Api\Data\InvoiceInterface; | ||
use Magento\Sales\Api\Data\OrderInterface; | ||
use Magento\Sales\Api\OrderRepositoryInterface; | ||
use Magento\Sales\Model\AdminOrder\Create; | ||
use Magento\Sales\Model\Order; | ||
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; | ||
use Magento\Sales\Model\Order\Invoice; | ||
use Magento\Sales\Model\Service\InvoiceService; | ||
use Mollie\Payment\Config; | ||
use Mollie\Payment\Service\Order\OrderCommentHistory; | ||
|
||
class MarkAsPaid extends Action | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $orderRepository; | ||
|
||
/** | ||
* @var Create | ||
*/ | ||
private $orderCreate; | ||
|
||
/** | ||
* @var Quote | ||
*/ | ||
private $quoteSession; | ||
|
||
/** | ||
* @var InvoiceService | ||
*/ | ||
private $invoiceService; | ||
|
||
/** | ||
* @var TransactionFactory | ||
*/ | ||
private $transactionFactory; | ||
|
||
/** | ||
* @var InvoiceSender | ||
*/ | ||
private $invoiceSender; | ||
|
||
/** | ||
* @var OrderCommentHistory | ||
*/ | ||
private $orderCommentHistory; | ||
|
||
/** | ||
* @var Transaction | ||
*/ | ||
private $transaction; | ||
|
||
public function __construct( | ||
Action\Context $context, | ||
Config $config, | ||
OrderRepositoryInterface $orderRepository, | ||
Create $orderCreate, | ||
Quote $quoteSession, | ||
InvoiceService $invoiceService, | ||
InvoiceSender $invoiceSender, | ||
OrderCommentHistory $orderCommentHistory, | ||
TransactionFactory $transactionFactory | ||
) { | ||
parent::__construct($context); | ||
|
||
$this->config = $config; | ||
$this->orderRepository = $orderRepository; | ||
$this->orderCreate = $orderCreate; | ||
$this->quoteSession = $quoteSession; | ||
$this->invoiceService = $invoiceService; | ||
$this->invoiceSender = $invoiceSender; | ||
$this->transactionFactory = $transactionFactory; | ||
$this->orderCommentHistory = $orderCommentHistory; | ||
} | ||
|
||
/** | ||
* This controller recreates the selected order with the checkmo payment method and marks it as completed. The | ||
* original order is then canceled. | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function execute() | ||
{ | ||
$orderId = $this->getRequest()->getParam('order_id'); | ||
|
||
$originalOrder = $this->orderRepository->get($orderId); | ||
$originalOrder->setReordered(true); | ||
|
||
$resultRedirect = $this->resultRedirectFactory->create(); | ||
try { | ||
$this->transaction = $this->transactionFactory->create(); | ||
|
||
$order = $this->recreateOrder($originalOrder); | ||
$invoice = $this->createInvoiceFor($order); | ||
$this->cancelOriginalOrder($originalOrder); | ||
$this->transaction->save(); | ||
|
||
$this->addCommentHistoryOriginalOrder($originalOrder, $order->getIncrementId()); | ||
$this->sendInvoice($invoice, $order); | ||
|
||
$this->messageManager->addSuccessMessage( | ||
__( | ||
'We cancelled order %1, created this order and marked it as complete.', | ||
$originalOrder->getIncrementId() | ||
) | ||
); | ||
|
||
return $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getEntityId()]); | ||
} catch (\Exception $exception) { | ||
$this->messageManager->addExceptionMessage($exception); | ||
|
||
return $resultRedirect->setPath('sales/order/view', ['order_id' => $originalOrder->getEntityId()]); | ||
} | ||
} | ||
|
||
/** | ||
* @param OrderInterface $originalOrder | ||
* @return OrderInterface | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
private function recreateOrder(OrderInterface $originalOrder) | ||
{ | ||
$this->quoteSession->setOrderId($originalOrder->getEntityId()); | ||
$this->quoteSession->setUseOldShippingMethod(true); | ||
$this->orderCreate->initFromOrder($originalOrder); | ||
$this->orderCreate->setPaymentMethod('mollie_methods_reorder'); | ||
|
||
$order = $this->orderCreate->createOrder(); | ||
$order->setState(Order::STATE_PROCESSING); | ||
$order->setStatus(Order::STATE_PROCESSING); | ||
|
||
$this->transaction->addObject($order); | ||
$this->transaction->addObject($originalOrder); | ||
|
||
return $order; | ||
} | ||
|
||
/** | ||
* @param OrderInterface $originalOrder | ||
*/ | ||
private function cancelOriginalOrder(OrderInterface $originalOrder) | ||
{ | ||
$originalOrder->cancel(); | ||
} | ||
|
||
/** | ||
* @param OrderInterface $originalOrder | ||
* @param string $newIncrementId | ||
*/ | ||
private function addCommentHistoryOriginalOrder(OrderInterface $originalOrder, $newIncrementId) | ||
{ | ||
$comment = __('We created a new order with increment ID: %1', $newIncrementId); | ||
$this->orderCommentHistory->add($originalOrder, $comment, false); | ||
} | ||
|
||
private function createInvoiceFor(OrderInterface $order) | ||
{ | ||
$invoice = $this->invoiceService->prepareInvoice($order); | ||
$invoice->setRequestedCaptureCase(Invoice::CAPTURE_OFFLINE); | ||
$invoice->register(); | ||
|
||
$this->transaction->addObject($invoice); | ||
|
||
return $invoice; | ||
} | ||
|
||
private function sendInvoice(InvoiceInterface $invoice, OrderInterface $order) | ||
{ | ||
/** @var Order\Invoice $invoice */ | ||
if ($invoice->getEmailSent() || !$this->config->sendInvoiceEmail($invoice->getStoreId())) { | ||
return; | ||
} | ||
|
||
try { | ||
$this->invoiceSender->send($invoice); | ||
$message = __('Notified customer about invoice #%1', $invoice->getIncrementId()); | ||
$this->orderCommentHistory->add($order, $message, true); | ||
} catch (\Throwable $exception) { | ||
$message = __('Unable to send the invoice: %1', $exception->getMessage()); | ||
$this->orderCommentHistory->add($order, $message, false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Copyright © 2018 Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Mollie\Payment\Model\Methods; | ||
|
||
use Magento\Framework\Api\AttributeValueFactory; | ||
use Magento\Framework\Api\ExtensionAttributesFactory; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Data\Collection\AbstractDb; | ||
use Magento\Framework\Model\Context; | ||
use Magento\Framework\Model\ResourceModel\AbstractResource; | ||
use Magento\Framework\Registry; | ||
use Magento\Payment\Helper\Data; | ||
use Magento\Payment\Model\Method\AbstractMethod; | ||
use Magento\Payment\Model\Method\Logger; | ||
|
||
/** | ||
* Class Reorder | ||
* | ||
* @package Mollie\Payment\Model\Methods | ||
*/ | ||
class Reorder extends AbstractMethod | ||
{ | ||
/** | ||
* Payment method code | ||
* | ||
* @var string | ||
*/ | ||
protected $_code = 'mollie_methods_reorder'; | ||
|
||
/** | ||
* Availability option | ||
* | ||
* @var bool | ||
*/ | ||
protected $_canUseCheckout = false; | ||
|
||
/** | ||
* Availability option | ||
* | ||
* @var bool | ||
*/ | ||
protected $_canUseInternal = true; | ||
|
||
/** | ||
* Payment Method feature | ||
* | ||
* @var bool | ||
*/ | ||
protected $_canOrder = true; | ||
|
||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
public function __construct( | ||
Context $context, | ||
Registry $registry, | ||
ExtensionAttributesFactory $extensionFactory, | ||
AttributeValueFactory $customAttributeFactory, | ||
Data $paymentData, | ||
ScopeConfigInterface $scopeConfig, | ||
Logger $logger, | ||
RequestInterface $request, | ||
AbstractResource $resource = null, | ||
AbstractDb $resourceCollection = null, | ||
array $data = [] | ||
) { | ||
$this->request = $request; | ||
|
||
parent::__construct( | ||
$context, | ||
$registry, | ||
$extensionFactory, | ||
$customAttributeFactory, | ||
$paymentData, | ||
$scopeConfig, | ||
$logger, | ||
$resource, | ||
$resourceCollection, | ||
$data | ||
); | ||
} | ||
|
||
/** | ||
* @param string $paymentAction | ||
* @param object $stateObject | ||
* | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @throws \Mollie\Api\Exceptions\ApiException | ||
*/ | ||
public function initialize($paymentAction, $stateObject) | ||
{ | ||
/** @var \Magento\Sales\Model\Order\Payment $payment */ | ||
$payment = $this->getInfoInstance(); | ||
|
||
/** @var \Magento\Sales\Model\Order $order */ | ||
$order = $payment->getOrder(); | ||
$order->setCanSendNewEmailFlag(false); | ||
$order->save(); | ||
} | ||
|
||
public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) | ||
{ | ||
return $this->request->getModuleName() == 'mollie' && $this->request->getActionName() == 'markAsPaid'; | ||
} | ||
} |
Oops, something went wrong.