Skip to content

Commit

Permalink
Merge pull request #36 from payrexx/bugfix/PP-12519
Browse files Browse the repository at this point in the history
bugfix/PP-12519: Shopware 6: Uncaught PaymentException
  • Loading branch information
vinothss4u authored Dec 3, 2024
2 parents c28095a + 4a6453c commit 2622253
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 8 deletions.
8 changes: 8 additions & 0 deletions PayrexxPaymentGatewaySW6/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

## [1.0.44]
### Fixed
- Bug fix: Resolved an issue where deleting a payment gateway for transition change.

### Added
- Added support for a new URL to handle unexpected errors.
2 changes: 1 addition & 1 deletion PayrexxPaymentGatewaySW6/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "payrexx/payment",
"description": "A Shopware plugin to accept payments with Payrexx",
"version": "1.0.43",
"version": "1.0.44",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
Expand Down
24 changes: 21 additions & 3 deletions PayrexxPaymentGatewaySW6/src/Handler/PaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
use Shopware\Core\Checkout\Payment\PaymentException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

class PaymentHandler implements AsynchronousPaymentHandlerInterface
{
Expand Down Expand Up @@ -72,14 +74,20 @@ class PaymentHandler implements AsynchronousPaymentHandlerInterface
*/
protected $logger;

/**
* @var RouterInterface
*/
protected $router;

/**
* @param OrderTransactionStateHandler $transactionStateHandler
* @param ContainerInterface $container
* @param CustomerService $customerService
* @param PayrexxApiService $payrexxApiService
* @param TransactionHandler $transactionHandler
* @param ConfigService $configService
* @param type $logger
* @param LoggerInterface $logger
* @param RouterInterface $router
*/
public function __construct(
OrderTransactionStateHandler $transactionStateHandler,
Expand All @@ -88,7 +96,8 @@ public function __construct(
PayrexxApiService $payrexxApiService,
TransactionHandler $transactionHandler,
ConfigService $configService,
$logger
LoggerInterface $logger,
RouterInterface $router
) {
$this->transactionStateHandler = $transactionStateHandler;
$this->container = $container;
Expand All @@ -97,6 +106,7 @@ public function __construct(
$this->transactionHandler = $transactionHandler;
$this->configService = $configService;
$this->logger = $logger;
$this->router = $router;
}

/**
Expand Down Expand Up @@ -176,6 +186,13 @@ public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $
if (in_array($paymentMean, ['sofortueberweisung_de', 'postfinance_card', 'postfinance_efinance'])) {
throw new Exception('Unavailable payment method error');
}
$cancelUrl = $this->router->generate('frontend.payrexx-payment.cancel',
[
'orderId' => $order->getOrderNumber(),
'transactionId' => $transactionId,
],
UrlGeneratorInterface::ABSOLUTE_URL
);
// Create Payrexx Gateway link for checkout and redirect user
try {
$payrexxGateway = $this->payrexxApiService->createPayrexxGateway(
Expand All @@ -188,7 +205,8 @@ public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $
$transaction->getReturnUrl(),
$basket,
$salesChannelId,
$purpose
$purpose,
$cancelUrl
);

if (!$payrexxGateway) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

<import resource="../../../../Controller/**/*Controller.php" type="attribute"/>
<import resource="../../../../Webhook/**/Dispatcher.php" type="attribute"/>
<import resource="../../../../Webhook/**/Cancel.php" type="attribute"/>
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

<import resource="../../../../Controller/**/*Controller.php" type="annotation"/>
<import resource="../../../../Webhook/**/Dispatcher.php" type="annotation"/>
</routes>
<import resource="../../../../Webhook/**/Cancel.php" type="annotation"/>
</routes>
7 changes: 6 additions & 1 deletion PayrexxPaymentGatewaySW6/src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<argument type="service" id="PayrexxPaymentGateway\Handler\TransactionHandler" />
<argument type="service" id="PayrexxPaymentGateway\Service\ConfigService" />
<argument type="service" id="monolog.logger"/>
<argument type="service" id="router" />
<tag name="shopware.payment.method.async" />
</service>

Expand Down Expand Up @@ -65,5 +66,9 @@
<argument type="service" id="monolog.logger"/>
</service>

</services>
<service id="PayrexxPaymentGateway\Webhook\Cancel">
<argument type="service" id="service_container" />
<argument type="service" id="PayrexxPaymentGateway\Handler\TransactionHandler" />
</service>
</services>
</container>
7 changes: 5 additions & 2 deletions PayrexxPaymentGatewaySW6/src/Service/PayrexxApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

Expand Down Expand Up @@ -64,6 +65,7 @@ private function getInterface($salesChannelId): \Payrexx\Payrexx
* @param array $basket
* @param string $salesChannelId
* @param string $purpose
* @param string $cancelUrl
* @return Gateway
*
*/
Expand All @@ -77,7 +79,8 @@ public function createPayrexxGateway(
string $url,
array $basket,
string $salesChannelId,
string $purpose
string $purpose,
string $cancelUrl
) {
$config = $this->configService->getPluginConfiguration($salesChannelId);
$lookAndFeelId = !empty($config['lookFeelID']) ? $config['lookFeelID'] : null;
Expand All @@ -89,7 +92,7 @@ public function createPayrexxGateway(
$gateway->setCurrency($currency);
$gateway->setSuccessRedirectUrl($url);
$gateway->setFailedRedirectUrl($url);
$gateway->setCancelRedirectUrl($url);
$gateway->setCancelRedirectUrl($cancelUrl);
$gateway->setSkipResultPage(true);
$gateway->setLookAndFeelProfile($lookAndFeelId);

Expand Down
124 changes: 124 additions & 0 deletions PayrexxPaymentGatewaySW6/src/Webhook/Cancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/*
* Copyright (c) Pickware GmbH. All rights reserved.
* This file is part of software that is released under a proprietary license.
* You must not copy, modify, distribute, make publicly available, or execute
* its contents or parts thereof without express permission by the copyright
* holder, unless otherwise permitted by law.
*/

declare(strict_types=1);

namespace PayrexxPaymentGateway\Webhook;

use PayrexxPaymentGateway\Handler\TransactionHandler;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* @Route(defaults={"_routeScope"={"storefront"}})
*/
#[Route(defaults: ['_routeScope' => ['storefront']])]
class Cancel
{

/**
* @var ContainerInterface
*/
protected $container;

/**
* @var TransactionHandler
*/
protected $transactionHandler;

/**
* @param ContainerInterface $container
* @param TransactionHandler $transactionHandler
*/
public function __construct(
ContainerInterface $container,
TransactionHandler $transactionHandler
) {
$this->container = $container;
$this->transactionHandler = $transactionHandler;
}

/**
* @Route(
* "/payrexx-payment/cancel",
* name="frontend.payrexx-payment.cancel",
* methods={"GET"},
* defaults={"csrf_protected"=false}
* )
*
* @param Request $request
* @param Context $context
* @return Response
*/
#[Route(path: '/payrexx-payment/cancel', name: 'frontend.payrexx-payment.cancel', methods: ['GET'], defaults: ['csrf_protected' => false])]
public function executeCancel(Request $request, Context $context): Response
{
$orderRepository = $this->container->get('order.repository');
$transactionRepo = $this->container->get('order_transaction.repository');
$router = $this->container->get('router');

// Get parameters from the request
$orderNumber = $request->query->get('orderId');
$transactionId = $request->query->get('transactionId');

// Validate input
if (!$orderNumber || !$transactionId) {
return new Response('Invalid request parameters.', Response::HTTP_BAD_REQUEST);
}

// Search for the order
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('orderNumber', $orderNumber)
);

$order = $orderRepository->search($criteria, $context)->first();
if (!$order) {
return new Response('Order not found.', Response::HTTP_NOT_FOUND);
}

// Search for the transaction
$criteria = new Criteria([$transactionId]);
$criteria->addFilter(
new EqualsFilter('orderId', $order->getId())
);
$criteria->addAssociation('stateMachineState');

$transaction = $transactionRepo->search($criteria, $context)->first();

if (!$transaction) {
return new Response('Transaction not found.', Response::HTTP_NOT_FOUND);
}

if (!class_exists(\Payrexx\Models\Response\Transaction::class)) {
require_once dirname(dirname(__DIR__)). '/vendor/autoload.php';
}

$this->transactionHandler->handleTransactionStatus(
$transaction,
OrderTransactionStates::STATE_CANCELLED,
$context
);
$redirectUrl = $router->generate(
'frontend.account.edit-order.page',
['orderId' => $order->getId()],
UrlGeneratorInterface::ABSOLUTE_URL
);
return new RedirectResponse($redirectUrl);
}
}

0 comments on commit 2622253

Please sign in to comment.