Skip to content

Commit 340252c

Browse files
author
Ievgen Sentiabov
committed
MAGETWO-45127: Paypal doesn't work correctly if discount brings the subtotal to negative but the grand total is positive
- Changed subtotal calculating for PayPal Hosted Pro request params - Added discount param to request - Added total param to request
1 parent 180ca97 commit 340252c

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

app/code/Magento/Paypal/Model/Hostedpro/Request.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,11 @@ protected function _getAmountData(Order $order)
174174
private function getNonTaxableAmount(Order $order)
175175
{
176176
return [
177-
'subtotal' => $this->_formatPrice(
178-
$this->_formatPrice(
179-
$order->getPayment()->getBaseAmountAuthorized()
180-
) - $this->_formatPrice(
181-
$order->getBaseTaxAmount()
182-
) - $this->_formatPrice(
183-
$order->getBaseShippingAmount()
184-
)
185-
),
177+
'subtotal' => $this->_formatPrice($order->getBaseSubtotal()),
178+
'total' => $this->_formatPrice($order->getPayment()->getBaseAmountAuthorized()),
186179
'tax' => $this->_formatPrice($order->getBaseTaxAmount()),
187180
'shipping' => $this->_formatPrice($order->getBaseShippingAmount()),
181+
'discount' => $this->_formatPrice(abs($order->getBaseDiscountAmount()))
188182
];
189183
}
190184

app/code/Magento/Paypal/Test/Unit/Model/Hostedpro/RequestTest.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Paypal\Test\Unit\Model\Hostedpro;
77

8+
use Magento\Sales\Model\Order;
9+
use Magento\Sales\Model\Order\Payment;
10+
811
class RequestTest extends \PHPUnit_Framework_TestCase
912
{
1013
/**
@@ -181,48 +184,65 @@ public function testSetOrder()
181184
}
182185

183186
/**
184-
* @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount
187+
* @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount()
188+
* @param $subtotal
189+
* @param $total
190+
* @param $tax
191+
* @param $shipping
192+
* @param $discount
193+
* @dataProvider amountWithoutTaxDataProvider
185194
*/
186-
public function testSetAmountWithoutTax()
195+
public function testSetAmountWithoutTax($total, $subtotal, $tax, $shipping, $discount)
187196
{
188197
$expectation = [
189-
'subtotal' => 12.04,
190-
'tax' => 2.03,
191-
'shipping' => 5.05
198+
'subtotal' => $subtotal,
199+
'total' => $total,
200+
'tax' => $tax,
201+
'shipping' => $shipping,
202+
'discount' => abs($discount)
192203
];
193-
$amount = array_sum($expectation);
194204

195205
static::assertFalse($this->taxData->priceIncludesTax());
196206

197-
$payment = $this->getMockBuilder('Magento\Sales\Model\Order\Payment')
207+
$payment = $this->getMockBuilder(Payment::class)
198208
->disableOriginalConstructor()
199209
->getMock();
200210

201-
$order = $this->getMockBuilder('Magento\Sales\Model\Order')
211+
$order = $this->getMockBuilder(Order::class)
202212
->disableOriginalConstructor()
203213
->getMock();
204214

205215
$payment->expects(static::once())
206216
->method('getBaseAmountAuthorized')
207-
->willReturn($amount);
217+
->willReturn($total);
208218

209219
$order->expects(static::once())
210220
->method('getPayment')
211221
->willReturn($payment);
212222

213-
$order->expects(static::atLeastOnce())
223+
$order->expects(static::once())
224+
->method('getBaseDiscountAmount')
225+
->willReturn($discount);
226+
227+
$order->expects(static::once())
214228
->method('getBaseTaxAmount')
215-
->willReturn($expectation['tax']);
229+
->willReturn($tax);
216230

217-
$order->expects(static::atLeastOnce())
231+
$order->expects(static::once())
218232
->method('getBaseShippingAmount')
219-
->willReturn($expectation['shipping']);
233+
->willReturn($shipping);
220234

235+
$order->expects(static::once())
236+
->method('getBaseSubtotal')
237+
->willReturn($subtotal);
221238
$this->_model->setAmount($order);
222239

223240
static::assertEquals($expectation, $this->_model->getData());
224241
}
225242

243+
/**
244+
* @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount()
245+
*/
226246
public function testSetAmountWithIncludedTax()
227247
{
228248
/** @var \Magento\Tax\Model\Config $config */
@@ -273,4 +293,16 @@ public function testSetAmountWithIncludedTax()
273293

274294
static::assertEquals($expectation, $this->_model->getData());
275295
}
296+
297+
/**
298+
* Get data for amount with tax tests
299+
* @return array
300+
*/
301+
public function amountWithoutTaxDataProvider()
302+
{
303+
return [
304+
['total' => 31.00, 'subtotal' => 10.00, 'tax' => 1.00, 'shipping' => 20.00, 'discount' => 0.00],
305+
['total' => 5.00, 'subtotal' => 10.00, 'tax' => 0.00, 'shipping' => 20.00, 'discount' => -25.00],
306+
];
307+
}
276308
}

0 commit comments

Comments
 (0)