Forums / Basic Configuration / Updating Currencies

Updating Currencies

Views: 5,623

Results 1 to 11 of 11
2 May 2016, 9:03 AM
#1
christopher_clough avatar

christopher_clough

New Zenner

Join Date:
May 2013
Location:
Brisbane, Australia
Posts:
6
Plugin Contributions:
0

Updating Currencies

Is anyone having issues with updating currencies in V155? I have set up 6 currencies for use in different locations and changed the default currency to Australian dollars. Clicking on the "Update Currencies" button under the list of currencies results in a list of warnings like "Warning: The primary exchange rate server (ecb) failed for Canadian Dollar (CAD) - trying the secondary exchange rate server." - one for each currency except for the default currency. If I copy the URL from the localisation.php file and paste it in a browser, the required rates are returned.

This all worked fine in v154 but I can't see what I am doing wrong now.

This is a clean install of v155 but I have made changes to Australian date format and timezone and added ckeditor. Also have set up zones and taxes, but nothing else. No further plugins added yet.

I am running in XAMPP at the moment with PHP7.0.5 and Apache 2.4.18

Debug log:
[02-May-2016 16:48:54 Australia/Brisbane] Request URI: /store/crystalsadmin/currencies.php?page=1&cID=5&action=update_currencies, IP address: ::1
#1 quote_boc_currency() called at [C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php:37]
#2 zen_update_currencies() called at [C:\xampp\htdocs\store\crystalsadmin\currencies.php:94]

[02-May-2016 16:48:54 Australia/Brisbane] PHP Warning: Division by zero in C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php on line 128

[02-May-2016 16:48:54 Australia/Brisbane] Request URI: /store/crystalsadmin/currencies.php?page=1&cID=5&action=update_currencies, IP address: ::1
#1 quote_boc_currency() called at [C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php:37]
#2 zen_update_currencies() called at [C:\xampp\htdocs\store\crystalsadmin\currencies.php:94]

[02-May-2016 16:48:54 Australia/Brisbane] PHP Warning: Division by zero in C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php on line 128

[02-May-2016 16:48:54 Australia/Brisbane] Request URI: /store/crystalsadmin/currencies.php?page=1&cID=5&action=update_currencies, IP address: ::1
#1 quote_boc_currency() called at [C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php:37]
#2 zen_update_currencies() called at [C:\xampp\htdocs\store\crystalsadmin\currencies.php:94]

[02-May-2016 16:48:54 Australia/Brisbane] PHP Warning: Division by zero in C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php on line 128

[02-May-2016 16:48:54 Australia/Brisbane] Request URI: /store/crystalsadmin/currencies.php?page=1&cID=5&action=update_currencies, IP address: ::1
#1 quote_boc_currency() called at [C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php:37]
#2 zen_update_currencies() called at [C:\xampp\htdocs\store\crystalsadmin\currencies.php:94]

[02-May-2016 16:48:54 Australia/Brisbane] PHP Warning: Division by zero in C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php on line 128

[02-May-2016 16:48:54 Australia/Brisbane] Request URI: /store/crystalsadmin/currencies.php?page=1&cID=5&action=update_currencies, IP address: ::1
#1 quote_boc_currency() called at [C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php:37]
#2 zen_update_currencies() called at [C:\xampp\htdocs\store\crystalsadmin\currencies.php:94]

[02-May-2016 16:48:54 Australia/Brisbane] PHP Warning: Division by zero in C:\xampp\htdocs\store\crystalsadmin\includes\functions\localization.php on line 128

Any help would be appreciated.

Thanks
Chris

2 May 2016, 2:03 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Updating Currencies

Hmm, interesting. Running a vanilla ZC 1.5.5-2016-03-29 under XAMPP via PHP 5.4, the currency update occurs successfully. Changing to PHP 7.0.2, and only the USD currency (the default) is successfully processed via ECB (but that's a red herring).

When I look at my /logs folder, the logs report the same information as you'd posted, with the exception of:

[02-May-2016 13:10:13 UTC] PHP Warning:  file(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in C:\xampp\htdocs\zc155\cAndy-iVH-SloaN\includes\functions\localization.php on line 115

That log entry is the "tell". Apparently either PHP 7.x or that version of XAMPP has got allow_url_fopen set to Off. Having that set causes the currencies-updater to head down a CURL path to retrieve the information, but an invalid response is received from ecb; here's the var_export dump of the $XMLContent after the request:

[02-May-2016 13:20:39 UTC] array (
  0 => '<html lang="en"> <head> <title>European Central Bank</title> <style type="text/css"> body{ font-family: "Droid Sans", sans-serif; margin:0; padding:0; background-color: #9099ab; text-align: center; } h1{  font-size:38px; font-weight: normal; color:#003299; }  @media screen and (max-width: 480px) { h1{ font-size: 30px; } }  p { font-size: 15px; line-height: 20px; color: #333; margin-bottom: 20px; }  p.signature{ font-style: italic; color:#DF0101; }  .centredFrame { margin: 0 auto; text-align: center; width: 70%;   } .content{ margin-top:40px; padding:40px; background-color: #fff; border:1px solid #CDE3E0; -moz-border-radius: 20px; border-radius: 20px; } </style>',
  1 => '			   <link href="https://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css">',
  2 => '			   </head> <body> <div class="centredFrame"> <div class="content"> <img src="https://www.ecb.europa.eu/pic_failover/ecb_logo_EN.png"> <h1>A problem occurred while trying to access this page</h1> <body>We apologize for any inconvenience this might have caused.</body> <p class="signature">Reference number:  #18.12b81cb8.1462195235.4fbb3617</p> <p class="signature">HTTP status code: 403</p> </div> </div> </body> </html>',
)

So, I went back to PHP 5.4 and commented-out the file-request to retrieve the ecb information (so that the code would go down the CURL path there as well) and re-ran the currency-update with ... no issue.

That said, it looks like the ecb issue has to do with a curl-related change introduced somewhere :( in the PHP 5.5, 5.6 and 7.0 timeframe!

The boc processing is going to run into the same issue, with the exception that its handling is missing the highlighted check (which is why the divide-by-0 is being issued):

function quote_boc_currency($currencyCode = '', $base = DEFAULT_CURRENCY)
{
  if ($currencyCode == $base) return 1;
  static $CSVContent;
  $requested = $currencyCode;
  $url = 'http://www.bankofcanada.ca/stats/assets/csv/fx-seven-day.csv';
  $currencyArray = array();
  $currencyArray['CAD'] = 1;
  if (!isset($CSVContent) || $CSVContent == '') {
    $CSVContent = file($url);
    if (! is_object($CSVContent) && function_exists('curl_init')) {
      $CSVContent = doCurlCurrencyRequest('GET', $url);
      $CSVContent = explode("\n", $CSVContent);
    }
  }
  foreach ($CSVContent as $line) {
    if (substr($line, 0, 1) == '#' || substr($line, 0, 4) == 'Date' || trim($line) == '') continue;
    $data = explode(',', $line);
    $curName = $data[1];
    $curRate = $data[sizeof($data)-1];
    $currencyArray[trim($curName)] = (float)$curRate;
  }
[B]  if (!isset($currencyArray[DEFAULT_CURRENCY]) || 0 == $currencyArray[DEFAULT_CURRENCY]) return ''; // no valid value, so abort[/B]
  $rate = (string)($currencyArray[DEFAULT_CURRENCY]/(float)$currencyArray[$requested]);
  return $rate;
}

So, net-net there are two separate issues:

  1. The fact that the currency updates aren't happening under PHP 7.0. As a work-around, you could update your XAMPP installation's php.ini file to set allow_url_fopen=On while the ZC-dev CURL experts figure out what the root-cause is.
  2. The divide-by-0 coming from the boc currency handler; see suggested change above.
2 May 2016, 3:37 PM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Updating Currencies

Hmmm ... I think the problem is something in XAMP.

  1. I just tested it on a Linux Ubuntu server running PHP 7.0.5. No errors. I also hacked the code to force it to run via curl, and got the exact same success results:```
    The exchange rate for US Dollar (USD) was updated successfully to 1 via ecb.
    The exchange rate for Euro (EUR) was updated successfully to 0.91359958235448 via ecb.
    The exchange rate for GB Pound (GBP) was updated successfully to 0.71487340120073 via ecb.
    The exchange rate for Canadian Dollar (CAD) was updated successfully to 1.3144870790916 via ecb.
    The exchange rate for Australian Dollar (AUD) was updated successfully to 1.3726833724876 via ecb.

So I think the "issues with PHP7" are actually issues with XAMP's implementation of PHP7.


2. But testing the BOC results yields the need for a few changes.
The code-change lat9 offered for divide-by-zero protection is incorrect. It's doing a protection against the origin amount, not the divisor. 
Plus, BOC appears to have changed their currency code notations.
I recommend the following change in **/admin/includes/functions/localization.php**:```
function quote_boc_currency($currencyCode = '', $base = DEFAULT_CURRENCY)
{
  if ($currencyCode == $base) return 1;
  static $CSVContent;
  $requested = $currencyCode;
  $url = 'http://www.bankofcanada.ca/stats/assets/csv/fx-seven-day.csv';
  $currencyArray = array();
  $currencyArray['CAD'] = 1;
  if (!isset($CSVContent) || $CSVContent == '') {
    $CSVContent = file($url);
    if (! is_object($CSVContent) && function_exists('curl_init')) {
      $CSVContent = doCurlCurrencyRequest('GET', $url);
      $CSVContent = explode("\n", $CSVContent);
    }
  }
  foreach ($CSVContent as $line) {
    if (substr($line, 0, 1) == '#' || substr($line, 0, 4) == 'Date' || trim($line) == '') continue;
[B]    $data = explode(',', $line); // make an array, where each value is a separate column from the CSV
    $curName = substr(trim($data[1]), 0, 3); // take only first 3 chars of currency code (ie: removes "_NOON" suffix, or whatever future suffix BOC adds)
    $curRate = trim($data[sizeof($data)-1]);  // grab the value from the last column
[/B]    $currencyArray[trim($curName)] = (float)$curRate;
  }

[B]  if (!isset($currencyArray[$requested])) return false; // requested not found
  if ($currencyArray[$requested] == 0) return false; // can't divide by zero
[/B]
  $rate = (string)($currencyArray[DEFAULT_CURRENCY]/(float)$currencyArray[$requested]);
  return $rate;
}

Also available here: https://github.com/zencart/zencart/pull/911/files

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 May 2016, 5:40 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Updating Currencies

Thanks for the update, DrByte! That's what I get for copying and pasting without that required additional step (thinking)!

2 May 2016, 5:47 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Updating Currencies

Could the XAMPP issue be that SSL isn't set up?

2 May 2016, 7:32 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Updating Currencies

lat9:

Could the XAMPP issue be that SSL isn't set up?
Given that the URLs being used to retrieve currency data aren't https, I don't think it's an SSL issue.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

3 May 2016, 3:15 AM
#7
christopher_clough avatar

christopher_clough

New Zenner

Join Date:
May 2013
Location:
Brisbane, Australia
Posts:
6
Plugin Contributions:
0

Re: Updating Currencies

Thanks Lat9 and DrByte for your input on this. My next move this morning was to install a clean version on my web site host account to see if the issue was resolved but you have done that DrByte. I was doubting myself on this one because updating of currencies has been around forever.

So now I will make the code changes that you have suggested for boc and check the host site vanilla install. As long as I know that ecb works on the host, then I am OK with it defaulting to boc on XAMPP.

I really do appreciate your help folks.

3 May 2016, 3:26 AM
#8
christopher_clough avatar

christopher_clough

New Zenner

Join Date:
May 2013
Location:
Brisbane, Australia
Posts:
6
Plugin Contributions:
0

Re: Updating Currencies

Hello again.
Update on what has happened.
Lat9, allow_url_fopen was already set to On in php.ini so no effect there.

DrByte, code changes installed in localisation.php. Warning received from ecb for all updates, but all updates from boc were installed successfully on XAMPP.

Thanks
Chris

3 May 2016, 3:39 AM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Updating Currencies

You could use a different server, such as OpenExchangeRates, as long as you register for a free API key from them: https://www.zen-cart.com/downloads.php?do=file&id=2035

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

3 May 2016, 9:31 AM
#10
christopher_clough avatar

christopher_clough

New Zenner

Join Date:
May 2013
Location:
Brisbane, Australia
Posts:
6
Plugin Contributions:
0

Re: Updating Currencies

Hi DrByte and Lat9
Updating worked fine on my web host server with ecb.

Thanks for all your help.
Chris

7 Jul 2017, 4:51 PM
#11
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,296
Plugin Contributions:
39

Re: Updating Currencies

I am having this issue with a 1.5.4 cart on PHP 5.6. I tried this (https://www.zen-cart.com/downloads.php?do=file&id=2035) but there is no such setting under configuration >> my store.

Here is my debug from the currencies update. I also tried updating the files to 1.5.5 as suggested in another thread for the same result.

currencies.php:
---------------------------------------------------------------------------------------------
7922 9.20532512665 4.05311584473e-06 connect(5, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("23.1.120.11")}, 16) = -1 EINPROGRESS (Operation now in progress)
7923 9.20532894135 2.86102294922e-06 poll([{fd=5, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=5, revents=POLLOUT}])
7924 9.20533204079 4.05311584473e-06 getsockopt(5, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
7925 9.2053360939 4.05311584473e-06 fcntl(5, F_SETFL, O_RDWR) = 0
7926 9.2053399086 2.86102294922e-06 sendto(5, "GET /stats/eurofxref/eurofxref-daily.xml HTTP/1.0\r\n", 51, MSG_DONTWAIT, NULL, 0) = 51
7927 9.20534300804 4.05311584473e-06 sendto(5, "Host: www.ecb.europa.eu\r\n", 25, MSG_DONTWAIT, NULL, 0) = 25
7928 9.20534706116 4.05311584473e-06 sendto(5, "Connection: close\r\n", 19, MSG_DONTWAIT, NULL, 0) = 19
7929 9.25250005722 0.0471498966217 sendto(5, "\r\n", 2, MSG_DONTWAIT, NULL, 0) = 2
7930 9.25253009796 3.31401824951e-05 poll([{fd=5, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout)
7931 9.25254106522 1.00135803223e-05 poll([{fd=5, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=5, revents=POLLIN}])
7932 9.25255203247 1.19209289551e-05 recvfrom(5, "HTTP/1.0 200 OK\r\nServer: Apache\r\nLast-Modified: Fri, 23 Jun 2017 13:55:14 GMT\r\nETag: \"658-552a0f3517480\"\r\nAccept-Ranges: bytes\r\nContent-Length: 1624\r\nContent-Type: text/xml\r\nDate: Sat, 24 Jun 2017 00:55:27 GMT\r\nConnection: close\r\n\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">\n\t<gesmes:subject>Reference rates</gesmes:subject>\n\t<gesmes:Sender>\n\t\t<gesmes:name>European Central Bank</gesmes:name>\n\t</gesmes:Sender>\n\t<Cube>\n\t\t<Cube time='2017-06-23'>\n\t\t\t<Cube currency='USD' rate='1.1173'/>\n\t\t\t<Cube currency='JPY' rate='124.36'/>\n\t\t\t<Cube currency='BGN' rate='1.9558'/>\n\t\t\t<Cube currency='CZK' rate='26.297'/>\n\t\t\t<Cube currency='DKK' rate='7.4372'/>\n\t\t\t<Cube currency='GBP' rate='0.87805'/>\n\t\t\t<Cube currency='HUF' rate='309.13'/>\n\t\t\t<Cube currency='PLN' rate='4.2320'/>\n\t\t\t<Cube currency='RON' rate='4.5715'/>\n\t\t\t<Cube currency='SEK' rate='9.7700'/>\n\t\t\t<Cube currency='CHF' rate='1.0851'/>\n\t\t\t<Cube currency='NOK' rate='9.4613'/>\n\t\t\t<Cube currency='HRK' rate='7.4030'/>\n\t\t\t<Cube currency='RUB' rate='66.6677'/>\n\t\t\t<Cube currency='TRY' rate='3.9235'/>\n\t\t\t<Cube currency='AUD' rate='1.4764'/>\n\t\t\t<Cube currency='BRL' rate='3.7293'/>\n\t\t\t<Cube currency='CAD' rate='1.4783'/>\n\t\t\t<Cube currency='CNY' rate='7.6413'/>\n\t\t\t<Cube currency='HKD' rate='8.7147'/>\n\t\t\t<Cube currency='IDR' rate='14865.68'/>\n\t\t\t<Cube currency='ILS' rate='3.9572'/>\n\t\t\t<Cube currency='INR' rate='72.1025'/>\n\t\t\t<Cube currency='KRW' rate='1271.16'/>\n\t\t\t<Cube currency='MXN' rate='20.1620'/>\n\t\t\t<Cube currency='MYR' rate='4.7910'/>\n\t\t\t<Cube currency='NZD' rate='1.5345'/>\n\t\t\t<Cube currency='PHP' rate='56.117'/>\n\t\t\t<Cube currency='SGD' rate='1.5496'/>\n\t\t\t<Cube currency='THB' rate='37.927'/>\n\t\t\t<Cube currency='ZAR' rate='14.4531'/>\n\t\t</Cube>\n\t</Cube>\n</gesmes:Envelope>", 8192, MSG_DONTWAIT, NULL, NULL) = 1856
---------------------------------------------------------------------------------------------

We can also successfully directly curl the relevant URLs:
---------------------------------------------------------------------------------------------
root@server [/home/xxx/public_html/xxx]# curl -ks http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2017-06-26'>
<Cube currency='USD' rate='1.1187'/>
<Cube currency='JPY' rate='124.93'/>
<Cube currency='BGN' rate='1.9558'/>
<Cube currency='CZK' rate='26.243'/>
<Cube currency='DKK' rate='7.4364'/>
<Cube currency='GBP' rate='0.87830'/>
<Cube currency='HUF' rate='309.48'/>
<Cube currency='PLN' rate='4.2108'/>
<Cube currency='RON' rate='4.5749'/>
<Cube currency='SEK' rate='9.7618'/>
<Cube currency='CHF' rate='1.0881'/>
<Cube currency='NOK' rate='9.4788'/>
<Cube currency='HRK' rate='7.4126'/>
<Cube currency='RUB' rate='66.0390'/>
<Cube currency='TRY' rate='3.9244'/>
<Cube currency='AUD' rate='1.4771'/>
<Cube currency='BRL' rate='3.7259'/>
<Cube currency='CAD' rate='1.4812'/>
<Cube currency='CNY' rate='7.6541'/>
<Cube currency='HKD' rate='8.7254'/>
<Cube currency='IDR' rate='14882.47'/>
<Cube currency='ILS' rate='3.9501'/>
<Cube currency='INR' rate='72.0980'/>
<Cube currency='KRW' rate='1271.44'/>
<Cube currency='MXN' rate='20.0412'/>
<Cube currency='MYR' rate='4.7970'/>
<Cube currency='NZD' rate='1.5371'/>
<Cube currency='PHP' rate='56.151'/>
<Cube currency='SGD' rate='1.5533'/>
<Cube currency='THB' rate='38.008'/>
<Cube currency='ZAR' rate='14.4089'/>
</Cube>
</Cube>
</gesmes:Envelope>

PRO-Webs, Inc. since 2003 https://pro-webs.net/