Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ class SalesOrderBeforeSaveObserver implements ObserverInterface
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException in case order has no payment specified.
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Sales\Model\Order $order */
$order = $observer->getEvent()->getOrder();

if (!$order->getPayment()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Please provide payment for the order.')
);
}

if ($order->getPayment()->getMethodInstance()->getCode() != 'free') {
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testSalesOrderBeforeSaveCantUnhold()
$paymentMock = $this->getMockBuilder(
\Magento\Sales\Model\Order\Payment::class
)->disableOriginalConstructor()->setMethods([])->getMock();
$order->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
$order->method('getPayment')->will($this->returnValue($paymentMock));
$methodInstance = $this->getMockBuilder(
\Magento\Payment\Model\MethodInterface::class
)->getMockForAbstractClass();
Expand All @@ -86,7 +86,7 @@ public function testSalesOrderBeforeSaveIsCanceled()
$paymentMock = $this->getMockBuilder(
\Magento\Sales\Model\Order\Payment::class
)->disableOriginalConstructor()->setMethods([])->getMock();
$order->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
$order->method('getPayment')->will($this->returnValue($paymentMock));
$methodInstance = $this->getMockBuilder(
\Magento\Payment\Model\MethodInterface::class
)->getMockForAbstractClass();
Expand Down Expand Up @@ -114,7 +114,7 @@ public function testSalesOrderBeforeSaveIsClosed()
$paymentMock = $this->getMockBuilder(
\Magento\Sales\Model\Order\Payment::class
)->disableOriginalConstructor()->setMethods([])->getMock();
$order->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
$order->method('getPayment')->will($this->returnValue($paymentMock));
$methodInstance = $this->getMockBuilder(
\Magento\Payment\Model\MethodInterface::class
)->getMockForAbstractClass();
Expand Down Expand Up @@ -156,6 +156,29 @@ public function testSalesOrderBeforeSaveSetForced()
$this->salesOrderBeforeSaveObserver->execute($this->observerMock);
}

/**
* The method should check that the payment is available, as this is not always the case.
*
* @expectedException \Magento\Framework\Exception\LocalizedException
* @exceptedExceptionMessage Please provide payment for the order.
*/
public function testDoesNothingWhenNoPaymentIsAvailable()
{
$this->_prepareEventMockWithMethods(['getOrder']);

$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)->disableOriginalConstructor()->setMethods(
array_merge(['__wakeup', 'getPayment'])
)->getMock();

$this->eventMock->expects($this->once())->method('getOrder')->will(
$this->returnValue($order)
);

$order->expects($this->exactly(1))->method('getPayment')->willReturn(null);

$this->salesOrderBeforeSaveObserver->execute($this->observerMock);
}

/**
* Prepares EventMock with set of methods
*
Expand Down Expand Up @@ -184,7 +207,7 @@ private function _getPreparedOrderMethod($methodCode, $orderMethods = [])
$paymentMock = $this->getMockBuilder(
\Magento\Sales\Model\Order\Payment::class
)->disableOriginalConstructor()->setMethods([])->getMock();
$order->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
$order->method('getPayment')->will($this->returnValue($paymentMock));
$methodInstance = $this->getMockBuilder(
\Magento\Payment\Model\MethodInterface::class
)->getMockForAbstractClass();
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Paypal/Plugin/OrderCanInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct(Express $express)
*/
public function afterCanInvoice(Order $order, bool $result): bool
{
if (!$order->getPayment()) {
return false;
}

if ($this->express->isOrderAuthorizationAllowed($order->getPayment())) {
return false;
}
Expand Down