News:

Looking for documentation? Take a look on our wiki

Main Menu

[solved]Bug in Standard and Paypal payment methods

Started by Marttyn, August 08, 2013, 22:07:15 PM

Previous topic - Next topic

Marttyn

Hello!
Im using Joomla 2.5.8 and VM2.0.22a
Im setting up the payment methods, and ive realized that if i set a fee for some payment method, the fee is not calculated correctly.
The problem is on both plugins Standard, and Paypal.

I will start explaining with Standard as its easier:
I have a "Cash on delivery" payment method, using Standard plugin. The shipping company charges me 3% of the amount plus TAX.
Im using "percentage of total amount" = 3, and general TAX rules, here in Spain, TAX is 21%.
That means that 3% of the TOTAL AMOUNT should be calculated, but it calculates 3% of products + shipping.
If i have an order of $100 (and free shipping to make it easy), then the calculated fee for this payment method is: $3.63. This is because 3% of $100 is $3, plus 21% TAX, makes $3.63.
But in fact, the fee should be $3.77, plus $100 product makes $103.77 order total.
3% of $103.77 is $3.11. $3.11 plus 21% tax makes $3.77 <-- this is how shipping companies charges for "cash on delivery service".
So, if i charge $103.77, and the shipping company keeps $3.77, i have my $100 of the order...
Thats why i think the plug in is not working properly.


With Paypal occurs the same, but more complex:
Paypal charges me $0.35 fixed fee, plus 3.4% of total amount.
So I configure the plugin with 0.35 transaction fee, and 3.4 for percentage of total amount. With 21% tax
Same example: $100 product, free shipping.
Paypal fee charged by VM is $4.54 ($100 * 3.4% = $3.4 | $3.4 + $0.35 = $3.75 | $3.75 * 21% = $4.54)
The correct fee should be $4.73, plus $100 product makes $104.73 order total.
3.4% of $104.73 is $3.56. $3.56 plus 0.35 fixed fee is $3.91. And $3.91 plus 21% tax makes $4.73 <-- this is how paypal charges the fees. (although 21% TAX is not charged by paypal, im obliged to do it by estate treasury)
Thats why i think the plug in is not working properly.

Maybe TAX is making it more difficult to explain and the calculations are a little bit more difficult, but the problem is still the same: the fee is calculated over the product + shipping total, and not over the amount to pay.
Here you have a Paypal calculator to check http://www.rememberingrolbe.com/paypal.htm
Check the image attached to quickly see how the plug in is calculating wrong.


If you consider this a bug as i do, ive made all calculations and formulas to get the correct values. If you need this just ask.

Regards!

Maxim Pishnyak

Are you sure that it's not misconfiguration? There're plenty threads on forum with calculation 'bugs'. Find some of them and study.
You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart

Marttyn

#2
Quote from: Maxim Pishnyak on August 09, 2013, 13:50:33 PM
Are you sure that it's not misconfiguration? There're plenty threads on forum with calculation 'bugs'. Find some of them and study.

Thank you Maxim for your attention.
Yesterday i spend 3 hours trying to understand why I get this values, and how are the calculations made.
Im not the best mathematician, but Im not the worst also. Im sure that the calculation is wrong, unless VM want this behaviour like it is now. But in this case you should notice people that the calculated fee will not match the real paypal fee.
And of course, with Standard, and other payment methods should happen the same.

You can check very easily adding a new Paypal payment method, and configuring only percentage of total amount to 5% like this:


Then you go to your cart and add an item (i added 100€ to make it more simple) and choose the previously created payment method, mine is called "Prueba", like this:


And as you can see, VM will calculate the fee over the total of products plus shipping (no shipping used in this example). But Paypal will charge you over the total of the whole order. You can clearly see that (supposing that paypal charges a 5% fee, and without fixed fees) 5% of the total (105€) is 5.25€.
So VM charges 5€ for Paypal fee, but in fact Paypal will charge you 5.25€ for this transaction...

Regards!

Marttyn

#3
Would be great if someone pay attention to this. Its a verified bug, and its charging wrong fees to people around the world!

alatak


Marttyn

Thank you Alatak!
I was thinking that nobody was "interested" in this bug...
Can you please at least confirm if it is a calculation error on the plugin? This way people interested on this can take action, deactivate or charge different percentage to be closer to the real fee charged.
Please remember this is not only a Paypal plugin problem, also with Standard plugin happens the same (and probably with other payment methods, but i didnt check).
Regards!

alatak

hello
yes it is bug.
thank you for the detailed description.
It helped a lot.
I have done the fix. It will be available in the next version.

alatak

hello
you can test the fix by adding this function in both file
- plugins/vmpayment/paypal/paypal.php
- plugins/vmpayment/standard/standard.php
function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method) {


if (!class_exists ('calculationHelper')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
}

if (!class_exists ('calculationHelper')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
}

$calculator = calculationHelper::getInstance ();
$_psType = ucfirst ($this->_psType);

if(isset($method->tax_id) and (int)$method->tax_id === -1){

} else if (!empty($method->tax_id)) {
$cart_prices[$this->_psType . '_calc_id'] = $method->tax_id;

$db = JFactory::getDBO ();
$q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $method->tax_id . '" ';
$db->setQuery ($q);
$taxrules = $db->loadAssocList ();
} else {
//This construction makes trouble, if there are products with different vats in the cart
//on the other side, it is very unlikely to have different vats in the cart and simultaneous it is not possible to use a fixed tax rule for the shipment
if(!empty($calculator->_cartData['VatTax']) and count ($calculator->_cartData['VatTax']) == 1){
$taxrules = $calculator->_cartData['VatTax'];
foreach($taxrules as &$rule){
$rule['subTotal'] = $cart_prices[$this->_psType . 'Value'];
}
} else {
$taxrules = $calculator->_cartData['taxRulesBill'];
foreach($taxrules as &$rule){
unset($rule['subTotal']);
}
}
}


$cartTotalAmount=$cart_prices['salesPrice'] + $cart_prices['salesPriceShipment'] - $cart_prices['salesPriceCoupon'] ;
if (isset($method->cost_percent_total)) {
if (preg_match ('/%$/', $method->cost_percent_total)) {
$cost_percent_total = (substr ($method->cost_percent_total, 0, -1))/100;
} else {
$cost_percent_total = $method->cost_percent_total;
}
} else {
$cost_percent_total=0;
}
if (isset($method->cost_per_transaction)) {
$cost_per_transaction=$method->cost_per_transaction;
} else {
$cost_per_transaction=0;
}
if (count ($taxrules) > 0) {
$cost_percent_total_vat =  $calculator->executeCalculation($taxrules, $cost_percent_total, true);
$cost_per_transaction_vat =  $calculator->executeCalculation($taxrules, $cost_per_transaction, true);
$NewTotalAmount= ($cartTotalAmount+$cost_per_transaction_vat) / ( 1 -  $cost_percent_total_vat );
$feeWithVat=$NewTotalAmount-$cartTotalAmount;

$calculator->setRevert(true);
$feeNoVat = $calculator->roundInternal($calculator->executeCalculation($taxrules,$feeWithVat, true), 'salesPrice');
$calculator->setRevert(false);

$cart_prices[$this->_psType . 'Tax']=$feeWithVat-$feeNoVat;
$cart_prices['salesPrice' . $_psType] =$feeWithVat;
$cart_prices[ $_psType.'Value'] = $feeNoVat;

reset($taxrules);
$taxrule =  current($taxrules);
$cart_prices[$this->_psType . '_calc_id'] = $taxrule['virtuemart_calc_id'];

} else {
$NewTotalAmount=($cartTotalAmount+ $method->cost_per_transaction) / (1 -$cost_percent_total);
$fee=$NewTotalAmount-$cartTotalAmount;
$cart_prices['salesPrice' . $_psType] = $fee;
$cart_prices[$this->_psType . 'Tax'] = 0;
$cart_prices[$this->_psType . '_calc_id'] = 0;
}

return $cart_prices['salesPrice' . $_psType];
}

Marttyn

Hello Alatak!
Thank you for your attention  :)
Ive place the code where you told me, but it seems that the price is still the same. For testing, i use 5% fee, and for 100€, it shows a fee of 5€.
The correct fee should be 105.26€, as the 5% of this its 5.26€.
Im a C programmer, but dont fully understand PHP. Im trying to understand where do you call the new function added. Because its a new function, shouldnt you be make a call from other place? But as you didnt modify any other part, it looks like you are not calling it...
Well, i dont know... you are the expert  :)

Please reffer to this calculator to compare the real fees with the ones given by VM: http://www.rememberingrolbe.com/paypal.htm

Regards!

alatak

#9
Hello
Yes you are rigth.
There was a error when the Fee per transaction was a value.
If 5 or 5% is the same.

The correct code is this one
function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method) {


if (!class_exists ('calculationHelper')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
}

if (!class_exists ('calculationHelper')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
}

$calculator = calculationHelper::getInstance ();
$_psType = ucfirst ($this->_psType);

if(isset($method->tax_id) and (int)$method->tax_id === -1){

} else if (!empty($method->tax_id)) {
$cart_prices[$this->_psType . '_calc_id'] = $method->tax_id;

$db = JFactory::getDBO ();
$q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $method->tax_id . '" ';
$db->setQuery ($q);
$taxrules = $db->loadAssocList ();
} else {
//This construction makes trouble, if there are products with different vats in the cart
//on the other side, it is very unlikely to have different vats in the cart and simultaneous it is not possible to use a fixed tax rule for the shipment
if(!empty($calculator->_cartData['VatTax']) and count ($calculator->_cartData['VatTax']) == 1){
$taxrules = $calculator->_cartData['VatTax'];
foreach($taxrules as &$rule){
$rule['subTotal'] = $cart_prices[$this->_psType . 'Value'];
}
} else {
$taxrules = $calculator->_cartData['taxRulesBill'];
foreach($taxrules as &$rule){
unset($rule['subTotal']);
}
}
}


$cartTotalAmount=$cart_prices['salesPrice'] + $cart_prices['salesPriceShipment'] - $cart_prices['salesPriceCoupon'] ;
if (isset($method->cost_percent_total)) {
if (preg_match ('/%$/', $method->cost_percent_total)) {
$cost_percent_total = (substr ($method->cost_percent_total, 0, -1)) * 0.01;
} else {
$cost_percent_total = $method->cost_percent_total * 0.01;
}
} else {
$cost_percent_total=0;
}
if (isset($method->cost_per_transaction)) {
$cost_per_transaction=$method->cost_per_transaction;
} else {
$cost_per_transaction=0;
}
if (count ($taxrules) > 0) {
$cost_percent_total_vat =  $calculator->executeCalculation($taxrules, $cost_percent_total, true);
$cost_per_transaction_vat =  $calculator->executeCalculation($taxrules, $cost_per_transaction, true);
$NewTotalAmount= ($cartTotalAmount+$cost_per_transaction_vat) / ( 1 -  $cost_percent_total_vat );
$feeWithVat=$NewTotalAmount-$cartTotalAmount;

$calculator->setRevert(true);
$feeNoVat = $calculator->roundInternal($calculator->executeCalculation($taxrules,$feeWithVat, true), 'salesPrice');
$calculator->setRevert(false);

$cart_prices[$this->_psType . 'Tax']=$feeWithVat-$feeNoVat;
$cart_prices['salesPrice' . $_psType] =$feeWithVat;
$cart_prices[ $_psType.'Value'] = $feeNoVat;

reset($taxrules);
$taxrule =  current($taxrules);
$cart_prices[$this->_psType . '_calc_id'] = $taxrule['virtuemart_calc_id'];

} else {
$NewTotalAmount=($cartTotalAmount+ $method->cost_per_transaction) / (1 -$cost_percent_total);
$fee=$NewTotalAmount-$cartTotalAmount;
$cart_prices['salesPrice' . $_psType] = $fee;
$cart_prices[$this->_psType . 'Tax'] = 0;
$cart_prices[$this->_psType . '_calc_id'] = 0;
}

return $cart_prices['salesPrice' . $_psType];
}

Marttyn

Thank you Valerie for your attention, and for sharing!
I will make changes, and as you told, in the next release this will be added, so my changes will be overwritten with the new corrected one, right? or should i be alert to undo the changes in next release?

alatak

Hello
The fix will be included in the next release.

Marttyn

Hello Valerie!
This bug was corrected in the new release 2.0.22c?
Regards!

alatak


sardonic32

By doing a discount on the total payment by the method chosen, the system does not correctly calculate the amount.

1. a product of € 100 without taxes and with a 2% discount on payment ---> the discount percentage is wrong.
2. I installed the plug istraxx quantity for quantity discounts and neither ignored.

Where is the error? I installed an old version of VM (2.0. 8) and it works perfectly.

How I can solve the problem?

Attached some screenshots to see the problem

thanks

[attachment cleanup by admin]