Skip to content

Commit

Permalink
Merge pull request #741 from PrestaShopCorp/fix/difference-paypal-ord…
Browse files Browse the repository at this point in the history
…er-and-cart

Fix difference PayPal order and cart amounts
  • Loading branch information
seiwan authored Apr 1, 2021
2 parents c79b4eb + 8fd4f01 commit 01bdcc3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/ValidateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class ValidateOrder
*/
private $merchantId;

/**
* @var \Context
*/
private $context;

/**
* @param string $paypalOrderId
* @param string $merchantId
Expand All @@ -58,6 +63,7 @@ public function __construct($paypalOrderId, $merchantId)
{
$this->merchantId = $merchantId;
$this->paypalOrderId = $paypalOrderId;
$this->context = \Context::getContext();
}

/**
Expand Down Expand Up @@ -97,15 +103,14 @@ public function validateOrder($payload)

// Check if the PayPal order amount is the same than the cart amount
// We tolerate a difference of more or less 0.05
$context = \Context::getContext();
$paypalOrderAmount = number_format($order['purchase_units'][0]['amount']['value'], 2);
$cartAmount = number_format($context->cart->getOrderTotal(true, \Cart::BOTH), 2);
$cartAmount = number_format($this->context->cart->getOrderTotal(true, \Cart::BOTH), 2);

if ($paypalOrderAmount + 0.05 < $cartAmount || $paypalOrderAmount - 0.05 > $cartAmount) {
throw new PsCheckoutException('The transaction amount doesn\'t match with the cart amount.', PsCheckoutException::DIFFERENCE_BETWEEN_TRANSACTION_AND_CART);
}

$apiOrder = new Order(\Context::getContext()->link);
$apiOrder = new Order($this->context->link);

$fundingSource = false === $psCheckoutCart ? 'paypal' : $psCheckoutCart->paypal_funding;

Expand Down

0 comments on commit 01bdcc3

Please sign in to comment.