Skip to content

Commit

Permalink
Merge pull request #132 from mollie/1.5.1
Browse files Browse the repository at this point in the history
1.5.1
  • Loading branch information
Marvin-Magmodules authored Apr 8, 2019
2 parents 2e451fc + 3ab2d38 commit ed609d5
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 175 deletions.
9 changes: 9 additions & 0 deletions Helper/Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
class Tests extends AbstractHelper
{
const XML_PATH_BANKTRANSFER_ACTIVE = 'payment/mollie_methods_banktransfer/active';
const XML_PATH_BANKTRANSFER_STATUS_PENDING = 'payment/mollie_methods_banktransfer/order_status_pending';

/**
* @var ObjectManagerInterface
Expand Down Expand Up @@ -146,6 +148,13 @@ public function compatibilityChecker()
$results[] = '<span class="mollie-error">' . $msg . '</span>';
}

$bankTransferActive = $this->scopeConfig->isSetFlag(static::XML_PATH_BANKTRANSFER_ACTIVE);
$bankTransferStatus = $this->scopeConfig->getValue(static::XML_PATH_BANKTRANSFER_STATUS_PENDING);
if ($bankTransferActive && $bankTransferStatus == 'pending_payment') {
$msg = __('Warning: we recommend to use a unique payment status for pending Banktransfer payments');
$results[] = '<span class="mollie-error">' . $msg . '</span>';
}

return $results;
}
}
61 changes: 0 additions & 61 deletions Model/Adminhtml/Source/PaymentMethods.php

This file was deleted.

109 changes: 98 additions & 11 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;

Expand Down Expand Up @@ -71,6 +72,10 @@ class Orders extends AbstractModel
* @var Registry
*/
private $registry;
/**
* @var PaymentFactory
*/
private $paymentFactory;

/**
* Orders constructor.
Expand All @@ -85,6 +90,7 @@ class Orders extends AbstractModel
* @param ManagerInterface $messageManager
* @param Registry $registry
* @param MollieHelper $mollieHelper
* @param PaymentFactory $paymentFactory
*/
public function __construct(
OrderLines $orderLines,
Expand All @@ -96,7 +102,8 @@ public function __construct(
CheckoutSession $checkoutSession,
ManagerInterface $messageManager,
Registry $registry,
MollieHelper $mollieHelper
MollieHelper $mollieHelper,
PaymentFactory $paymentFactory
) {
$this->orderLines = $orderLines;
$this->orderSender = $orderSender;
Expand All @@ -108,6 +115,7 @@ public function __construct(
$this->messageManager = $messageManager;
$this->registry = $registry;
$this->mollieHelper = $mollieHelper;
$this->paymentFactory = $paymentFactory;
}

/**
Expand Down Expand Up @@ -181,7 +189,7 @@ public function getAddressLine($address)
{
return [
'organizationName' => $address->getCompany(),
'title' => $address->getPrefix(),
'title' => trim($address->getPrefix()),
'givenName' => $address->getFirstname(),
'familyName' => $address->getLastname(),
'email' => $address->getEmail(),
Expand Down Expand Up @@ -253,7 +261,7 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
$method = $order->getPayment()->getMethodInstance()->getTitle();
$order->getPayment()->setAdditionalInformation('payment_status', $lastPaymentStatus);
$this->orderRepository->save($order);
$this->mollieHelper->registerCancellation($order, $status);
$this->mollieHelper->registerCancellation($order, $lastPaymentStatus);
$msg = ['success' => false, 'status' => $lastPaymentStatus, 'order_id' => $orderId, 'type' => $type, 'method' => $method];
$this->mollieHelper->addTolog('success', $msg);
return $msg;
Expand Down Expand Up @@ -529,10 +537,26 @@ public function createShipment(Order\Shipment $shipment, Order $order)
try {
$mollieApi = $this->loadMollieApi($apiKey);
$mollieOrder = $mollieApi->orders->get($transactionId);

if ($mollieOrder->status == 'completed') {
$this->messageManager->addWarningMessage(
__('All items in this order where already marked as shipped in the Mollie dashboard.')
);
return $this;
}

if ($shipAll) {
$mollieShipment = $mollieOrder->shipAll();
} else {
$orderLines = $this->orderLines->getShipmentOrderLines($shipment);

if ($mollieOrder->status == 'shipping' && !$this->itemsAreShippable($mollieOrder, $orderLines)) {
$this->messageManager->addWarningMessage(
__('All items in this order where already marked as shipped in the Mollie dashboard.')
);
return $this;
}

$mollieShipment = $mollieOrder->createShipment($orderLines);
}
$mollieShipmentId = isset($mollieShipment) ? $mollieShipment->id : 0;
Expand Down Expand Up @@ -635,7 +659,8 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
* 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(__(
$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.'
)
Expand Down Expand Up @@ -663,14 +688,41 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
return $this;
}

try {
$mollieApi = $this->loadMollieApi($apiKey);
} catch (\Exception $exception) {
$this->mollieHelper->addTolog('error', $exception->getMessage());
throw new LocalizedException(
__('Mollie API: %1', $exception->getMessage())
);
}

/**
* Check for creditmemo adjusment fee's, positive and negative.
* Throw exception if these are set, as this is not supportef by the orders api.
* Check for creditmemo adjustment fee's, positive and negative.
*/
if ($creditmemo->getAdjustmentPositive() > 0 || $creditmemo->getAdjustmentNegative() > 0) {
$msg = __('Creating an online refund with adjustment fee\'s is not supported by Mollie');
$this->mollieHelper->addTolog('error', $msg);
throw new LocalizedException($msg);
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())
);
}
}

/**
Expand All @@ -690,8 +742,11 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
}
}

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

try {
$mollieApi = $this->loadMollieApi($apiKey);
$mollieOrder = $mollieApi->orders->get($transactionId);
if ($order->getState() == Order::STATE_CLOSED) {
$mollieOrder->refundAll();
Expand All @@ -708,4 +763,36 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)

return $this;
}

/**
* When an order line is already marked as shipped in the Mollie dashboard, and we try this action again we get
* an exception and the user is unable to create an order. This code checks if the selected lines are already
* marked as shipped. If that's the case a warning will be shown, but the order is still created.
*
* @param \Mollie\Api\Resources\Order $mollieOrder
* @param $orderLines
* @return bool
*/
private function itemsAreShippable(\Mollie\Api\Resources\Order $mollieOrder, $orderLines)
{
$lines = [];
foreach ($orderLines['lines'] as $line) {
$id = $line['id'];
$lines[$id] = $line['quantity'];
}

foreach ($mollieOrder->lines as $line) {
if (!isset($lines[$line->id])) {
continue;
}

$quantityToShip = $lines[$line->id];

if ($line->shippableQuantity < $quantityToShip) {
return false;
}
}

return true;
}
}
2 changes: 1 addition & 1 deletion Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)

/**
* Order Api does not use amount to refund, but refunds per itemLine
* See SalesOrderCreditmemoAfter Observer for logic.
* See SalesOrderCreditmemoSaveAfter Observer for logic.
*/
$checkoutType = $this->mollieHelper->getCheckoutType($order);
if ($checkoutType == 'order') {
Expand Down
38 changes: 38 additions & 0 deletions Tests/Integration/Model/Client/OrdersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mollie\Payment\Tests\Unit\Model\Client;

use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\TestFramework\ObjectManager;
use Mollie\Payment\Model\Client\Orders;
use PHPUnit\Framework\TestCase;

class OrdersTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

protected function setUp()
{
parent::setUp();

$this->objectManager = ObjectManager::getInstance();
}

public function testRemovesEmptySpaceFromThePrefix()
{
/** @var Orders $instance */
$instance = $this->objectManager->get(Orders::class);

/** @var OrderAddressInterface $address */
$address = $this->objectManager->get(OrderAddressInterface::class);

$address->setPrefix(' ');

$result = $instance->getAddressLine($address);

$this->assertEmpty($result['title']);
}
}
Loading

0 comments on commit ed609d5

Please sign in to comment.