Skip to content

Commit

Permalink
Merge pull request #105 from mollie/1.4.6
Browse files Browse the repository at this point in the history
Combined v1.4.6 fixes
  • Loading branch information
Marvin-Magmodules authored Jan 3, 2019
2 parents 988b8f8 + fdae177 commit 578b282
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 61 deletions.
63 changes: 39 additions & 24 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
Expand Down Expand Up @@ -61,6 +63,14 @@ class Orders extends AbstractModel
* @var CheckoutSession
*/
private $checkoutSession;
/**
* @var ManagerInterface
*/
private $messageManager;
/**
* @var Registry
*/
private $registry;

/**
* Orders constructor.
Expand All @@ -72,6 +82,8 @@ class Orders extends AbstractModel
* @param OrderRepository $orderRepository
* @param InvoiceRepository $invoiceRepository
* @param CheckoutSession $checkoutSession
* @param ManagerInterface $messageManager
* @param Registry $registry
* @param MollieHelper $mollieHelper
*/
public function __construct(
Expand All @@ -82,6 +94,8 @@ public function __construct(
OrderRepository $orderRepository,
InvoiceRepository $invoiceRepository,
CheckoutSession $checkoutSession,
ManagerInterface $messageManager,
Registry $registry,
MollieHelper $mollieHelper
) {
$this->orderLines = $orderLines;
Expand All @@ -91,6 +105,8 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->invoiceRepository = $invoiceRepository;
$this->checkoutSession = $checkoutSession;
$this->messageManager = $messageManager;
$this->registry = $registry;
$this->mollieHelper = $mollieHelper;
}

Expand Down Expand Up @@ -145,6 +161,10 @@ public function startTransaction(Order $order, $mollieApi)
$orderData['payment']['dueDate'] = $this->mollieHelper->getBanktransferDueDate($storeId);
}

if (isset($additionalData['limited_methods'])) {
$orderData['method'] = $additionalData['limited_methods'];
}

$this->mollieHelper->addTolog('request', $orderData);
$mollieOrder = $mollieApi->orders->create($orderData);
$this->processResponse($order, $mollieOrder);
Expand Down Expand Up @@ -342,9 +362,12 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
if ($mollieOrder->method == 'banktransfer' && !$order->getEmailSent()) {
$this->orderSender->send($order);
$message = __('New order email sent');
$defaultStatusPending = $this->mollieHelper->getStatusPendingBanktransfer($storeId);
if (!$statusPending = $this->mollieHelper->getStatusPendingBanktransfer($storeId)) {
$statusPending = $order->getStatus();
}

$order->setState(Order::STATE_PENDING_PAYMENT);
$order->addStatusToHistory($defaultStatusPending, $message, true);
$order->addStatusToHistory($statusPending, $message, true);
$this->orderRepository->save($order);
}
$msg = ['success' => true, 'status' => $status, 'order_id' => $orderId, 'type' => $type];
Expand Down Expand Up @@ -588,9 +611,6 @@ public function updateShipmentTrack($shipment, $track, $order)
}
} catch (\Exception $e) {
$this->mollieHelper->addTolog('error', $e->getMessage());
throw new LocalizedException(
__('Mollie API: %1', $e->getMessage())
);
}

return $this;
Expand All @@ -605,10 +625,22 @@ public function updateShipmentTrack($shipment, $track, $order)
*/
public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
{
$refundAll = false;
$storeId = $order->getStoreId();
$orderId = $order->getId();

/**
* Skip the creation of an online refund if an offline refund is used + add notice msg.
* Registry set at the Mollie\Payment\Model\Mollie::refund and is set once an online refund is used.
*/
if (!$this->registry->registry('online_refund')) {
$this->messageManager->addNoticeMessage(__(
'An offline refund has been created, please make sure to also create this
refund on mollie.com/dashboard or use the online refund option.'
)
);
return $this;
}

$methodCode = $this->mollieHelper->getMethodCode($order);
if (!$order->hasShipments() && ($methodCode == 'klarnapaylater' || $methodCode == 'klarnasliceit')) {
$msg = __('Order can only be refunded after Klara has been captured (after shipment)');
Expand Down Expand Up @@ -656,27 +688,10 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
}
}

/**
* If products ordered qty equals refunded qty,
* complete order can be shipped incl. shipping & discount itemLines.
*/
if ((int)$order->getTotalQtyOrdered() == (int)$creditmemo->getTotalQty()) {
$refundAll = true;
}

/**
* If refunded qty equals total paid physical products count,
* all remaining lines can be refunded, incl. shipping & discount itemLines.
*/
$openForRefundQty = $this->orderLines->getOpenForRefundQty($orderId);
if ((int)$creditmemo->getTotalQty() == (int)$openForRefundQty) {
$refundAll = true;
}

try {
$mollieApi = $this->loadMollieApi($apiKey);
$mollieOrder = $mollieApi->orders->get($transactionId);
if ($refundAll) {
if ($order->getState() == Order::STATE_CLOSED) {
$mollieOrder->refundAll();
} else {
$orderLines = $this->orderLines->getCreditmemoOrderLines($creditmemo, $addShippingToRefund);
Expand Down
24 changes: 13 additions & 11 deletions Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ public function startTransaction(Order $order, $mollieApi)
$paymentToken = $this->mollieHelper->getPaymentToken();
$method = $this->mollieHelper->getMethodCode($order);
$paymentData = [
'amount' => $this->mollieHelper->getOrderAmountByOrder($order),
'description' => $order->getIncrementId(),
'billingAddress' => $this->getAddressLine($order->getBillingAddress()),
'redirectUrl' => $this->mollieHelper->getRedirectUrl($orderId, $paymentToken),
'webhookUrl' => $this->mollieHelper->getWebhookUrl(),
'method' => $method,
'issuer' => isset($additionalData['selected_issuer']) ? $additionalData['selected_issuer'] : null,
'metadata' => [
'amount' => $this->mollieHelper->getOrderAmountByOrder($order),
'description' => $order->getIncrementId(),
'billingAddress' => $this->getAddressLine($order->getBillingAddress()),
'redirectUrl' => $this->mollieHelper->getRedirectUrl($orderId, $paymentToken),
'webhookUrl' => $this->mollieHelper->getWebhookUrl(),
'method' => $method,
'issuer' => isset($additionalData['selected_issuer']) ? $additionalData['selected_issuer'] : null,
'metadata' => [
'order_id' => $orderId,
'store_id' => $order->getStoreId(),
'payment_token' => $paymentToken
],
'locale' => $this->mollieHelper->getLocaleCode($storeId, self::CHECKOUT_TYPE)
'locale' => $this->mollieHelper->getLocaleCode($storeId, self::CHECKOUT_TYPE)
];

if (!$order->getIsVirtual() && $order->hasData('shipping_address_id')) {
Expand Down Expand Up @@ -261,9 +261,11 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
if ($paymentData->method == 'banktransfer' && !$order->getEmailSent()) {
$this->orderSender->send($order);
$message = __('New order email sent');
$status = $this->mollieHelper->getStatusPendingBanktransfer($storeId);
if (!$statusPending = $this->mollieHelper->getStatusPendingBanktransfer($storeId)) {
$statusPending = $order->getStatus();
}
$order->setState(Order::STATE_PENDING_PAYMENT);
$order->addStatusToHistory($status, $message, true);
$order->addStatusToHistory($statusPending, $message, true);
$this->orderRepository->save($order);
}
$msg = ['success' => true, 'status' => 'open', 'order_id' => $orderId, 'type' => $type];
Expand Down
23 changes: 23 additions & 0 deletions Model/Methods/Paymentlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Mollie\Payment\Model\Methods;

use Mollie\Payment\Model\Mollie;
use Magento\Framework\DataObject;

/**
* Class Paymentlink
Expand Down Expand Up @@ -69,4 +70,26 @@ public function initialize($paymentAction, $stateObject)

$this->startTransaction($order);
}

/**
* @param DataObject $data
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function assignData(DataObject $data)
{
$limitedMethods = null;
parent::assignData($data);

if ($additionalData = $data->getData('additional_data')) {
if (isset($additionalData['limited_methods'])) {
$limitedMethods = $additionalData['limited_methods'];
}
}

$this->getInfoInstance()->setAdditionalInformation('limited_methods', $limitedMethods);

return $this;
}
}
28 changes: 16 additions & 12 deletions Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,20 @@ public function processTransaction($orderId, $type = 'webhook', $paymentToken =
*/
public function assignData(DataObject $data)
{
$issuer = null;
parent::assignData($data);

if (is_array($data)) {
$this->getInfoInstance()->setAdditionalInformation('selected_issuer', $data['selected_issuer']);
if (is_array($data) && isset($data['selected_issuer'])) {
$issuer = $data['selected_issuer'];
} elseif ($data instanceof \Magento\Framework\DataObject) {
$additionalData = $data->getAdditionalData();
if (isset($additionalData['selected_issuer'])) {
$issuer = $additionalData['selected_issuer'];
$this->getInfoInstance()->setAdditionalInformation('selected_issuer', $issuer);
}
}

$this->getInfoInstance()->setAdditionalInformation('selected_issuer', $issuer);

return $this;
}

Expand Down Expand Up @@ -396,6 +399,16 @@ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
$order = $payment->getOrder();
$storeId = $order->getStoreId();

/**
* Order Api does not use amount to refund, but refunds per itemLine
* See SalesOrderCreditmemoAfter Observer for logic.
*/
$checkoutType = $this->mollieHelper->getCheckoutType($order);
if ($checkoutType == 'order') {
$this->_registry->register('online_refund', true);
return $this;
}

$transactionId = $order->getMollieTransactionId();
if (empty($transactionId)) {
$msg = ['error' => true, 'msg' => __('Transaction ID not found')];
Expand All @@ -410,15 +423,6 @@ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
return $this;
}

/**
* Order Api does not use amount to refund, but refunds per itemLine
* See SalesOrderCreditmemoAfter Observer for logic.
*/
$checkoutType = $this->mollieHelper->getCheckoutType($order);
if ($checkoutType == 'order') {
return $this;
}

try {
$mollieApi = $this->loadMollieApi($apiKey);
$payment = $mollieApi->payments->get($transactionId);
Expand Down
15 changes: 10 additions & 5 deletions Model/OrderLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ public function getShipmentOrderLines($shipment)
*/
public function getOrderLineByItemId($itemId)
{
return $this->orderLinesFactory->create()->load($itemId, 'item_id');
$orderLine = $this->orderLinesCollection->create()
->addFieldToFilter('item_id', ['eq' => $itemId])
->addFieldToFilter('line_id', ['notnull' => true])
->getLastItem();

return $orderLine;
}

/**
Expand Down Expand Up @@ -374,8 +379,8 @@ public function getOpenForShipmentQty($orderId)
->addFieldToFilter('type', ['eq' => 'physical'])
->addExpressionFieldToSelect(
'open',
'SUM(qty_paid - qty_shipped - qty_refunded)',
['qty_paid', 'qty_shipped', 'qty_refunded']
'SUM(qty_ordered - qty_shipped - qty_refunded)',
['qty_ordered', 'qty_shipped', 'qty_refunded']
);
$orderLinesCollection->getSelect()->group('order_id');

Expand All @@ -401,8 +406,8 @@ public function getOpenForRefundQty($orderId)
->addFieldToFilter('type', ['in' => ['physical', 'digital']])
->addExpressionFieldToSelect(
'open',
'SUM(qty_paid - qty_refunded)',
['qty_paid', 'qty_refunded']
'SUM(qty_ordered - qty_refunded)',
['qty_ordered', 'qty_refunded']
);
$orderLinesCollection->getSelect()->group('order_id');

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mollie/magento2",
"description": "Mollie Payment Module for Magento 2",
"version": "1.4.5",
"version": "1.4.6",
"require": {
"mollie/mollie-api-php": "^2.1",
"magento/framework": ">=100.1.0",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mollie_Payment" setup_version="1.4.5" />
<module name="Mollie_Payment" setup_version="1.4.6" />
</config>
39 changes: 32 additions & 7 deletions view/adminhtml/templates/form/mollie_paymentlink.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,42 @@
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile
/**
* @var \Mollie\Payment\Block\Form\Paymentlink $block
* @var \Mollie\Payment\Block\Form\Paymentlink $block
* @see \Mollie\Payment\Block\Form\Paymentlink
// @codingStandardsIgnoreFile
/**
*
* /**
* @var \Magento\Payment\Block\Adminhtml\Transparent\Form $block
*/
$code = $block->escapeHtml($block->getMethodCode());
?>
<fieldset class="admin__fieldset payment-method" id="payment_form_<?php /* @noEscape */ echo $code; ?>" style="display:none">
<?= $block->escapeHtml(__('Create a Mollie Payment link and add this to the order email.')); ?>
<fieldset class="admin__fieldset payment-method" id="payment_form_<?= /* @noEscape */
$code; ?>" style="display:none">
<div class="mollie-admin-method">
<?= $block->escapeHtml(__('Create a Mollie Payment link and add this to the order email.')); ?>
<div class="field admin__field">
<label for="<?= $code ?>_methods" class="label admin__field-label"><span><?= $block->escapeHtml(__('Limit to the following method(s)')) ?></span></label>
<div class="control admin__field-control">
<select multiple="multiple" size="16" id="<?= $code ?>_methods" name="payment[limited_methods][]" class="admin__control-multiselect">
<option value="bancontact">Bancontact</option>
<option value="banktransfer">Banktransfer</option>
<option value="belfius">Belfius</option>
<option value="bitcoin">Bitcoin</option>
<option value="creditcard">Creditcard</option>
<option value="eps">EPS</option>
<option value="giftcard">Giftcard</option>
<option value="giropay">Giropay</option>
<option value="ideal">iDEAL</option>
<option value="inghomepay">ING Homepay</option>
<option value="kbc">KBC/CBC</option>
<option value="klarnapaylater">Klarna Pay Later</option>
<option value="klarnasliceit">Klarna Slice It</option>
<option value="paypal">Paypal</option>
<option value="paysafecard">Paysafecard</option>
<option value="sofort">Sofort</option>
</select>
</div>
</div>
<p><?= $block->escapeHtml(__('If one method is chosen, it will skip the selection screen and the customer is sent directly to the payment method.')); ?></p>
</div>
</fieldset>
5 changes: 5 additions & 0 deletions view/adminhtml/web/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ a.mollie-tooltip:hover span {
padding: 10px;
}

.mollie-admin-method {
padding: 10px;
margin: 10px 0;
}

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
.mm-block-mollie .icon1 {
background: url(../images/icons@x2.png) no-repeat scroll 0px -7px;
Expand Down

0 comments on commit 578b282

Please sign in to comment.