PHP programmer
Discussion
Dave_ST220 said:
Looking into this the problem seems to be with this snippet of code maybe??
$total = $row[4];
$centinel_total = $total * 100;
$centinel_delivery = $delivery * 100;
$centinel_vatamount = $vatamount * 100;
What does the * 100; mean after each field? Thanks
Multiply by 100.$total = $row[4];
$centinel_total = $total * 100;
$centinel_delivery = $delivery * 100;
$centinel_vatamount = $vatamount * 100;
What does the * 100; mean after each field? Thanks
Give us a clue - what's it doing that it shouldn't ? or not doing that it should ?
Basically on a sale less than £1000 it works fine. On a sale over £1000 it passes the wrong amounts, the 3Ds logs show this for the test I did on a £1000 :-
<Amount>100</Amount
><CurrencyCode>826</CurrencyCode>
<ShippingAmount>1049</ShippingAmount>
<TaxAmount>-3751</TaxAmount>
They strip off decimal places so I can see the shipping was correct at £10.49 but the total has become £1 & the tax amount has become -£37.51! The code is as follows :-
$subtotal = $row[0];
$delivery = $row[1];
$discount = $row[2];
$vatrate = $row[3];
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
$vatamount = ($totalex - $discount) * ($vatrate/100);
$vatamount = number_format($vatamount, 2, '.', ',');
$total = $row[4];
$centinel_total = $total * 100;
$centinel_delivery = $delivery * 100;
$centinel_vatamount = $vatamount * 100;
This then becomes this to send off to them for processing:-
$centinelClient->add('Amount', $centinel_total);
$centinelClient->add('CurrencyCode', '826');
$centinelClient->add('ShippingAmount', $centinel_delivery);
$centinelClient->add('TaxAmount', $centinel_vatamount);
Does that help at all??
<Amount>100</Amount
><CurrencyCode>826</CurrencyCode>
<ShippingAmount>1049</ShippingAmount>
<TaxAmount>-3751</TaxAmount>
They strip off decimal places so I can see the shipping was correct at £10.49 but the total has become £1 & the tax amount has become -£37.51! The code is as follows :-
$subtotal = $row[0];
$delivery = $row[1];
$discount = $row[2];
$vatrate = $row[3];
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
$vatamount = ($totalex - $discount) * ($vatrate/100);
$vatamount = number_format($vatamount, 2, '.', ',');
$total = $row[4];
$centinel_total = $total * 100;
$centinel_delivery = $delivery * 100;
$centinel_vatamount = $vatamount * 100;
This then becomes this to send off to them for processing:-
$centinelClient->add('Amount', $centinel_total);
$centinelClient->add('CurrencyCode', '826');
$centinelClient->add('ShippingAmount', $centinel_delivery);
$centinelClient->add('TaxAmount', $centinel_vatamount);
Does that help at all??
In this section your replacing the comma with space before doing a calculation
Try something like this ?
Dave_ST220 said:
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
But you never do the same with the total valueDave_ST220 said:
$total = $row[4];
$centinel_total = $total * 100;
$centinelClient->add('Amount', $centinel_total);
which probably explains why the total becomes one as you are doing "1,234 * 100" which I expect php is converting to 100.$centinel_total = $total * 100;
$centinelClient->add('Amount', $centinel_total);
Try something like this ?
Dave_ST220 said:
$total = str_replace(",","",$row[4]);
Disclaimer , I've never writen any php !Edited by plover on Monday 8th February 17:01
Strange that they're (I presume, not having seen the data) using a formatted number and stripping that out to do calculations on it. But that might be normal for that application.
Step through it with a debugger (e.g. PHP Storm) or add some debug writes to a temp file to see what is going on.
Step through it with a debugger (e.g. PHP Storm) or add some debug writes to a temp file to see what is going on.
plover said:
Disclaimer , I've never writen any php !
Not with a space, with nothing. At least how it is appearing on my screen here. The forum may have messed up the formatting though. Edited by budgie smuggler on Monday 8th February 17:05
Edited by budgie smuggler on Monday 8th February 17:06
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
I'm not really a php programmer, but that line seems to be adding subtotal and delivery to give a total which is then processed. Before it adds the two figures it's stripping out any commas.
That line and the fact that you're only having problems >£1000 makes me wonder if the value somewhere is being stored or passed as 1,000 and this line is meant to be a work around but it isn't covering everything and thus you're trying to calculate using a string which isn't a number.
That is dreadful code, treating numbers as strings to manipulate them is a recipe for disaster, as you're seeing. The only time any commas should come into it is at the presentation layer, the rest of the time they should be nothing but numbers.
Looking at that code, that could well be the least of your worries.
Looking at that code, that could well be the least of your worries.
The code was actually written by a PH'er who never finished the job! I'm no programmer, as you have gathered, in fact I know nothing about this at all so I'm just trying to bungle my way around it to fix this issue. Of course I could just switch off the 3DS but it would be nice to fix the issue. Thanks for the help so far everyone
rxtx said:
That is dreadful code, treating numbers as strings to manipulate them is a recipe for disaster, as you're seeing. The only time any commas should come into it is at the presentation layer, the rest of the time they should be nothing but numbers.
Looking at that code, that could well be the least of your worries.
Strongly agree! PHP does something called type juggling:Looking at that code, that could well be the least of your worries.
http://php.net/manual/en/language.types.type-juggl...
Which will attempt to convert variables between types (a string to a number, for example) but it looks like the root of the issue is numbers being stored as strings (with commas!) in a database somewhere. PHP can't help withj that so someone's bodged.
Scary!
Well I guess that's my luck summed up!! If anyone can help fix it I can email over the file I ***think*** is the cause. I've got something to try now and will give it a go. The amounts that are passed for 3D secure processing require the decimal points removing btw hence the code I guess? All I can add is the VAT amount on the actual webpage was not displaying correctly, I found this line :-
$totalex = $subtotal + $delivery;
& replaced it with this :-
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
& that worked. Whether hat helps diagnose this issue or has anything to do with it I haven't got a clue!!
$totalex = $subtotal + $delivery;
& replaced it with this :-
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
& that worked. Whether hat helps diagnose this issue or has anything to do with it I haven't got a clue!!
cwis said:
rxtx said:
That is dreadful code, treating numbers as strings to manipulate them is a recipe for disaster, as you're seeing. The only time any commas should come into it is at the presentation layer, the rest of the time they should be nothing but numbers.
Looking at that code, that could well be the least of your worries.
Strongly agree! PHP does something called type juggling:Looking at that code, that could well be the least of your worries.
http://php.net/manual/en/language.types.type-juggl...
Which will attempt to convert variables between types (a string to a number, for example) but it looks like the root of the issue is numbers being stored as strings (with commas!) in a database somewhere. PHP can't help withj that so someone's bodged.
Scary!
It's probably mixing number presentation systems somewhere which breaks the conversion.
i.e. a German system would have the numbers as 1.234,99 but a UK type one would use 1,234.99 - basically the commas and decimals are reversed so the formatting conversion breaks as it gets to the first 1000.
Can't suggest a code fix but the language/country/formatting setting is likely the issue that screws the converter. Ideally bin the conversion and just have the numbers stored & processed as numbers.
i.e. a German system would have the numbers as 1.234,99 but a UK type one would use 1,234.99 - basically the commas and decimals are reversed so the formatting conversion breaks as it gets to the first 1000.
Can't suggest a code fix but the language/country/formatting setting is likely the issue that screws the converter. Ideally bin the conversion and just have the numbers stored & processed as numbers.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff