Skip to content

Commit

Permalink
RATESWSX-292: sw66x: improve backward compatibility to 6.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Feb 7, 2024
1 parent ee7636a commit 7f4d8f1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ parameters:
- '#generic class Shopware\\Core\\Framework\\Struct\\ArrayStruct does not specify its types#'
- '#generic class Shopware\\Core\\Framework\\Struct\\ArrayStruct but does not specify its types#'

# backward compatibility
- '#Call to method getOrderTransactionId\(\) on an unknown class Shopware\\Core\\Checkout\\Payment\\Exception\\SyncPaymentProcessException#'

# NEXT-29041 - Shopware issue: https://github.com/shopware/shopware/blob/v6.6.0.0-rc2/phpstan.neon.dist#L221
- '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#'
- '#\(\) should return .*\|null but returns Shopware\\Core\\Framework\\DataAbstractionLayer\\Entity\|null#'
Expand Down
12 changes: 11 additions & 1 deletion src/Components/OrderManagement/Controller/ProductPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentDeliverService;
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentReturnService;
use Ratepay\RpayPayments\Util\CriteriaHelper;
use RuntimeException;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Exception\InvalidRequestParameterException;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\Framework\Validation\DataValidationDefinition;
use Shopware\Core\Framework\Validation\DataValidator;
Expand Down Expand Up @@ -181,7 +183,15 @@ protected function processModify(Request $request, Context $context, string $ope
if ($order instanceof OrderEntity) {
$params = $request->request->all();
if (!isset($params['items']) || !is_array($params['items'])) {
throw RoutingException::invalidRequestParameter('items');
if (class_exists(RoutingException::class) && method_exists(RoutingException::class, 'invalidRequestParameter')) {
throw RoutingException::invalidRequestParameter('items');
} elseif (class_exists(InvalidRequestParameterException::class)) {
// required for shopware == 6.5.0.0
throw new InvalidRequestParameterException('items');
}

// should never occur - just to be safe
throw new RuntimeException('Invalid request parameter: items');
}

$items = [];
Expand Down
27 changes: 18 additions & 9 deletions src/Components/PaymentHandler/AbstractPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentRequestService;
use Ratepay\RpayPayments\Exception\RatepayException;
use Ratepay\RpayPayments\Util\CriteriaHelper;
use RuntimeException;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface;
use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct;
use Shopware\Core\Checkout\Payment\Exception\SyncPaymentProcessException;
use Shopware\Core\Checkout\Payment\PaymentException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
Expand All @@ -42,6 +44,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Throwable;

abstract class AbstractPaymentHandler implements SynchronousPaymentHandlerInterface
{
Expand Down Expand Up @@ -71,10 +74,7 @@ public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $d
$order = $this->getOrderWithAssociations($transaction->getOrder(), $salesChannelContext->getContext());

if (!$order instanceof OrderEntity || $ratepayData->count() === 0) {
throw PaymentException::syncProcessInterrupted(
$transaction->getOrderTransaction()->getId(),
'unknown error during payment'
);
throw $this->syncProcessInterrupted($transaction->getOrderTransaction()->getId(), 'unknown error during payment');
}

try {
Expand Down Expand Up @@ -137,11 +137,7 @@ public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $d
$session->getFlashBag()->add(StorefrontController::DANGER, $ratepayException->getMessage());
}

throw PaymentException::syncProcessInterrupted(
$transaction->getOrderTransaction()->getId(),
$ratepayException->getMessage(),
$ratepayException
);
throw $this->syncProcessInterrupted($transaction->getOrderTransaction()->getId(), $ratepayException->getMessage(), $ratepayException);
}
}

Expand Down Expand Up @@ -187,4 +183,17 @@ protected function getOrderWithAssociations(OrderEntity $order, Context $context
{
return $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($order->getId()), $context)->first();
}

private function syncProcessInterrupted(string $orderTransactionId, string $errorMessage, ?Throwable $e = null): Throwable
{
if (class_exists(PaymentException::class)) {
return PaymentException::syncProcessInterrupted($orderTransactionId, $errorMessage, $e);
} elseif (class_exists(SyncPaymentProcessException::class)) {
// required for shopware version <= 6.5.3
return new SyncPaymentProcessException($orderTransactionId, $errorMessage, $e); // @phpstan-ignore-line
}

// should never occur - just to be safe
return new RuntimeException('payment interrupted: ' . $errorMessage, 0, $e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ratepay\RpayPayments\Components\RedirectException\Exception\ForwardException;
use Ratepay\RpayPayments\Components\RedirectException\Exception\RedirectException;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Payment\Exception\SyncPaymentProcessException;
use Shopware\Core\Checkout\Payment\PaymentException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Routing\RequestTransformerInterface;
Expand Down Expand Up @@ -47,7 +48,8 @@ public function onKernelException(ExceptionEvent $event): void
$throwable = $event->getThrowable();

$prevThrowable = $throwable->getPrevious();
if ($prevThrowable instanceof PaymentException) {
// SyncPaymentProcessException is required for sw <= 6.5.3 compatibility
if ($prevThrowable instanceof PaymentException || (class_exists(SyncPaymentProcessException::class) && $prevThrowable instanceof SyncPaymentProcessException)) {
// set the transaction to failed. Without this, the customer will not be able to try a repayment.
$this->orderTransactionStateHandler->fail($prevThrowable->getOrderTransactionId(), Context::createDefaultContext());
}
Expand Down

0 comments on commit 7f4d8f1

Please sign in to comment.