Download & Extend

EC substracts taxes from base price that doesn't include them

Project:Commerce PayPal
Version:7.x-2.x-dev
Component:PayPal EC
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Is this correct?

<?php
      $shipping_price
= $line_item_wrapper->commerce_unit_price->value();
     
$shipping_tax = commerce_tax_total_amount($shipping_price['data']['components'], FALSE, $currency_code);

     
// Track the total costs of all shipping line items on an order, though
      // typically this should be limited to one shipping line item per order.
     
$shippingamt += $shipping_price['amount'] - $shipping_tax;
?>

The second parameter of commerce_tax_total_amount determines whether it returns a total of taxes that is (TRUE) or is not (FALSE) included in the base price. Thus, the value of $shipping_tax would be the amount of tax not included in the base price. So, if it's not included in the base price, why is it being subtracted from it?

Comments

#1

Hey thatoneguy,

I tried to understand your problem and why you were looking at this part of the code. Did you tried to add some taxes on your shipping method and the payment didn't work or was just a coding remark ?

#2

Ok, so I created a tax on every shipping services, added some products in my cart and complete the checkout process to the end, but the request to Paypal aborted due to an error between the total amount of each line items and the total of the order.

The current code doesn't allow us to do that. I had a look on Paypal's API but I didn't see anything about a Shipping tax field to fill in during the request. I Googled but found nothing.

The only way to add taxes on shipping services is to add the shipping tax amount to the shipping amount. That's not a proper solution because I would have preferred to put this amount to the total tax amount but Paypal compares the total tax amount to the tax amount of each line items. That solution could not work otherwise we have to divide the shipping tax amount to each line items.

I did a little patch to support taxes on shipping services. I added the tax amount to the total shipping amount and substracted the tax amount to the total of tax amount

The order review on Drupal's side
Capture d’écran 2013-01-22 à 15.13.44.png

The order review on Paypal's site
Capture d’écran 2013-01-22 à 15.13.32.png

Is that what you was asking thatoneguy ?

AttachmentSize
Capture d’écran 2013-01-22 à 15.13.44.png 54.89 KB
Capture d’écran 2013-01-22 à 15.13.32.png 53.91 KB
commerce_paypal-Support_taxes_on_shipping_services-1895174-2.patch 2.03 KB

#3

More of a coding remark/question. I haven't tested it myself; I was evaluating it in reference to Ryan's last comment (#50) on #1301570: Send itemized shopping cart to PayPal WPS. I don't understand why one would subtract taxes from a price that doesn't include them to begin with. Unless 2.x is doing something different with the price components than what 1.x did.

Are you sure that the PayPal error wasn't related to the fact that the shipping charges didn't add up? If you are subtracting the amount of a price component that isn't included in the line item's amount to begin with, that would stand to reason why they the total of the line items isn't match the order. Below, it seemed you tallied the order totals correctly in the original code.

I can't tell, since your screen shot doesn't show the shipping line item itself, but I imagine that, in your example, the tax on the shipping line item isn't included in the line item's amount as it would be if it were, say, VAT. In your patch, you subtract the shipping tax amount from the total taxes and add it to the total shipping amount -- either way, it's a wash and shouldn't have an effect on the order total.

What happens when you undo those last two changes to PAYMENTREQUEST_0_TAXAMT and PAYMENTREQUEST_0_SHIPPINGAMT, but leave the change to $shippingamt above? Does it still produce an error? I guess the point is moot for PPS, since it doesn't require an order total, but should I ever move to Payflow, I'd like to understand what Commerce is doing here.

#4

Here is a bigger screenshot, I don't have any shipping line item
Capture d’écran 2013-01-22 à 16.13.20.png

In your patch, you are taking the shipping tax amount away from the total taxes and adding it to the total shipping amount. Either way, it's a wash and makes no difference to the order total.

To the end, you are right there is no difference, the total is still the same, but not for Paypal. Because they are comparing the total tax amount with the tax amount of each line items.

What happens when you undo those last two changes to PAYMENTREQUEST_0_TAXAMT and PAYMENTREQUEST_0_SHIPPINGAMT, but leave the change to $shippingamt above?

The request fail
Capture d’écran 2013-01-22 à 16.21.21.png

AttachmentSize
Capture d’écran 2013-01-22 à 16.13.20.png 74.89 KB
Capture d’écran 2013-01-22 à 16.21.21.png 116.78 KB

#5

Okay, I think I've made sense of this now. I was confused about how the two changes you made related to each other but I'm good now after looking at the code again.

#6

Status:active» fixed

Ahh, you know, the issue here is that we simply don't need to track shipping tax at all. The only reason it worked for me in testing was my shipping tax was always 0. As the screenies above show, it didn't work once you put a sales tax on a shipping rate. However, it will work just fine to simply remove those lines tracking shipping entirely.

I also ran into a bit of a rounding issue, which I resolved by rounding the amount of tax when building the order level totals. That could come back to bite us later on, but I don't have a better strategy off the top of my head because commerce_tax_total_amount() doesn't appear to be rounding total tax amounts. Right now I think the worst we'd see would be a one penny difference in the amount of tax collected on site vs. shown in PayPal.

Let me know if you can reproduce any problems with this.

Commit: http://drupalcode.org/project/commerce_paypal.git/commitdiff/6dd35d8

#7

Your patch doesn't solve the problem if a merchant wants to add a shipping tax on a shipping price. Paypal will throw an error and payments will not succeed.

Here is what I have and what doesn't work :

A product at $2 + $1 VAT = $3
An order preparation fee = $0.5
A shipping amount $10 + shipping tax $2 = $12
_____________________
Total product = $2
Total taxes = $1 + $0.5 + $2 = $3.5
Total shipping = 10 $
_____________________
Total = $15.5

Here is the request that will be sent to Paypal.

Line item product (L_PAYMENTREQUEST_0_AMT0) = $3.00
Line item product tax (L_PAYMENTREQUEST_0_TAXAMT0) = $0.50
Line item product Qty (L_PAYMENTREQUEST_0_QTY0) = 1
_____________________
Total product (PAYMENTREQUEST_0_ITEMAMT )= $3
Total taxes (PAYMENTREQUEST_0_TAXAMT)= $0.5 (product tax) + $2 (shipping tax) = $2.5
Total shipping (PAYMENTREQUEST_0_SHIPPINGAMT) = 10 $
_____________________
Total (PAYMENTREQUEST_0_AMT) = $15.5

When Paypal will check the request, they will make the comparison between line items and total infos and that will not match.
L_PAYMENTREQUEST_0_TAXAMT0 * L_PAYMENTREQUEST_0_QTY0 != PAYMENTREQUEST_0_TAXAMT
$0.5 * 1 != $2.5

We lost the $2 of shipping tax, that why I included it in the total shipping cost.

(I hope to have been enough clear)

#8

Status:fixed» needs review

Hmm, I'll give it another look - I did do my test with shipping tax, but maybe I didn't test an order with both shipping tax and product tax.

#9

Status:needs review» fixed

Ahh, I do see it. Ok, what I think we should do is revert to using a line item in the case of shipping line items with tax amounts. That's going to be the cleanest way to represent the same thing to the user at PayPal that they would see on site - otherwise we end up with a different tax amount reported on PayPal than we show on site and a magically larger cost of shipping. It's a pity that we can't pass in a shipping amount - I'll bring it up on the call today.

For orders that don't have shipping tax, it will continue to pass a shipping amount.

Commit: http://drupalcode.org/project/commerce_paypal.git/commitdiff/9bac2e8

#10

Just did a full walkthrough with a couple PayPal engineers and found out I don't need to send tax on the line items anyways, so that will get us around any rounding errors at all. : )

Commit: http://drupalcode.org/project/commerce_paypal.git/commitdiff/d68919a

#11

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.