Paypal standard fix - allows showing of items in cart

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
bfranklin825 wrote:


This may be an issue that needs to be brought up to the nopCommerce team... tax should be calculated AFTER any discounts are taken off - at least that is the way that I've always understood it to be.


There are very complex rules about tax and discounts in the UK (and EU), and most carts get this wrong which can tend to cause a bit of bother with the tax man.

Picture this scenario... you have a basket of items, some of which have 17.5% tax, others zero tax, and perhaps one with 5%.

Now you have a £10 discount voucher. How is it applied?

This is (and I've taken expert accounting advice on this) how it should be done.

You total up the entire order, and the entire tax, and work out the tax as a percentage. Since the tax on the items is varying rates, we might end up finding out that the tax proportion of the total order is  eg13.45%.

Now you have to go back to your £10 voucher, and deduce the price of it, for the discount it gives BEFORE tax. So in this case, its is £8.81, with £1.19 tax.

I couldn't get nop to do it properly, can someone confirm that this is how its calculating tax on discounts? I've used a couple of other carts that do appear to get it right.
13 years ago
Sandman wrote:


This may be an issue that needs to be brought up to the nopCommerce team... tax should be calculated AFTER any discounts are taken off - at least that is the way that I've always understood it to be.

There are very complex rules about tax and discounts in the UK (and EU), and most carts get this wrong which can tend to cause a bit of bother with the tax man.

Picture this scenario... you have a basket of items, some of which have 17.5% tax, others zero tax, and perhaps one with 5%.

Now you have a £10 discount voucher. How is it applied?

This is (and I've taken expert accounting advice on this) how it should be done.

You total up the entire order, and the entire tax, and work out the tax as a percentage. Since the tax on the items is varying rates, we might end up finding out that the tax proportion of the total order is  eg13.45%.

Now you have to go back to your £10 voucher, and deduce the price of it, for the discount it gives BEFORE tax. So in this case, its is £8.81, with £1.19 tax.

I couldn't get nop to do it properly, can someone confirm that this is how its calculating tax on discounts? I've used a couple of other carts that do appear to get it right.


I don't quite get what you're saying. Are you saying that in the UK discounts are taken AFTER tax has been applied to an entire order? Here in Amercia taxes are calculated on the total of taxable items AFTER any discounts are taken. Because when you give a discount of 20% on a taxable item of $10, you are effectively setting the PRICE for that item at $8.00, so legally you can only charge tax on that $8.00.

Is what you're saying that customers get £8.81 off of the total and £1.19 off of tax with a £10 discount in the UK? Does that work out to actually being £10 off of the order?
13 years ago
Ok, here's my fix:

Ok... after going about it all wrong for two days, I finally figured out how to do this.

All you have to do it figure out what the discount percent is in relation to the order total (i.e. 20%, 30%) and multiply that by the tax total.

This fix requires minor changes to TaxManager.cs, ShoppingCartManager.cs, OrderTotals.ascx.cs, and OrderManager.cs.

The first thing you need to do is add the order subtotal to the GetTaxTotal method in TaxManager (around line 180).

public static decimal GetTaxTotal(ShoppingCart cart, int paymentMethodId, Customer customer, decimal OrderSubTotal, ref String error)
{
   ...
}

I use "OrderSubTotal", but you can use whatever you like.

This change will require fixing the broken method calls in other pages:

OrderManger.cs line 3095

paymentInfo.PaymentMethodId, customer, orderSubTotalExclTax, ref taxerror);

OrderTotals.ascx.cs line 143

decimal shoppingCartTaxBase = TaxManager.GetTaxTotal(cart, paymentMethodId, NopContext.Current.User, subtotalBaseWithoutPromo, ref TaxError);

ShoppingCartManager.cs line 342

decimal shoppingCartTax = TaxManager.GetTotalTax(cart, paymentMentodId, customer, subtotalBaseWithoutPromo, ref taxerror);

Next we go back to TaxManager and add this to the using/imports section:

using NopSolutions.NopCommerce.BusinessLogic.Promo.Discounts;

For the last step we go down to the end of GetTaxTotal (around line 276) and find the lines:

taxTotal = itemsTaxTotal + checkoutAttributesTax + shippingTax + paymentMethodAdditionalFeeTax;
taxTotal = Math.Round(taxTotal, 2);
return taxTotal;

and add a few lines in between them like this:

decimal taxSubtotal = itemsTaxTotal + checkoutAttributesTax;
Discount orderDiscount = null;
Decimal appliedDiscount = ShoppingCartManager.GetOrderDiscount(NopContext.Current.User, OrderSubTotal, out orderDiscount);
taxSubtotal = taxSubtotal - (taxSubtotal * (appliedDiscount / OrderSubTotal));
taxTotal = taxSubtotal + shippingTax + paymentMethodAdditionalFeeTax;
taxTotal = Math.Round(taxTotal, 2);
return taxTotal;

I'm using 1.70 by the way.

This fix presupposes that no matter what the total or tax or discount amount is, the discount CAN be broken down into a percent of the total. This discount percent is then subtracted from the tax total leaving you with the proper tax.

I know of one alternative way to get OrderSubTotal without having to add it to the GetTaxTotal method, but it requires totalling up the order myself inside GetTaxTotal and I didn't like that way. Does anyone know of a way to get the order subtotal without having to import it in using GetTaxTotal? I'd be happy to know that... otherwise this should work just fine.

I have it working correctly with a discount on product variants in a cart that has a discount code applied to it with two different taxes on two different items as well as and item that has no tax on it. And to the best of my knowledge, when I figure out what the tax should be with a calulator, it matches up.

Any problems with this, let me know.
13 years ago
Barry, thanks for the modification. I really appreciate it. I'll try it this weekend.
13 years ago
bfranklin825 wrote:



I don't quite get what you're saying. Are you saying that in the UK discounts are taken AFTER tax has been applied to an entire order? Here in Amercia taxes are calculated on the total of taxable items AFTER any discounts are taken. Because when you give a discount of 20% on a taxable item of $10, you are effectively setting the PRICE for that item at $8.00, so legally you can only charge tax on that $8.00.

Is what you're saying that customers get £8.81 off of the total and £1.19 off of tax with a £10 discount in the UK? Does that work out to actually being £10 off of the order?


The issue is that how you apply the coupon leads to different amounts of tax being paid (hence the taxman's interest in this).

Imagine we have these items in an order

item 1 - £100 - tax £17.50 (17.5%)
item 2 - £100 - tax £0.00 (0%)
item 3 - £100 - tax £5.00 (5%).

So the order total is £300 plus £22.50 tax. The tax man gets £22.50

Now imagine we have a discount voucher for £10.00. How do you apply it? As you say, you have to apply the discount BEFORE tax (for sound reasons, if you apply it after tax all sorts of problems with the tax man).

But if you apply before tax, how do you apply it? Imagine I apply the discount to the first item above. It is now £90.00, so the tax on it is £15.75, making tax for the whole order £20.75 - the tax man is now down £1.75.

But if you apply it to item 2 instead, there is no tax difference, since that item has no tax... so the taxman still gets £22.50.

The taxman of course won't let you do this! So the logic I outlined in my post above is how the taxman says tax on discounts should be handled to avoid arbitrary allocation of discounts to fiddle him out of what he demands.

If effect the discount voucher isn't £10 - its a variable amount, plus a variable amount of tax, that adds up to £10.00 after tax. My post above explained how to calculate this value.
13 years ago
bfranklin825 wrote:


Another fix I would like (which I'll probably have to do myself) is that when you hit "confirm" (the last button before being sent to Paypal) the order is automatically placed with nopCommerce and an email is sent to the admin and the customer about the order that isn't even finished yet.

What happens if the customer closes out the browser before completing the transaction with Paypal? Now you have an order that is not paid for but has been created and emails have been sent out... a mess! I would like to see the order creation in nopCommerce happen after the customer is sent back to the site on the return link or after the payment has been confirmed with Paypal - I'll have to look into the best way.

Also... if the customer changes his/her mind on something and hits the "cancel and return" link on Paypal then they are taken back to the main page of the site and their cart is empty! What if they wanted to change their order? Now they have to do the order ALL OVER AGAIN - lost sale! I'd like to see the "cancel and return" link on Paypal return the user to their cart which still contains their not-yet-processed order. I'll have to look into that as well...

B


Yep, just replied to this thread, basically saying the same
https://www.nopcommerce.com/boards/t/6365/order-processing-back-to-front.aspx


As said, I don't mind the first email when someone hits the confirm button. If I'm not mistaken there is an option to deactivate an mail being sent to the customer. But I'm absolutley missing an email being sent when the order gets marked as paid, whether manual processing or paypal, as this makes way more sense.

Cheers.
13 years ago
Barry,

I'm still getting this error:

Attempted to divide by zero.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DivideByZeroException: Attempted to divide by zero.

Source Error:


Line 277:            Discount orderDiscount = null; Decimal appliedDiscount = ShoppingCartManager.GetOrderDiscount(NopContext.Current.User, OrderSubTotal, out orderDiscount);
Line 278:            decimal taxSubtotal = itemsTaxTotal + checkoutAttributesTax;
Line 279:            taxSubtotal = taxSubtotal - (taxSubtotal * (appliedDiscount / OrderSubTotal));
Line 280:            taxTotal = taxSubtotal + shippingTax + paymentMethodAdditionalFeeTax;
Line 281:            taxTotal = Math.Round(taxTotal, 2);


Source File: C:~\Libraries\Nop.BusinessLogic\Tax\TaxManager.cs    Line: 279

Any solution? Thanks in advance.
13 years ago
ttun76 wrote:
Barry,

I'm still getting this error:

Attempted to divide by zero.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DivideByZeroException: Attempted to divide by zero.

Source Error:


Line 277:            Discount orderDiscount = null; Decimal appliedDiscount = ShoppingCartManager.GetOrderDiscount(NopContext.Current.User, OrderSubTotal, out orderDiscount);
Line 278:            decimal taxSubtotal = itemsTaxTotal + checkoutAttributesTax;
Line 279:            taxSubtotal = taxSubtotal - (taxSubtotal * (appliedDiscount / OrderSubTotal));
Line 280:            taxTotal = taxSubtotal + shippingTax + paymentMethodAdditionalFeeTax;
Line 281:            taxTotal = Math.Round(taxTotal, 2);


Source File: C:~\Libraries\Nop.BusinessLogic\Tax\TaxManager.cs    Line: 279

Any solution? Thanks in advance.


We had discussed this in another thread on the forums here... this is happening because the total tax is 0 - am I correct that there is no tax here? The way it can be fixed is this:

Replace Line 279 above with:

if (taxTotal > 0)
{
     taxTotal = taxTotal - (taxTotal * (appliedDiscount / OrderSubTotal));
}

I forgot to put it in my code, but it was discussed...
13 years ago
There is tax.

By the way, I'm using v1.7. I'm still getting the error even when I made suggested the changes:

Attempted to divide by zero.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DivideByZeroException: Attempted to divide by zero.

Source Error:


Line 279:            if (taxTotal > 0)
Line 280:            {
Line 281:                taxTotal = taxTotal - (taxTotal * (appliedDiscount / OrderSubTotal));
Line 282:            }
Line 283:            taxTotal = taxSubtotal + shippingTax + paymentMethodAdditionalFeeTax;


Source File: C:~\Libraries\Nop.BusinessLogic\Tax\TaxManager.cs    Line: 281
13 years ago
Any idea what your OrderSubTotal is? SOMETHING in this equation is zero....
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.