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 6ecbf5795..6f13b9128 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -67,8 +67,8 @@ %kernel.shopware_version% - + diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index eb3917637..81b450b4a 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -6,25 +6,25 @@ 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; +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\RegisterRoute; 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\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -75,8 +75,11 @@ class CustomerService implements CustomerServiceInterface /** @var string */ private $shopwareVersion; - /** @var NumberRangeValueGeneratorInterface */ - private $valueGenerator; + + /** + * @var ContainerInterface + */ + private $container; /** @@ -89,11 +92,21 @@ class CustomerService implements CustomerServiceInterface * @param SalutationRepositoryInterface $salutationRepository * @param SettingsService $settingsService * @param string $shopwareVersion - * @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, + ConfigService $configService, + 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; $this->customerApiService = $customerApiService; @@ -103,8 +116,8 @@ public function __construct(CountryRepositoryInterface $countryRepository, Custo $this->salutationRepository = $salutationRepository; $this->settingsService = $settingsService; $this->shopwareVersion = $shopwareVersion; - $this->valueGenerator = $valueGenerator; $this->configService = $configService; + $this->container = $container; } /** @@ -447,58 +460,43 @@ 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 */ - 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()); + $salutationId = $this->getSalutationId($context->getContext()); - $customerNumber = $this->valueGenerator->getValue( - 'customer', - $context->getContext(), - $context->getSalesChannelId() - ); + $data = new RequestDataBag(); + $data->set('salutationId', $salutationId); + $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 { + $abstractRegisterRoute = $this->container->get(RegisterRoute::class); + $response = $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..5f62b13a9 100644 --- a/tests/PHPUnit/Service/CustomerServiceTest.php +++ b/tests/PHPUnit/Service/CustomerServiceTest.php @@ -11,12 +11,15 @@ 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; +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; @@ -51,8 +54,8 @@ public function setUp(): void $this->createMock(SalutationRepository::class), $this->settingsService, 'does.not.matter.here', - $this->createMock(NumberRangeValueGeneratorInterface::class), - $this->createMock(ConfigService::class) + $this->createMock(ConfigService::class), + new FakeContainer(), ); }