From eb3685e681c5def4ef8e575be70bea70b226f5c8 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Wed, 20 Mar 2024 13:04:32 +0100 Subject: [PATCH 1/3] NTR: PISHPS-243: create guest account over route --- .../ApplePayDirect/ApplePayDirect.php | 1 - src/Resources/config/services.xml | 1 + src/Service/CustomerService.php | 95 ++++++++++--------- src/Service/CustomerServiceInterface.php | 2 +- tests/PHPUnit/Fakes/FakeCustomerService.php | 2 +- tests/PHPUnit/Service/CustomerServiceTest.php | 4 +- 6 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/Components/ApplePayDirect/ApplePayDirect.php b/src/Components/ApplePayDirect/ApplePayDirect.php index bcb490c93..4f2670bde 100644 --- a/src/Components/ApplePayDirect/ApplePayDirect.php +++ b/src/Components/ApplePayDirect/ApplePayDirect.php @@ -360,7 +360,6 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema $zipcode, $city, $countryCode, - $applePayID, $context ); diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index b79e98b45..cf1a5e9fd 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -69,6 +69,7 @@ %kernel.shopware_version% + diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index eb3917637..199b8d324 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -6,10 +6,8 @@ use Kiener\MolliePayments\Exception\CouldNotCreateMollieCustomerException; use Kiener\MolliePayments\Exception\CouldNotFetchMollieCustomerException; use Kiener\MolliePayments\Exception\CustomerCouldNotBeFoundException; -use Kiener\MolliePayments\Repository\Country\CountryRepository; use Kiener\MolliePayments\Repository\Country\CountryRepositoryInterface; use Kiener\MolliePayments\Repository\Customer\CustomerRepositoryInterface; -use Kiener\MolliePayments\Repository\Salutation\SalutationRepository; use Kiener\MolliePayments\Repository\Salutation\SalutationRepositoryInterface; use Kiener\MolliePayments\Service\MollieApi\Customer; use Kiener\MolliePayments\Struct\CustomerStruct; @@ -18,12 +16,14 @@ use Shopware\Core\Checkout\Customer\CustomerEntity; use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent; use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent; +use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRegisterRoute; use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; -use Shopware\Core\Framework\Uuid\Uuid; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; use Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface; use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister; use Shopware\Core\System\SalesChannel\SalesChannelContext; @@ -78,6 +78,11 @@ class CustomerService implements CustomerServiceInterface /** @var NumberRangeValueGeneratorInterface */ private $valueGenerator; + /** + * @var AbstractRegisterRoute + */ + private $abstractRegisterRoute; + /** * @param CountryRepositoryInterface $countryRepository @@ -92,8 +97,20 @@ class CustomerService implements CustomerServiceInterface * @param NumberRangeValueGeneratorInterface $valueGenerator * @param ConfigService $configService */ - public function __construct(CountryRepositoryInterface $countryRepository, CustomerRepositoryInterface $customerRepository, Customer $customerApiService, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, SalesChannelContextPersister $salesChannelContextPersister, SalutationRepositoryInterface $salutationRepository, SettingsService $settingsService, string $shopwareVersion, NumberRangeValueGeneratorInterface $valueGenerator, ConfigService $configService) - { + public function __construct( + CountryRepositoryInterface $countryRepository, + CustomerRepositoryInterface $customerRepository, + Customer $customerApiService, + EventDispatcherInterface $eventDispatcher, + LoggerInterface $logger, + SalesChannelContextPersister $salesChannelContextPersister, + SalutationRepositoryInterface $salutationRepository, + SettingsService $settingsService, + string $shopwareVersion, + NumberRangeValueGeneratorInterface $valueGenerator, + ConfigService $configService, + AbstractRegisterRoute $abstractRegisterRoute + ) { $this->countryRepository = $countryRepository; $this->customerRepository = $customerRepository; $this->customerApiService = $customerApiService; @@ -105,6 +122,7 @@ public function __construct(CountryRepositoryInterface $countryRepository, Custo $this->shopwareVersion = $shopwareVersion; $this->valueGenerator = $valueGenerator; $this->configService = $configService; + $this->abstractRegisterRoute = $abstractRegisterRoute; } /** @@ -451,54 +469,37 @@ public function getAddressArray($address, CustomerEntity $customer): array * @param SalesChannelContext $context * @return null|CustomerEntity */ - public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, string $paymentMethodId, SalesChannelContext $context): ?CustomerEntity + public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, SalesChannelContext $context): ?CustomerEntity { - $customerId = Uuid::randomHex(); - $addressId = Uuid::randomHex(); - - $salutationId = $this->getSalutationId($context->getContext()); $countryId = $this->getCountryId($countryISO2, $context->getContext()); - $customerNumber = $this->valueGenerator->getValue( - 'customer', - $context->getContext(), - $context->getSalesChannelId() - ); + $data = new RequestDataBag(); + $data->set('guest', true); + $data->set('firstName', $firstname); + $data->set('lastName', $lastname); + $data->set('email', $email); - $customer = [ - 'id' => $customerId, - 'salutationId' => $salutationId, - 'firstName' => $firstname, - 'lastName' => $lastname, - 'customerNumber' => $customerNumber, - 'guest' => true, - 'email' => $email, - 'password' => Uuid::randomHex(), - 'defaultPaymentMethodId' => $paymentMethodId, - 'groupId' => $context->getSalesChannel()->getCustomerGroupId(), - 'salesChannelId' => $context->getSalesChannel()->getId(), - 'defaultBillingAddressId' => $addressId, - 'defaultShippingAddressId' => $addressId, - 'addresses' => [ - [ - 'id' => $addressId, - 'customerId' => $customerId, - 'countryId' => $countryId, - 'salutationId' => $salutationId, - 'firstName' => $firstname, - 'lastName' => $lastname, - 'street' => $street, - 'zipcode' => $zipCode, - 'city' => $city, - 'phoneNumber' => $phone, - ], - ], - ]; + $billingAddress = new RequestDataBag(); + $billingAddress->set('street', $street); + $billingAddress->set('zipcode', $zipCode); + $billingAddress->set('city', $city); + $billingAddress->set('phoneNumber', $phone); + $billingAddress->set('countryId', $countryId); - // Add the customer to the database - $this->customerRepository->upsert([$customer], $context->getContext()); + $data->set('billingAddress', $billingAddress); - return $this->getCustomer($customerId, $context->getContext()); + try { + $response = $this->abstractRegisterRoute->register($data, $context, false); + return $response->getCustomer(); + } catch (ConstraintViolationException $e) { + $errors = []; + /** we have to store the errors in an array because getErrors returns a generator */ + foreach ($e->getErrors() as $error) { + $errors[]=$error; + } + $this->logger->error($e->getMessage(), ['errors'=>$errors]); + return null; + } } /** diff --git a/src/Service/CustomerServiceInterface.php b/src/Service/CustomerServiceInterface.php index 47505d23f..2be4f80ef 100644 --- a/src/Service/CustomerServiceInterface.php +++ b/src/Service/CustomerServiceInterface.php @@ -38,7 +38,7 @@ public function getCustomerStruct(string $customerId, Context $context): Custome * @return array */ public function getAddressArray($address, CustomerEntity $customer): array; - public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, string $paymentMethodId, SalesChannelContext $context): ?CustomerEntity; + public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, SalesChannelContext $context): ?CustomerEntity; public function getCountryId(string $countryCode, Context $context): ?string; public function getSalutationId(Context $context): ?string; public function createMollieCustomer(string $customerId, string $salesChannelId, Context $context): void; diff --git a/tests/PHPUnit/Fakes/FakeCustomerService.php b/tests/PHPUnit/Fakes/FakeCustomerService.php index 446956bd6..0b04c97f3 100644 --- a/tests/PHPUnit/Fakes/FakeCustomerService.php +++ b/tests/PHPUnit/Fakes/FakeCustomerService.php @@ -83,7 +83,7 @@ public function getAddressArray($address, CustomerEntity $customer): array return []; } - public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, string $paymentMethodId, SalesChannelContext $context): ?CustomerEntity + public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, SalesChannelContext $context): ?CustomerEntity { return null; } diff --git a/tests/PHPUnit/Service/CustomerServiceTest.php b/tests/PHPUnit/Service/CustomerServiceTest.php index 3c2e580ca..f9ad6011d 100644 --- a/tests/PHPUnit/Service/CustomerServiceTest.php +++ b/tests/PHPUnit/Service/CustomerServiceTest.php @@ -17,6 +17,7 @@ use Psr\Log\NullLogger; use Shopware\Core\Checkout\Customer\CustomerDefinition; use Shopware\Core\Checkout\Customer\CustomerEntity; +use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRegisterRoute; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; @@ -52,7 +53,8 @@ public function setUp(): void $this->settingsService, 'does.not.matter.here', $this->createMock(NumberRangeValueGeneratorInterface::class), - $this->createMock(ConfigService::class) + $this->createMock(ConfigService::class), + $this->createMock(AbstractRegisterRoute::class), ); } From 3d3ae1fc32688353511a77844110035f85bd58b6 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Wed, 20 Mar 2024 14:28:23 +0100 Subject: [PATCH 2/3] NTR: CSfixer --- src/Resources/config/services.xml | 2 +- src/Service/CustomerService.php | 7 ------- tests/PHPUnit/Service/CustomerServiceTest.php | 1 - 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index cf1a5e9fd..d362c3068 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -67,7 +67,6 @@ %kernel.shopware_version% - @@ -109,6 +108,7 @@ + diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index 199b8d324..779cd825a 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -24,7 +24,6 @@ use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; -use Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface; use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -75,8 +74,6 @@ class CustomerService implements CustomerServiceInterface /** @var string */ private $shopwareVersion; - /** @var NumberRangeValueGeneratorInterface */ - private $valueGenerator; /** * @var AbstractRegisterRoute @@ -94,7 +91,6 @@ class CustomerService implements CustomerServiceInterface * @param SalutationRepositoryInterface $salutationRepository * @param SettingsService $settingsService * @param string $shopwareVersion - * @param NumberRangeValueGeneratorInterface $valueGenerator * @param ConfigService $configService */ public function __construct( @@ -107,7 +103,6 @@ public function __construct( SalutationRepositoryInterface $salutationRepository, SettingsService $settingsService, string $shopwareVersion, - NumberRangeValueGeneratorInterface $valueGenerator, ConfigService $configService, AbstractRegisterRoute $abstractRegisterRoute ) { @@ -120,7 +115,6 @@ public function __construct( $this->salutationRepository = $salutationRepository; $this->settingsService = $settingsService; $this->shopwareVersion = $shopwareVersion; - $this->valueGenerator = $valueGenerator; $this->configService = $configService; $this->abstractRegisterRoute = $abstractRegisterRoute; } @@ -465,7 +459,6 @@ public function getAddressArray($address, CustomerEntity $customer): array * @param string $zipCode * @param string $city * @param string $countryISO2 - * @param string $paymentMethodId * @param SalesChannelContext $context * @return null|CustomerEntity */ diff --git a/tests/PHPUnit/Service/CustomerServiceTest.php b/tests/PHPUnit/Service/CustomerServiceTest.php index f9ad6011d..3febee1ca 100644 --- a/tests/PHPUnit/Service/CustomerServiceTest.php +++ b/tests/PHPUnit/Service/CustomerServiceTest.php @@ -52,7 +52,6 @@ public function setUp(): void $this->createMock(SalutationRepository::class), $this->settingsService, 'does.not.matter.here', - $this->createMock(NumberRangeValueGeneratorInterface::class), $this->createMock(ConfigService::class), $this->createMock(AbstractRegisterRoute::class), ); From 8b766998e492d29c073bfce38786e541ade311e5 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Fri, 22 Mar 2024 13:31:48 +0100 Subject: [PATCH 3/3] NTR: fix 6.4.20.2 --- src/Resources/config/services.xml | 2 +- src/Service/CustomerService.php | 16 ++++++++++------ tests/PHPUnit/Service/CustomerServiceTest.php | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index d362c3068..6f13b9128 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -68,7 +68,7 @@ %kernel.shopware_version% - + diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index 779cd825a..81b450b4a 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -11,12 +11,13 @@ use Kiener\MolliePayments\Repository\Salutation\SalutationRepositoryInterface; use Kiener\MolliePayments\Service\MollieApi\Customer; use Kiener\MolliePayments\Struct\CustomerStruct; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity; use Shopware\Core\Checkout\Customer\CustomerEntity; use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent; use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent; -use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRegisterRoute; +use Shopware\Core\Checkout\Customer\SalesChannel\RegisterRoute; use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; @@ -76,9 +77,9 @@ class CustomerService implements CustomerServiceInterface /** - * @var AbstractRegisterRoute + * @var ContainerInterface */ - private $abstractRegisterRoute; + private $container; /** @@ -104,7 +105,7 @@ public function __construct( SettingsService $settingsService, string $shopwareVersion, ConfigService $configService, - AbstractRegisterRoute $abstractRegisterRoute + ContainerInterface $container //we have to inject the container, because in SW 6.4.20.2 we have circular injection for the register route ) { $this->countryRepository = $countryRepository; $this->customerRepository = $customerRepository; @@ -116,7 +117,7 @@ public function __construct( $this->settingsService = $settingsService; $this->shopwareVersion = $shopwareVersion; $this->configService = $configService; - $this->abstractRegisterRoute = $abstractRegisterRoute; + $this->container = $container; } /** @@ -465,8 +466,10 @@ public function getAddressArray($address, CustomerEntity $customer): array public function createApplePayDirectCustomer(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, SalesChannelContext $context): ?CustomerEntity { $countryId = $this->getCountryId($countryISO2, $context->getContext()); + $salutationId = $this->getSalutationId($context->getContext()); $data = new RequestDataBag(); + $data->set('salutationId', $salutationId); $data->set('guest', true); $data->set('firstName', $firstname); $data->set('lastName', $lastname); @@ -482,7 +485,8 @@ public function createApplePayDirectCustomer(string $firstname, string $lastname $data->set('billingAddress', $billingAddress); try { - $response = $this->abstractRegisterRoute->register($data, $context, false); + $abstractRegisterRoute = $this->container->get(RegisterRoute::class); + $response = $abstractRegisterRoute->register($data, $context, false); return $response->getCustomer(); } catch (ConstraintViolationException $e) { $errors = []; diff --git a/tests/PHPUnit/Service/CustomerServiceTest.php b/tests/PHPUnit/Service/CustomerServiceTest.php index 3febee1ca..5f62b13a9 100644 --- a/tests/PHPUnit/Service/CustomerServiceTest.php +++ b/tests/PHPUnit/Service/CustomerServiceTest.php @@ -11,9 +11,11 @@ use Kiener\MolliePayments\Service\MollieApi\Mandate; use Kiener\MolliePayments\Service\SettingsService; use Kiener\MolliePayments\Struct\CustomerStruct; +use MolliePayments\Tests\Fakes\FakeContainer; use MolliePayments\Tests\Fakes\FakeEntityRepository; use MolliePayments\Tests\Fakes\Repositories\FakeCustomerRepository; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; use Psr\Log\NullLogger; use Shopware\Core\Checkout\Customer\CustomerDefinition; use Shopware\Core\Checkout\Customer\CustomerEntity; @@ -53,7 +55,7 @@ public function setUp(): void $this->settingsService, 'does.not.matter.here', $this->createMock(ConfigService::class), - $this->createMock(AbstractRegisterRoute::class), + new FakeContainer(), ); }