Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/show-methods-in-the-order-grid
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed May 29, 2019
2 parents da018f9 + d527864 commit 38d4d98
Show file tree
Hide file tree
Showing 50 changed files with 977 additions and 113 deletions.
63 changes: 56 additions & 7 deletions Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

namespace Mollie\Payment\Controller\Checkout;

use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Store\Model\ScopeInterface;
use Mollie\Payment\Helper\General as MollieHelper;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
Expand Down Expand Up @@ -37,27 +42,41 @@ class Redirect extends Action
* @var MollieHelper
*/
protected $mollieHelper;
/**
* @var OrderManagementInterface
*/
private $orderManagement;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Redirect constructor.
*
* @param Context $context
* @param Session $checkoutSession
* @param PageFactory $resultPageFactory
* @param PaymentHelper $paymentHelper
* @param MollieHelper $mollieHelper
* @param Context $context
* @param Session $checkoutSession
* @param PageFactory $resultPageFactory
* @param PaymentHelper $paymentHelper
* @param MollieHelper $mollieHelper
* @param OrderManagementInterface $orderManagement
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
Context $context,
Session $checkoutSession,
PageFactory $resultPageFactory,
PaymentHelper $paymentHelper,
MollieHelper $mollieHelper
MollieHelper $mollieHelper,
OrderManagementInterface $orderManagement,
ScopeConfigInterface $scopeConfig
) {
$this->checkoutSession = $checkoutSession;
$this->resultPageFactory = $resultPageFactory;
$this->paymentHelper = $paymentHelper;
$this->mollieHelper = $mollieHelper;
$this->orderManagement = $orderManagement;
$this->scopeConfig = $scopeConfig;
parent::__construct($context);
}

Expand Down Expand Up @@ -102,11 +121,41 @@ public function execute()
$this->checkoutSession->restoreQuote();
$this->_redirect('checkout/cart');
}
} catch (\Exception $e) {
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __($e->getMessage()));
$this->mollieHelper->addTolog('error', $e->getMessage());
$this->checkoutSession->restoreQuote();
$this->cancelUnprocessedOrder($order, $e->getMessage());
$this->_redirect('checkout/cart');
}
}

private function cancelUnprocessedOrder(OrderInterface $order, $message)
{
if (!empty($order->getMollieTransactionId())) {
return;
}

if (!$this->scopeConfig->isSetFlag(
'payment/mollie_general/cancel_failed_orders',
ScopeInterface::SCOPE_STORE
)) {
return;
}

try {
$historyMessage = __('Canceled because an error occurred while redirecting the customer to Mollie');
if ($message) {
$historyMessage .= ':<br>' . PHP_EOL . $message;
}

$this->orderManagement->cancel($order->getEntityId());
$order->addCommentToStatusHistory($order->getEntityId(), $historyMessage);

$this->mollieHelper->addToLog('info', sprintf('Canceled order %s', $order->getIncrementId()));
} catch (Exception $e) {
$message = sprintf('Cannot cancel order %s: %s', $order->getIncrementId(), $e->getMessage());
$this->mollieHelper->addToLog('error', $message);
}
}
}
5 changes: 5 additions & 0 deletions Controller/Checkout/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function execute()
}
} catch (\Exception $e) {
$this->mollieHelper->addTolog('error', $e->getMessage());

$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setHttpResponseCode(503);

return $result;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Helper/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ public function getAllActiveMethods($storeId)
'mollie_methods_bancontact',
'mollie_methods_banktransfer',
'mollie_methods_belfius',
'mollie_methods_bitcoin',
'mollie_methods_creditcard',
'mollie_methods_ideal',
'mollie_methods_kbc',
Expand All @@ -614,6 +613,7 @@ public function getAllActiveMethods($storeId)
'mollie_methods_klarnasliceit',
'mollie_methods_giftcard',
'mollie_methods_przelewy24',
'mollie_methods_applepay',
];

foreach ($methodCodes as $methodCode) {
Expand Down
11 changes: 9 additions & 2 deletions Helper/Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public function getMethods($testKey = null, $liveKey = null)
try {
$availableMethods = [];
$mollieApi = $this->mollieModel->loadMollieApi($testKey);
$methods = $mollieApi->methods->all(["resource" => "orders"]);
$methods = $mollieApi->methods->all([
'resource' => 'orders',
'includeWallets' => 'applepay',
]);

foreach ($methods as $apiMethod) {
$availableMethods[] = ucfirst($apiMethod->id);
Expand Down Expand Up @@ -96,7 +99,11 @@ public function getMethods($testKey = null, $liveKey = null)
try {
$availableMethods = [];
$mollieApi = $this->mollieModel->loadMollieApi($liveKey);
$methods = $mollieApi->methods->all(["resource" => "orders"]);
$methods = $mollieApi->methods->all([
'resource' => 'orders',
'includeWallets' => 'applepay',
]);

foreach ($methods as $apiMethod) {
$availableMethods[] = ucfirst($apiMethod->id);
}
Expand Down
59 changes: 18 additions & 41 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Magento\Sales\Model\Order\InvoiceRepository;
use Magento\Sales\Model\Service\InvoiceService;
use Magento\Checkout\Model\Session\Proxy as CheckoutSession;
use Mollie\Api\Resources\PaymentFactory;
use Mollie\Payment\Helper\General as MollieHelper;
use Mollie\Payment\Model\OrderLines;
use Mollie\Payment\Service\Order\ProcessAdjustmentFee;

/**
* Class Orders
Expand Down Expand Up @@ -74,24 +74,24 @@ class Orders extends AbstractModel
*/
private $registry;
/**
* @var PaymentFactory
* @var ProcessAdjustmentFee
*/
private $paymentFactory;
private $adjustmentFee;

/**
* Orders constructor.
*
* @param OrderLines $orderLines
* @param OrderSender $orderSender
* @param InvoiceSender $invoiceSender
* @param InvoiceService $invoiceService
* @param OrderRepository $orderRepository
* @param InvoiceRepository $invoiceRepository
* @param CheckoutSession $checkoutSession
* @param ManagerInterface $messageManager
* @param Registry $registry
* @param MollieHelper $mollieHelper
* @param PaymentFactory $paymentFactory
* @param OrderLines $orderLines
* @param OrderSender $orderSender
* @param InvoiceSender $invoiceSender
* @param InvoiceService $invoiceService
* @param OrderRepository $orderRepository
* @param InvoiceRepository $invoiceRepository
* @param CheckoutSession $checkoutSession
* @param ManagerInterface $messageManager
* @param Registry $registry
* @param MollieHelper $mollieHelper
* @param ProcessAdjustmentFee $adjustmentFee
*/
public function __construct(
OrderLines $orderLines,
Expand All @@ -104,7 +104,7 @@ public function __construct(
ManagerInterface $messageManager,
Registry $registry,
MollieHelper $mollieHelper,
PaymentFactory $paymentFactory
ProcessAdjustmentFee $adjustmentFee
) {
$this->orderLines = $orderLines;
$this->orderSender = $orderSender;
Expand All @@ -116,7 +116,7 @@ public function __construct(
$this->messageManager = $messageManager;
$this->registry = $registry;
$this->mollieHelper = $mollieHelper;
$this->paymentFactory = $paymentFactory;
$this->adjustmentFee = $adjustmentFee;
}

/**
Expand Down Expand Up @@ -692,30 +692,7 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
/**
* Check for creditmemo adjustment fee's, positive and negative.
*/
if ($creditmemo->getAdjustment() !== 0.0) {
$mollieOrder = $mollieApi->orders->get($order->getMollieTransactionId(), ['embed' => 'payments']);
$payments = $mollieOrder->_embedded->payments;

try {
$payment = $this->paymentFactory->create([$mollieApi]);
$payment->id = current($payments)->id;

$mollieApi->payments->refund($payment, [
'amount' => [
'currency' => $order->getOrderCurrencyCode(),
'value' => $this->mollieHelper->formatCurrencyValue(
$creditmemo->getAdjustment(),
$order->getOrderCurrencyCode()
),
]
]);
} catch (\Exception $exception) {
$this->mollieHelper->addTolog('error', $exception->getMessage());
throw new LocalizedException(
__('Mollie API: %1', $exception->getMessage())
);
}
}
$this->adjustmentFee->handle($mollieApi, $order, $creditmemo);

/**
* Check if Shipping Fee needs to be refunded.
Expand All @@ -734,7 +711,7 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
}
}

if (!$creditmemo->getAllItems()) {
if (!$creditmemo->getAllItems() || $this->adjustmentFee->doNotRefundInMollie()) {
return $this;
}

Expand Down
14 changes: 4 additions & 10 deletions Model/Methods/Bitcoin.php → Model/Methods/ApplePay.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@

namespace Mollie\Payment\Model\Methods;

use Mollie\Payment\Model\Mollie;

/**
* Class Bitcoin
*
* @package Mollie\Payment\Model\Methods
*/
class Bitcoin extends Mollie
class ApplePay extends \Mollie\Payment\Model\Mollie
{

/**
* Payment method code
*
* @var string
*/
protected $_code = 'mollie_methods_bitcoin';
protected $_code = 'mollie_methods_applepay';

/**
* Info instructions block path
*
* @var string
*/
protected $_infoBlockType = 'Mollie\Payment\Block\Info\Base';
}
}
5 changes: 4 additions & 1 deletion Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ public function getPaymentMethods($storeId)

$mollieApi = $this->loadMollieApi($apiKey);

return $mollieApi->methods->all(["resource" => "orders"]);
return $mollieApi->methods->all([
'resource' => 'orders',
'includeWallets' => 'applepay',
]);
}
}
10 changes: 6 additions & 4 deletions Model/MollieConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class MollieConfigProvider implements ConfigProviderInterface
'mollie_methods_bancontact',
'mollie_methods_banktransfer',
'mollie_methods_belfius',
'mollie_methods_bitcoin',
'mollie_methods_creditcard',
'mollie_methods_ideal',
'mollie_methods_kbc',
Expand All @@ -44,6 +43,7 @@ class MollieConfigProvider implements ConfigProviderInterface
'mollie_methods_klarnasliceit',
'mollie_methods_giftcard',
'mollie_methods_przelewy24',
'mollie_methods_applepay',
];
/**
* @var array
Expand Down Expand Up @@ -207,9 +207,11 @@ public function getActiveMethods($mollieApi)
try {
$quote = $this->checkoutSession->getQuote();
$amount = $this->mollieHelper->getOrderAmountByQuote($quote);
$params = ["amount[value]" => $amount['value'],
"amount[currency]" => $amount['currency'],
"resource" => "orders"
$params = [
'amount[value]' => $amount['value'],
'amount[currency]' => $amount['currency'],
'resource' => 'orders',
'includeWallets' => 'applepay',
];
$apiMethods = $mollieApi->methods->all($params);

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ Mollie requires no minimum costs, no fixed contracts, no hidden costs. At Mollie

- Klarna

- Giftcards
- Giftcards

- Apple Pay

## Configuration, FAQ and Troubleshooting ##
If you experience problems with the extension installation, setup or whenever you need more information about how to setup the Mollie Payment extension in Magento 2.x, please see our [WIKI Page](https://github.com/mollie/magento2/wiki) or send an e-mail to [[email protected]](mailto:[email protected]) with an exact description of the problem.
Expand Down
Loading

0 comments on commit 38d4d98

Please sign in to comment.