Skip to content

Instantly share code, notes, and snippets.

@joomdonation
Last active September 20, 2016 01:39
Show Gist options
  • Save joomdonation/09752d504afa4aaa2007718281e2ce7b to your computer and use it in GitHub Desktop.
Save joomdonation/09752d504afa4aaa2007718281e2ce7b to your computer and use it in GitHub Desktop.
<?php
/**
* Convert payment amount to USD currency in case the currency is not supported by the payment gateway
*
* @param $amount
* @param $currency
*
* @return float
*/
public static function convertAmountToUSD($amount, $currency)
{
static $rate = null;
if ($rate === null)
{
$http = JHttpFactory::getHttp();
$url = 'http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=USD' . $currency . '=X';
$response = $http->get($url);
if ($response->code == 200)
{
$currencyData = explode(',', $response->body);
$rate = floatval($currencyData[1]);
}
}
if ($rate > 0)
{
$amount = $amount / $rate;
}
return round($amount, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment