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
21 changes: 21 additions & 0 deletions Components/Order/OrderUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use MollieShopware\Components\Config;
use MollieShopware\Components\Constants\PaymentStatus;
use MollieShopware\Components\StatusConverter\OrderStatusConverter;
use MollieShopware\Components\StatusConverter\PaymentStatusConverter;
use MollieShopware\Components\Validator\PaymentStatusMailValidator;
Expand All @@ -15,12 +16,15 @@
use Psr\Log\LoggerInterface;
use Shopware\Components\ContainerAwareEventManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Article\Detail;
use Shopware\Models\Order\DetailStatus;
use Shopware\Models\Order\History;
use Shopware\Models\Order\Order;
use sOrder;

class OrderUpdater
{
const ORDER_DETAIL_STATUS_CANCELLED = 2;

/**
* @var LoggerInterface
Expand Down Expand Up @@ -408,6 +412,23 @@ private function updateOrderStatus(Order $order, $mollieStatus, $sendMail)
);
}

if ($mollieStatus === PaymentStatus::MOLLIE_PAYMENT_CANCELED) {
$this->cancelOrderPositions($order);
}

return true;
}

private function cancelOrderPositions(Order $order)
{
/** @var DetailStatus $cancelledStatus */
$cancelledStatus = $this->modelManager->find(DetailStatus::class, self::ORDER_DETAIL_STATUS_CANCELLED);

/** @var Detail $detail */
foreach ($order->getDetails() as $detail) {
$detail->setStatus($cancelledStatus);
}

$this->modelManager->flush();
}
}
22 changes: 20 additions & 2 deletions Components/Services/StockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Enlight_Components_Db_Adapter_Pdo_Mysql;
use MollieShopware\Components\Config;
use MollieShopware\Services\IsMolliePaymentValidator;
use Psr\Log\LoggerInterface;
use Shopware\Models\Payment\Payment;
use function sprintf;

class StockService
{
Expand All @@ -31,18 +34,25 @@ class StockService
*/
private $config;

/**
* @var IsMolliePaymentValidator
*/
private $isMolliePaymentValidator;

/**
* @param OrderService $orderService
* @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
* @param LoggerInterface $logger
* @param mixed $config
* @param Config $config
* @param IsMolliePaymentValidator $isMolliePaymentValidator
*/
public function __construct($orderService, $config, $db, $logger)
public function __construct($orderService, $config, $db, $logger, $isMolliePaymentValidator)
{
$this->orderService = $orderService;
$this->db = $db;
$this->logger = $logger;
$this->config = $config;
$this->isMolliePaymentValidator = $isMolliePaymentValidator;
}


Expand All @@ -59,6 +69,14 @@ public function updateOrderStocks($orderId, $reset = true)
}
$order = $this->orderService->getOrderById($orderId);

/** @var Payment $payment */
$payment = $order->getPayment();
if (!$this->isMolliePaymentValidator->validate($payment)) {
$this->logger->debug(sprintf('payment "%s" is not a mollie payment', $payment->getName()));

return;
}

$this->logger->debug('Start to reset the stocks for order', ['orderId' => $orderId]);

foreach ($order->getDetails() as $orderDetail) {
Expand Down
3 changes: 2 additions & 1 deletion Resources/services/components/mixed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@
<argument type="service" id="mollie_shopware.config"/>
<argument type="service" id="db"/>
<argument type="service" id="mollie_shopware.components.logger"/>
<argument type="service" id="mollie_shopware.services.is_mollie_payment_validator"/>
</service>

</services>
</container>
</container>
6 changes: 5 additions & 1 deletion Resources/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

<service class="MollieShopware\Services\Mollie\Payments\Extractor\ApiExceptionDetailsExtractor" id="mollie_shopware.services.payments.extractor.api_exception_details_extractor" public="true"/>

<service class="MollieShopware\Services\IsMolliePaymentValidator"
id="mollie_shopware.services.is_mollie_payment_validator">
<argument>%mollie_shopware.plugin_name%</argument>
</service>
</services>

</container>
</container>
36 changes: 36 additions & 0 deletions Services/IsMolliePaymentValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MollieShopware\Services;

use Shopware\Models\Payment\Payment;
use Shopware\Models\Plugin\Plugin;

class IsMolliePaymentValidator
{
/** @var string */
private $pluginName;

/**
* @param string $pluginName
*/
public function __construct($pluginName)
{
$this->pluginName = $pluginName;
}

/**
* @param Payment $payment
* @return bool
*/
public function validate(Payment $payment)
{
/** @var null|Plugin $plugin */
$plugin = $payment->getPlugin();

if (!($plugin instanceof Plugin)) {
return false;
}

return $plugin->getName() === $this->pluginName;
}
}
17 changes: 14 additions & 3 deletions Subscriber/StockSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

use Enlight\Event\SubscriberInterface;
use Enlight_Event_EventArgs;
use Enlight_Hook_HookArgs;
use function in_array;
use MollieShopware\Components\Services\StockService;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Order;
use Shopware\Models\Order\Status;

class StockSubscriber implements SubscriberInterface
{
const PAYMENT_STATE_FOR_STOCK_INCREASE = [
Status::PAYMENT_STATE_COMPLETELY_PAID,
Status::PAYMENT_STATE_THE_PAYMENT_HAS_BEEN_ORDERED,
];

/**
* @var StockService
*/
Expand Down Expand Up @@ -42,7 +47,7 @@ public function increaseStocks(Enlight_Event_EventArgs $eventArgs)
$orderId = (int)$eventArgs->get('orderId');
$paymentStatusId = (int)$eventArgs->get('paymentStatusId');

if ($paymentStatusId !== Status::PAYMENT_STATE_COMPLETELY_PAID) {
if (!in_array($paymentStatusId, self::PAYMENT_STATE_FOR_STOCK_INCREASE, true)) {
return;
}

Expand Down Expand Up @@ -74,7 +79,13 @@ public function entityEvent(Enlight_Event_EventArgs $eventArgs)
if ($oldId === $newId) {
return;
}
if ($newId !== Status::PAYMENT_STATE_COMPLETELY_PAID) {

if ($oldId === Status::PAYMENT_STATE_THE_PAYMENT_HAS_BEEN_ORDERED) {
// stock was already updated so we may not update it again
return;
}

if (!in_array($newId, self::PAYMENT_STATE_FOR_STOCK_INCREASE, true)) {
return;
}

Expand Down
57 changes: 57 additions & 0 deletions Tests/PHPUnit/Services/IsMolliePaymentValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace MollieShopware\Tests\PHPUnit\Services;

use MollieShopware\MollieShopware;
use MollieShopware\Services\IsMolliePaymentValidator;
use PHPUnit\Framework\TestCase;
use Shopware\Models\Payment\Payment;
use Shopware\Models\Plugin\Plugin;

class IsMolliePaymentValidatorTest extends TestCase
{
/**
* @var IsMolliePaymentValidator
*/
private $isMolliePaymentValidator;

protected function setUp(): void
{
$this->isMolliePaymentValidator = new IsMolliePaymentValidator('MollieShopware');
}

/**
* @dataProvider validateDataProvider
*/
public function testValidate(Payment $payment, bool $expected)
{
$this->assertSame($expected, $this->isMolliePaymentValidator->validate($payment));
}

public static function validateDataProvider()
{
$molliePlugin = new Plugin();
$molliePlugin->setName('MollieShopware');

$prepayment = new Payment();
$prepayment->setName('prepayment');

$invoice = new Payment();
$invoice->setName('invoice');

$creditCard = new Payment();
$creditCard->setName(MollieShopware::PAYMENT_PREFIX . 'credit_card');
$creditCard->setPlugin($molliePlugin);

$klarnaPayNow = new Payment();
$klarnaPayNow->setName(MollieShopware::PAYMENT_PREFIX . 'klarna_pay_now');
$klarnaPayNow->setPlugin($molliePlugin);

return [
[$prepayment, false],
[$invoice, false],
[$creditCard, true],
[$klarnaPayNow, true],
];
}
}
Loading