PHP programmer

Author
Discussion

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
If anyone on here is a whiz with PHP please drop me an email. I've got a cart system with a weird bug that needs fixing. Easy little job for someone that knows what they are going! (I guess anyway!!). TIA smile

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
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

marshalla

15,902 posts

201 months

Monday 8th February 2016
quotequote all
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.

Give us a clue - what's it doing that it shouldn't ? or not doing that it should ?

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
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??

marshalla

15,902 posts

201 months

Monday 8th February 2016
quotequote all
Not really. There's still not enough context.

What data are you passing in to generate that output ?


Edited by marshalla on Monday 8th February 16:53

marshalla

15,902 posts

201 months

Monday 8th February 2016
quotequote all
Dave_ST220 said:
Are you OK with me emailing you? I've got the files it uses and can explain how I think it's all working(or not when it goes over £1000!).
Sure - use the email address on my company website since PH's mail system is borked.

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
Are you OK with me emailing you? I've got the files it uses and can explain how I think it's all working(or not when it goes over £1000!).

plover

362 posts

211 months

Monday 8th February 2016
quotequote all
In this section your replacing the comma with space before doing a calculation
Dave_ST220 said:
$totalex = str_replace(",","",$subtotal) + str_replace(",","",$delivery);
But you never do the same with the total value
Dave_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.

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

budgie smuggler

5,379 posts

159 months

Monday 8th February 2016
quotequote all
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.

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. smile

Edited by budgie smuggler on Monday 8th February 17:05


Edited by budgie smuggler on Monday 8th February 17:06

rpguk

4,465 posts

284 months

Monday 8th February 2016
quotequote all

$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.

rxtx

6,016 posts

210 months

Monday 8th February 2016
quotequote all
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.

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
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 smile

rxtx

6,016 posts

210 months

Monday 8th February 2016
quotequote all
Oh I know it's not your code, but the person that did it is no programmer either smile

cwis

1,158 posts

179 months

Monday 8th February 2016
quotequote all
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:

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!

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
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!!

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
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:

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!
Haha, why does that not surprise me! This is making sense, the amounts are stored in a DB so I guess this code is trying to strip the commas??

rxtx

6,016 posts

210 months

Monday 8th February 2016
quotequote all
Databases don't store numbers with commas either, so if the person that did this has stored numbers in the database with commas in it, then what you have is a huge, giant clusterfk. If it were an animal, it would probably be put to sleep.

Jonesy23

4,650 posts

136 months

Monday 8th February 2016
quotequote all
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.

essayer

9,064 posts

194 months

Monday 8th February 2016
quotequote all
What does $discount contain? It isn't filtered at all.

Dave_ST220

Original Poster:

10,294 posts

205 months

Monday 8th February 2016
quotequote all
Seem to have fixed it, I stuck this in

$total = str_replace(",","",$total);

before

$centinel_total = $total * 100;

& the correct amount was passed to 3DS smile I now need to test with amounts less than £1000....