Last active
September 20, 2016 01:39
-
-
Save joomdonation/09752d504afa4aaa2007718281e2ce7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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