Skip to content

Commit

Permalink
PISHPS-243: create guest account over route (#730)
Browse files Browse the repository at this point in the history
* NTR: PISHPS-243: create guest account over route

* NTR: CSfixer

* NTR: fix 6.4.20.2

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Mar 22, 2024
1 parent c2fdc22 commit 584f234
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/Components/ApplePayDirect/ApplePayDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema
$zipcode,
$city,
$countryCode,
$applePayID,
$context
);

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
<argument type="service" id="Kiener\MolliePayments\Repository\Salutation\SalutationRepository"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument>%kernel.shopware_version%</argument>
<argument type="service" id="Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface"/>
<argument type="service" id="Kiener\MolliePayments\Service\ConfigService"/>
<argument type="service" id="service_container"/>
</service>

<service id="Kiener\MolliePayments\Service\MandateService" class="Kiener\MolliePayments\Service\MandateService">
Expand Down
104 changes: 51 additions & 53 deletions src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,8 +75,11 @@ class CustomerService implements CustomerServiceInterface
/** @var string */
private $shopwareVersion;

/** @var NumberRangeValueGeneratorInterface */
private $valueGenerator;

/**
* @var ContainerInterface
*/
private $container;


/**
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Service/CustomerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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, 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;
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, 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;
}
Expand Down
7 changes: 5 additions & 2 deletions tests/PHPUnit/Service/CustomerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
);
}

Expand Down

0 comments on commit 584f234

Please sign in to comment.