I have added discount from a custom module that I made.I am giving the observer's code below.Its working fine on the Cart page but tax is not getting added on Checkout Page. Getting paypal error: "PayPal gateway has rejected request. The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details)." during checkout.

class Hs_Customcartdiscount_Model_Observer {

const XML_PATH_MODULE_ENABLE    = 'customcartdiscount/customcartdiscount_group/enable';
const XML_PATH_DISCOUNT_AMOUNT  = 'customcartdiscount/customcartdiscount_group/discount';

public function setDiscount($observer) {
        $quote = $observer->getEvent()->getQuote();
        $quoteid = $quote->getId();
        $discountAmount =floor($quote->getBaseSubtotal()/100) * Mage::getStoreConfig(self::XML_PATH_DISCOUNT_AMOUNT);
        if ($quoteid) {
            if ($discountAmount > 0) {
                $total = $quote->getBaseSubtotal();



                $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');
                foreach ($quote->getAllAddresses() as $address) {




                    $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
                    $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());

                            (float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
                            (float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()

                    $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
                    $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());


                    $quote->setGrandTotal($quote->getBaseSubtotal() - $discountAmount)
                            ->setBaseGrandTotal($quote->getBaseSubtotal() - $discountAmount)
                            ->setSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)
                            ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)

                    if ($address->getAddressType() == $canAddItems) {
                        //echo $address->setDiscountAmount; exit;
                        $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount() - $discountAmount);
                        $address->setGrandTotal((float) $address->getGrandTotal() - $discountAmount);
                        $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount() - $discountAmount);
                        $address->setBaseGrandTotal((float) $address->getBaseGrandTotal() - $discountAmount);
                        if ($address->getDiscountDescription()) {
                            $address->setDiscountAmount(-($address->getDiscountAmount() - $discountAmount));
                            $address->setDiscountDescription($address->getDiscountDescription() . ', Custom Discount');
                            $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount() - $discountAmount));
                        } else {
                            $address->setDiscountDescription('Custom Discount');
                    }//end: if
                } //end: foreach
                //echo $quote->getGrandTotal();

                foreach ($quote->getAllItems() as $item) {
                    //We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                    $rat = $item->getPriceInclTax() / $total;
                    $ratdisc = $discountAmount * $rat;
                    $item->setDiscountAmount(($item->getDiscountAmount() + $ratdisc) * $item->getQty());
                    $item->setBaseDiscountAmount(($item->getBaseDiscountAmount() + $ratdisc) * $item->getQty())->save();


two screenshots of cart page and checkout page: cart page checkout page

Please someone help me.

  • This problem usually occurs due to the price display settings. I have faced same issue with the IWD One Page Checkout extension for Magento1.9. To fix this issue, go to the Config => Sales => Taxes => Price Display Settings and Shopping Cart Display Settings. Change the settings to show prices Including Tax.
    – MGento
    Dec 11, 2017 at 15:35
  • @MGento thank you for your reply .i have done all of the above but still doesn't work. whenever I enable my custom module it stops working .thats why i pasted my observer's code. even the shipping options on the checkout page does not have any effect on the grand total.
    – pratik
    Dec 12, 2017 at 5:25
  • Are you using HHVM ? Some extensions, do not function properly with HHVM
    – MGento
    Dec 12, 2017 at 5:45
  • No, I am using PHP interpreter.
    – pratik
    Dec 12, 2017 at 11:54