Skip to content

Commit 5bc04c6

Browse files
author
Spandana Chittimala
committed
Merge branch 'MC-18365' of https://github.com/magento-chaika/magento2ce into PR-16-08-2019
2 parents af72789 + 7e92a37 commit 5bc04c6

File tree

4 files changed

+108
-44
lines changed

4 files changed

+108
-44
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
namespace Magento\Paypal\Model\Api;
88

9+
use Magento\Framework\DataObject;
910
use Magento\Payment\Model\Cart;
1011
use Magento\Payment\Model\Method\Logger;
1112

1213
/**
1314
* NVP API wrappers model
15+
*
1416
* @TODO: move some parts to abstract, don't hesitate to throw exceptions on api calls
1517
*
1618
* @method string getToken()
@@ -1085,11 +1087,11 @@ public function callUpdateBillingAgreement()
10851087
* Import callback request array into $this public data
10861088
*
10871089
* @param array $request
1088-
* @return \Magento\Framework\DataObject
1090+
* @return DataObject
10891091
*/
10901092
public function prepareShippingOptionsCallbackAddress(array $request)
10911093
{
1092-
$address = new \Magento\Framework\DataObject();
1094+
$address = new DataObject();
10931095
\Magento\Framework\DataObject\Mapper::accumulateByMap($request, $address, $this->_callbackRequestMap);
10941096
$address->setExportedKeys(array_values($this->_callbackRequestMap));
10951097
$this->_applyStreetAndRegionWorkarounds($address);
@@ -1126,6 +1128,7 @@ protected function _addMethodToRequest($methodName, $request)
11261128

11271129
/**
11281130
* Additional response processing.
1131+
*
11291132
* Hack to cut off length from API type response params.
11301133
*
11311134
* @param array $response
@@ -1414,6 +1417,7 @@ protected function _validateResponse($method, $response)
14141417

14151418
/**
14161419
* Parse an NVP response string into an associative array
1420+
*
14171421
* @param string $nvpstr
14181422
* @return array
14191423
*/
@@ -1477,7 +1481,7 @@ protected function _exportAddressses($data)
14771481
*/
14781482
protected function _exportAddresses($data)
14791483
{
1480-
$address = new \Magento\Framework\DataObject();
1484+
$address = new DataObject();
14811485
\Magento\Framework\DataObject\Mapper::accumulateByMap($data, $address, $this->_billingAddressMap);
14821486
$address->setExportedKeys(array_values($this->_billingAddressMap));
14831487
$this->_applyStreetAndRegionWorkarounds($address);
@@ -1488,18 +1492,18 @@ protected function _exportAddresses($data)
14881492
\Magento\Framework\DataObject\Mapper::accumulateByMap($data, $shippingAddress, $this->_shippingAddressMap);
14891493
$this->_applyStreetAndRegionWorkarounds($shippingAddress);
14901494
// PayPal doesn't provide detailed shipping name fields, so the name will be overwritten
1491-
$shippingAddress->addData(['firstname' => $data['SHIPTONAME']]);
1495+
$this->updateShippingAddressWithShipToName($shippingAddress, $data);
14921496
$this->setExportedShippingAddress($shippingAddress);
14931497
}
14941498
}
14951499

14961500
/**
14971501
* Adopt specified address object to be compatible with Magento
14981502
*
1499-
* @param \Magento\Framework\DataObject $address
1503+
* @param DataObject $address
15001504
* @return void
15011505
*/
1502-
protected function _applyStreetAndRegionWorkarounds(\Magento\Framework\DataObject $address)
1506+
protected function _applyStreetAndRegionWorkarounds(DataObject $address)
15031507
{
15041508
// merge street addresses into 1
15051509
if ($address->getData('street2') !== null) {
@@ -1515,11 +1519,10 @@ protected function _applyStreetAndRegionWorkarounds(\Magento\Framework\DataObjec
15151519
)->setPageSize(
15161520
1
15171521
);
1518-
foreach ($regions as $region) {
1519-
$address->setRegionId($region->getId());
1520-
$address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
1521-
break;
1522-
}
1522+
$regionItems = $regions->getItems();
1523+
$region = array_shift($regionItems);
1524+
$address->setRegionId($region->getId());
1525+
$address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
15231526
}
15241527
}
15251528

@@ -1757,4 +1760,23 @@ protected function _prepareExpressCheckoutCallRequest(&$requestFields)
17571760
}
17581761
}
17591762
}
1763+
1764+
/**
1765+
* Updates shipping address with 'ship to name' data
1766+
*
1767+
* @param DataObject $shippingAddress
1768+
* @param array $data
1769+
* @return void
1770+
*/
1771+
private function updateShippingAddressWithShipToName(DataObject $shippingAddress, array $data)
1772+
{
1773+
if (isset($data['SHIPTONAME'])) {
1774+
$nameParts = explode(' ', $data['SHIPTONAME'], 2);
1775+
$shippingAddress->addData(['firstname' => $nameParts[0]]);
1776+
1777+
if (isset($nameParts[1])) {
1778+
$shippingAddress->addData(['lastname' => $nameParts[1]]);
1779+
}
1780+
}
1781+
}
17601782
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ public function __construct(
357357
if (isset($params['config']) && $params['config'] instanceof PaypalConfig) {
358358
$this->_config = $params['config'];
359359
} else {
360+
// phpcs:ignore Magento2.Exceptions.DirectThrow
360361
throw new \Exception('Config instance is required.');
361362
}
362363

363364
if (isset($params['quote']) && $params['quote'] instanceof \Magento\Quote\Model\Quote) {
364365
$this->_quote = $params['quote'];
365366
} else {
367+
// phpcs:ignore Magento2.Exceptions.DirectThrow
366368
throw new \Exception('Quote instance is required.');
367369
}
368370
}
@@ -631,10 +633,9 @@ public function returnFromPaypal($token, string $payerIdentifier = null)
631633
if ($shippingAddress) {
632634
if ($exportedShippingAddress && $isButton) {
633635
$this->_setExportedAddressData($shippingAddress, $exportedShippingAddress);
634-
// PayPal doesn't provide detailed shipping info: prefix, middlename, lastname, suffix
636+
// PayPal doesn't provide detailed shipping info: prefix, middlename, suffix
635637
$shippingAddress->setPrefix(null);
636638
$shippingAddress->setMiddlename(null);
637-
$shippingAddress->setLastname(null);
638639
$shippingAddress->setSuffix(null);
639640
$shippingAddress->setCollectShippingRates(true);
640641
$shippingAddress->setSameAsBilling(0);
@@ -1037,7 +1038,7 @@ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = f
10371038

10381039
// Magento will transfer only first 10 cheapest shipping options if there are more than 10 available.
10391040
if (count($options) > 10) {
1040-
usort($options, [get_class($this), 'cmpShippingOptions']);
1041+
usort($options, [$this, 'cmpShippingOptions']);
10411042
array_splice($options, 10);
10421043
// User selected option will be always included in options list
10431044
if ($userSelectedOption !== null && !in_array($userSelectedOption, $options)) {
@@ -1058,7 +1059,7 @@ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = f
10581059
* @param \Magento\Framework\DataObject $option2
10591060
* @return int
10601061
*/
1061-
protected static function cmpShippingOptions(DataObject $option1, DataObject $option2)
1062+
protected function cmpShippingOptions(DataObject $option1, DataObject $option2)
10621063
{
10631064
return $option1->getAmount() <=> $option2->getAmount();
10641065
}

app/code/Magento/Paypal/Test/Unit/Model/Api/NvpTest.php

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Paypal\Test\Unit\Model\Api;
88

9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1011
use Magento\Paypal\Model\Info;
1112

@@ -35,7 +36,7 @@ class NvpTest extends \PHPUnit\Framework\TestCase
3536
/** @var \Magento\Paypal\Model\Api\ProcessableException|\PHPUnit_Framework_MockObject_MockObject */
3637
protected $processableException;
3738

38-
/** @var \Magento\Framework\Exception\LocalizedException|\PHPUnit_Framework_MockObject_MockObject */
39+
/** @var LocalizedException|\PHPUnit_Framework_MockObject_MockObject */
3940
protected $exception;
4041

4142
/** @var \Magento\Framework\HTTP\Adapter\Curl|\PHPUnit_Framework_MockObject_MockObject */
@@ -47,6 +48,9 @@ class NvpTest extends \PHPUnit\Framework\TestCase
4748
/** @var \Magento\Payment\Model\Method\Logger|\PHPUnit_Framework_MockObject_MockObject */
4849
protected $customLoggerMock;
4950

51+
/**
52+
* {@inheritDoc}
53+
*/
5054
protected function setUp()
5155
{
5256
$this->customerAddressHelper = $this->createMock(\Magento\Customer\Helper\Address::class);
@@ -64,26 +68,32 @@ protected function setUp()
6468
);
6569
$processableExceptionFactory->expects($this->any())
6670
->method('create')
67-
->will($this->returnCallback(function ($arguments) {
68-
$this->processableException = $this->getMockBuilder(
69-
\Magento\Paypal\Model\Api\ProcessableException::class
71+
->will(
72+
$this->returnCallback(
73+
function ($arguments) {
74+
$this->processableException = $this->getMockBuilder(
75+
\Magento\Paypal\Model\Api\ProcessableException::class
76+
)->setConstructorArgs([$arguments['phrase'], null, $arguments['code']])->getMock();
77+
return $this->processableException;
78+
}
7079
)
71-
->setConstructorArgs([$arguments['phrase'], null, $arguments['code']])
72-
->getMock();
73-
return $this->processableException;
74-
}));
80+
);
7581
$exceptionFactory = $this->createPartialMock(
7682
\Magento\Framework\Exception\LocalizedExceptionFactory::class,
7783
['create']
7884
);
7985
$exceptionFactory->expects($this->any())
8086
->method('create')
81-
->will($this->returnCallback(function ($arguments) {
82-
$this->exception = $this->getMockBuilder(\Magento\Framework\Exception\LocalizedException::class)
83-
->setConstructorArgs([$arguments['phrase']])
84-
->getMock();
85-
return $this->exception;
86-
}));
87+
->will(
88+
$this->returnCallback(
89+
function ($arguments) {
90+
$this->exception = $this->getMockBuilder(LocalizedException::class)
91+
->setConstructorArgs([$arguments['phrase']])
92+
->getMock();
93+
return $this->exception;
94+
}
95+
)
96+
);
8797
$this->curl = $this->createMock(\Magento\Framework\HTTP\Adapter\Curl::class);
8898
$curlFactory = $this->createPartialMock(\Magento\Framework\HTTP\Adapter\CurlFactory::class, ['create']);
8999
$curlFactory->expects($this->any())->method('create')->will($this->returnValue($this->curl));
@@ -155,7 +165,7 @@ public function callDataProvider()
155165
[
156166
"\r\n" . 'ACK=Failure&L_ERRORCODE0=10417&L_SHORTMESSAGE0=Message.&L_LONGMESSAGE0=Long%20Message.',
157167
[],
158-
\Magento\Framework\Exception\LocalizedException::class,
168+
LocalizedException::class,
159169
'PayPal gateway has rejected request. Long Message (#10417: Message).',
160170
0
161171
],
@@ -184,27 +194,56 @@ public function callDataProvider()
184194
];
185195
}
186196

187-
public function testCallGetExpressCheckoutDetails()
197+
/**
198+
* Test getting of the ExpressCheckout details
199+
*
200+
* @param $input
201+
* @param $expected
202+
* @dataProvider callGetExpressCheckoutDetailsDataProvider
203+
*/
204+
public function testCallGetExpressCheckoutDetails($input, $expected)
188205
{
189206
$this->curl->expects($this->once())
190207
->method('read')
191-
->will($this->returnValue(
192-
"\r\n" . 'ACK=Success&SHIPTONAME=Ship%20To%20Name'
208+
->will($this->returnValue($input));
209+
$this->model->callGetExpressCheckoutDetails();
210+
$address = $this->model->getExportedShippingAddress();
211+
$this->assertEquals($expected['firstName'], $address->getData('firstname'));
212+
$this->assertEquals($expected['lastName'], $address->getData('lastname'));
213+
$this->assertEquals($expected['street'], $address->getStreet());
214+
$this->assertEquals($expected['company'], $address->getCompany());
215+
$this->assertEquals($expected['city'], $address->getCity());
216+
$this->assertEquals($expected['telephone'], $address->getTelephone());
217+
$this->assertEquals($expected['region'], $address->getRegion());
218+
}
219+
220+
/**
221+
* Data Provider
222+
*
223+
* @return array
224+
*/
225+
public function callGetExpressCheckoutDetailsDataProvider()
226+
{
227+
return [
228+
[
229+
"\r\n" . 'ACK=Success&SHIPTONAME=Jane%20Doe'
193230
. '&SHIPTOSTREET=testStreet'
194231
. '&SHIPTOSTREET2=testApartment'
195232
. '&BUSINESS=testCompany'
196233
. '&SHIPTOCITY=testCity'
197234
. '&PHONENUM=223322'
198-
. '&STATE=testSTATE'
199-
));
200-
$this->model->callGetExpressCheckoutDetails();
201-
$address = $this->model->getExportedShippingAddress();
202-
$this->assertEquals('Ship To Name', $address->getData('firstname'));
203-
$this->assertEquals(implode("\n", ['testStreet','testApartment']), $address->getStreet());
204-
$this->assertEquals('testCompany', $address->getCompany());
205-
$this->assertEquals('testCity', $address->getCity());
206-
$this->assertEquals('223322', $address->getTelephone());
207-
$this->assertEquals('testSTATE', $address->getRegion());
235+
. '&STATE=testSTATE',
236+
[
237+
'firstName' => 'Jane',
238+
'lastName' => 'Doe',
239+
'street' => 'testStreet' . "\n" . 'testApartment',
240+
'company' => 'testCompany',
241+
'city' => 'testCity',
242+
'telephone' => '223322',
243+
'region' => 'testSTATE',
244+
]
245+
]
246+
];
208247
}
209248

210249
/**
@@ -243,6 +282,9 @@ public function testCallDoReauthorization()
243282
$this->assertEquals($expectedImportedData, $this->model->getData());
244283
}
245284

285+
/**
286+
* Test replace keys for debug data
287+
*/
246288
public function testGetDebugReplacePrivateDataKeys()
247289
{
248290
$debugReplacePrivateDataKeys = $this->_invokeNvpProperty($this->model, '_debugReplacePrivateDataKeys');

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ public function testReturnFromPaypal()
278278
$this->assertTrue((bool)$shippingAddress->getSameAsBilling());
279279
$this->assertNull($shippingAddress->getPrefix());
280280
$this->assertNull($shippingAddress->getMiddlename());
281-
$this->assertNull($shippingAddress->getLastname());
282281
$this->assertNull($shippingAddress->getSuffix());
283282
$this->assertTrue($shippingAddress->getShouldIgnoreValidation());
284283
$this->assertContains('exported', $shippingAddress->getFirstname());

0 commit comments

Comments
 (0)