[Fix] Prestashop error when trying to pay with Paypal: order error

[Fix] Prestashop error when trying to pay with Paypal: order error

The error appears because the Prestashop store and Paypal have different ways of rounding the final sum that is to be paid.

How to fix it?

You need some basic php skills, basically, just replace some code.

Open from prestashop using FTP: modules/paypal/express_checkout/
The file we need to edit is named “process.php”.

Open process.php within Dreamweaver, notepad or any web editor you find and do these changes.

Find:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private function setProductsList(&$fields, &$index, &$total)
    {
        foreach ($this->product_list as $product) {
            $fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = (int) $product['id_product'];
 
            $fields['L_PAYMENTREQUEST_0_NAME'.$index] = $product['name'];
 
            if (isset($product['attributes']) && (empty($product['attributes']) === false)) {
                $fields['L_PAYMENTREQUEST_0_NAME'.$index] .= ' - '.$product['attributes'];
            }
 
            $fields['L_PAYMENTREQUEST_0_DESC'.$index] = Tools::substr(strip_tags($product['description_short']), 0, 50).'...';
 
            $fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($product['price_wt'], $this->decimals);
            $fields['L_PAYMENTREQUEST_0_QTY'.$index] = $product['quantity'];
 
            $total = $total + ($fields['L_PAYMENTREQUEST_0_AMT'.$index] * $product['quantity']);
        }
    }

Replace with:

2
3
4
5
6
7
8
9
10
11
12
13
private function setProductsList(&$fields, &$index, &$total)
{
foreach ($this->product_list as $product) {
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = 1;
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = 'Total Productos '; // o el texto que deseemos
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = $this->context->cart->getOrderTotal(true,Cart::ONLY_PRODUCTS);
//mandamos el total de los productos con IVA incluido
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$total = $total + ($fields['L_PAYMENTREQUEST_0_AMT'.$index] * 1);
break;
}
}

Find:

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public function getTotalPaid()
    {
        $total = 0.00;
 
        foreach ($this->product_list as $product) {
            $price = Tools::ps_round($product['price_wt'], $this->decimals);
            $quantity = Tools::ps_round($product['quantity'], $this->decimals);
            $total = Tools::ps_round($total + ($price * $quantity), $this->decimals);
        }
 
        if ($this->context->cart->gift == 1) {
            $total = Tools::ps_round($total + $this->getGiftWrappingPrice(), $this->decimals);
        }
 
        if (version_compare(_PS_VERSION_, '1.5', '<')) {
            $discounts = $this->context->cart->getDiscounts();
            $shipping_cost = $this->context->cart->getOrderShippingCost();
        } else {
            $discounts = $this->context->cart->getCartRules();
            $shipping_cost = $this->context->cart->getTotalShippingCost();
        }
 
        if (count($discounts) > 0) {
            foreach ($discounts as $product) {
                $price = -1 * Tools::ps_round($product['value_real'], $this->decimals);
                $total = Tools::ps_round($total + $price, $this->decimals);
            }
        }
 
        return Tools::ps_round($shipping_cost, $this->decimals) + $total;
    }

Replace with:

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public function getTotalPaid()
{
$total = 0.00;
$total = $total + $this->context->cart->getOrderTotal(true,Cart::ONLY_PRODUCTS);
if ($this->context->cart->gift == 1) {
$total = Tools::ps_round($total + $this->getGiftWrappingPrice(), $this->decimals);
}
if (version_compare(_PS_VERSION_, '1.5', '<')) { $discounts = $this->context->cart->getDiscounts();
$shipping_cost = $this->context->cart->getOrderShippingCost();
} else {
$discounts = $this->context->cart->getCartRules();
$shipping_cost = $this->context->cart->getTotalShippingCost();
}
if (count($discounts) > 0) {
foreach ($discounts as $product) {
$price = -1 * Tools::ps_round($product['value_real'], $this->decimals);
$total = Tools::ps_round($total + $price, $this->decimals);
}
}
return Tools::ps_round($shipping_cost, $this->decimals) + $total;
}

That is all. Save and upload the file. Give it a test.
Notice that if you update the paypal module within Prestashop, you will have to do these changes once again.

Add a comment: