Skip to content

Commit

Permalink
Merge pull request #325 from mollie/1.18.1
Browse files Browse the repository at this point in the history
1.18.1
  • Loading branch information
Marvin-Magmodules authored Nov 20, 2020
2 parents 7ba6aee + 89a31aa commit 514c01b
Show file tree
Hide file tree
Showing 27 changed files with 320 additions and 65 deletions.
3 changes: 3 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public function isSecondChanceEmailEnabled($storeId = null)
*/
public function automaticallySendSecondChanceEmails($storeId = null)
{
if (!$this->isSecondChanceEmailEnabled($storeId)) {
return false;
}
return $this->isSetFlag(static::GENERAL_AUTOMATICALLY_SEND_SECOND_CHANCE_EMAILS, $storeId);
}

Expand Down
4 changes: 3 additions & 1 deletion Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Result\PageFactory;
use Mollie\Payment\Model\Methods\ApplePay;
use Mollie\Payment\Model\Methods\Creditcard;
use Mollie\Payment\Model\Methods\Directdebit;
use Mollie\Payment\Model\Mollie;

Expand Down Expand Up @@ -233,7 +234,8 @@ private function startTransaction(Mollie $methodInstance, OrderInterface $order)
$redirectUrl = $this->_url->getUrl('checkout/onepage/success/');
}

if (!$redirectUrl && $methodInstance instanceof ApplePay) {
$emptyUrlAllowed = $methodInstance instanceof ApplePay || $methodInstance instanceof Creditcard;
if (!$redirectUrl && $emptyUrlAllowed) {
$redirectUrl = $this->_url->getUrl('checkout/onepage/success/');
}

Expand Down
34 changes: 31 additions & 3 deletions Controller/Checkout/SecondChance.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Mollie\Payment\Api\Data\PaymentTokenInterface;
use Mollie\Payment\Api\PaymentTokenRepositoryInterface;
use Mollie\Payment\Model\Methods\Paymentlink;
use Mollie\Payment\Service\Order\Reorder;
use Mollie\Payment\Service\PaymentToken\Generate;

class SecondChance extends Action
{
Expand All @@ -38,18 +41,25 @@ class SecondChance extends Action
*/
private $paymentlink;

/**
* @var Generate
*/
private $generatePaymentToken;

public function __construct(
Context $context,
OrderRepositoryInterface $orderRepository,
PaymentTokenRepositoryInterface $paymentTokenRepository,
Reorder $reorder,
Paymentlink $paymentlink
Paymentlink $paymentlink,
Generate $generatePaymentToken
) {
parent::__construct($context);
$this->orderRepository = $orderRepository;
$this->paymentTokenRepository = $paymentTokenRepository;
$this->reorder = $reorder;
$this->paymentlink = $paymentlink;
$this->generatePaymentToken = $generatePaymentToken;
}

public function execute()
Expand Down Expand Up @@ -88,7 +98,25 @@ private function reorder()
}

$order = $this->reorder->create($order);
$information = $order->getPayment()->getAdditionalInformation();
return $this->_redirect($information['checkout_url']);

$token = $this->getToken($order);
$url = $this->_url->getUrl('mollie/checkout/redirect', ['paymentToken' => $token->getToken()]);

return $this->_redirect($url);
}

/**
* @param OrderInterface $order
* @throws \Magento\Framework\Exception\LocalizedException
* @return PaymentTokenInterface|null
*/
private function getToken(OrderInterface $order): PaymentTokenInterface
{
$token = $this->paymentTokenRepository->getByOrder($order);
if ($token) {
return $token;
}

return $this->generatePaymentToken->forOrder($order);
}
}
30 changes: 24 additions & 6 deletions Controller/Checkout/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,44 @@ public function __construct(
public function execute()
{
if ($this->getRequest()->getParam('testByMollie')) {
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setHeader('content-type', 'text/plain');
$result->setContents('OK', true);
return $result;
return $this->getOkResponse();
}

if ($transactionId = $this->getRequest()->getParam('id')) {
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
try {
if ($orderId = $this->mollieModel->getOrderIdByTransactionId($transactionId)) {
$this->mollieModel->processTransaction($orderId, 'webhook');

return $this->getOkResponse();
}

$result->setData([
'error' => true,
'message' => __('There is no order found that belongs to "%1"', $transactionId)->render(),
]);
$result->setHttpResponseCode(404);

return $result;
} catch (\Exception $e) {
$this->mollieHelper->addTolog('error', $e->getMessage());

$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData('error', true);
$result->setData(['error' => true]);
$result->setHttpResponseCode(503);

return $result;
}
}
}

/**
* @return \Magento\Framework\Controller\ResultInterface|\Magento\Framework\View\Result\Layout
*/
private function getOkResponse()
{
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setHeader('content-type', 'text/plain');
$result->setContents('OK', true);
return $result;
}
}
28 changes: 13 additions & 15 deletions Cron/SendPendingPaymentReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,21 @@ public function execute()
return;
}

do {
/** @var SortOrder $sortOrder */
$sortOrder = $this->sortOrderFactory->create();
$sortOrder->setField('entity_id');
$sortOrder->setDirection(SortOrder::SORT_ASC);
/** @var SortOrder $sortOrder */
$sortOrder = $this->sortOrderFactory->create();
$sortOrder->setField('entity_id');
$sortOrder->setDirection(SortOrder::SORT_ASC);

$delay = $this->config->secondChanceEmailDelay();
$date = (new \DateTimeImmutable($this->dateTime->gmtDate()))->sub(new \DateInterval('PT' . $delay . 'H'));
$this->builder->addFilter(Order::CREATED_AT, $date, 'lt');
$this->builder->addSortOrder($sortOrder);
$this->builder->setPageSize(10);
$delay = $this->config->secondChanceEmailDelay();
$date = (new \DateTimeImmutable($this->dateTime->gmtDate()))->sub(new \DateInterval('PT' . $delay . 'H'));
$this->builder->addFilter(Order::CREATED_AT, $date, 'lt');
$this->builder->addSortOrder($sortOrder);
$this->builder->setPageSize(200);

$result = $this->paymentReminderRepository->getList($this->builder->create());
$result = $this->paymentReminderRepository->getList($this->builder->create());

foreach ($result->getItems() as $item) {
$this->paymentReminder->send($item);
}
} while ($result->getTotalCount());
foreach ($result->getItems() as $item) {
$this->paymentReminder->send($item);
}
}
}
2 changes: 1 addition & 1 deletion Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function loadMollieApi($apiKey)
* @param $storeId
* @return MollieApiClient|null
*/
public function getMollieApi($storeId)
public function getMollieApi($storeId = null)
{
$apiKey = $this->mollieHelper->getApiKey($storeId);

Expand Down
32 changes: 27 additions & 5 deletions Observer/MollieStartTransaction/SavePendingOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
use Mollie\Payment\Api\Data\PendingPaymentReminderInterfaceFactory;
use Mollie\Payment\Api\PendingPaymentReminderRepositoryInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Helper\General;

class SavePendingOrder implements ObserverInterface
{
/**
* @var General
*/
private $mollieHelper;

/**
* @var Config
*/
Expand All @@ -32,13 +38,15 @@ class SavePendingOrder implements ObserverInterface
private $repository;

public function __construct(
General $mollieHelper,
Config $config,
PendingPaymentReminderInterfaceFactory $reminderFactory,
PendingPaymentReminderRepositoryInterface $repository
) {
$this->mollieHelper = $mollieHelper;
$this->config = $config;
$this->reminderFactory = $reminderFactory;
$this->repository = $repository;
$this->config = $config;
}

public function execute(Observer $observer)
Expand All @@ -50,10 +58,24 @@ public function execute(Observer $observer)
return;
}

/** @var PendingPaymentReminderInterface $reminder */
$reminder = $this->reminderFactory->create();
$reminder->setOrderId($order->getEntityId());
try {
// If this succeeds there already exists a reminder.
$this->repository->getByOrderId($order->getEntityId());
return;
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
// Ignore.
}

$this->repository->save($reminder);
try {
/** @var PendingPaymentReminderInterface $reminder */
$reminder = $this->reminderFactory->create();
$reminder->setOrderId($order->getEntityId());

$this->repository->save($reminder);
} catch (\Exception $exception) {
$message = 'Got an exception while trying to save a payment reminder: ' . $exception->getMessage();
$message .= ' - Store ID: ' . $order->getStoreId();
$this->mollieHelper->addTolog('error', $message);
}
}
}
53 changes: 53 additions & 0 deletions Observer/SalesOrderPlaceBefore/RemovePendingPaymentReminders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Observer\SalesOrderPlaceBefore;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Mollie\Payment\Config;
use Mollie\Payment\Service\Order\DeletePaymentReminder;

class RemovePendingPaymentReminders implements ObserverInterface
{
/**
* @var Config
*/
private $config;

/**
* @var DeletePaymentReminder
*/
private $deletePaymentReminder;

public function __construct(
Config $config,
DeletePaymentReminder $deletePaymentReminder
) {
$this->config = $config;
$this->deletePaymentReminder = $deletePaymentReminder;
}

/**
* Remove any pending payment reminders. This is to prevent that payment reminders get send if the order was paid
* by a payment method that is not from Mollie.
*
* @param Observer $observer
*/
public function execute(Observer $observer)
{
/** @var OrderInterface $order */
$order = $observer->getData('order');

if (!$this->config->automaticallySendSecondChanceEmails($order->getStoreId())) {
return;
}

$this->deletePaymentReminder->byEmail($order->getCustomerEmail());
}
}
70 changes: 70 additions & 0 deletions Service/Order/Lines/Processor/BundleWithoutDynamicPricing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Service\Order\Lines\Processor;

use Magento\Bundle\Model\Product\Price;
use Magento\Catalog\Model\Product\Type;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Mollie\Payment\Helper\General;

class BundleWithoutDynamicPricing implements ProcessorInterface
{
/**
* @var General
*/
private $mollieHelper;

public function __construct(
General $mollieHelper
) {
$this->mollieHelper = $mollieHelper;
}

public function process($orderLine, OrderInterface $order, OrderItemInterface $orderItem = null): array
{
if (
!$orderItem ||
$orderItem->getProductType() !== Type::TYPE_BUNDLE ||
!$orderItem->getProduct() ||
$orderItem->getProduct()->getPriceType() != Price::PRICE_TYPE_FIXED
) {
return $orderLine;
}

$forceBaseCurrency = (bool)$this->mollieHelper->useBaseCurrency($order->getStoreId());
$currency = $forceBaseCurrency ? $order->getBaseCurrencyCode() : $order->getOrderCurrencyCode();

$discountAmount = $this->getDiscountAmountWithTax($orderItem, $forceBaseCurrency);
if (!$discountAmount) {
return $orderLine;
}

// Magento provides us with a discount amount without tax, but calculates with tax in this case. So recalculate
// the unit price, total amount and vat amount.

$taxPercent = $orderItem->getTaxPercent();
$unitPrice = $orderLine['totalAmount']['value'] / $orderItem->getQtyOrdered();
$newVatAmount = (($unitPrice - $discountAmount) / (100 + $taxPercent)) * $taxPercent;

$orderLine['unitPrice'] = $this->mollieHelper->getAmountArray($currency, $unitPrice);
$orderLine['totalAmount'] = $this->mollieHelper->getAmountArray($currency, $unitPrice - $discountAmount);
$orderLine['vatAmount'] = $this->mollieHelper->getAmountArray($currency, $newVatAmount);
$orderLine['discountAmount'] = $this->mollieHelper->getAmountArray($currency, $discountAmount);

return $orderLine;
}

private function getDiscountAmountWithTax(OrderItemInterface $item, bool $forceBaseCurrency)
{
if ($forceBaseCurrency) {
return abs($item->getBaseDiscountAmount());
}

return abs($item->getDiscountAmount());
}
}
Loading

0 comments on commit 514c01b

Please sign in to comment.