Skip to content

Commit b424fdc

Browse files
MAGETWO-64833: [Github] PayPal Express Checkout Discards Address Line 2 For Billing and Shipping Addresses #8313
- Fixed handling for the 'street2' additional param from the Paypal response - Removed code that allows to mix system address data and data from Paypal response what could lead to unpredictable mixing data - Added integration tests
1 parent b180d87 commit b424fdc

File tree

3 files changed

+184
-52
lines changed

3 files changed

+184
-52
lines changed

app/code/Magento/Paypal/Model/Api/Nvp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,9 @@ protected function _exportAddressses($data)
14821482
protected function _applyStreetAndRegionWorkarounds(\Magento\Framework\DataObject $address)
14831483
{
14841484
// merge street addresses into 1
1485-
if ($address->hasStreet2()) {
1486-
$address->setStreet(implode("\n", [$address->getStreet(), $address->getStreetLine(2)]));
1487-
$address->unsStreet2();
1485+
if ($address->getData('street2') !== null) {
1486+
$address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
1487+
$address->unsetData('street2');
14881488
}
14891489
// attempt to fetch region_id from directory
14901490
if ($address->getCountryId() && $address->getRegion()) {

app/code/Magento/Paypal/Model/Express/Checkout.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -905,30 +905,15 @@ public function getCheckoutMethod()
905905
protected function _setExportedAddressData($address, $exportedAddress)
906906
{
907907
// Exported data is more priority if we came from Express Checkout button
908-
$isButton = (bool)$this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON);
908+
$isButton = (bool)$this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON);
909909
if (!$isButton) {
910-
foreach ($exportedAddress->getExportedKeys() as $key) {
911-
$oldData = $address->getDataUsingMethod($key);
912-
$isEmpty = null;
913-
if (is_array($oldData)) {
914-
foreach ($oldData as $val) {
915-
if (!empty($val)) {
916-
$isEmpty = false;
917-
break;
918-
}
919-
$isEmpty = true;
920-
}
921-
}
922-
if (empty($oldData) || $isEmpty === true) {
923-
$address->setDataUsingMethod($key, $exportedAddress->getData($key));
924-
}
925-
}
926-
} else {
927-
foreach ($exportedAddress->getExportedKeys() as $key) {
928-
$data = $exportedAddress->getData($key);
929-
if (!empty($data)) {
930-
$address->setDataUsingMethod($key, $data);
931-
}
910+
return;
911+
}
912+
913+
foreach ($exportedAddress->getExportedKeys() as $key) {
914+
$data = $exportedAddress->getData($key);
915+
if (!empty($data)) {
916+
$address->setDataUsingMethod($key, $data);
932917
}
933918
}
934919
}

dev/tests/integration/testsuite/Magento/Paypal/Model/Express/CheckoutTest.php

Lines changed: 173 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
*/
66
namespace Magento\Paypal\Model\Express;
77

8+
use Magento\Framework\ObjectManagerInterface;
89
use Magento\Quote\Model\Quote;
910
use Magento\Checkout\Model\Type\Onepage;
1011
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\Paypal\Model\Config;
13+
use Magento\Paypal\Model\Express\Checkout;
14+
use Magento\Paypal\Model\Api\Type\Factory;
15+
use Magento\Paypal\Model\Api\Nvp;
16+
use Magento\Paypal\Model\Info;
1117

1218
/**
1319
* Class CheckoutTest
@@ -17,9 +23,34 @@
1723
class CheckoutTest extends \PHPUnit_Framework_TestCase
1824
{
1925
/**
20-
* @var \Magento\Framework\ObjectManagerInterface
26+
* @var ObjectManagerInterface
2127
*/
22-
protected $_objectManager;
28+
private $_objectManager;
29+
30+
/**
31+
* @var Info
32+
*/
33+
private $paypalInfo;
34+
35+
/**
36+
* @var Config
37+
*/
38+
private $paypalConfig;
39+
40+
/**
41+
* @var Factory
42+
*/
43+
private $apiTypeFactory;
44+
45+
/**
46+
* @var Nvp
47+
*/
48+
private $api;
49+
50+
/**
51+
* @var Checkout
52+
*/
53+
private $checkoutModel;
2354

2455
/**
2556
* Set up
@@ -29,6 +60,31 @@ class CheckoutTest extends \PHPUnit_Framework_TestCase
2960
protected function setUp()
3061
{
3162
$this->_objectManager = Bootstrap::getObjectManager();
63+
64+
$this->paypalInfo = $this->getMockBuilder(Info::class)
65+
->disableOriginalConstructor()
66+
->getMock();
67+
68+
$this->paypalConfig = $this->getMockBuilder(Config::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
72+
$this->api = $this->getMockBuilder(Nvp::class)
73+
->disableOriginalConstructor()
74+
->setMethods(['call', 'getExportedShippingAddress', 'getExportedBillingAddress'])
75+
->getMock();
76+
77+
$this->api->expects($this->any())
78+
->method('call')
79+
->will($this->returnValue([]));
80+
81+
$this->apiTypeFactory = $this->getMockBuilder(Factory::class)
82+
->disableOriginalConstructor()
83+
->getMock();
84+
85+
$this->apiTypeFactory->expects($this->any())
86+
->method('create')
87+
->will($this->returnValue($this->api));
3288
}
3389

3490
/**
@@ -111,7 +167,7 @@ public function testPlaceGuestQuote()
111167
protected function _getCheckout(Quote $quote)
112168
{
113169
return $this->_objectManager->create(
114-
\Magento\Paypal\Model\Express\Checkout::class,
170+
Checkout::class,
115171
[
116172
'params' => [
117173
'config' => $this->getMock(\Magento\Paypal\Model\Config::class, [], [], '', false),
@@ -131,43 +187,30 @@ protected function _getCheckout(Quote $quote)
131187
public function testReturnFromPaypal()
132188
{
133189
$quote = $this->_getFixtureQuote();
134-
$paypalConfigMock = $this->getMock(\Magento\Paypal\Model\Config::class, [], [], '', false);
135-
$apiTypeFactory = $this->getMock(\Magento\Paypal\Model\Api\Type\Factory::class, [], [], '', false);
136-
$paypalInfo = $this->getMock(\Magento\Paypal\Model\Info::class, [], [], '', false);
137-
$checkoutModel = $this->_objectManager->create(
138-
\Magento\Paypal\Model\Express\Checkout::class,
190+
$this->checkoutModel = $this->_objectManager->create(
191+
Checkout::class,
139192
[
140-
'params' => ['quote' => $quote, 'config' => $paypalConfigMock],
141-
'apiTypeFactory' => $apiTypeFactory,
142-
'paypalInfo' => $paypalInfo
193+
'params' => ['quote' => $quote, 'config' => $this->paypalConfig],
194+
'apiTypeFactory' => $this->apiTypeFactory,
195+
'paypalInfo' => $this->paypalInfo
143196
]
144197
);
145198

146-
$api = $this->getMock(
147-
\Magento\Paypal\Model\Api\Nvp::class,
148-
['call', 'getExportedShippingAddress', 'getExportedBillingAddress'],
149-
[],
150-
'',
151-
false
152-
);
153-
$api->expects($this->any())->method('call')->will($this->returnValue([]));
154-
$apiTypeFactory->expects($this->any())->method('create')->will($this->returnValue($api));
155-
156199
$exportedBillingAddress = $this->_getExportedAddressFixture($quote->getBillingAddress()->getData());
157-
$api->expects($this->any())
200+
$this->api->expects($this->any())
158201
->method('getExportedBillingAddress')
159202
->will($this->returnValue($exportedBillingAddress));
160203

161204
$exportedShippingAddress = $this->_getExportedAddressFixture($quote->getShippingAddress()->getData());
162-
$api->expects($this->any())
205+
$this->api->expects($this->any())
163206
->method('getExportedShippingAddress')
164207
->will($this->returnValue($exportedShippingAddress));
165208

166-
$paypalInfo->expects($this->once())->method('importToPayment')->with($api, $quote->getPayment());
209+
$this->paypalInfo->expects($this->once())->method('importToPayment')->with($this->api, $quote->getPayment());
167210

168211
$quote->getPayment()->setAdditionalInformation(Checkout::PAYMENT_INFO_BUTTON, 1);
169212

170-
$checkoutModel->returnFromPaypal('token');
213+
$this->checkoutModel->returnFromPaypal('token');
171214

172215
$billingAddress = $quote->getBillingAddress();
173216

@@ -182,7 +225,6 @@ public function testReturnFromPaypal()
182225
$this->assertNull($shippingAddress->getSuffix());
183226
$this->assertTrue($shippingAddress->getShouldIgnoreValidation());
184227
$this->assertContains('exported', $shippingAddress->getFirstname());
185-
186228
$paymentAdditionalInformation = $quote->getPayment()->getAdditionalInformation();
187229
$this->assertArrayHasKey(Checkout::PAYMENT_INFO_TRANSPORT_SHIPPING_METHOD, $paymentAdditionalInformation);
188230
$this->assertArrayHasKey(Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID, $paymentAdditionalInformation);
@@ -191,6 +233,111 @@ public function testReturnFromPaypal()
191233
$this->assertTrue($quote->getTotalsCollectedFlag());
192234
}
193235

236+
/**
237+
* The case when handling address data from Paypal button.
238+
* System's address fields are replacing from export Paypal data.
239+
*
240+
* @magentoDataFixture Magento/Paypal/_files/quote_payment_express_with_customer.php
241+
* @magentoAppIsolation enabled
242+
* @magentoDbIsolation enabled
243+
*/
244+
public function testReturnFromPaypalButton()
245+
{
246+
$quote = $this->_getFixtureQuote();
247+
$this->prepareCheckoutModel($quote);
248+
$quote->getPayment()->setAdditionalInformation(Checkout::PAYMENT_INFO_BUTTON, 1);
249+
250+
$this->checkoutModel->returnFromPaypal('token');
251+
252+
$shippingAddress = $quote->getShippingAddress();
253+
254+
$prefix = 'exported';
255+
$this->assertEquals([$prefix .$this->getExportedData()['street']], $shippingAddress->getStreet());
256+
$this->assertEquals($prefix . $this->getExportedData()['firstname'], $shippingAddress->getFirstname());
257+
$this->assertEquals($prefix . $this->getExportedData()['city'], $shippingAddress->getCity());
258+
$this->assertEquals($prefix . $this->getExportedData()['telephone'], $shippingAddress->getTelephone());
259+
// This fields not in exported keys list. Fields the same as quote shipping and billing address.
260+
$this->assertNotEquals($prefix . $this->getExportedData()['region'], $shippingAddress->getRegion());
261+
$this->assertNotEquals($prefix . $this->getExportedData()['email'], $shippingAddress->getEmail());
262+
}
263+
264+
/**
265+
* The case when handling address data from the checkout.
266+
* System's address fields are not replacing from export Paypal data.
267+
*
268+
* @magentoDataFixture Magento/Paypal/_files/quote_payment_express_with_customer.php
269+
* @magentoAppIsolation enabled
270+
* @magentoDbIsolation enabled
271+
*/
272+
public function testReturnFromPaypalIfCheckout()
273+
{
274+
$quote = $this->_getFixtureQuote();
275+
$this->prepareCheckoutModel($quote);
276+
$quote->getPayment()->setAdditionalInformation(Checkout::PAYMENT_INFO_BUTTON, 0);
277+
278+
$this->checkoutModel->returnFromPaypal('token');
279+
280+
$shippingAddress = $quote->getShippingAddress();
281+
282+
$prefix = 'exported';
283+
284+
$this->assertNotEquals([$prefix .$this->getExportedData()['street']], $shippingAddress->getStreet());
285+
$this->assertNotEquals($prefix . $this->getExportedData()['firstname'], $shippingAddress->getFirstname());
286+
$this->assertNotEquals($prefix . $this->getExportedData()['city'], $shippingAddress->getCity());
287+
$this->assertNotEquals($prefix . $this->getExportedData()['telephone'], $shippingAddress->getTelephone());
288+
}
289+
290+
/**
291+
* Initialize a checkout model mock.
292+
*
293+
* @param Quote $quote
294+
*/
295+
private function prepareCheckoutModel(Quote $quote)
296+
{
297+
$this->checkoutModel = $this->_objectManager->create(
298+
Checkout::class,
299+
[
300+
'params' => ['quote' => $quote, 'config' => $this->paypalConfig],
301+
'apiTypeFactory' => $this->apiTypeFactory,
302+
'paypalInfo' => $this->paypalInfo
303+
]
304+
);
305+
306+
$exportedBillingAddress = $this->_getExportedAddressFixture($this->getExportedData());
307+
$this->api->expects($this->any())
308+
->method('getExportedBillingAddress')
309+
->will($this->returnValue($exportedBillingAddress));
310+
311+
$exportedShippingAddress = $this->_getExportedAddressFixture($this->getExportedData());
312+
$this->api->expects($this->any())
313+
->method('getExportedShippingAddress')
314+
->will($this->returnValue($exportedShippingAddress));
315+
316+
$this->paypalInfo->expects($this->once())
317+
->method('importToPayment')
318+
->with($this->api, $quote->getPayment());
319+
}
320+
321+
/**
322+
* A Paypal response stub.
323+
*
324+
* @return array
325+
*/
326+
private function getExportedData()
327+
{
328+
return
329+
[
330+
'company' => 'Testcompany',
331+
'email' => 'buyeraccountmpi@gmail.com',
332+
'firstname' => 'testFirstName',
333+
'country_id' => 'US',
334+
'region' => 'testRegion',
335+
'city' => 'testSity',
336+
'street' => 'testStreet',
337+
'telephone' => '223344',
338+
];
339+
}
340+
194341
/**
195342
* Prepare fixture for exported address
196343
*

0 commit comments

Comments
 (0)