Skip to content

Commit

Permalink
Remove session dependency from payment redirect handler
Browse files Browse the repository at this point in the history
ISSUE: CS-3209
  • Loading branch information
goran-stamenkovski-logeecom committed Jun 29, 2022
1 parent 9c38b3f commit 03298ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 0 additions & 1 deletion AdyenPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ final class AdyenPayment extends Plugin
public const NAME = 'AdyenPayment';
public const ADYEN_CODE = 'adyen_type';
public const ADYEN_STORED_PAYMENT_UMBRELLA_CODE = 'adyen_stored_payment_umbrella';
public const SESSION_ADYEN_RESTRICT_EMAILS = 'adyenRestrictEmail';
public const SESSION_ADYEN_PAYMENT_INFO_ID = 'adyenPaymentInfoId';
public const SESSION_ADYEN_STORED_METHOD_ID = 'adyenStoredMethodId';

Expand Down
25 changes: 25 additions & 0 deletions Components/OrderMailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@ class OrderMailService
{
private ModelManager $modelManager;
private BasketService $basketService;
private bool $isOrderConfirmationEmailRestricted = false;

public function __construct(ModelManager $modelManager, BasketService $basketService)
{
$this->modelManager = $modelManager;
$this->basketService = $basketService;
}

/**
* Executes provided callback without sending order confirmation email.
*
* @param callable $callback The callback to execute without email sending
* @param array $args The parameters to be passed to the callback, as an indexed array
*/
public function doWithoutSendingOrderConfirmationMail(callable $callback, array $args = [])
{
$this->isOrderConfirmationEmailRestricted = true;

try {
$result = call_user_func_array($callback, $args);
} finally {
$this->isOrderConfirmationEmailRestricted = false;
}

return $result;
}

/**
* Sends the mail after a payment is confirmed.
*/
Expand Down Expand Up @@ -49,4 +69,9 @@ public function sendOrderConfirmationMail(string $orderNumber): void
$this->modelManager->persist($paymentInfo);
$this->modelManager->flush($paymentInfo);
}

public function isOrderConfirmationEmailRestricted(): bool
{
return $this->isOrderConfirmationEmailRestricted;
}
}
17 changes: 5 additions & 12 deletions Controllers/Frontend/Adyen.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use AdyenPayment\AdyenPayment;
use AdyenPayment\Components\Adyen\PaymentMethodService;
use AdyenPayment\Components\BasketService;
use AdyenPayment\Components\OrderMailService;
use AdyenPayment\Models\PaymentResultCode;
use AdyenPayment\Components\Manager\AdyenManager;
use AdyenPayment\Components\Payload\Chain;
Expand All @@ -25,6 +26,7 @@ class Shopware_Controllers_Frontend_Adyen extends Shopware_Controllers_Frontend_
private Logger $logger;
private Chain $paymentPayloadProvider;
private BasketService $basketService;
private OrderMailService $orderMailService;

/**
* @return void
Expand All @@ -36,6 +38,7 @@ public function preDispatch()
$this->logger = $this->get('adyen_payment.logger');
$this->paymentPayloadProvider = $this->get(PaymentPayloadProvider::class);
$this->basketService = $this->get(BasketService::class);
$this->orderMailService = $this->get(OrderMailService::class);
}

public function ajaxDoPaymentAction(): void
Expand Down Expand Up @@ -192,11 +195,6 @@ private function prepareOrder(PaymentInfo $transaction): Order
{
$signature = "adyen_{$transaction->getId()}_{$this->persistBasket()}";

Shopware()->Session()->offsetSet(
AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS,
0 < $transaction->getId()
);

Shopware()->Session()->offsetSet(
AdyenPayment::SESSION_ADYEN_PAYMENT_INFO_ID,
$transaction->getId()
Expand All @@ -206,15 +204,10 @@ private function prepareOrder(PaymentInfo $transaction): Order
Shopware()->Session()->offsetSet('sComment', $this->Request()->getParam('sComment'));
}

$orderNumber = $this->saveOrder(
$transaction->getId(),
$signature,
Status::PAYMENT_STATE_OPEN,
false
$orderNumber = $this->orderMailService->doWithoutSendingOrderConfirmationMail(
[$this, 'saveOrder'], [$transaction->getId(), $signature, Status::PAYMENT_STATE_OPEN, false]
);

Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, false);

/** @var Order $order */
$order = $this->getModelManager()->getRepository(Order::class)->findOneBy([
'number' => $orderNumber,
Expand Down
2 changes: 2 additions & 0 deletions Controllers/Frontend/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public function returnAction(): void
]);
break;
}

$this->orderManager->save($order);
}
}

Expand Down
5 changes: 3 additions & 2 deletions Subscriber/OrderEmailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ public function shouldStopEmailSending(Enlight_Event_EventArgs $args)
{
$variables = $args->get('variables');
$paymentMean = PaymentMean::createFromShopwareArray($variables['additional']['payment'] ?? []);
if ($paymentMean->getSource()->equals(SourceType::adyen())
&& true === Shopware()->Session()->get(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, true)
if (
$this->orderMailService->isOrderConfirmationEmailRestricted()
&& $paymentMean->getSource()->equals(SourceType::adyen())
) {
/** @var PaymentInfo $paymentInfo */
$paymentInfo = $this->paymentInfoRepository->findOneBy([
Expand Down

0 comments on commit 03298ba

Please sign in to comment.