Skip to content

Commit e3b7d35

Browse files
MC-29946: [On Prem] - When transaction is declined, no default shipping / billing gets set after entering another card
1 parent 3e23510 commit e3b7d35

File tree

2 files changed

+107
-37
lines changed

2 files changed

+107
-37
lines changed

app/code/Magento/Customer/Model/ResourceModel/Address/Relation.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
/**
3-
* Customer address entity resource model
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
6+
declare(strict_types=1);
7+
88
namespace Magento\Customer\Model\ResourceModel\Address;
99

10+
use Magento\Customer\Api\Data\CustomerInterface;
1011
use Magento\Customer\Model\Address;
1112
use Magento\Customer\Model\Customer;
13+
use Magento\Customer\Model\CustomerFactory;
14+
use Magento\Customer\Model\CustomerRegistry;
15+
use Magento\Framework\Model\AbstractModel;
1216
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationInterface;
1317

1418
/**
@@ -17,29 +21,36 @@
1721
class Relation implements RelationInterface
1822
{
1923
/**
20-
* @var \Magento\Customer\Model\CustomerFactory
24+
* @var CustomerFactory
2125
*/
2226
protected $customerFactory;
2327

2428
/**
25-
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
29+
* @var CustomerRegistry
2630
*/
27-
public function __construct(\Magento\Customer\Model\CustomerFactory $customerFactory)
28-
{
31+
private $customerRegistry;
32+
33+
/**
34+
* @param CustomerFactory $customerFactory
35+
* @param CustomerRegistry $customerRegistry
36+
*/
37+
public function __construct(
38+
CustomerFactory $customerFactory,
39+
CustomerRegistry $customerRegistry
40+
) {
2941
$this->customerFactory = $customerFactory;
42+
$this->customerRegistry = $customerRegistry;
3043
}
3144

3245
/**
3346
* Process object relations
3447
*
35-
* @param \Magento\Framework\Model\AbstractModel $object
48+
* @param AbstractModel $object
3649
* @return void
3750
*/
38-
public function processRelation(\Magento\Framework\Model\AbstractModel $object)
51+
public function processRelation(AbstractModel $object): void
3952
{
40-
/**
41-
* @var $object Address
42-
*/
53+
/** @var $object Address */
4354
if (!$object->getIsCustomerSaveTransaction() && $object->getId()) {
4455
$customer = $this->customerFactory->create()->load($object->getCustomerId());
4556

@@ -53,6 +64,7 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
5364
$changedAddresses,
5465
$customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
5566
);
67+
$this->updateCustomerRegistry($customer, $changedAddresses);
5668
}
5769
}
5870
}
@@ -71,12 +83,12 @@ private function getDefaultBillingChangedAddress(
7183
array $changedAddresses
7284
): array {
7385
if ($object->getIsDefaultBilling()) {
74-
$changedAddresses['default_billing'] = $object->getId();
86+
$changedAddresses[CustomerInterface::DEFAULT_BILLING] = $object->getId();
7587
} elseif ($customer->getDefaultBillingAddress()
7688
&& $object->getIsDefaultBilling() === false
7789
&& (int)$customer->getDefaultBillingAddress()->getId() === (int)$object->getId()
7890
) {
79-
$changedAddresses['default_billing'] = null;
91+
$changedAddresses[CustomerInterface::DEFAULT_BILLING] = null;
8092
}
8193

8294
return $changedAddresses;
@@ -96,27 +108,47 @@ private function getDefaultShippingChangedAddress(
96108
array $changedAddresses
97109
): array {
98110
if ($object->getIsDefaultShipping()) {
99-
$changedAddresses['default_shipping'] = $object->getId();
111+
$changedAddresses[CustomerInterface::DEFAULT_SHIPPING] = $object->getId();
100112
} elseif ($customer->getDefaultShippingAddress()
101113
&& $object->getIsDefaultShipping() === false
102114
&& (int)$customer->getDefaultShippingAddress()->getId() === (int)$object->getId()
103115
) {
104-
$changedAddresses['default_shipping'] = null;
116+
$changedAddresses[CustomerInterface::DEFAULT_SHIPPING] = null;
105117
}
106118

107119
return $changedAddresses;
108120
}
109121

122+
/**
123+
* Push updated customer entity to the registry.
124+
*
125+
* @param Customer $customer
126+
* @param array $changedAddresses
127+
* @return void
128+
*/
129+
private function updateCustomerRegistry(Customer $customer, array $changedAddresses): void
130+
{
131+
if (array_key_exists(CustomerInterface::DEFAULT_BILLING, $changedAddresses)) {
132+
$customer->setDefaultBilling($changedAddresses[CustomerInterface::DEFAULT_BILLING]);
133+
}
134+
135+
if (array_key_exists(CustomerInterface::DEFAULT_SHIPPING, $changedAddresses)) {
136+
$customer->setDefaultShipping($changedAddresses[CustomerInterface::DEFAULT_SHIPPING]);
137+
}
138+
139+
$this->customerRegistry->push($customer);
140+
}
141+
110142
/**
111143
* Checks if address has chosen as default and has had an id
112144
*
113145
* @deprecated Is not used anymore due to changes in logic of save of address.
114146
* If address was default and becomes not default than default address id for customer must be
115147
* set to null
116-
* @param \Magento\Framework\Model\AbstractModel $object
148+
* @param AbstractModel $object
117149
* @return bool
118150
*/
119-
protected function isAddressDefault(\Magento\Framework\Model\AbstractModel $object)
151+
protected function isAddressDefault(AbstractModel $object)
120152
{
121153
return $object->getId() && ($object->getIsDefaultBilling() || $object->getIsDefaultShipping());
122154
}

dev/tests/integration/testsuite/Magento/Customer/Model/ResourceModel/AddressRepositoryTest.php

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class AddressRepositoryTest extends \PHPUnit\Framework\TestCase
4040
/** @var \Magento\Framework\Api\DataObjectHelper */
4141
private $dataObjectHelper;
4242

43+
private $customerRegistry;
44+
4345
/**
4446
* Set up.
4547
*/
@@ -56,6 +58,7 @@ protected function setUp()
5658
\Magento\Customer\Api\Data\AddressInterfaceFactory::class
5759
);
5860
$this->dataObjectHelper = $this->objectManager->create(\Magento\Framework\Api\DataObjectHelper::class);
61+
$this->customerRegistry = $this->objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
5962

6063
$regionFactory = $this->objectManager->get(RegionInterfaceFactory::class);
6164
$region = $regionFactory->create()
@@ -96,10 +99,7 @@ protected function setUp()
9699
*/
97100
protected function tearDown()
98101
{
99-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
100-
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
101-
$customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
102-
$customerRegistry->remove(1);
102+
$this->customerRegistry->remove(1);
103103
}
104104

105105
/**
@@ -506,6 +506,60 @@ public function testSaveAddressWithRestrictedCountries()
506506
self::assertNotEmpty($saved->getId());
507507
}
508508

509+
/**
510+
* Test for saving address with extra spaces in phone.
511+
*
512+
* @magentoDataFixture Magento/Customer/_files/customer.php
513+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
514+
*/
515+
public function testSaveNewAddressWithExtraSpacesInPhone()
516+
{
517+
$proposedAddress = $this->_createSecondAddress()
518+
->setCustomerId(1)
519+
->setTelephone(' 123456 ');
520+
$returnedAddress = $this->repository->save($proposedAddress);
521+
$savedAddress = $this->repository->getById($returnedAddress->getId());
522+
$this->assertEquals('123456', $savedAddress->getTelephone());
523+
}
524+
525+
/**
526+
* Scenario for customer's default shipping and billing address saving and rollback.
527+
*
528+
* @magentoDataFixture Magento/Customer/_files/customer_without_addresses.php
529+
*/
530+
public function testCustomerAddressRelationSynchronisation()
531+
{
532+
/**
533+
* Creating new address which is default shipping and billing for existing customer.
534+
*/
535+
$address = $this->expectedAddresses[0];
536+
$address->setId(null);
537+
$address->setCustomerId(1);
538+
$address->setIsDefaultShipping(true);
539+
$address->setIsDefaultBilling(true);
540+
$savedAddress = $this->repository->save($address);
541+
542+
/**
543+
* Customer registry should be updated with default shipping and billing addresses.
544+
*/
545+
$customer = $this->getCustomer('customer@example.com', 1);
546+
$this->assertEquals($savedAddress->getId(), $customer->getDefaultShipping());
547+
$this->assertEquals($savedAddress->getId(), $customer->getDefaultBilling());
548+
549+
/**
550+
* Registry should be clean up for reading data from DB.
551+
*/
552+
$this->repository->deleteById($savedAddress->getId());
553+
$this->customerRegistry->removeByEmail('customer@example.com');
554+
555+
/**
556+
* Customer's default shipping and billing addresses should be updated.
557+
*/
558+
$customer = $this->getCustomer('customer@example.com', 1);
559+
$this->assertNull($customer->getDefaultShipping());
560+
$this->assertNull($customer->getDefaultBilling());
561+
}
562+
509563
/**
510564
* Helper function that returns an Address Data Object that matches the data from customer_address fixture
511565
*
@@ -571,20 +625,4 @@ private function getWebsite(string $code): WebsiteInterface
571625
$repository = $this->objectManager->get(WebsiteRepositoryInterface::class);
572626
return $repository->get($code);
573627
}
574-
575-
/**
576-
* Test for saving address with extra spaces in phone.
577-
*
578-
* @magentoDataFixture Magento/Customer/_files/customer.php
579-
* @magentoDataFixture Magento/Customer/_files/customer_address.php
580-
*/
581-
public function testSaveNewAddressWithExtraSpacesInPhone()
582-
{
583-
$proposedAddress = $this->_createSecondAddress()
584-
->setCustomerId(1)
585-
->setTelephone(' 123456 ');
586-
$returnedAddress = $this->repository->save($proposedAddress);
587-
$savedAddress = $this->repository->getById($returnedAddress->getId());
588-
$this->assertEquals('123456', $savedAddress->getTelephone());
589-
}
590628
}

0 commit comments

Comments
 (0)