Skip to content

Commit

Permalink
Merge pull request #210 from mollie/1.9.0
Browse files Browse the repository at this point in the history
1.9.0
  • Loading branch information
Marvin-Magmodules authored Dec 3, 2019
2 parents b4b7c3d + 1281430 commit cfe458f
Show file tree
Hide file tree
Showing 15 changed files with 663 additions and 37 deletions.
30 changes: 30 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

class Config
{
const GENERAL_INVOICE_NOTIFY = 'payment/mollie_general/invoice_notify';
const XML_PATH_STATUS_PENDING_BANKTRANSFER = 'payment/mollie_methods_banktransfer/order_status_pending';
const XML_PATH_STATUS_NEW_PAYMENT_LINK = 'payment/mollie_methods_paymentlink/order_status_new';
const PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE = 'payment/mollie_methods_klarnapaylater/payment_surcharge';
const PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE_TAX_CLASS = 'payment/mollie_methods_klarnapaylater/payment_surcharge_tax_class';
const PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE = 'payment/mollie_methods_klarnasliceit/payment_surcharge';
const PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE_TAX_CLASS = 'payment/mollie_methods_klarnasliceit/payment_surcharge_tax_class';
const PAYMENT_PAYMENTLINK_ALLOW_MARK_AS_PAID = 'payment/mollie_methods_paymentlink/allow_mark_as_paid';

/**
* @var ScopeConfigInterface
Expand All @@ -39,6 +41,25 @@ private function getPath($path, $storeId)
return $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
}

/**
* @param $path
* @param $storeId
* @return bool
*/
private function getFlag($path, $storeId)
{
return $this->config->isSetFlag($path, ScopeInterface::SCOPE_STORE, $storeId);
}

/**
* @param null $storeId
* @return bool
*/
public function sendInvoiceEmail($storeId = null)
{
return $this->getFlag(static::GENERAL_INVOICE_NOTIFY, $storeId);
}

public function statusPendingBanktransfer($storeId = null)
{
return $this->config->getValue(
Expand Down Expand Up @@ -92,4 +113,13 @@ public function klarnaSliceitPaymentSurchargeTaxClass($storeId = null)
{
return $this->getPath(static::PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE_TAX_CLASS, $storeId);
}

/**
* @param null $storeId
* @return bool
*/
public function paymentlinkAllowMarkAsPaid($storeId = null)
{
return $this->getFlag(static::PAYMENT_PAYMENTLINK_ALLOW_MARK_AS_PAID, $storeId);
}
}
201 changes: 201 additions & 0 deletions Controller/Adminhtml/Action/MarkAsPaid.php
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);
}
}
}
112 changes: 112 additions & 0 deletions Model/Methods/Reorder.php
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';
}
}
Loading

0 comments on commit cfe458f

Please sign in to comment.