Skip to content

Commit

Permalink
PAYOSWXP-158: Improve customer registration process
Browse files Browse the repository at this point in the history
  • Loading branch information
momocode-de committed Dec 15, 2024
1 parent 51fdf92 commit 569dd27
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/Components/GenericExpressCheckout/CustomerRegistrationUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\Components\GenericExpressCheckout;

use PayonePayment\Core\Utils\AddressCompare;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Context;
Expand All @@ -14,22 +15,25 @@
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\Country\CountryEntity;
use Shopware\Core\System\Salutation\SalutationEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Contracts\Translation\TranslatorInterface;

class CustomerRegistrationUtil
{
public function __construct(
private readonly EntityRepository $salutationRepository,
private readonly EntityRepository $countryRepository,
private readonly TranslatorInterface $translator
private readonly TranslatorInterface $translator,
private readonly SystemConfigService $systemConfigService,
private readonly LoggerInterface $logger
) {
}

public function getCustomerDataBagFromGetCheckoutSessionResponse(array $response, Context $context): RequestDataBag
{
$salutationId = $this->getSalutationId($context);

$billingAddress = array_filter([
$billingAddress = [
'salutationId' => $salutationId,
'company' => $this->extractBillingData($response, 'company'),
'firstName' => $this->extractBillingData($response, 'firstname'),
Expand All @@ -40,9 +44,9 @@ public function getCustomerDataBagFromGetCheckoutSessionResponse(array $response
'city' => $this->extractBillingData($response, 'city'),
'countryId' => $this->getCountryIdByCode($this->extractBillingData($response, 'country') ?? '', $context),
'phone' => $this->extractBillingData($response, 'telephonenumber'),
]);
];

$shippingAddress = array_filter([
$shippingAddress = [
'salutationId' => $salutationId,
'company' => $this->extractShippingData($response, 'company'),
'firstName' => $this->extractShippingData($response, 'firstname'),
Expand All @@ -53,12 +57,18 @@ public function getCustomerDataBagFromGetCheckoutSessionResponse(array $response
'city' => $this->extractShippingData($response, 'city'),
'countryId' => $this->getCountryIdByCode($this->extractShippingData($response, 'country') ?? '', $context),
'phone' => $this->extractShippingData($response, 'telephonenumber'),
]);
];

$isBillingAddressComplete = $this->hasAddressRequiredData($billingAddress);
$isShippingAddressComplete = $this->hasAddressRequiredData($shippingAddress);

if (!$isBillingAddressComplete && !$isShippingAddressComplete) {
$this->logger->error('PAYONE Express Checkout: The delivery and billing address is incomplete', [
'billingAddress' => $billingAddress,
'shippingAddress' => $shippingAddress,
'requiredFields' => $this->getRequiredFields(),
]);

throw new RuntimeException($this->translator->trans('PayonePayment.errorMessages.genericError'));
}

Expand All @@ -70,8 +80,8 @@ public function getCustomerDataBagFromGetCheckoutSessionResponse(array $response
'guest' => true,
'salutationId' => $salutationId,
'email' => $response['addpaydata']['email'],
'firstName' => $billingAddress['firstName'], /** @phpstan-ignore offsetAccess.notFound */
'lastName' => $billingAddress['lastName'], /** @phpstan-ignore offsetAccess.notFound */
'firstName' => $billingAddress['firstName'],
'lastName' => $billingAddress['lastName'],
'acceptedDataProtection' => true,
'billingAddress' => $billingAddress,
'shippingAddress' => $shippingAddress,
Expand Down Expand Up @@ -159,6 +169,17 @@ private function getCountryIdByCode(string $code, Context $context): ?string
}

private function hasAddressRequiredData(array $address): bool
{
foreach ($this->getRequiredFields() as $field) {
if (!isset($address[$field])) {
return false;
}
}

return true;
}

private function getRequiredFields(): array
{
$requiredFields = [
'firstName',
Expand All @@ -168,12 +189,26 @@ private function hasAddressRequiredData(array $address): bool
'countryId',
];

foreach ($requiredFields as $field) {
if (!isset($address[$field])) {
return false;
}
$phoneRequired = $this->systemConfigService->get('core.loginRegistration.phoneNumberFieldRequired') ?? false;
if ($phoneRequired) {
$requiredFields[] = 'phone';
}

return true;
$birthdayRequired = $this->systemConfigService->get('core.loginRegistration.birthdayFieldRequired') ?? false;
if ($birthdayRequired) {
$requiredFields[] = 'birthday';
}

$additionalAddress1Required = $this->systemConfigService->get('core.loginRegistration.additionalAddressField1Required') ?? false;
if ($additionalAddress1Required) {
$requiredFields[] = 'additionalAddressLine1';
}

$additionalAddress2Required = $this->systemConfigService->get('core.loginRegistration.additionalAddressField2Required') ?? false;
if ($additionalAddress2Required) {
$requiredFields[] = 'additionalAddressLine2';
}

return $requiredFields;
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
<argument key="$translator" type="service" id="translator" />
<argument key="$countryRepository" type="service" id="country.repository" />
<argument key="$salutationRepository" type="service" id="salutation.repository" />
<argument key="$systemConfigService" type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
<argument key="$logger" type="service" id="monolog.logger.payone" />
</service>

<service id="PayonePayment\Components\AmazonPay\ButtonConfiguration" autowire="true">
Expand Down

0 comments on commit 569dd27

Please sign in to comment.