Skip to content

Commit

Permalink
PAYOSWXP-115: generic-express: prevent changing addresses/payment-met…
Browse files Browse the repository at this point in the history
…hod and add back-link
  • Loading branch information
rommelfreddy committed Mar 21, 2024
1 parent eac0da5 commit e522438
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final public function filterPaymentMethods(

// Validate and remove all supported payment methods if necessary
try {
if ($filterContext->getFlag(PaymentFilterContext::FLAG_SKIP_EC_REQUIRED_DATA_VALIDATION) !== true) {
if ($filterContext->hasFlag(PaymentFilterContext::FLAG_SKIP_EC_REQUIRED_DATA_VALIDATION) !== true) {
$this->validateGenericExpressCheckout($filterContext);
}
$this->validateCurrency($currency);
Expand Down
27 changes: 27 additions & 0 deletions src/Components/PaymentFilter/GenericExpressCheckoutFilterOther.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\PaymentHandler\PaymentHandlerGroups;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;

class GenericExpressCheckoutFilterOther implements PaymentFilterServiceInterface
{
/**
* if AmazonPay/PayPal Express is selected, no other payment methods should be available.
*/
public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): PaymentMethodCollection
{
$actualPaymentMethod = $filterContext->getSalesChannelContext()->getPaymentMethod();

if ($methodCollection->has($actualPaymentMethod->getId())
&& \in_array($actualPaymentMethod->getHandlerIdentifier(), PaymentHandlerGroups::GENERIC_EXPRESS, true)
) {
return new PaymentMethodCollection([$actualPaymentMethod]);
}

return $methodCollection;
}
}
7 changes: 5 additions & 2 deletions src/Components/PaymentFilter/PaymentFilterContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class PaymentFilterContext extends Struct

private bool $_areAddressesIdentical;

/**
* @param string[] $flags
*/
public function __construct(
private readonly SalesChannelContext $salesChannelContext,
private readonly CustomerAddressEntity|OrderAddressEntity|null $billingAddress = null,
Expand Down Expand Up @@ -87,8 +90,8 @@ public function areAddressesIdentical(): bool
return $this->_areAddressesIdentical = true;
}

public function getFlag(string $key): bool
public function hasFlag(string $key): bool
{
return $this->flags[$key] ?? false;
return \in_array($key, $this->flags, true);
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,10 @@
<tag name="controller.service_arguments" />
</service>

<service id="PayonePayment\Storefront\Controller\CheckoutController" autowire="true">
<argument key="$translator" type="service" id="translator"/>
<tag name="controller.service_arguments" />
</service>

</services>
</container>
4 changes: 4 additions & 0 deletions src/DependencyInjection/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,9 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="PayonePayment\EventListener\CheckoutConfirmGenericExpressCheckoutEventListener">
<tag name="kernel.event_subscriber"/>
</service>

</services>
</container>
5 changes: 5 additions & 0 deletions src/DependencyInjection/payment_method_filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,17 @@
</argument>
<tag name="payone.payment_method.filter"/>
</service>

<service id="payone.payment_filter_method.amazonpay.express"
class="PayonePayment\Components\PaymentFilter\DefaultPaymentFilterService"
parent="payone.payment_filter_method.amazonpay">
<argument key="$paymentHandlerClass">PayonePayment\PaymentHandler\PayoneAmazonPayExpressPaymentHandler</argument>
<tag name="payone.payment_method.filter"/>
</service>

<service id="PayonePayment\Components\PaymentFilter\GenericExpressCheckoutFilterOther">
<tag name="payone.payment_method.filter"/>
</service>

</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelPaymentMethod\SalesChannelPaymentMethodDefinition;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function onCartLoaded(CheckoutCartPageLoadedEvent|OffcanvasCartPageLoaded
currency: $event->getSalesChannelContext()->getCurrency(),
cart: $event->getPage()->getCart(),
flags: [
PaymentFilterContext::FLAG_SKIP_EC_REQUIRED_DATA_VALIDATION => true,
PaymentFilterContext::FLAG_SKIP_EC_REQUIRED_DATA_VALIDATION,
]
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PayonePayment\EventListener;

use PayonePayment\PaymentHandler\PaymentHandlerGroups;
use PayonePayment\Storefront\Struct\CheckoutConfirmPaymentData;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CheckoutConfirmGenericExpressCheckoutEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
];
}

public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$paymentMethod = $event->getSalesChannelContext()->getPaymentMethod();
if (\in_array($paymentMethod->getHandlerIdentifier(), PaymentHandlerGroups::GENERIC_EXPRESS, true) === false) {
// payment handler is not a generic express-checkout
return;
}

$extension = $event->getPage()->getExtension(CheckoutConfirmPaymentData::EXTENSION_NAME) ?? new CheckoutConfirmPaymentData();
$extension->assign([
'showExitExpressCheckoutLink' => true,
'preventAddressEdit' => true,
]);
$event->getPage()->addExtension(CheckoutConfirmPaymentData::EXTENSION_NAME, $extension);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/routes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<import resource="@PayonePayment/Storefront/Controller/Ratepay/RatepayController.php" type="annotation"/>
<import resource="@PayonePayment/Storefront/Controller/AmazonPay/AmazonRedirectController.php" type="annotation"/>
<import resource="@PayonePayment/Storefront/Controller/CheckoutController.php" type="annotation"/>

<!-- Store-Api routes -->
<import resource="@PayonePayment/StoreApi/Route/CardRoute.php" type="annotation" />
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/snippet/de_DE/messages.de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"checkoutConfirmPage": {
"defaultCardTitle": "Zahlungsinformationen",
"save": "Daten für zukünftige Nutzung speichern",
"saveMandate": "Lastschrift-Mandat für zukünftige Nutzung speichern"
"saveMandate": "Lastschrift-Mandat für zukünftige Nutzung speichern",
"back-to-standard-checkout": "Zahlungsart ändern",
"express-checkout-canceled": "Der Express-Checkout wurde abgebrochen."
},
"checkoutFinishPage": {
"mandateIdentification": "SEPA-Lastschriftmandat"
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/snippet/en_GB/messages.en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"checkoutConfirmPage": {
"defaultCardTitle": "Payment Information",
"save": "Save data for future use",
"saveMandate": "Save direct debit mandate for future use"
"saveMandate": "Save direct debit mandate for future use",
"back-to-standard-checkout": "Change payment method",
"express-checkout-canceled": "The express checkout has been cancelled."
},
"checkoutFinishPage": {
"mandateIdentification": "Direct Debit Mandate"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% sw_extends '@Storefront/storefront/page/checkout/confirm/confirm-address.html.twig' %}

{% block page_checkout_confirm_address_shipping_actions %}
{% if not page.extensions.payone or not page.extensions.payone.preventAddressEdit %}
{{ parent() }}
{% endif %}
{% endblock %}

{% block page_checkout_confirm_address_billing_actions %}
{% if not page.extensions.payone or not page.extensions.payone.preventAddressEdit %}
{{ parent() }}
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% sw_extends '@Storefront/storefront/page/checkout/confirm/confirm-payment.html.twig' %}

{% block page_checkout_change_payment_form %}
{{ parent() }}

{% block page_checkout_change_payment_form_payone_back_to_standard_checkout %}
{% if page.extensions.payone and page.extensions.payone.showExitExpressCheckoutLink|default(false) %}
<div class="card-actions">
<a href="{{ url('frontend.payone.delete-checkout-data') }}">{{ 'PayonePayment.checkoutConfirmPage.back-to-standard-checkout' | trans }}</a>
</div>
{% endif %}
{% endblock %}

{% endblock %}
47 changes: 47 additions & 0 deletions src/Storefront/Controller/CheckoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Storefront\Controller;

use PayonePayment\Storefront\Struct\CheckoutCartPaymentData;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class CheckoutController
{
public function __construct(
private readonly CartService $cartService,
private readonly RouterInterface $router,
private readonly TranslatorInterface $translator
) {
}

#[Route(path: '/payone/delete-checkout-data', name: 'frontend.payone.delete-checkout-data')]
public function deleteCheckoutData(
Request $request,
SalesChannelContext $salesChannelContext
): Response {
$cart = $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
$cart->removeExtension(CheckoutCartPaymentData::EXTENSION_NAME);
$this->cartService->recalculate($cart, $salesChannelContext);

$session = $request->getSession();
if (method_exists($session, 'getFlashBag')) {
$session->getFlashBag()->add(
StorefrontController::SUCCESS,
$this->translator->trans('PayonePayment.checkoutConfirmPage.express-checkout-canceled')
);
}

return new RedirectResponse($this->router->generate('frontend.checkout.confirm.page'));
}
}
14 changes: 14 additions & 0 deletions src/Storefront/Struct/CheckoutConfirmPaymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class CheckoutConfirmPaymentData extends Struct

protected ?EntitySearchResult $savedMandates = null;

protected bool $showExitExpressCheckoutLink = false;

protected bool $preventAddressEdit = false;

public function getCardRequest(): array
{
return $this->cardRequest;
Expand Down Expand Up @@ -59,4 +63,14 @@ public function getSavedMandates(): ?EntitySearchResult
{
return $this->savedMandates;
}

public function isshowExitExpressCheckoutLink(): bool
{
return $this->showExitExpressCheckoutLink;
}

public function isPreventAddressEdit(): bool
{
return $this->preventAddressEdit;
}
}

0 comments on commit e522438

Please sign in to comment.