diff --git a/assets/js/admin/components/Payment.js b/assets/js/admin/components/Payment.js index 302498419d..f4e87b508c 100644 --- a/assets/js/admin/components/Payment.js +++ b/assets/js/admin/components/Payment.js @@ -8,12 +8,12 @@ const onPaymentChange = function () { const selectedType = $paymentType.val(); - const $goPayPaymentMethodFormLine = $('.js-payment-gopay-payment-method').closest('.form-line'); + const $goPayPaymentMethodDiv = $('.js-payment-gopay-payment-method'); if (selectedType === 'goPay') { - $goPayPaymentMethodFormLine.show(); + $goPayPaymentMethodDiv.show(); } else { - $goPayPaymentMethodFormLine.hide(); + $goPayPaymentMethodDiv.hide(); } }; diff --git a/assets/js/admin/validation/form/index.js b/assets/js/admin/validation/form/index.js index e6d039e210..233903a8c2 100644 --- a/assets/js/admin/validation/form/index.js +++ b/assets/js/admin/validation/form/index.js @@ -5,6 +5,7 @@ import './validationMailTemplate'; import './validationAdvert'; import './validationOrder'; import './validationParameterName'; +import './validationPayment'; import './validationPromoCode'; import './validationHreflangSetting'; import './validationStore'; diff --git a/assets/js/admin/validation/form/validationPayment.js b/assets/js/admin/validation/form/validationPayment.js new file mode 100644 index 0000000000..250c3ff1e7 --- /dev/null +++ b/assets/js/admin/validation/form/validationPayment.js @@ -0,0 +1,13 @@ +import Register from '../../../common/utils/Register'; + +export default function gopayPaymentMethodValidator ($container) { + window.$('form[name="payment_form"]').jsFormValidator({ + callbacks: { + validateGopayPaymentMethod: function () { + // JS validation is not necessary + } + } + }); +} + +(new Register()).registerCallback(gopayPaymentMethodValidator, 'gopayPaymentMethodValidator'); diff --git a/composer.json b/composer.json index b1a8c4285c..4be3df198d 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "endroid/qr-code": "^3.9.6", "fakerphp/faker": "^1.19.0", "friendsofsymfony/ckeditor-bundle": "^2.1", - "gopay/payments-sdk-php": ">=1.4 <1.7", + "gopay/payments-sdk-php": "^1.7", "gedmo/doctrine-extensions": "^3.5", "gordalina/cachetool": "^8.5", "guzzlehttp/guzzle": "^7.5", diff --git a/src/Form/Admin/Payment/PaymentFormType.php b/src/Form/Admin/Payment/PaymentFormType.php index 9ef934c082..f9e79304a3 100644 --- a/src/Form/Admin/Payment/PaymentFormType.php +++ b/src/Form/Admin/Payment/PaymentFormType.php @@ -5,13 +5,17 @@ namespace Shopsys\FrameworkBundle\Form\Admin\Payment; use FOS\CKEditorBundle\Form\Type\CKEditorType; +use Shopsys\FormTypesBundle\MultidomainType; use Shopsys\FormTypesBundle\YesNoType; +use Shopsys\FrameworkBundle\Component\Domain\Domain; use Shopsys\FrameworkBundle\Form\DisplayOnlyType; use Shopsys\FrameworkBundle\Form\DomainsType; use Shopsys\FrameworkBundle\Form\GroupType; use Shopsys\FrameworkBundle\Form\ImageUploadType; use Shopsys\FrameworkBundle\Form\Locale\LocalizedType; use Shopsys\FrameworkBundle\Form\PriceAndVatTableByDomainsType; +use Shopsys\FrameworkBundle\Form\WarningMessageType; +use Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod; use Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodFacade; use Shopsys\FrameworkBundle\Model\Payment\Payment; use Shopsys\FrameworkBundle\Model\Payment\PaymentData; @@ -24,6 +28,8 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Constraints\Callback; +use Symfony\Component\Validator\Context\ExecutionContextInterface; class PaymentFormType extends AbstractType { @@ -31,11 +37,13 @@ class PaymentFormType extends AbstractType * @param \Shopsys\FrameworkBundle\Model\Transport\TransportFacade $transportFacade * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentFacade $paymentFacade * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodFacade $goPayPaymentMethodFacade + * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain */ public function __construct( private readonly TransportFacade $transportFacade, private readonly PaymentFacade $paymentFacade, private readonly GoPayPaymentMethodFacade $goPayPaymentMethodFacade, + private readonly Domain $domain, ) { } @@ -75,7 +83,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'required' => false, 'label' => t('Display on'), ]) - ->add('hidden', YesNoType::class, $this->getHiddenFieldOptions($payment)) + ->add('hidden', YesNoType::class, [ + 'required' => false, + 'label' => t('Hidden'), + ]) ->add('transports', ChoiceType::class, [ 'required' => false, 'choices' => $this->transportFacade->getAll(), @@ -99,19 +110,35 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'class' => 'js-payment-type', ], ]) - ->add('goPayPaymentMethod', ChoiceType::class, [ + ->add('goPayPaymentMethodByDomainId', MultidomainType::class, [ + 'entry_type' => ChoiceType::class, + 'options_by_domain_id' => $this->getGopayPaymentMethodOptionsByDomainId(), + 'entry_options' => [ + 'placeholder' => '---', + 'choice_label' => 'name', + 'choice_value' => 'id', + 'multiple' => false, + 'expanded' => false, + 'required' => true, + ], 'label' => t('GoPay payment method'), - 'choices' => $this->goPayPaymentMethodFacade->getAll(), - 'choice_label' => 'name', - 'choice_value' => 'id', - 'multiple' => false, - 'expanded' => false, 'required' => true, + 'macro' => [ + 'name' => 'seoFormRowMacros.multidomainRow', + 'recommended_length' => null, + ], 'attr' => [ 'class' => 'js-payment-gopay-payment-method', ], ]); + if ($payment !== null) { + $this->addHiddenByGoPayWarning( + $options['data'], + $builderBasicInformationGroup, + ); + } + $builderPriceGroup = $builder->create('prices', GroupType::class, [ 'label' => t('Prices'), ]); @@ -178,6 +205,27 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ->add('save', SubmitType::class); } + /** + * @return array + */ + private function getGopayPaymentMethodOptionsByDomainId(): array + { + $allGoPayPaymentMethods = $this->goPayPaymentMethodFacade->getAll(); + $optionsByDomainId = []; + + foreach ($allGoPayPaymentMethods as $goPayPaymentMethod) { + $optionsByDomainId[$goPayPaymentMethod->getDomainId()]['choices'][] = $goPayPaymentMethod; + } + + foreach ($optionsByDomainId as $domainId => $options) { + $optionsByDomainId[$domainId]['group_by'] = function (GoPayPaymentMethod $goPayPaymentMethod): string { + return $goPayPaymentMethod->isAvailable() ? t('Available') : t('Hidden in GoPay'); + }; + } + + return $optionsByDomainId; + } + /** * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver */ @@ -188,28 +236,53 @@ public function configureOptions(OptionsResolver $resolver): void ->setDefaults([ 'data_class' => PaymentData::class, 'attr' => ['novalidate' => 'novalidate'], + 'constraints' => [ + new Callback([$this, 'validateGopayPaymentMethod']), + ], ]); } /** - * @param \Shopsys\FrameworkBundle\Model\Payment\Payment|null $payment - * @return array + * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData + * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context + */ + public function validateGopayPaymentMethod(PaymentData $paymentData, ExecutionContextInterface $context): void + { + if ($paymentData->type !== Payment::TYPE_GOPAY) { + return; + } + + foreach ($paymentData->enabled as $domainId => $enabled) { + if ($enabled && $paymentData->goPayPaymentMethodByDomainId[$domainId] === null) { + $context->buildViolation('Please select GoPay payment method for enabled domain ' . $this->domain->getDomainConfigById($domainId)->getName()) + ->atPath('goPayPaymentMethodByDomainId[1]') + ->addViolation(); + } + } + } + + /** + * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData + * @param \Symfony\Component\Form\FormBuilderInterface $builder */ - private function getHiddenFieldOptions(?Payment $payment): array + public function addHiddenByGoPayWarning(PaymentData $paymentData, FormBuilderInterface $builder): void { - $hiddenFieldOptions = [ - 'required' => false, - 'label' => t('Hidden'), - ]; - - if ($payment !== null && $payment->isHiddenByGoPay()) { - $hiddenFieldOptions['attr'] = [ - 'disabled' => true, - 'icon' => true, - 'iconTitle' => t('This payment method is hidden by GoPay.'), - ]; + $domainIdsWithHiddenByGoPay = array_keys(array_filter($paymentData->hiddenByGoPay)); + $domainNames = []; + + if (count($domainIdsWithHiddenByGoPay) === 0) { + return; + } + + foreach ($domainIdsWithHiddenByGoPay as $domainId) { + $domainNames[] = $this->domain->getDomainConfigById($domainId)->getName(); } - return $hiddenFieldOptions; + $builder->add('hiddenByGoPay', WarningMessageType::class, [ + 'data' => t('This payment method is hidden by GoPay on domains: %domains%', [ + '%domains%' => implode(', ', $domainNames), + ]), + 'position' => ['after' => 'enabled'], + ]); } } diff --git a/src/Migrations/Version20240803005343.php b/src/Migrations/Version20240803005343.php new file mode 100644 index 0000000000..efade0064d --- /dev/null +++ b/src/Migrations/Version20240803005343.php @@ -0,0 +1,66 @@ +sql('DROP INDEX gopay_payment_method_unique'); + $this->sql('ALTER TABLE gopay_payment_methods ADD domain_id INT DEFAULT NULL'); + $this->sql('CREATE UNIQUE INDEX gopay_payment_method_unique ON gopay_payment_methods (domain_id, identifier)'); + + foreach ($this->getAllDomainIds() as $domainId) { + $locale = $this->getDomainLocale($domainId); + + $this->sql( + 'UPDATE gopay_payment_methods SET domain_id = :domainId WHERE LOWER(name) LIKE \'[' . strtolower($locale) . ']%\'', + ['domainId' => $domainId], + ); + } + $this->sql('ALTER TABLE gopay_payment_methods ALTER domain_id SET NOT NULL'); + + $this->sql('ALTER TABLE payment_domains ADD go_pay_payment_method_id INT DEFAULT NULL'); + $this->sql(' + ALTER TABLE + payment_domains + ADD + CONSTRAINT FK_9532B177B1E3A4E9 FOREIGN KEY (go_pay_payment_method_id) REFERENCES gopay_payment_methods (id) ON DELETE + SET + NULL NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->sql('CREATE INDEX IDX_9532B177B1E3A4E9 ON payment_domains (go_pay_payment_method_id)'); + $this->sql('UPDATE payment_domains + SET go_pay_payment_method_id = payments.go_pay_payment_method_id + FROM payments + WHERE payment_domains.payment_id = payments.id AND payment_domains.enabled = TRUE + '); + $this->sql('ALTER TABLE payments DROP COLUMN go_pay_payment_method_id'); + + $this->sql('ALTER TABLE payment_domains ADD hidden_by_go_pay BOOLEAN NOT NULL DEFAULT FALSE'); + $this->sql('UPDATE payment_domains + SET hidden_by_go_pay = payments.hidden_by_go_pay + FROM payments + WHERE payment_domains.payment_id = payments.id AND payment_domains.enabled = TRUE + '); + $this->sql('ALTER TABLE payment_domains ALTER COLUMN hidden_by_go_pay DROP DEFAULT '); + $this->sql('ALTER TABLE payments DROP COLUMN hidden_by_go_pay'); + } + + /** + * @param \Doctrine\DBAL\Schema\Schema $schema + */ + public function down(Schema $schema): void + { + } +} diff --git a/src/Migrations/Version20240822144131.php b/src/Migrations/Version20240822144131.php new file mode 100644 index 0000000000..b9727dda23 --- /dev/null +++ b/src/Migrations/Version20240822144131.php @@ -0,0 +1,27 @@ +sql('ALTER TABLE gopay_payment_methods ADD available BOOLEAN NOT NULL DEFAULT TRUE'); + $this->sql('ALTER TABLE gopay_payment_methods ALTER available DROP DEFAULT'); + } + + /** + * @param \Doctrine\DBAL\Schema\Schema $schema + */ + public function down(Schema $schema): void + { + } +} diff --git a/src/Model/GoPay/Exception/GoPayNotEnabledOnDomainException.php b/src/Model/GoPay/Exception/GoPayNotEnabledOnDomainException.php new file mode 100644 index 0000000000..723b8107e5 --- /dev/null +++ b/src/Model/GoPay/Exception/GoPayNotEnabledOnDomainException.php @@ -0,0 +1,11 @@ +em->beginTransaction(); $this->downloadAndUpdatePaymentMethodsForAllDomains(); $this->em->commit(); - } catch (GoPayNotConfiguredException $exception) { - $this->logger->alert('GoPay configuration is not set.'); } catch (Exception $exception) { $this->logger->error($exception->getMessage(), ['exception' => $exception]); $this->em->rollback(); @@ -63,14 +60,25 @@ protected function downloadAndUpdatePaymentMethodsForAllDomains(): void $allDomains = $this->domain->getAll(); foreach ($allDomains as $domain) { - if (array_key_exists($domain->getLocale(), $this->goPayConfig) === false) { - continue; - } - - $this->logger->info(sprintf('downloading for %s locale', $domain->getLocale())); - try { + $this->logger->info(sprintf( + 'Downloading for the domain %d (%s)', + $domain->getId(), + $domain->getName(), + )); $this->paymentMethodFacade->downloadAndUpdatePaymentMethods($domain); + } catch (GoPayNotEnabledOnDomainException $ex) { + $this->logger->info(sprintf( + 'GoPay is not enabled on the domain %d (%s)', + $domain->getId(), + $domain->getName(), + )); + } catch (GoPayNotConfiguredException $exception) { + $this->logger->alert(sprintf( + 'GoPay configuration is not set properly for the domain %d (%s)', + $domain->getId(), + $domain->getName(), + )); } catch (GoPayPaymentDownloadException $ex) { $this->logger->error($ex->getMessage(), [ 'exception' => $ex, diff --git a/src/Model/GoPay/GoPayClient.php b/src/Model/GoPay/GoPayClient.php index b24fef62c9..f5fe5da4bf 100644 --- a/src/Model/GoPay/GoPayClient.php +++ b/src/Model/GoPay/GoPayClient.php @@ -18,7 +18,7 @@ class GoPayClient { - public const RESPONSE_STATUS_CODE_OK = 200; + protected const int RESPONSE_STATUS_CODE_OK = 200; protected GoPay $goPay; @@ -56,9 +56,9 @@ protected function sendApiRequest( if ($token->token) { return $this->goPay->call( $urlPath, - $contentType, 'Bearer ' . $token->token, $method, + $contentType, $data, ); } @@ -98,11 +98,11 @@ public function getStatus(string $id): Response $response = $this->sendApiRequest($urlPath, GoPay::FORM, RequestMethods::GET); - if ((int)$response->statusCode !== (int)self::RESPONSE_STATUS_CODE_OK) { + if ((int)$response->statusCode !== self::RESPONSE_STATUS_CODE_OK) { throw new GoPayPaymentDownloadException( $this->goPay->buildUrl($urlPath), RequestMethods::GET, - self::RESPONSE_STATUS_CODE_OK, + static::RESPONSE_STATUS_CODE_OK, null, $response, ); @@ -130,11 +130,11 @@ public function refundTransaction(string $id, int $amount): Response $body, ); - if ((int)$response->statusCode !== (int)self::RESPONSE_STATUS_CODE_OK) { + if ((int)$response->statusCode !== self::RESPONSE_STATUS_CODE_OK) { throw new GoPayPaymentDownloadException( $this->goPay->buildUrl($urlPath), RequestMethods::POST, - self::RESPONSE_STATUS_CODE_OK, + static::RESPONSE_STATUS_CODE_OK, $body, $response, ); @@ -157,17 +157,17 @@ public function downloadGoPayPaymentMethodsByCurrency(Currency $currency): array RequestMethods::GET, ); - if ((int)$response->statusCode !== (int)self::RESPONSE_STATUS_CODE_OK) { + if ((int)$response->statusCode !== self::RESPONSE_STATUS_CODE_OK) { throw new GoPayPaymentDownloadException( $this->goPay->buildUrl($urlPath), RequestMethods::GET, - self::RESPONSE_STATUS_CODE_OK, + static::RESPONSE_STATUS_CODE_OK, null, $response, ); } - return $response->json['enabledPaymentInstruments']; + return $response->json['enabledPaymentInstruments'] ?? []; } /** diff --git a/src/Model/GoPay/GoPayClientFactory.php b/src/Model/GoPay/GoPayClientFactory.php index 2f97e65147..4b542a242a 100644 --- a/src/Model/GoPay/GoPayClientFactory.php +++ b/src/Model/GoPay/GoPayClientFactory.php @@ -4,43 +4,102 @@ namespace Shopsys\FrameworkBundle\Model\GoPay; +use GoPay\Definition\TokenScope; +use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig; use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayNotConfiguredException; +use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayNotEnabledOnDomainException; class GoPayClientFactory { + protected const string PRODUCTION_URL = 'https://gate.gopay.cz/'; + protected const string TEST_URL = 'https://gw.sandbox.gopay.com/'; + + /** + * @var array + */ + protected array $clientsByDomainId = []; + + /** + * @param array $clientConfigs + */ + public function __construct( + protected array $clientConfigs, + ) { + } + /** - * @param array $config + * @param array $gopayConfig + * @return \Shopsys\FrameworkBundle\Model\GoPay\GoPayClient */ - public function __construct(protected array $config) + protected function createInstance(array $gopayConfig): GoPayClient { + return new GoPayClient($gopayConfig); } /** - * @param string $locale + * @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig * @return \Shopsys\FrameworkBundle\Model\GoPay\GoPayClient */ - public function createByLocale(string $locale): GoPayClient + public function createByDomain(DomainConfig $domainConfig): GoPayClient { - return new GoPayClient($this->getConfigByLocale($locale)); + if (!array_key_exists($domainConfig->getId(), $this->clientsByDomainId)) { + $gopayConfig = $this->mapConfigByDomain($domainConfig); + $this->clientsByDomainId[$domainConfig->getId()] = $this->createInstance($gopayConfig); + } + + return $this->clientsByDomainId[$domainConfig->getId()]; } /** - * @param string $locale + * @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig * @return array */ - protected function getConfigByLocale(string $locale): array + protected function mapConfigByDomain(DomainConfig $domainConfig): array { - if (!array_key_exists($locale, $this->config)) { - throw new GoPayNotConfiguredException(); + foreach ($this->clientConfigs as $clientConfig) { + if (!in_array($domainConfig->getId(), $clientConfig['domains'], true)) { + continue; + } + + return $this->createConfig( + $clientConfig['goid'], + $clientConfig['clientId'], + $clientConfig['clientSecret'], + $clientConfig['isProductionMode'] ? static::PRODUCTION_URL : static::TEST_URL, + $domainConfig->getLocale(), + ); } - $configByLocale = $this->config[$locale]; - $this->config = array_merge($this->config, $configByLocale); + throw new GoPayNotEnabledOnDomainException('GoPay is not enabled on domain with ID "' . $domainConfig->getId() . '"'); + } - if ($this->config['goid'] === null || $this->config['goid'] === '') { + /** + * @param string $goid + * @param string $clientId + * @param string $clientSecret + * @param string $gatewayUrl + * @param string $locale + * @return array + */ + protected function createConfig( + string $goid, + string $clientId, + string $clientSecret, + string $gatewayUrl, + string $locale, + ): array { + if ($goid === '' || $clientId === '' || $clientSecret === '') { throw new GoPayNotConfiguredException(); } - return $this->config; + return [ + 'goid' => $goid, + 'clientId' => $clientId, + 'clientSecret' => $clientSecret, + 'gatewayUrl' => $gatewayUrl, + 'scope' => TokenScope::ALL, + 'language' => $locale, + 'timeout' => 30, + ]; } } diff --git a/src/Model/GoPay/GoPayFacade.php b/src/Model/GoPay/GoPayFacade.php index 88a6655f57..6ebb5d0015 100644 --- a/src/Model/GoPay/GoPayFacade.php +++ b/src/Model/GoPay/GoPayFacade.php @@ -6,7 +6,6 @@ use DateTime; use GoPay\Definition\Response\PaymentStatus; -use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig; use Shopsys\FrameworkBundle\Component\Domain\Domain; use Shopsys\FrameworkBundle\Component\Money\Money; use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPaySendPaymentException; @@ -17,12 +16,7 @@ class GoPayFacade implements PaymentServiceInterface { - protected const GOPAY_RESULT_FAILED = 'FAILED'; - - /** - * @var \Shopsys\FrameworkBundle\Model\GoPay\GoPayClient[] - */ - protected array $goPayClients; + protected const string GOPAY_RESULT_FAILED = 'FAILED'; /** * @param \Shopsys\FrameworkBundle\Model\GoPay\GoPayClientFactory $goPayClientFactory @@ -36,22 +30,6 @@ public function __construct( protected readonly Domain $domain, protected readonly GoPayRepository $goPayRepository, ) { - $this->goPayClients = []; - } - - /** - * @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domain - * @return \Shopsys\FrameworkBundle\Model\GoPay\GoPayClient - */ - protected function getGoPayClientByDomainConfig(DomainConfig $domain): GoPayClient - { - $locale = $domain->getLocale(); - - if (array_key_exists($locale, $this->goPayClients) === false) { - $this->goPayClients[$locale] = $this->goPayClientFactory->createByLocale($locale); - } - - return $this->goPayClients[$locale]; } /** @@ -63,12 +41,13 @@ public function sendPaymentToGoPay(Order $order, ?string $goPayBankSwift): array { $goPayPaymentData = $this->goPayOrderMapper->createGoPayPaymentData($order, $goPayBankSwift); $domainConfig = $this->domain->getDomainConfigById($order->getDomainId()); - $response = $this->getGoPayClientByDomainConfig($domainConfig)->sendPaymentToGoPay($goPayPaymentData); + $goPayClient = $this->goPayClientFactory->createByDomain($domainConfig); + $response = $goPayClient->sendPaymentToGoPay($goPayPaymentData); if ($response->hasSucceed()) { return [ 'gatewayUrl' => $response->json['gw_url'], - 'embedJs' => $this->getGoPayClientByDomainConfig($domainConfig)->urlToEmbedJs(), + 'embedJs' => $goPayClient->urlToEmbedJs(), 'goPayId' => $response->json['id'], 'state' => $response->json['state'], ]; @@ -85,8 +64,10 @@ public function createTransaction( PaymentTransactionData $paymentTransactionData, PaymentSetupCreationData $paymentSetupCreationData, ): void { - $goPayCreatePaymentSetup = $this->sendPaymentToGoPay($paymentTransactionData->order, $paymentTransactionData->order->getGoPayBankSwift()); - + $goPayCreatePaymentSetup = $this->sendPaymentToGoPay( + $paymentTransactionData->order, + $paymentTransactionData->order->getGoPayBankSwift(), + ); $paymentTransactionData->externalPaymentIdentifier = (string)$goPayCreatePaymentSetup['goPayId']; $paymentTransactionData->externalPaymentStatus = (string)$goPayCreatePaymentSetup['state']; @@ -100,7 +81,8 @@ public function createTransaction( public function updateTransaction(PaymentTransactionData $paymentTransactionData): bool { $domainConfig = $this->domain->getDomainConfigById($paymentTransactionData->order->getDomainId()); - $goPayStatusResponse = $this->getGoPayClientByDomainConfig($domainConfig)->getStatus($paymentTransactionData->externalPaymentIdentifier); + $goPayClient = $this->goPayClientFactory->createByDomain($domainConfig); + $goPayStatusResponse = $goPayClient->getStatus($paymentTransactionData->externalPaymentIdentifier); if (array_key_exists('state', (array)$goPayStatusResponse->json)) { $paymentTransactionData->externalPaymentStatus = (string)$goPayStatusResponse->json['state']; @@ -123,7 +105,11 @@ public function updateTransaction(PaymentTransactionData $paymentTransactionData public function refundTransaction(PaymentTransactionData $paymentTransactionData, Money $refundAmount): bool { $domainConfig = $this->domain->getDomainConfigById($paymentTransactionData->order->getDomainId()); - $refundResponse = $this->getGoPayClientByDomainConfig($domainConfig)->refundTransaction($paymentTransactionData->externalPaymentIdentifier, $this->goPayOrderMapper->formatPriceForGoPay($refundAmount)); + $goPayClient = $this->goPayClientFactory->createByDomain($domainConfig); + $refundResponse = $goPayClient->refundTransaction( + $paymentTransactionData->externalPaymentIdentifier, + $this->goPayOrderMapper->formatPriceForGoPay($refundAmount), + ); if (array_key_exists('result', (array)$refundResponse->json) && $refundResponse->json['result'] !== static::GOPAY_RESULT_FAILED) { $paymentTransactionData->refundedAmount = $paymentTransactionData->refundedAmount->add($refundAmount); diff --git a/src/Model/GoPay/GoPayOrderMapper.php b/src/Model/GoPay/GoPayOrderMapper.php index 8e38129a5d..3d8ead5e33 100644 --- a/src/Model/GoPay/GoPayOrderMapper.php +++ b/src/Model/GoPay/GoPayOrderMapper.php @@ -31,7 +31,8 @@ public function __construct( public function createGoPayPaymentData(Order $order, ?string $goPayBankSwift): array { $orderPayment = $order->getPayment(); - $defaultPaymentInstrument = $orderPayment->getGoPayPaymentMethod() !== null ? $orderPayment->getGoPayPaymentMethod()->getIdentifier() : ''; + $gopayPaymentMethod = $orderPayment->getGoPayPaymentMethodByDomainId($order->getDomainId()); + $defaultPaymentInstrument = $gopayPaymentMethod !== null ? $gopayPaymentMethod->getIdentifier() : ''; $goPayPaymentItemsData = $this->createGoPayPaymentItemsData($order); $router = $this->domainRouterFactory->getRouter($order->getDomainId()); diff --git a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethod.php b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethod.php index c818e4031d..0b35d36545 100644 --- a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethod.php +++ b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethod.php @@ -10,7 +10,7 @@ * @ORM\Table( * name="gopay_payment_methods", * uniqueConstraints={ - * @ORM\UniqueConstraint(name="gopay_payment_method_unique", columns={"currency_id", "identifier"}) + * @ORM\UniqueConstraint(name="gopay_payment_method_unique", columns={"domain_id", "identifier"}) * } * ) * @ORM\Entity @@ -47,6 +47,12 @@ class GoPayPaymentMethod */ protected $currency; + /** + * @var int + * @ORM\Column(type="integer") + */ + protected $domainId; + /** * @var string * @ORM\Column(type="string", length=255) @@ -65,28 +71,42 @@ class GoPayPaymentMethod */ protected $paymentGroup; + /** + * @var bool + * @ORM\Column(type="boolean") + */ + protected $available; + /** * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodData $paymentMethodData */ public function __construct(GoPayPaymentMethodData $paymentMethodData) { $this->identifier = $paymentMethodData->identifier; - $this->name = $paymentMethodData->name; $this->currency = $paymentMethodData->currency; - $this->imageNormalUrl = $paymentMethodData->imageNormalUrl; - $this->imageLargeUrl = $paymentMethodData->imageLargeUrl; - $this->paymentGroup = $paymentMethodData->paymentGroup; + + $this->fillCommonFields($paymentMethodData); } /** * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodData $goPayPaymentMethodData */ public function edit(GoPayPaymentMethodData $goPayPaymentMethodData): void + { + $this->fillCommonFields($goPayPaymentMethodData); + } + + /** + * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodData $goPayPaymentMethodData + */ + public function fillCommonFields(GoPayPaymentMethodData $goPayPaymentMethodData): void { $this->name = $goPayPaymentMethodData->name; $this->imageNormalUrl = $goPayPaymentMethodData->imageNormalUrl; $this->imageLargeUrl = $goPayPaymentMethodData->imageLargeUrl; $this->paymentGroup = $goPayPaymentMethodData->paymentGroup; + $this->domainId = $goPayPaymentMethodData->domainId; + $this->available = $goPayPaymentMethodData->available; } /** @@ -144,4 +164,25 @@ public function getPaymentGroup() { return $this->paymentGroup; } + + /** + * @return int + */ + public function getDomainId() + { + return $this->domainId; + } + + /** + * @return bool + */ + public function isAvailable() + { + return $this->available; + } + + public function setUnavailable() + { + $this->available = false; + } } diff --git a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodData.php b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodData.php index 11022b02f2..ba04b4aa3c 100644 --- a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodData.php +++ b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodData.php @@ -35,4 +35,14 @@ class GoPayPaymentMethodData * @var \Shopsys\FrameworkBundle\Model\Pricing\Currency\Currency|null */ public $currency; + + /** + * @var int + */ + public $domainId; + + /** + * @var bool + */ + public $available; } diff --git a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodDataFactory.php b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodDataFactory.php index 6bf52c2f06..4b797e940b 100644 --- a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodDataFactory.php +++ b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodDataFactory.php @@ -28,6 +28,8 @@ public function createFromGoPayPaymentMethod(GoPayPaymentMethod $paymentMethod): $goPayPaymentMethodData->imageNormalUrl = $paymentMethod->getImageNormalUrl(); $goPayPaymentMethodData->imageLargeUrl = $paymentMethod->getImageLargeUrl(); $goPayPaymentMethodData->paymentGroup = $paymentMethod->getPaymentGroup(); + $goPayPaymentMethodData->domainId = $paymentMethod->getDomainId(); + $goPayPaymentMethodData->available = $paymentMethod->isAvailable(); return $goPayPaymentMethodData; } diff --git a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodFacade.php b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodFacade.php index 1b986ca5c7..5a05302e2e 100644 --- a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodFacade.php +++ b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodFacade.php @@ -73,10 +73,9 @@ protected function edit( public function downloadAndUpdatePaymentMethods(DomainConfig $domainConfig): void { $currency = $this->currencyFacade->getDomainDefaultCurrencyByDomainId($domainConfig->getId()); - $goPayClient = $this->goPayClientFactory->createByLocale($domainConfig->getLocale()); + $goPayClient = $this->goPayClientFactory->createByDomain($domainConfig); $goPayPaymentMethodsRawData = $goPayClient->downloadGoPayPaymentMethodsByCurrency($currency); - $paymentMethodByIdentifier = - $this->goPayPaymentMethodRepository->getAllIndexedByIdentifierByCurrencyId($currency->getId()); + $paymentMethodByIdentifier = $this->goPayPaymentMethodRepository->getAllIndexedByIdentifierByDomainId($domainConfig->getId()); foreach ($goPayPaymentMethodsRawData as $goPayPaymentMethodRawData) { $paymentIdentifier = $goPayPaymentMethodRawData['paymentInstrument']; @@ -84,16 +83,24 @@ public function downloadAndUpdatePaymentMethods(DomainConfig $domainConfig): voi if (array_key_exists($paymentIdentifier, $paymentMethodByIdentifier)) { $paymentMethod = $paymentMethodByIdentifier[$paymentIdentifier]; $this->editByRawData($paymentMethod, $goPayPaymentMethodRawData, $goPayClient->getLanguage()); - $this->paymentFacade->unHideByGoPayPaymentMethod($paymentMethod); + $this->paymentFacade->unHideByGoPayPaymentMethod($paymentMethod, $domainConfig->getId()); unset($paymentMethodByIdentifier[$paymentIdentifier]); } else { - $this->createFromRawData($goPayPaymentMethodRawData, $currency, $goPayClient->getLanguage()); + $this->createFromRawData( + $goPayPaymentMethodRawData, + $currency, + $goPayClient->getLanguage(), + $domainConfig->getId(), + ); } } foreach ($paymentMethodByIdentifier as $paymentMethod) { - $this->paymentFacade->hideByGoPayPaymentMethod($paymentMethod); + $paymentMethod->setUnavailable(); + $this->paymentFacade->hideByGoPayPaymentMethod($paymentMethod, $domainConfig->getId()); } + + $this->em->flush(); } /** @@ -127,6 +134,7 @@ public function setFromGoPayRawData( $goPayPaymentMethodData->imageNormalUrl = $goPayMethodRawData['image']['normal']; $goPayPaymentMethodData->imageLargeUrl = $goPayMethodRawData['image']['large']; $goPayPaymentMethodData->paymentGroup = $goPayMethodRawData['group']; + $goPayPaymentMethodData->available = true; } /** @@ -150,15 +158,18 @@ protected function editByRawData( * @param array $goPayMethodRawData * @param \Shopsys\FrameworkBundle\Model\Pricing\Currency\Currency $currency * @param string $language + * @param int $domainId * @return \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod */ protected function createFromRawData( array $goPayMethodRawData, Currency $currency, string $language, + int $domainId, ): GoPayPaymentMethod { $paymentMethodData = $this->goPayPaymentMethodDataFactory->createInstance(); $paymentMethodData->currency = $currency; + $paymentMethodData->domainId = $domainId; $this->setFromGoPayRawData($paymentMethodData, $goPayMethodRawData, $language); $paymentMethod = $this->create($paymentMethodData); diff --git a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodRepository.php b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodRepository.php index 125d44178f..1b626a85df 100644 --- a/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodRepository.php +++ b/src/Model/GoPay/PaymentMethod/GoPayPaymentMethodRepository.php @@ -31,18 +31,19 @@ protected function getPaymentMethodRepository(): EntityRepository */ public function getAll(): array { - return $this->getPaymentMethodRepository()->findAll(); + return $this->getPaymentMethodRepository()->findBy([], ['available' => 'desc']); } /** - * @param int $currencyId + * @param int $domainId * @return \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod[] */ - public function getAllIndexedByIdentifierByCurrencyId(int $currencyId): array + public function getAllIndexedByIdentifierByDomainId(int $domainId): array { return $this->getPaymentMethodRepository() ->createQueryBuilder('pm') - ->where('pm.currency = :currency')->setParameter('currency', $currencyId) + ->where('pm.domainId = :domainId') + ->setParameter('domainId', $domainId) ->indexBy('pm', 'pm.identifier') ->getQuery() ->execute(); diff --git a/src/Model/Payment/IndependentPaymentVisibilityCalculation.php b/src/Model/Payment/IndependentPaymentVisibilityCalculation.php index e1a578e939..9f42f446c3 100644 --- a/src/Model/Payment/IndependentPaymentVisibilityCalculation.php +++ b/src/Model/Payment/IndependentPaymentVisibilityCalculation.php @@ -31,7 +31,7 @@ public function isIndependentlyVisible(Payment $payment, $domainId) return false; } - if ($payment->isHidden() || $payment->isDeleted() || $payment->isHiddenByGoPay()) { + if ($payment->isHidden() || $payment->isDeleted() || $payment->isHiddenByGoPayByDomainId($domainId)) { return false; } diff --git a/src/Model/Payment/Payment.php b/src/Model/Payment/Payment.php index 3328cdbb81..abb91dc587 100644 --- a/src/Model/Payment/Payment.php +++ b/src/Model/Payment/Payment.php @@ -94,25 +94,12 @@ class Payment extends AbstractTranslatableEntity implements OrderableEntityInter */ protected $uuid; - /** - * @var \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null - * @ORM\ManyToOne(targetEntity="Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod") - * @ORM\JoinColumn(nullable=true, onDelete="SET NULL") - */ - protected $goPayPaymentMethod; - /** * @var string * @ORM\Column(type="string") */ protected $type; - /** - * @var bool - * @ORM\Column(type="boolean") - */ - protected $hiddenByGoPay; - /** * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData */ @@ -146,8 +133,15 @@ protected function setData(PaymentData $paymentData): void $this->hidden = $paymentData->hidden; $this->czkRounding = $paymentData->czkRounding; $this->type = $paymentData->type; - $this->setGoPayPaymentMethod($paymentData); - $this->hiddenByGoPay = $paymentData->hiddenByGoPay; + + if ($paymentData->type !== self::TYPE_GOPAY) { + $this->resetGopayPaymentMethods(); + } else { + foreach ($this->domains as $paymentDomain) { + $paymentDomain->setHiddenByGoPay(!$paymentDomain->getGoPayPaymentMethod()?->isAvailable()); + } + } + $this->setTranslations($paymentData); } @@ -363,49 +357,45 @@ public function isGoPay(): bool } /** + * @param int $domainId * @return bool */ - public function isHiddenByGoPay() + public function isHiddenByGoPayByDomainId(int $domainId): bool { - return $this->hiddenByGoPay; - } - - public function hideByGoPay(): void - { - $this->hiddenByGoPay = true; + return $this->getPaymentDomain($domainId)->isHiddenByGoPay(); } /** - * @return \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null + * @param int $domainId */ - public function getGoPayPaymentMethod() + public function hideByGoPayOnDomain(int $domainId): void { - return $this->goPayPaymentMethod; + $this->getPaymentDomain($domainId)->setHiddenByGoPay(true); } /** - * @return string + * @param int $domainId */ - public function getType() + public function unHideByGoPayOnDomain(int $domainId): void { - return $this->type; + $this->getPaymentDomain($domainId)->setHiddenByGoPay(false); } - public function unHideByGoPay(): void + /** + * @param int $domainId + * @return \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null + */ + public function getGoPayPaymentMethodByDomainId(int $domainId) { - $this->hiddenByGoPay = false; + return $this->getPaymentDomain($domainId)->getGoPayPaymentMethod(); } /** - * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData + * @return string */ - protected function setGoPayPaymentMethod(PaymentData $paymentData): void + public function getType() { - $this->goPayPaymentMethod = null; - - if ($this->type === self::TYPE_GOPAY) { - $this->goPayPaymentMethod = $paymentData->goPayPaymentMethod; - } + return $this->type; } /** @@ -425,6 +415,8 @@ protected function setDomains(PaymentData $paymentData) $domainId = $paymentDomain->getDomainId(); $paymentDomain->setEnabled($paymentData->enabled[$domainId]); $paymentDomain->setVat($paymentData->vatsIndexedByDomainId[$domainId]); + $paymentDomain->setGoPayPaymentMethod($paymentData->goPayPaymentMethodByDomainId[$domainId] ?? null); + $paymentDomain->setHiddenByGoPay($paymentData->hiddenByGoPay[$domainId] ?? false); } } @@ -436,13 +428,27 @@ protected function createDomains(PaymentData $paymentData) $domainIds = array_keys($paymentData->enabled); foreach ($domainIds as $domainId) { - $paymentDomain = new PaymentDomain($this, $domainId, $paymentData->vatsIndexedByDomainId[$domainId]); + $paymentDomain = new PaymentDomain( + $this, + $domainId, + $paymentData->vatsIndexedByDomainId[$domainId], + $paymentData->goPayPaymentMethodByDomainId[$domainId] ?? null, + $paymentData->hiddenByGoPay[$domainId] ?? false, + ); $this->domains->add($paymentDomain); } $this->setDomains($paymentData); } + protected function resetGopayPaymentMethods(): void + { + foreach ($this->domains as $paymentDomain) { + $paymentDomain->setGoPayPaymentMethod(null); + $paymentDomain->setHiddenByGoPay(false); + } + } + /** * @param int $domainId * @return \Shopsys\FrameworkBundle\Model\Payment\PaymentDomain diff --git a/src/Model/Payment/PaymentData.php b/src/Model/Payment/PaymentData.php index 03c5d30286..4d9cbdc56e 100644 --- a/src/Model/Payment/PaymentData.php +++ b/src/Model/Payment/PaymentData.php @@ -62,9 +62,9 @@ class PaymentData public $uuid; /** - * @var \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null + * @var array */ - public $goPayPaymentMethod; + public $goPayPaymentMethodByDomainId; /** * @var string @@ -72,7 +72,7 @@ class PaymentData public $type; /** - * @var bool + * @var array */ public $hiddenByGoPay; @@ -87,6 +87,8 @@ public function __construct() $this->czkRounding = false; $this->pricesIndexedByDomainId = []; $this->vatsIndexedByDomainId = []; + $this->goPayPaymentMethodByDomainId = []; + $this->hiddenByGoPay = []; $this->type = Payment::TYPE_BASIC; } } diff --git a/src/Model/Payment/PaymentDataFactory.php b/src/Model/Payment/PaymentDataFactory.php index d5f7744021..7e44c53a4a 100644 --- a/src/Model/Payment/PaymentDataFactory.php +++ b/src/Model/Payment/PaymentDataFactory.php @@ -51,6 +51,8 @@ protected function fillNew(PaymentData $paymentData): void $paymentData->enabled[$domainId] = true; $paymentData->pricesIndexedByDomainId[$domainId] = Money::zero(); $paymentData->vatsIndexedByDomainId[$domainId] = $this->vatFacade->getDefaultVatForDomain($domainId); + $paymentData->goPayPaymentMethodByDomainId[$domainId] = null; + $paymentData->hiddenByGoPay[$domainId] = false; } foreach ($this->domain->getAllLocales() as $locale) { @@ -58,8 +60,8 @@ protected function fillNew(PaymentData $paymentData): void $paymentData->description[$locale] = null; $paymentData->instructions[$locale] = null; } + $paymentData->image = $this->imageUploadDataFactory->create(); - $paymentData->hiddenByGoPay = false; } /** @@ -105,11 +107,11 @@ protected function fillFromPayment(PaymentData $paymentData, Payment $payment): $paymentData->enabled[$domainId] = $payment->isEnabled($domainId); $paymentData->pricesIndexedByDomainId[$domainId] = $payment->getPrice($domainId)->getPrice(); $paymentData->vatsIndexedByDomainId[$domainId] = $payment->getPaymentDomain($domainId)->getVat(); + $paymentData->goPayPaymentMethodByDomainId[$domainId] = $payment->getGoPayPaymentMethodByDomainId($domainId); + $paymentData->hiddenByGoPay[$domainId] = $payment->isHiddenByGoPayByDomainId($domainId); } $paymentData->image = $this->imageUploadDataFactory->createFromEntityAndType($payment); $paymentData->type = $payment->getType(); - $paymentData->goPayPaymentMethod = $payment->getGoPayPaymentMethod(); - $paymentData->hiddenByGoPay = $payment->isHiddenByGoPay(); } } diff --git a/src/Model/Payment/PaymentDomain.php b/src/Model/Payment/PaymentDomain.php index 4728c2d201..5fa1787673 100644 --- a/src/Model/Payment/PaymentDomain.php +++ b/src/Model/Payment/PaymentDomain.php @@ -5,6 +5,7 @@ namespace Shopsys\FrameworkBundle\Model\Payment; use Doctrine\ORM\Mapping as ORM; +use Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod; use Shopsys\FrameworkBundle\Model\Pricing\Vat\Vat; /** @@ -52,17 +53,39 @@ class PaymentDomain */ protected $vat; + /** + * @var \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null + * @ORM\ManyToOne(targetEntity="Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod") + * @ORM\JoinColumn(nullable=true, onDelete="SET NULL") + */ + protected $goPayPaymentMethod; + + /** + * @var bool + * @ORM\Column(type="boolean") + */ + protected $hiddenByGoPay; + /** * @param \Shopsys\FrameworkBundle\Model\Payment\Payment $payment * @param int $domainId * @param \Shopsys\FrameworkBundle\Model\Pricing\Vat\Vat $vat + * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null $goPayPaymentMethod + * @param bool $hiddenByGoPay */ - public function __construct(Payment $payment, int $domainId, Vat $vat) - { + public function __construct( + Payment $payment, + int $domainId, + Vat $vat, + ?GoPayPaymentMethod $goPayPaymentMethod = null, + bool $hiddenByGoPay = false, + ) { $this->payment = $payment; $this->domainId = $domainId; $this->vat = $vat; $this->enabled = true; + $this->goPayPaymentMethod = $goPayPaymentMethod; + $this->hiddenByGoPay = $hiddenByGoPay; } /** @@ -104,4 +127,36 @@ public function setVat($vat): void { $this->vat = $vat; } + + /** + * @return \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null + */ + public function getGoPayPaymentMethod() + { + return $this->goPayPaymentMethod; + } + + /** + * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod|null $goPayPaymentMethod + */ + public function setGoPayPaymentMethod($goPayPaymentMethod): void + { + $this->goPayPaymentMethod = $goPayPaymentMethod; + } + + /** + * @param bool $state + */ + public function setHiddenByGoPay(bool $state): void + { + $this->hiddenByGoPay = $state; + } + + /** + * @return bool + */ + public function isHiddenByGoPay() + { + return $this->hiddenByGoPay; + } } diff --git a/src/Model/Payment/PaymentFacade.php b/src/Model/Payment/PaymentFacade.php index 69b7ac831e..bd47c0bed0 100644 --- a/src/Model/Payment/PaymentFacade.php +++ b/src/Model/Payment/PaymentFacade.php @@ -307,13 +307,14 @@ protected function getVisibleOnDomainByTransport(int $domainId, Transport $trans /** * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod $goPayPaymentMethod + * @param int $domainId */ - public function hideByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod): void + public function hideByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod, int $domainId): void { - $payments = $this->paymentRepository->getByGoPayPaymentMethod($goPayPaymentMethod); + $payments = $this->paymentRepository->getByGoPayPaymentMethod($goPayPaymentMethod, $domainId); foreach ($payments as $payment) { - $payment->hideByGoPay(); + $payment->hideByGoPayOnDomain($domainId); } $this->em->flush(); @@ -321,13 +322,14 @@ public function hideByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod) /** * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod $goPayPaymentMethod + * @param int $domainId */ - public function unHideByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod): void + public function unHideByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod, int $domainId): void { - $payments = $this->paymentRepository->getByGoPayPaymentMethod($goPayPaymentMethod); + $payments = $this->paymentRepository->getByGoPayPaymentMethod($goPayPaymentMethod, $domainId); foreach ($payments as $payment) { - $payment->unHideByGoPay(); + $payment->unHideByGoPayOnDomain($domainId); } $this->em->flush(); diff --git a/src/Model/Payment/PaymentRepository.php b/src/Model/Payment/PaymentRepository.php index aa7f8c6026..42d90bd9be 100644 --- a/src/Model/Payment/PaymentRepository.php +++ b/src/Model/Payment/PaymentRepository.php @@ -127,7 +127,7 @@ public function getEnabledOnDomainByUuid(string $uuid, int $domainId): Payment ->andWhere('p.deleted = false') ->andWhere('pd.enabled = true') ->andWhere('p.hidden = false') - ->andWhere('p.hiddenByGoPay = false'); + ->andWhere('pd.hiddenByGoPay = false'); $payment = $queryBuilder->getQuery()->getOneOrNullResult(); @@ -140,11 +140,19 @@ public function getEnabledOnDomainByUuid(string $uuid, int $domainId): Payment /** * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod $goPayPaymentMethod + * @param int $domainId * @return \Shopsys\FrameworkBundle\Model\Payment\Payment[] */ - public function getByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod): array + public function getByGoPayPaymentMethod(GoPayPaymentMethod $goPayPaymentMethod, int $domainId): array { - return $this->getPaymentRepository()->findBy(['goPayPaymentMethod' => $goPayPaymentMethod]); + return $this->getPaymentRepository() + ->createQueryBuilder('p') + ->join(PaymentDomain::class, 'pd', Join::WITH, 'p.id = pd.payment AND pd.domainId = :domainId') + ->where('pd.goPayPaymentMethod = :goPayPaymentMethod') + ->setParameter('domainId', $domainId) + ->setParameter('goPayPaymentMethod', $goPayPaymentMethod) + ->getQuery() + ->execute(); } /** diff --git a/src/Model/Payment/Service/PaymentServiceFacade.php b/src/Model/Payment/Service/PaymentServiceFacade.php index 01fcb6aee8..433587b59b 100644 --- a/src/Model/Payment/Service/PaymentServiceFacade.php +++ b/src/Model/Payment/Service/PaymentServiceFacade.php @@ -8,6 +8,7 @@ use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessageTrait; use Shopsys\FrameworkBundle\Component\Money\Money; use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayNotConfiguredException; +use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayNotEnabledOnDomainException; use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayPaymentDownloadException; use Shopsys\FrameworkBundle\Model\GoPay\GoPayFacade; use Shopsys\FrameworkBundle\Model\Order\Order; @@ -81,7 +82,10 @@ public function payOrder(Order $order): PaymentSetupCreationData $paymentServiceFacade->createTransaction($paymentTransactionData, $paymentSetupCreationData); $this->paymentTransactionFacade->create($paymentTransactionData); } catch (PaymentServiceFacadeNotRegisteredException $exception) { - $this->logger->error($exception->getMessage()); + $this->logger->error( + $exception->getMessage(), + ['exception' => $exception], + ); } return $paymentSetupCreationData; @@ -102,8 +106,11 @@ public function updatePaymentTransactionsByOrder(Order $order): void if ($update) { $this->paymentTransactionFacade->edit($paymentTransaction->getId(), $paymentTransactionData); } - } catch (PaymentServiceFacadeNotRegisteredException|GoPayNotConfiguredException|PaymentTransactionHasNoAssignedPayment $exception) { - $this->logger->error($exception->getMessage()); + } catch (PaymentServiceFacadeNotRegisteredException|GoPayNotConfiguredException|GoPayNotEnabledOnDomainException|PaymentTransactionHasNoAssignedPayment $exception) { + $this->logger->error( + $exception->getMessage(), + ['exception' => $exception], + ); } } } @@ -123,7 +130,10 @@ public function refundTransaction(PaymentTransaction $paymentTransaction, Money $update = $paymentServiceFacade->refundTransaction($paymentTransactionData, $refundAmount); } catch (GoPayPaymentDownloadException $exception) { $this->addErrorFlash(t('GoPay API return error - go to GoPay admin and find transaction %paymentId% and check if is all right.', ['%paymentId%' => $paymentTransaction->getExternalPaymentIdentifier()])); - $this->logger->error('GoPay API return error.', [$exception]); + $this->logger->error( + 'GoPay API return error.', + ['exception' => $exception], + ); $update = false; } @@ -137,7 +147,10 @@ public function refundTransaction(PaymentTransaction $paymentTransaction, Money $this->paymentTransactionFacade->edit($paymentTransaction->getId(), $paymentTransactionData); } } catch (PaymentServiceFacadeNotRegisteredException|PaymentTransactionHasNoAssignedPayment $exception) { - $this->logger->error($exception->getMessage()); + $this->logger->error( + $exception->getMessage(), + ['exception' => $exception], + ); } } } diff --git a/src/Resources/config/parameters_common.yaml b/src/Resources/config/parameters_common.yaml index eed6bb1155..165ba64505 100644 --- a/src/Resources/config/parameters_common.yaml +++ b/src/Resources/config/parameters_common.yaml @@ -10,8 +10,6 @@ parameters: # Seed for demo data generation faker.seed: 1234 - gopay_config: [] - # Time zone that the admin use for entering and viewing dates and times in the datetime pickers shopsys.admin_display_timezone: ~ diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index fc90d9b755..9585773169 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -690,7 +690,7 @@ services: Shopsys\FrameworkBundle\Model\GoPay\GoPayClientFactory: arguments: - - '%gopay_config%' + $clientConfigs: '%env(json:GOPAY_CONFIG)%' Shopsys\FrameworkBundle\Model\Heureka\HeurekaSetting: ~ diff --git a/src/Resources/translations/messages.cs.po b/src/Resources/translations/messages.cs.po index 40cd49fa56..7610640773 100644 --- a/src/Resources/translations/messages.cs.po +++ b/src/Resources/translations/messages.cs.po @@ -37,6 +37,9 @@ msgstr "-- Vyber roli --" msgid "-- Select name of the transfer --" msgstr "-- Vyberte název převodu --" +msgid "---" +msgstr "" + msgid "0" msgstr "" @@ -346,6 +349,9 @@ msgstr "Ověřovací kód" msgid "Authenticator key" msgstr "Ověřovací klíč" +msgid "Available" +msgstr "Dostupné" + msgid "Available immediately" msgstr "Ihned k odběru" @@ -1816,6 +1822,9 @@ msgstr "Skryté" msgid "Hidden copy" msgstr "Skrytá kopie" +msgid "Hidden in GoPay" +msgstr "Skryté v GoPay" + msgid "Hide" msgstr "Skrýt" @@ -3415,8 +3424,8 @@ msgstr "Toto se používá pro řazení parametrů na kartě produktu a řazení msgid "This item can be set in product detail of a specific variant" msgstr "Tuto položku můžete nastavit na kartě konkrétní varianty" -msgid "This payment method is hidden by GoPay." -msgstr "Tento způsob platby je skrytý systémem GoPay." +msgid "This payment method is hidden by GoPay on domains: %domains%" +msgstr "Tento způsob platby je skrytý systémem GoPay na doménách: %domains%" msgid "This status can't be deleted, because there is a functionality bounded to it - it identifies orders that were not successfully processed." msgstr "Tento stav není možné smazat, jelikož je na něj navázána funkčnost - určuje objednávky, které nebyly úspěšně vyřízeny." diff --git a/src/Resources/translations/messages.en.po b/src/Resources/translations/messages.en.po index 03a9939ed3..8b32a569e9 100644 --- a/src/Resources/translations/messages.en.po +++ b/src/Resources/translations/messages.en.po @@ -37,6 +37,9 @@ msgstr "" msgid "-- Select name of the transfer --" msgstr "" +msgid "---" +msgstr "" + msgid "0" msgstr "" @@ -346,6 +349,9 @@ msgstr "" msgid "Authenticator key" msgstr "" +msgid "Available" +msgstr "" + msgid "Available immediately" msgstr "" @@ -1816,6 +1822,9 @@ msgstr "" msgid "Hidden copy" msgstr "" +msgid "Hidden in GoPay" +msgstr "" + msgid "Hide" msgstr "" @@ -3415,7 +3424,7 @@ msgstr "" msgid "This item can be set in product detail of a specific variant" msgstr "" -msgid "This payment method is hidden by GoPay." +msgid "This payment method is hidden by GoPay on domains: %domains%" msgstr "" msgid "This status can't be deleted, because there is a functionality bounded to it - it identifies orders that were not successfully processed." diff --git a/src/Resources/views/Admin/Form/theme.html.twig b/src/Resources/views/Admin/Form/theme.html.twig index ad6ffad0a7..17f0da557e 100644 --- a/src/Resources/views/Admin/Form/theme.html.twig +++ b/src/Resources/views/Admin/Form/theme.html.twig @@ -83,6 +83,10 @@ {%- endblock localized_form_rows %} {% block multidomain_row -%} + {% set attrClass = form.vars.attr.class ?? '' %} + {% if attrClass %} +
+ {% endif %} {% if form.vars.macro is not null %} {% if form.vars.macro.name == 'seoFormRowMacros' %} {% import '@ShopsysFramework/Admin/Form/seoFormRowMacros.html.twig' as seoFormRowMacros %} @@ -109,6 +113,9 @@
{% endif %} {% endif %} + {% if attrClass %} + + {% endif %} {%- endblock multidomain_row %} {% block multidomain_form_rows_no_padding %}