-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAYOSWXP-138: saving customer information: add switch to enable/disable
+ 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
1 parent
c66db9a
commit 12702c6
Showing
27 changed files
with
210 additions
and
107 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
src/Components/CustomerDataPersistor/CustomerDataPersistor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.