From 0226e2f3caff7bfc5dc4bcf88a80d7bba9e1a5af Mon Sep 17 00:00:00 2001 From: Aleksandar Sasa Boljanovic Date: Wed, 21 Feb 2024 17:52:24 +0100 Subject: [PATCH] Add support for language shops ISSUE: CS-5124 --- Components/CheckoutConfigProvider.php | 14 +++-- .../ShopperReferenceProcessor.php | 5 +- Components/PaymentMeansEnricher.php | 4 +- Controllers/Frontend/AdyenDonations.php | 21 +++++++- Controllers/Frontend/AdyenPaymentProcess.php | 54 +++++++++++++++++-- Subscriber/EnrichPaymentSubscriber.php | 3 +- Utilities/Shop.php | 21 ++++++++ 7 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 Utilities/Shop.php diff --git a/Components/CheckoutConfigProvider.php b/Components/CheckoutConfigProvider.php index a50179cb..68d72f71 100644 --- a/Components/CheckoutConfigProvider.php +++ b/Components/CheckoutConfigProvider.php @@ -13,6 +13,7 @@ use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Country; use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\PaymentMethodCode; use Adyen\Core\BusinessLogic\Domain\Payment\Models\MethodAdditionalData\CardConfig; +use AdyenPayment\Utilities\Shop; use Enlight_Components_Session_Namespace; use Shopware\Models\Customer\Customer; @@ -41,7 +42,10 @@ public function __construct(Enlight_Components_Session_Namespace $session) } /** + * @param Amount|null $forceAmount + * * @return Response + * * @throws InvalidCurrencyCode */ public function getCheckoutConfig(?Amount $forceAmount = null): Response @@ -50,7 +54,7 @@ public function getCheckoutConfig(?Amount $forceAmount = null): Response $response = $this->getCheckoutConfigResponse($request, false, static function (PaymentCheckoutConfigRequest $request) { return CheckoutAPI::get() - ->checkoutConfig(Shopware()->Shop()->getId()) + ->checkoutConfig(Shop::getShopId()) ->getPaymentCheckoutConfig($request); }); @@ -71,7 +75,9 @@ public function getCheckoutConfig(?Amount $forceAmount = null): Response /** * @param Amount|null $forceAmount + * * @return Response + * * @throws InvalidCurrencyCode */ public function getExpressCheckoutConfig(Amount $forceAmount): Response @@ -80,7 +86,7 @@ public function getExpressCheckoutConfig(Amount $forceAmount): Response return $this->getCheckoutConfigResponse($request, true, static function (PaymentCheckoutConfigRequest $request) { return CheckoutAPI::get() - ->checkoutConfig(Shopware()->Shop()->getId()) + ->checkoutConfig(Shop::getShopId()) ->getExpressPaymentCheckoutConfig($request); }); } @@ -109,7 +115,7 @@ private function buildConfigRequest(?Amount $forceAmount = null): PaymentCheckou $shop = Shopware()->Shop(); $userId = (int)$this->session->offsetGet('sUserId'); - $shopperReference = ($userId !== 0) ? $shop->getHost() . '_' . $shop->getId() . '_' . $userId : null; + $shopperReference = ($userId !== 0) ? $shop->getHost() . '_' . Shop::getShopId() . '_' . $userId : null; return new PaymentCheckoutConfigRequest( $this->getAmount($forceAmount), @@ -123,7 +129,9 @@ private function buildConfigRequest(?Amount $forceAmount = null): PaymentCheckou * Gets the response from cache or makes the response and cache the result by calling $responseCallback * * @param PaymentCheckoutConfigRequest $request + * @param bool $isExpressCheckout * @param callable $responseCallback + * * @return Response|PaymentCheckoutConfigResponse */ private function getCheckoutConfigResponse( diff --git a/Components/Integration/PaymentProcessors/ShopperReferenceProcessor.php b/Components/Integration/PaymentProcessors/ShopperReferenceProcessor.php index 04dc4a23..01d5c49f 100644 --- a/Components/Integration/PaymentProcessors/ShopperReferenceProcessor.php +++ b/Components/Integration/PaymentProcessors/ShopperReferenceProcessor.php @@ -8,6 +8,7 @@ use Adyen\Core\BusinessLogic\Domain\Integration\Processors\PaymentLinkRequest\ShopperReferenceProcessor as PaymentLinkShopperReferenceProcessorInterface; use Adyen\Core\BusinessLogic\Domain\Multistore\StoreContext; use AdyenPayment\Repositories\Wrapper\OrderRepository; +use AdyenPayment\Utilities\Shop; use Exception; /** @@ -35,7 +36,7 @@ public function __construct(OrderRepository $orderRepository) * @param PaymentLinkRequestContext $context * * @return void - * + * * @throws Exception */ public function processPaymentLink(PaymentLinkRequestBuilder $builder, PaymentLinkRequestContext $context): void @@ -53,7 +54,7 @@ public function processPaymentLink(PaymentLinkRequestBuilder $builder, PaymentLi $builder->setShopperReference( ShopperReference::parse( - $shop->getHost() . '_' . $shop->getId() . '_' . $order->getCustomer()->getId() + $shop->getHost() . '_' . Shop::getShopId() . '_' . $order->getCustomer()->getId() ) ); } diff --git a/Components/PaymentMeansEnricher.php b/Components/PaymentMeansEnricher.php index 16d12694..a22d4ad8 100644 --- a/Components/PaymentMeansEnricher.php +++ b/Components/PaymentMeansEnricher.php @@ -9,6 +9,7 @@ use Adyen\Core\BusinessLogic\Domain\Payment\Models\PaymentMethod; use AdyenPayment\AdyenPayment; use AdyenPayment\Utilities\Plugin; +use AdyenPayment\Utilities\Shop; use DateTime; use Shopware_Components_Snippet_Manager; @@ -49,8 +50,7 @@ public function __construct( */ public function enrich(array $paymentMeans): array { - if (AdminAPI::get()->integration(Shopware()->Shop()->getId())->getState()->toArray( - ) !== StateResponse::dashboard()->toArray()) { + if (AdminAPI::get()->integration(Shop::getShopId())->getState()->toArray() !== StateResponse::dashboard()->toArray()) { $this->removeAdyenPaymentMeans($paymentMeans); return $paymentMeans; diff --git a/Controllers/Frontend/AdyenDonations.php b/Controllers/Frontend/AdyenDonations.php index d3668f00..ed7c1c19 100644 --- a/Controllers/Frontend/AdyenDonations.php +++ b/Controllers/Frontend/AdyenDonations.php @@ -2,8 +2,11 @@ use Adyen\Core\BusinessLogic\CheckoutAPI\CheckoutAPI; use Adyen\Core\BusinessLogic\CheckoutAPI\Donations\Request\MakeDonationRequest; +use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Exceptions\InvalidCurrencyCode; +use Adyen\Core\BusinessLogic\Domain\Connection\Exceptions\ConnectionSettingsNotFountException; use AdyenPayment\Components\ErrorMessageProvider; use AdyenPayment\Controllers\Common\AjaxResponseSetter; +use AdyenPayment\Utilities\Shop; /** * Class Shopware_Controllers_Frontend_AdyenDonations @@ -43,6 +46,7 @@ public function initController($request, $response): void /** * @return void + * * @throws Exception */ public function preDispatch(): void @@ -52,23 +56,36 @@ public function preDispatch(): void $this->snippets = $this->get('snippets'); } + /** + * @return void + * + * @throws ConnectionSettingsNotFountException + * @throws Exception + */ public function getDonationsConfigAction(): void { $merchantReference = $this->Request()->get('merchantReference'); $currencyFactor = Shopware()->Shop()->getCurrency()->getFactor(); $result = CheckoutAPI::get() - ->donation(Shopware()->Shop()->getId()) + ->donation(Shop::getShopId()) ->getDonationSettings($merchantReference, empty($currencyFactor) ? 1 : $currencyFactor); $this->returnAPIResponse($result); } + /** + * @return void + * + * @throws ConnectionSettingsNotFountException + * @throws InvalidCurrencyCode + * @throws Exception + */ public function makeDonationsAction(): void { $params = $this->Request()->getParams(); $result = CheckoutAPI::get() - ->donation(Shopware()->Shop()->getId()) + ->donation(Shop::getShopId()) ->makeDonation( new MakeDonationRequest( $params['amount']['value'] ?? '', diff --git a/Controllers/Frontend/AdyenPaymentProcess.php b/Controllers/Frontend/AdyenPaymentProcess.php index 95d567e5..1f57247c 100644 --- a/Controllers/Frontend/AdyenPaymentProcess.php +++ b/Controllers/Frontend/AdyenPaymentProcess.php @@ -7,6 +7,7 @@ use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Amount\Amount; use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Amount\Currency; use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\ShopperReference; +use Adyen\Core\BusinessLogic\Domain\Connection\Exceptions\ConnectionSettingsNotFountException; use Adyen\Core\Infrastructure\Logger\Logger; use AdyenPayment\AdyenPayment; use AdyenPayment\Components\CheckoutConfigProvider; @@ -14,6 +15,7 @@ use AdyenPayment\Components\PaymentMeansEnricher; use AdyenPayment\Utilities\Plugin; use AdyenPayment\Utilities\Url; +use AdyenPayment\Utilities\Shop; /** * Class Shopware_Controllers_Frontend_AdyenPaymentProcess @@ -57,6 +59,7 @@ public function initController($request, $response): void /** * @return void + * * @throws Exception */ public function preDispatch(): void @@ -70,7 +73,9 @@ public function preDispatch(): void /** * Main entry point for checkout processing of adyen payment + * * @return void + * * @throws Exception */ public function indexAction(): void @@ -108,7 +113,7 @@ public function indexAction(): void } $response = CheckoutAPI::get() - ->paymentRequest(Shopware()->Shop()->getId()) + ->paymentRequest(Shop::getShopId()) ->startTransaction( new StartTransactionRequest( $paymentMethodType, @@ -189,6 +194,11 @@ public function indexAction(): void $this->view->assign('orderReference', $orderReference); } + /** + * @return void + * + * @throws Exception + */ public function handleAdditionalDataAction(): void { $this->setupRedirectResponse( @@ -196,6 +206,11 @@ public function handleAdditionalDataAction(): void ); } + /** + * @return void + * + * @throws Exception + */ public function handleRedirectAction(): void { Logger::logDebug( @@ -234,6 +249,12 @@ public function getCheckoutConfigAction(): void ); } + /** + * @return void + * + * @throws Enlight_Exception + * @throws ConnectionSettingsNotFountException + */ public function disableCardDetailsAction(): void { $this->Front()->Plugins()->ViewRenderer()->setNoRender(); @@ -281,7 +302,7 @@ public function disableCardDetailsAction(): void $recurringToken ); - $result = CheckoutAPI::get()->checkoutConfig(Shopware()->Shop()->getId())->disableStoredDetails($disableRequest); + $result = CheckoutAPI::get()->checkoutConfig(Shop::getShopId())->disableStoredDetails($disableRequest); if (!$result->isSuccessful()) { $this->Response()->setBody( @@ -297,6 +318,13 @@ public function disableCardDetailsAction(): void $this->Response()->setBody(json_encode(['success' => true])); } + /** + * @param array $additionalData + * + * @return string + * + * @throws Exception + */ private function handleAdditionalDataAndGetRedirectUrl(array $additionalData): string { $basketSignature = $this->Request()->get('signature'); @@ -318,7 +346,7 @@ private function handleAdditionalDataAndGetRedirectUrl(array $additionalData): s } $response = CheckoutAPI::get() - ->paymentRequest(Shopware()->Shop()->getId()) + ->paymentRequest(Shop::getShopId()) ->updatePaymentDetails(array_key_exists('details', $additionalData) ? $additionalData : ['details' => $additionalData]); if (!$response->isSuccessful()) { @@ -342,6 +370,14 @@ private function handleAdditionalDataAndGetRedirectUrl(array $additionalData): s return Url::getFrontUrl('checkout', 'finish', ['sUniqueID' => $orderReference]); } + /** + * @param string $redirectUrl + * + * @return void + * + * @throws Enlight_Exception + * @throws Exception + */ private function setupRedirectResponse(string $redirectUrl) { if ($this->isAjaxRequest()) { @@ -356,11 +392,19 @@ private function setupRedirectResponse(string $redirectUrl) $this->redirect($redirectUrl); } - private function isAjaxRequest() + /** + * @return bool + */ + private function isAjaxRequest(): bool { return $this->Request()->getParam('isXHR') || $this->session->offsetGet('adyenIsXHR'); } + /** + * @param string $basketSignature + * + * @return string + */ private function generateOrderReference(string $basketSignature): string { /** @@ -384,6 +428,6 @@ private function getShopperReference(): ?ShopperReference return null; } - return ShopperReference::parse($shop->getHost() . '_' . $shop->getId() . '_' . $user); + return ShopperReference::parse($shop->getHost() . '_' . Shop::getShopId() . '_' . $user); } } diff --git a/Subscriber/EnrichPaymentSubscriber.php b/Subscriber/EnrichPaymentSubscriber.php index 2e08329e..d2ec5369 100755 --- a/Subscriber/EnrichPaymentSubscriber.php +++ b/Subscriber/EnrichPaymentSubscriber.php @@ -10,6 +10,7 @@ use Adyen\Core\Infrastructure\ServiceRegister; use AdyenPayment\AdyenPayment; use AdyenPayment\Components\PaymentMeansEnricher; +use AdyenPayment\Utilities\Shop; use Enlight\Event\SubscriberInterface; use Enlight_Event_EventArgs; use Exception; @@ -83,7 +84,7 @@ private function isCreditCardEnabled(): bool $repository = ServiceRegister::getService(PaymentMethodConfigRepository::class); $cardConfig = StoreContext::doWithStore( - '' . Shopware()->Shop()->getId(), + '' . Shop::getShopId(), [$repository, 'getPaymentMethodByCode'], [(string)PaymentMethodCode::scheme()] ); diff --git a/Utilities/Shop.php b/Utilities/Shop.php new file mode 100644 index 00000000..c634d93d --- /dev/null +++ b/Utilities/Shop.php @@ -0,0 +1,21 @@ +Shop()->getMain() ? Shopware()->Shop()->getMain()->getId() : Shopware()->Shop()->getId(); + } +}