Skip to content

Commit

Permalink
NTR: PISHPS-291: do not create new guest accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Jul 3, 2024
1 parent 7feec11 commit 56d74cf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Components/ApplePayDirect/ApplePayDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema
# if we are not logged in,
# then we have to create a new guest customer for our express order
if (!$this->customerService->isCustomerLoggedIn($context)) {
$customer = $this->customerService->createApplePayDirectCustomer(
$customer = $this->customerService->createApplePayDirectCustomerIfNotExists(
$firstname,
$lastname,
$email,
Expand Down
78 changes: 57 additions & 21 deletions src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ class CustomerService implements CustomerServiceInterface
* @param ConfigService $configService
*/
public function __construct(
CountryRepositoryInterface $countryRepository,
CustomerRepositoryInterface $customerRepository,
Customer $customerApiService,
EventDispatcherInterface $eventDispatcher,
LoggerInterface $logger,
SalesChannelContextPersister $salesChannelContextPersister,
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
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;
Expand Down Expand Up @@ -211,7 +211,7 @@ public function setCardToken(CustomerEntity $customer, string $cardToken, SalesC
$customFields = $customer->getCustomFields();

// If custom fields are empty, create a new array
if (!is_array($customFields)) {
if (! is_array($customFields)) {
$customFields = [];
}

Expand Down Expand Up @@ -277,7 +277,6 @@ public function saveCustomerCustomFields(string $customerID, array $customFields
}



/**
* @param CustomerEntity $customer
* @param string $terminalId
Expand All @@ -288,7 +287,7 @@ public function setPosTerminal(CustomerEntity $customer, string $terminalId, Con
{
$customFields = $customer->getCustomFields();

if (!is_array($customFields)) {
if (! is_array($customFields)) {
$customFields = [];
}

Expand Down Expand Up @@ -375,7 +374,7 @@ public function getCustomerStruct(string $customerId, Context $context): Custome

$customer = $this->getCustomer($customerId, $context);

if (!($customer instanceof CustomerEntity)) {
if (! ($customer instanceof CustomerEntity)) {
throw new CustomerCouldNotBeFoundException($customerId);
}

Expand All @@ -386,7 +385,7 @@ public function getCustomerStruct(string $customerId, Context $context): Custome
$struct->setLegacyCustomerId($customFields[self::CUSTOM_FIELDS_KEY_MOLLIE_CUSTOMER_ID]);
}
$molliePaymentsCustomFields = $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS] ?? [];
if (!is_array($molliePaymentsCustomFields)) {
if (! is_array($molliePaymentsCustomFields)) {
$this->logger->warning('Customer customFields for MolliePayments are invalid. Array is expected', [
'currentCustomFields' => $molliePaymentsCustomFields
]);
Expand Down Expand Up @@ -436,11 +435,16 @@ 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, SalesChannelContext $context): ?CustomerEntity
public function createApplePayDirectCustomerIfNotExists(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());

$customer = $this->findCustomerByEmail($email, $context);
if ($customer instanceof CustomerEntity) {
return $customer;
}

$data = new RequestDataBag();
$data->set('salutationId', $salutationId);
$data->set('guest', true);
Expand All @@ -465,9 +469,9 @@ public function createApplePayDirectCustomer(string $firstname, string $lastname
$errors = [];
/** we have to store the errors in an array because getErrors returns a generator */
foreach ($e->getErrors() as $error) {
$errors[]=$error;
$errors[] = $error;
}
$this->logger->error($e->getMessage(), ['errors'=>$errors]);
$this->logger->error($e->getMessage(), ['errors' => $errors]);
return null;
}
}
Expand All @@ -490,7 +494,7 @@ public function getCountryId(string $countryCode, Context $context): ?string
/** @var string[] $countries */
$countries = $this->countryRepository->searchIds($criteria, $context)->getIds();

return !empty($countries) ? (string)$countries[0] : null;
return ! empty($countries) ? (string)$countries[0] : null;
} catch (Exception $e) {
return null;
}
Expand All @@ -513,7 +517,7 @@ public function getSalutationId(Context $context): ?string
/** @var string[] $salutations */
$salutations = $this->salutationRepository->searchIds($criteria, $context)->getIds();

return !empty($salutations) ? (string)$salutations[0] : null;
return ! empty($salutations) ? (string)$salutations[0] : null;
} catch (Exception $e) {
return null;
}
Expand Down Expand Up @@ -576,7 +580,7 @@ public function createMollieCustomer(string $customerId, string $salesChannelId,

$customer = $this->getCustomer($customerId, $context);

if (!($customer instanceof CustomerEntity)) {
if (! ($customer instanceof CustomerEntity)) {
throw new CustomerCouldNotBeFoundException($customerId);
}

Expand All @@ -590,4 +594,36 @@ public function createMollieCustomer(string $customerId, string $salesChannelId,
$context
);
}

private function findCustomerByEmail(string $email, SalesChannelContext $context): ?CustomerEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('email', $email));
$criteria->addFilter(new EqualsFilter('guest', true));
$criteria->addFilter(new EqualsFilter('active', true));

$searchResult = $this->customerRepository->search($criteria, $context->getContext());

if ($searchResult->getTotal() === 0) {
return null;
}

/** @var CustomerEntity $foundCustomer */
$foundCustomer = $searchResult->first();

$boundCustomers = $searchResult->filter(function (CustomerEntity $customerEntity) use ($context) {
return $customerEntity->getBoundSalesChannelId() === $context->getSalesChannelId();
});

if ($boundCustomers->getTotal() > 0) {
$foundCustomer = $boundCustomers->first();
}

$bindCustomers = $this->configService->getSystemConfigService()->get('core.systemWideLoginRegistration.isCustomerBoundToSalesChannel');

if ($bindCustomers && $foundCustomer->getBoundSalesChannelId() === null) {
return null;
}
return $foundCustomer;
}
}
2 changes: 1 addition & 1 deletion src/Service/CustomerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getCustomerStruct(string $customerId, Context $context): Custome
* @return array<string, mixed>
*/
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;
public function createApplePayDirectCustomerIfNotExists(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;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/Fakes/FakeCustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, SalesChannelContext $context): ?CustomerEntity
public function createApplePayDirectCustomerIfNotExists(string $firstname, string $lastname, string $email, string $phone, string $street, string $zipCode, string $city, string $countryISO2, SalesChannelContext $context): ?CustomerEntity
{
return null;
}
Expand Down

0 comments on commit 56d74cf

Please sign in to comment.