Skip to content

Commit

Permalink
RATEPLUG-176: bidirectionality: fix order-fetch issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Mar 2, 2021
1 parent 67c1907 commit 90040d0
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 28 deletions.
28 changes: 28 additions & 0 deletions Exception/RatepayException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace RpayRatePay\Exception;


use Throwable;

class RatepayException extends \Exception
{

private $context;

public function __construct($message = "", $code = 0, Throwable $previous = null, $context = [])
{
parent::__construct($message, $code, $previous);
$this->context = $context;
}

/**
* @return array
*/
public function getContext()
{
return $this->context;
}

}
28 changes: 28 additions & 0 deletions Exception/RatepayPositionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace RpayRatePay\Exception;


use Shopware\Models\Order\Detail;
use Shopware\Models\Order\Order;

class RatepayPositionNotFoundException extends RatepayException
{

/**
* @param Order $order
* @param Detail $detail
* @param string $expectedClass
*/
public function __construct(Order $order, Detail $detail, $expectedClass)
{
parent::__construct('Ratepay position for order detail has not been found.', null, null, [
'order_id' => $order->getId(),
'order_number' => $order->getNumber(),
'order_detail_id' => $detail->getId(),
'expected_model' => $expectedClass
]);
}

}
10 changes: 7 additions & 3 deletions Helper/PositionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use RpayRatePay\DTO\BasketPosition;
use RpayRatePay\Exception\RatepayPositionNotFoundException;
use RpayRatePay\Models\Position\AbstractPosition;
use RpayRatePay\Models\Position\Discount;
use RpayRatePay\Models\Position\Product;
Expand Down Expand Up @@ -94,10 +95,15 @@ public function doesOrderHasOpenPositions(Order $_order, $items = [])
/**
* @param Detail $detail
* @return AbstractPosition
* @throws RatepayPositionNotFoundException
*/
public function getPositionForDetail(Detail $detail)
{
return $this->modelManager->find(self::getPositionClass($detail), $detail->getId());
$entity = $this->modelManager->find(self::getPositionClass($detail), $detail->getId());
if ($entity === null) {
throw new RatepayPositionNotFoundException($detail->getOrder(), $detail, self::getPositionClass($detail));
}
return $entity;
}

public static function getPositionClass(Detail $detail)
Expand All @@ -107,12 +113,10 @@ public static function getPositionClass(Detail $detail)
case self::MODE_SW_PRODUCT[0]: //product
case self::MODE_SW_PRODUCT[1]: //premium product
return Product::class;
break;
case self::MODE_SW_DISCOUNT[0]: //voucher
case self::MODE_SW_DISCOUNT[1]: //IS_REBATE = rabatt
case self::MODE_SW_DISCOUNT[2]: //IS_SURCHARGE_DISCOUNT //IST ZUSCHLAGSRABATT
return Discount::class;
break;
case self::MODE_RP_SHIPPING:
return ShippingPosition::class;
default:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
|Module | Ratepay Module for Shopware
|------|----------
|Shop Version | `5.5.0` - `5.7.x`
|Version | `6.0.0`
|Version | `6.0.1`
|Link | http://www.ratepay.com
|Mail | integration@ratepay.com
|Full Documentation | https://ratepay.gitbook.io/shopware5/
Expand Down
52 changes: 30 additions & 22 deletions Subscriber/Cron/UpdateTransactionsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Exception;
use Monolog\Logger;
use RpayRatePay\Enum\PaymentMethods;
use RpayRatePay\Exception\RatepayException;
use RpayRatePay\Services\Config\ConfigService;
use RpayRatePay\Services\OrderStatusChangeService;
use Shopware\Components\Model\ModelManager;
Expand All @@ -25,8 +26,6 @@

class UpdateTransactionsSubscriber implements SubscriberInterface
{
const MSG_NOTIFY_UPDATES_TO_RATEPAY = '[%d/%d] Processing order %d ...notify needed updates to Ratepay';

/**
* @var string
*/
Expand Down Expand Up @@ -131,24 +130,37 @@ public function updateRatepayTransactions(Shopware_Components_Cron_CronJob $job)
$this->logger->info('Ratepay bidirectionality is turned off.');
return 'Ratepay bidirectionality is turned off.';
}

try {
$orderIds = $this->findCandidateOrdersForUpdate();
$totalOrders = count($orderIds);
foreach ($orderIds as $key => $orderId) {
$order = $this->modelManager->find(Order::class, $orderId);
$this->logger->info(
sprintf(self::MSG_NOTIFY_UPDATES_TO_RATEPAY, ($key + 1), $totalOrders, $orderId)
sprintf('Bidirectionality: Processing %d/%d order-id %d ...', ($key + 1), $totalOrders, $orderId),
[
'order_id' => $order->getId(),
'order_number' => $order->getNumber()
]
);
$this->orderStatusChangeService->informRatepayOfOrderStatusChange($order);
try {
$this->orderStatusChangeService->informRatepayOfOrderStatusChange($order);
} catch (Exception $e) {
$context = [
'order_id' => $order->getId(),
'order_number' => $order->getNumber(),
'trace' => $e->getTraceAsString()
];
if ($e instanceof RatepayException) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$context = array_merge($context, $e->getContext());
}
$this->logger->error('Bidirectionality: ' . $e->getMessage(), $context);
}
}
} catch (Exception $e) {
$this->logger->error(
sprintf('Fehler UpdateTransactionsSubscriber: %s %s', $e->getMessage(), $e->getTraceAsString())
);
$this->logger->error('bidirectionality: ' .$e->getMessage(), [$e->getTraceAsString()]);
return $e->getMessage();
}

return 'Success';
}

Expand All @@ -173,24 +185,21 @@ private function findCandidateOrdersForUpdate()
}

$changeDate = $this->getChangeDateLimit();
$paymentMethods = PaymentMethods::getNames();

$query = $this->db->select()
->distinct(true)
->from(['history' => 's_order_history'], null)
->joinLeft(['order' => 's_order'], 'history.orderID = order.id', ['id'])
->joinLeft(['payment' => 's_core_paymentmeans'], 'order.paymentID = payment.id', null)
->joinLeft(['s_order' => 's_order'], 'history.orderID = s_order.id', ['id'])
->joinLeft(['payment' => 's_core_paymentmeans'], 's_order.paymentID = payment.id', null)
->where('history.change_date >= :changeDate')
->where('order.status IN (:allowed_orderstatus)')
->where('payment.name IN (:payment_methods)')
->distinct(true);

$rows = $this->db->fetchAll(
$query,
[
':changeDate' => $changeDate,
':allowed_orderstatus' => $allowedOrderStates,
':payment_methods' => PaymentMethods::getNames()
->where("s_order.status IN (" . implode(",", $allowedOrderStates) . ")")
->where("payment.name IN ('" . implode("','", $paymentMethods) . "')")
->bind([
'changeDate' => $changeDate
]);

$rows = $this->db->fetchAll($query);
return array_column($rows, 'id');
}

Expand Down Expand Up @@ -230,7 +239,6 @@ private function getLastUpdateDate()

$this->_cronjobLastExecutionDate = $date;
}

return $this->_cronjobLastExecutionDate;
}

Expand Down
21 changes: 19 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,32 @@
<label lang="de">Ratepay Payment Plugin für Shopware 5</label>
<label>Ratepay Payment Plugin for Shopware 5</label>

<version>6.0.0</version>
<version>6.0.1</version>
<copyright>Copyright (c) 2020, Ratepay GmbH</copyright>
<license>MIT</license>
<link>https://www.ratepay.com</link>
<author>Ratepay GmbH</author>
<compatibility minVersion="5.5.0"/>

<description>Ratepay enables you to accept online payments wrten zur Verfügung, ohne dass Sie sich mit Risikoprüfungen beschäftigen müssen.ithout any risk of fraud and without the hassle of debtors management.</description>
<description lang="de">Ratepay stellt verschiedene Zahlungsa</description>
<description lang="de">Ratepay stellt verschiedene Zahlungsarten für Ihren Onlineshop zur Verfügung, ohne dass Sie sich mit der Risikoprüfung oder dem Debitorenmanagement beschäftigen müssen.</description>

<changelog version="6.0.1">
<changes>
<![CDATA[
<ul>
<li>Fixes some issues with the automatic bidirectionality</li>
</ul>
]]>
</changes>
<changes lang="de">
<![CDATA[
<ul>
<li>Behebt einen Fehler mit der automatischen Bidirektionalität</li>
</ul>
]]>
</changes>
</changelog>

<changelog version="6.0.0">
<changes>
Expand Down

0 comments on commit 90040d0

Please sign in to comment.