Skip to content

Commit

Permalink
Add support for language shops
Browse files Browse the repository at this point in the history
ISSUE: CS-5124
  • Loading branch information
AleksandarBoljanovic committed Feb 21, 2024
1 parent e907e7b commit 0226e2f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 15 deletions.
14 changes: 11 additions & 3 deletions Components/CheckoutConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
});

Expand All @@ -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
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -35,7 +36,7 @@ public function __construct(OrderRepository $orderRepository)
* @param PaymentLinkRequestContext $context
*
* @return void
*
*
* @throws Exception
*/
public function processPaymentLink(PaymentLinkRequestBuilder $builder, PaymentLinkRequestContext $context): void
Expand All @@ -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()
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions Components/PaymentMeansEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions Controllers/Frontend/AdyenDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,6 +46,7 @@ public function initController($request, $response): void

/**
* @return void
*
* @throws Exception
*/
public function preDispatch(): void
Expand All @@ -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'] ?? '',
Expand Down
54 changes: 49 additions & 5 deletions Controllers/Frontend/AdyenPaymentProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
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;
use AdyenPayment\Components\ErrorMessageProvider;
use AdyenPayment\Components\PaymentMeansEnricher;
use AdyenPayment\Utilities\Plugin;
use AdyenPayment\Utilities\Url;
use AdyenPayment\Utilities\Shop;

/**
* Class Shopware_Controllers_Frontend_AdyenPaymentProcess
Expand Down Expand Up @@ -57,6 +59,7 @@ public function initController($request, $response): void

/**
* @return void
*
* @throws Exception
*/
public function preDispatch(): void
Expand All @@ -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
Expand Down Expand Up @@ -108,7 +113,7 @@ public function indexAction(): void
}

$response = CheckoutAPI::get()
->paymentRequest(Shopware()->Shop()->getId())
->paymentRequest(Shop::getShopId())
->startTransaction(
new StartTransactionRequest(
$paymentMethodType,
Expand Down Expand Up @@ -189,13 +194,23 @@ public function indexAction(): void
$this->view->assign('orderReference', $orderReference);
}

/**
* @return void
*
* @throws Exception
*/
public function handleAdditionalDataAction(): void
{
$this->setupRedirectResponse(
$this->handleAdditionalDataAndGetRedirectUrl($this->Request()->getParams())
);
}

/**
* @return void
*
* @throws Exception
*/
public function handleRedirectAction(): void
{
Logger::logDebug(
Expand Down Expand Up @@ -234,6 +249,12 @@ public function getCheckoutConfigAction(): void
);
}

/**
* @return void
*
* @throws Enlight_Exception
* @throws ConnectionSettingsNotFountException
*/
public function disableCardDetailsAction(): void
{
$this->Front()->Plugins()->ViewRenderer()->setNoRender();
Expand Down Expand Up @@ -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(
Expand All @@ -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');
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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
{
/**
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion Subscriber/EnrichPaymentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]
);
Expand Down
21 changes: 21 additions & 0 deletions Utilities/Shop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AdyenPayment\Utilities;

/**
* Class Shop
*
* @package AdyenPayment\Utilities
*/
class Shop
{
/**
* Returns current shop id. In case of language shop, main shop id is fetched.
*
* @return int
*/
public static function getShopId(): int
{
return Shopware()->Shop()->getMain() ? Shopware()->Shop()->getMain()->getId() : Shopware()->Shop()->getId();
}
}

0 comments on commit 0226e2f

Please sign in to comment.