Skip to content

Commit

Permalink
PAYOSWXP-138: saving customer information: add switch to enable/disable
Browse files Browse the repository at this point in the history
+ move saving to separate class to prevent saving during other request-builder calls
+ add saving of phonenumber into customer-address (prev. it was only saved into order-address)
  • Loading branch information
rommelfreddy committed Apr 29, 2024
1 parent c66db9a commit 12702c6
Show file tree
Hide file tree
Showing 27 changed files with 210 additions and 107 deletions.
125 changes: 125 additions & 0 deletions src/Components/CustomerDataPersistor/CustomerDataPersistor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\CustomerDataPersistor;

use PayonePayment\RequestConstants;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class CustomerDataPersistor
{
public function __construct(
private readonly EntityRepository $orderAddressRepository,
private readonly EntityRepository $customerRepository,
private readonly EntityRepository $customerAddressRepository,
private readonly SystemConfigService $configService
) {
}

public function save(OrderEntity $order, RequestDataBag $dataBag, Context $context): void
{
$this->saveOrderData($order, $dataBag, $context);

if ($this->configService->getBool('PayonePayment.settings.saveUserEnteredDataToCustomer')) {
$customer = $this->getCustomer($order, $context);
if ($customer instanceof CustomerEntity) {
$this->saveCustomerData($customer, $dataBag, $context);
$customerAddress = $this->getCustomerAddress($customer, $context);
if ($customerAddress instanceof CustomerAddressEntity) {
$this->saveCustomerAddressData($customerAddress, $dataBag, $context);
}
}
}
}

protected function saveCustomerData(CustomerEntity $customer, RequestDataBag $dataBag, Context $context): void
{
$birthday = $dataBag->get(RequestConstants::BIRTHDAY);
$birthday = \is_string($birthday) ? \DateTime::createFromFormat('Y-m-d', $birthday) ?: null : null;

if ($birthday instanceof \DateTime && $birthday->getTimestamp() !== $customer->getBirthday()?->getTimestamp()) {
$this->customerRepository->update(
[
[
'id' => $customer->getId(),
'birthday' => $birthday,
],
],
$context
);
}
}

protected function saveCustomerAddressData(CustomerAddressEntity $customerAddress, RequestDataBag $dataBag, Context $context): void
{
$phoneNumber = $dataBag->get(RequestConstants::PHONE);

if ($phoneNumber !== $customerAddress->getPhoneNumber()) {
$this->customerAddressRepository->update(
[
[
'id' => $customerAddress->getId(),
'phoneNumber' => $phoneNumber,
],
],
$context
);
}
}

protected function saveOrderData(OrderEntity $orderEntity, RequestDataBag $dataBag, Context $context): void
{
$phoneNumber = $dataBag->get(RequestConstants::PHONE);

$this->orderAddressRepository->update(
[
[
'id' => $orderEntity->getBillingAddressId(),
'phoneNumber' => $phoneNumber,
],
],
$context
);
}

private function getCustomer(OrderEntity $order, Context $context): ?CustomerEntity
{
$customer = $order->getOrderCustomer()?->getCustomer();
if ($customer instanceof CustomerEntity) {
return $customer;
}
$customerId = $order->getOrderCustomer()?->getCustomerId();
if (empty($customerId)) {
return null;
}

/** @var CustomerEntity|null $customer */
$customer = $this->customerRepository->search(new Criteria([$customerId]), $context)->first();

return $customer;
}

private function getCustomerAddress(CustomerEntity $customer, Context $context): ?CustomerAddressEntity
{
$customerAddress = $customer->getDefaultBillingAddress();
if ($customerAddress instanceof CustomerAddressEntity) {
return $customerAddress;
}

/** @var CustomerAddressEntity|null $customerAddress */
$customerAddress = $this->customerAddressRepository->search(
new Criteria([$customer->getDefaultBillingAddressId()]),
$context
)->first();

return $customerAddress;
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/handler/payment_handler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<argument key="$orderActionLogDataHandler" type="service" id="PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface" />
<argument key="$stateHandler" type="service" id="PayonePayment\Components\PaymentStateHandler\PaymentStateHandler" />
<argument key="$requestParameterFactory" type="service" id="PayonePayment\Payone\RequestParameter\RequestParameterFactory"/>
<argument key="$customerDataPersistor" type="service" id="PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor"/>
</service>

<service id="PayonePayment\PaymentHandler\AbstractSynchronousPayonePaymentHandler"
Expand All @@ -29,6 +30,7 @@
<argument key="$transactionDataHandler" type="service" id="PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface" />
<argument key="$orderActionLogDataHandler" type="service" id="PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface" />
<argument key="$requestParameterFactory" type="service" id="PayonePayment\Payone\RequestParameter\RequestParameterFactory"/>
<argument key="$customerDataPersistor" type="service" id="PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor"/>
</service>

<service id="PayonePayment\PaymentHandler\AbstractKlarnaPaymentHandler"
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,11 @@
<argument key="$languageRepository" type="service" id="language.repository" />
</service>

<service id="PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor" autowire="true">
<argument key="$orderAddressRepository" type="service" id="order_address.repository" />
<argument key="$customerRepository" type="service" id="customer.repository" />
<argument key="$customerAddressRepository" type="service" id="customer_address.repository" />
</service>

</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\PaymentHandler;

use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\DeviceFingerprint\AbstractDeviceFingerprintService;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function __construct(
protected readonly OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
protected readonly PaymentStateHandlerInterface $stateHandler,
protected readonly RequestParameterFactory $requestParameterFactory,
protected readonly CustomerDataPersistor $customerDataPersistor,
protected readonly ?AbstractDeviceFingerprintService $deviceFingerprintService = null
) {
parent::__construct($configReader, $lineItemRepository, $requestStack);
Expand All @@ -59,6 +61,8 @@ public function pay(
);
}

$this->customerDataPersistor->save($transaction->getOrder(), $dataBag, $salesChannelContext->getContext());

$paymentTransaction = PaymentTransaction::fromAsyncPaymentTransactionStruct($transaction, $transaction->getOrder());

$authorizationMethod = $this->getAuthorizationMethod(
Expand Down
5 changes: 4 additions & 1 deletion src/PaymentHandler/AbstractKlarnaPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PayonePayment\Components\CartHasher\CartHasherInterface;
use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\PaymentStateHandler\PaymentStateHandlerInterface;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function __construct(
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
PaymentStateHandlerInterface $stateHandler,
RequestParameterFactory $requestParameterFactory,
CustomerDataPersistor $customerDataPersistor,
protected CartHasherInterface $cartHasher
) {
parent::__construct(
Expand All @@ -45,7 +47,8 @@ public function __construct(
$transactionDataHandler,
$orderActionLogDataHandler,
$stateHandler,
$requestParameterFactory
$requestParameterFactory,
$customerDataPersistor
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\PaymentHandler;

use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\DeviceFingerprint\AbstractDeviceFingerprintService;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function __construct(
protected readonly TransactionDataHandlerInterface $transactionDataHandler,
protected readonly OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
protected readonly RequestParameterFactory $requestParameterFactory,
protected readonly CustomerDataPersistor $customerDataPersistor,
protected readonly ?AbstractDeviceFingerprintService $deviceFingerprintService = null
) {
parent::__construct($configReader, $lineItemRepository, $requestStack);
Expand All @@ -55,6 +57,8 @@ public function pay(
);
}

$this->customerDataPersistor->save($transaction->getOrder(), $dataBag, $salesChannelContext->getContext());

$paymentTransaction = PaymentTransaction::fromSyncPaymentTransactionStruct($transaction, $transaction->getOrder());

$authorizationMethod = $this->getAuthorizationMethod(
Expand Down
4 changes: 3 additions & 1 deletion src/PaymentHandler/PayoneAmazonPayPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Connection;
use PayonePayment\Components\AmazonPay\ButtonConfiguration;
use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\DeviceFingerprint\AbstractDeviceFingerprintService;
Expand Down Expand Up @@ -40,13 +41,14 @@ public function __construct(
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
PaymentStateHandlerInterface $stateHandler,
RequestParameterFactory $requestParameterFactory,
CustomerDataPersistor $customerDataPersistor,
private readonly RouterInterface $router,
private readonly Connection $connection,
private readonly ButtonConfiguration $buttonConfiguration,
private readonly EncoderInterface $encoder,
?AbstractDeviceFingerprintService $deviceFingerprintService = null
) {
parent::__construct($configReader, $lineItemRepository, $requestStack, $client, $translator, $transactionDataHandler, $orderActionLogDataHandler, $stateHandler, $requestParameterFactory, $deviceFingerprintService);
parent::__construct($configReader, $lineItemRepository, $requestStack, $client, $translator, $transactionDataHandler, $orderActionLogDataHandler, $stateHandler, $requestParameterFactory, $customerDataPersistor, $deviceFingerprintService);
}

public static function isCapturable(array $transactionData, array $payoneTransActionData): bool
Expand Down
5 changes: 4 additions & 1 deletion src/PaymentHandler/PayoneCreditCardPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PayonePayment\Components\CardRepository\CardRepositoryInterface;
use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\PaymentStateHandler\PaymentStateHandlerInterface;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function __construct(
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
PaymentStateHandlerInterface $stateHandler,
RequestParameterFactory $requestParameterFactory,
CustomerDataPersistor $customerDataPersistor,
protected CardRepositoryInterface $cardRepository
) {
parent::__construct(
Expand All @@ -52,7 +54,8 @@ public function __construct(
$transactionDataHandler,
$orderActionLogDataHandler,
$stateHandler,
$requestParameterFactory
$requestParameterFactory,
$customerDataPersistor
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/PaymentHandler/PayoneDebitPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\PaymentHandler;

use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\MandateService\MandateServiceInterface;
Expand Down Expand Up @@ -33,6 +34,7 @@ public function __construct(
TransactionDataHandlerInterface $transactionDataHandler,
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
RequestParameterFactory $requestParameterFactory,
CustomerDataPersistor $customerDataPersistor,
protected MandateServiceInterface $mandateService
) {
parent::__construct(
Expand All @@ -43,7 +45,8 @@ public function __construct(
$translator,
$transactionDataHandler,
$orderActionLogDataHandler,
$requestParameterFactory
$requestParameterFactory,
$customerDataPersistor
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PayonePayment\Components\CartHasher\CartHasherInterface;
use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\CustomerDataPersistor\CustomerDataPersistor;
use PayonePayment\Components\DataHandler\OrderActionLog\OrderActionLogDataHandlerInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\Validator\Birthday;
Expand Down Expand Up @@ -33,6 +34,7 @@ public function __construct(
TransactionDataHandlerInterface $transactionDataHandler,
OrderActionLogDataHandlerInterface $orderActionLogDataHandler,
RequestParameterFactory $requestParameterFactory,
CustomerDataPersistor $customerDataPersistor,
protected CartHasherInterface $cartHasher
) {
parent::__construct(
Expand All @@ -43,7 +45,8 @@ public function __construct(
$translator,
$transactionDataHandler,
$orderActionLogDataHandler,
$requestParameterFactory
$requestParameterFactory,
$customerDataPersistor
);
}

Expand Down
Loading

0 comments on commit 12702c6

Please sign in to comment.