-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from mollie/release/2.13.0
Release/2.13.0
- Loading branch information
Showing
8 changed files
with
210 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
Observer/SalesQuoteItemSetProduct/SetSubscriptionDataOnBuyRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Mollie\Payment\Observer\SalesQuoteItemSetProduct; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Exception\NotFoundException; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
use Magento\Quote\Api\Data\CartItemInterface; | ||
|
||
class SetSubscriptionDataOnBuyRequest implements ObserverInterface | ||
{ | ||
/** | ||
* @var SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
public function __construct( | ||
SerializerInterface $serializer | ||
) { | ||
$this->serializer = $serializer; | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $observer->getData('product'); | ||
if (!$product->getData('mollie_subscription_product')) { | ||
return; | ||
} | ||
|
||
$table = $product->getData('mollie_subscription_table'); | ||
if (!$table) { | ||
return; | ||
} | ||
|
||
$buyRequest = $this->getBuyRequest($observer->getData('quote_item')); | ||
$value = $this->serializer->unserialize($buyRequest->getValue()); | ||
if (isset($value['mollie_metadata'])) { | ||
return; | ||
} | ||
|
||
$data = $this->serializer->unserialize($table); | ||
$default = $this->getDefault($data); | ||
|
||
$value['mollie_metadata'] = [ | ||
'purchase' => 'subscription', | ||
'recurring_metadata' => [ | ||
'option_id' => $default['identifier'], | ||
], | ||
]; | ||
|
||
$buyRequest->setValue($this->serializer->serialize($value)); | ||
} | ||
|
||
private function getDefault(array $data): array | ||
{ | ||
foreach ($data as $row) { | ||
if (isset($row['isDefault']) && $row['isDefault']) { | ||
return $row; | ||
} | ||
} | ||
|
||
return array_shift($data); | ||
} | ||
|
||
private function getBuyRequest(CartItemInterface $item): OptionInterface | ||
{ | ||
/** @var OptionInterface[] $options */ | ||
$options = $item->getOptions(); | ||
foreach ($options as $option) { | ||
if ($option->getCode() == 'info_buyRequest') { | ||
return $option; | ||
} | ||
} | ||
|
||
throw new NotFoundException(__('No info_buyRequest option found')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace Mollie\Payment\Observer; | ||
|
||
use Magento\Framework\Event; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Sales\Api\Data\OrderInterface; | ||
use Mollie\Api\Exceptions\ApiException; | ||
use Mollie\Payment\Model\Mollie; | ||
use Mollie\Payment\Test\Integration\IntegrationTestCase; | ||
|
||
class TestOrderCancelAfter extends IntegrationTestCase | ||
{ | ||
public function testDoesNothingWhenReordered(): void | ||
{ | ||
$modelMock = $this->createMock(Mollie::class); | ||
$modelMock->expects($this->never())->method('cancelOrder'); | ||
|
||
/** @var OrderCancelAfter $instance */ | ||
$instance = $this->objectManager->create(OrderCancelAfter::class, [ | ||
'mollieModel' => $modelMock, | ||
]); | ||
|
||
$order = $this->objectManager->create(OrderInterface::class); | ||
$order->setReordered(true); | ||
|
||
$instance->execute($this->makeObserver($order)); | ||
} | ||
|
||
public function testDoesNothingWhenNotPaidUsingOrdersApi(): void | ||
{ | ||
$modelMock = $this->createMock(Mollie::class); | ||
$modelMock->expects($this->never())->method('cancelOrder'); | ||
|
||
$helperMock = $this->createMock(\Mollie\Payment\Helper\General::class); | ||
$helperMock->method('isPaidUsingMollieOrdersApi')->willReturn(false); | ||
|
||
/** @var OrderCancelAfter $instance */ | ||
$instance = $this->objectManager->create(OrderCancelAfter::class, [ | ||
'mollieHelper' => $helperMock, | ||
'mollieModel' => $modelMock, | ||
]); | ||
|
||
$order = $this->objectManager->create(OrderInterface::class); | ||
|
||
$instance->execute($this->makeObserver($order)); | ||
} | ||
|
||
public function testCancelsTheOrder(): void | ||
{ | ||
$modelMock = $this->createMock(Mollie::class); | ||
$modelMock->expects($this->once())->method('cancelOrder'); | ||
|
||
$helperMock = $this->createMock(\Mollie\Payment\Helper\General::class); | ||
$helperMock->method('isPaidUsingMollieOrdersApi')->willReturn(true); | ||
|
||
/** @var OrderCancelAfter $instance */ | ||
$instance = $this->objectManager->create(OrderCancelAfter::class, [ | ||
'mollieHelper' => $helperMock, | ||
'mollieModel' => $modelMock, | ||
]); | ||
|
||
$order = $this->objectManager->create(OrderInterface::class); | ||
|
||
$instance->execute($this->makeObserver($order)); | ||
} | ||
|
||
public function testConvertsExceptionToErrorMessage(): void | ||
{ | ||
$message = 'Error executing API call (422: Unprocessable Entity): The order cannot be canceled due to an open payment. Please wait until the payment is in a finalized state.. Documentation: https://docs.mollie.com/reference/v2/orders-api/cancel-order'; | ||
$exception = new LocalizedException(__($message)); | ||
$modelMock = $this->createMock(Mollie::class); | ||
$modelMock->method('cancelOrder')->willThrowException($exception); | ||
|
||
$helperMock = $this->createMock(\Mollie\Payment\Helper\General::class); | ||
$helperMock->method('isPaidUsingMollieOrdersApi')->willReturn(true); | ||
|
||
/** @var OrderCancelAfter $instance */ | ||
$instance = $this->objectManager->create(OrderCancelAfter::class, [ | ||
'mollieHelper' => $helperMock, | ||
'mollieModel' => $modelMock, | ||
]); | ||
|
||
$order = $this->objectManager->create(OrderInterface::class); | ||
|
||
$instance->execute($this->makeObserver($order)); | ||
|
||
/** @var \\Magento\Framework\Message\ManagerInterface $manager */ | ||
$manager = $this->objectManager->get(\Magento\Framework\Message\ManagerInterface::class); | ||
|
||
$messages = $manager->getMessages(); | ||
$this->assertCount(1, $messages->getErrors()); | ||
$this->assertEquals($message, $messages->getErrors()[0]->getText()); | ||
} | ||
|
||
private function makeObserver(OrderInterface $order): Observer | ||
{ | ||
return new Observer([ | ||
'event' => new Event([ | ||
'order' => $order, | ||
]), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters