-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PISHPS-352: Fix shipping for loggedin users (#854)
* NTR: PISHPS-352: Fix shipping for loggedin users * NTR: Cs fix --------- Co-authored-by: Vitalij Mik <[email protected]>
- Loading branch information
1 parent
2dcb708
commit 60d790a
Showing
8 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Components/ApplePayDirect/Services/ApplePayShippingAddressFaker.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,71 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Components\ApplePayDirect\Services; | ||
|
||
use Kiener\MolliePayments\Repository\Customer\CustomerRepositoryInterface; | ||
use Kiener\MolliePayments\Repository\CustomerAddress\CustomerAddressRepositoryInterface; | ||
use Shopware\Core\Checkout\Customer\CustomerEntity; | ||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\Uuid\Uuid; | ||
|
||
/** | ||
* We have to create a Fake Address in Case for Apple Pay Direct in order to load te correct shipping costs. | ||
* The shipping costs are loaded based on rules, if a customer is already logged in and using apple pay direct, then the country from his current address is used for the rules. | ||
* we have to add a temporary fake address to this customer in order to load correct shipping costs. afterwards we clear them | ||
*/ | ||
class ApplePayShippingAddressFaker | ||
{ | ||
private const ID_SUFFIX = 'applePayAddressId'; | ||
private CustomerRepositoryInterface $customerRepository; | ||
private CustomerAddressRepositoryInterface $customerAddressRepository; | ||
|
||
public function __construct( | ||
CustomerRepositoryInterface $customerRepository, | ||
CustomerAddressRepositoryInterface $customerAddressRepository | ||
) { | ||
$this->customerRepository = $customerRepository; | ||
$this->customerAddressRepository = $customerAddressRepository; | ||
} | ||
|
||
|
||
public function createFakeShippingAddress(string $countryId, CustomerEntity $customerEntity, Context $context): string | ||
{ | ||
$applePayAddressId = $this->generateAddressId($customerEntity); | ||
|
||
$this->customerRepository->update([ | ||
[ | ||
'id' => $customerEntity->getId(), | ||
'addresses' => [ | ||
[ | ||
'id' => $applePayAddressId, | ||
'salutationId' => $customerEntity->getSalutationId(), | ||
'countryId' => $countryId, | ||
'firstName' => $customerEntity->getFirstName(), | ||
'lastName' => $customerEntity->getLastName(), | ||
'city' => 'not provided', //city is not necessary for rule builder | ||
'street' => 'not provided' //apple pay event "onshippingcontactselected" does not prvide a street https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778009-onshippingcontactselected | ||
] | ||
] | ||
] | ||
], $context); | ||
|
||
return $applePayAddressId; | ||
} | ||
|
||
public function deleteFakeShippingAddress(CustomerEntity $customerEntity, Context $context): void | ||
{ | ||
$applePayAddressId = $this->generateAddressId($customerEntity); | ||
$this->customerAddressRepository->delete([ | ||
[ | ||
'id' => $applePayAddressId | ||
] | ||
], $context); | ||
} | ||
|
||
private function generateAddressId(CustomerEntity $customerEntity): string | ||
{ | ||
/** We cant use here Uuid::fromString because it does not exists in SW6.4 */ | ||
return Uuid::fromBytesToHex(md5($customerEntity->getId() . '-' . self::ID_SUFFIX, true)); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/Repository/CustomerAddress/CustomerAddressRepository.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,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Repository\CustomerAddress; | ||
|
||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; | ||
|
||
class CustomerAddressRepository implements CustomerAddressRepositoryInterface | ||
{ | ||
/** | ||
* @var EntityRepository | ||
*/ | ||
private $customerAddressRepository; | ||
|
||
/** | ||
* @param EntityRepository $customerAddressRepository | ||
*/ | ||
public function __construct($customerAddressRepository) | ||
{ | ||
$this->customerAddressRepository = $customerAddressRepository; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $ids | ||
* @param Context $context | ||
* @return EntityWrittenContainerEvent | ||
*/ | ||
public function delete(array $ids, Context $context): EntityWrittenContainerEvent | ||
{ | ||
return $this->customerAddressRepository->delete($ids, $context); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Repository/CustomerAddress/CustomerAddressRepositoryInterface.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,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Repository\CustomerAddress; | ||
|
||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; | ||
|
||
interface CustomerAddressRepositoryInterface | ||
{ | ||
/** | ||
* @param array<mixed> $ids | ||
* @param Context $context | ||
* @return EntityWrittenContainerEvent | ||
*/ | ||
public function delete(array $ids, Context $context): EntityWrittenContainerEvent; | ||
} |
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