10
10
* @package GoUrl PHP Bitcoin/Altcoin Payments and Crypto Captcha
11
11
* @copyright 2014-2018 Delta Consultants
12
12
* @category Libraries
13
- * @website https://gourl.io
13
+ * @website https://gourl.io
14
14
* @api https://gourl.io/bitcoin-payment-gateway-api.html
15
15
* @example https://gourl.io/lib/examples/example_customize_box.php <----
16
16
* @gitHub https://github.com/cryptoapi/Payment-Gateway
17
17
* @license Free GPLv2
18
- * @version 2.1.3
18
+ * @version 2.1.4
19
19
*
20
20
*
21
21
* CLASS CRYPTOBOX - LIST OF METHODS:
53
53
* E. function display_language_box(..) // Language selection dropdown list for cryptocoin payment box
54
54
* F. function display_currency_box(..) // Multiple crypto currency selection list. You can accept payments in multiple crypto currencies (for example: bitcoin, bitcoincash, litecoin, dogecoin)
55
55
* G. function get_country_name(..) // Get country name by country code or reverse
56
- * H. function convert_currency_live(..) // Fiat currency converter using XE.COM live exchange rates
56
+ * H. function convert_currency_live(..) // Fiat currency converter using live exchange rates websites
57
57
* I. function validate_gourlkey(..) // Validate gourl private/public/affiliate keys
58
58
* J. function run_sql(..) // Run SQL queries and return result in array/object formats
59
59
*
79
79
elseif (!defined ('ABSPATH ' )) exit ; // Wordpress
80
80
81
81
82
- define ("CRYPTOBOX_VERSION " , "2.1.3 " );
82
+ define ("CRYPTOBOX_VERSION " , "2.1.4 " );
83
83
84
84
// GoUrl supported crypto currencies
85
85
define ("CRYPTOBOX_COINS " , json_encode (array ('bitcoin ' , 'bitcoincash ' , 'litecoin ' , 'dash ' , 'dogecoin ' , 'speedcoin ' , 'reddcoin ' , 'potcoin ' , 'feathercoin ' , 'vertcoin ' , 'peercoin ' , 'monetaryunit ' , 'universalcurrency ' )));
@@ -1779,90 +1779,139 @@ function get_country_name($countryID, $reverse = false)
1779
1779
1780
1780
1781
1781
/* H. Function convert_currency_live()
1782
- *
1783
- * Currency Converter using XE.COM live exchange rates
1782
+ *
1783
+ * Currency Converter using live exchange rates websites
1784
1784
* Example - convert_currency_live("EUR", "USD", 22.37) - convert 22.37euro to usd
1785
- convert_currency_live("EUR", "BTC", 22.37) - convert 22.37euro to bitcoin
1785
+ convert_currency_live("EUR", "BTC", 22.37) - convert 22.37euro to bitcoin
1786
1786
*/
1787
1787
function convert_currency_live ($ from_Currency , $ to_Currency , $ amount )
1788
1788
{
1789
- static $ arr = array ();
1789
+ static $ arr = array ();
1790
1790
1791
- $ from_Currency = trim (strtoupper (urlencode ($ from_Currency )));
1792
- $ to_Currency = trim (strtoupper (urlencode ($ to_Currency )));
1793
-
1794
- if ($ from_Currency == "TRL " ) $ from_Currency = "TRY " ; // fix for Turkish Lyra
1795
- if ($ from_Currency == "ZWL " ) $ from_Currency = "ZWD " ; // fix for Zimbabwe Dollar
1796
- if ($ from_Currency == "RM " ) $ from_Currency = "MYR " ; // fix for Malaysian Ringgit
1797
- if ($ from_Currency == "BTC " ) $ from_Currency = "XBT " ; // fix for Bitcoin
1798
- if ($ to_Currency == "BTC " ) $ to_Currency = "XBT " ; // fix for Bitcoin
1799
-
1800
- if ($ from_Currency == "RIAL " ) $ from_Currency = "IRR " ; // fix for Iranian Rial
1801
- if ($ from_Currency == "IRT " ) { $ from_Currency = "IRR " ; $ amount = $ amount * 10 ; } // fix for Iranian Toman; 1IRT = 10IRR
1802
-
1803
- $ key = $ from_Currency ."_ " .$ to_Currency ;
1804
-
1805
-
1806
-
1807
- // from buffer
1808
- // ----------------
1809
- if (isset ($ arr [$ key ]))
1810
- {
1811
- if ($ arr [$ key ] > 0 )
1812
- {
1813
- $ val = $ arr [$ key ];
1814
- $ total = $ val *$ amount ;
1815
- if ($ to_Currency =="XBT " || $ total <0.01 ) $ total = sprintf ('%.5f ' , round ($ total , 5 ));
1816
- else $ total = round ($ total , 2 );
1817
- if ($ total == 0 ) $ total = sprintf ('%.5f ' , 0.00001 );
1818
- return $ total ;
1819
- }
1820
- else return -1 ;
1821
- }
1822
-
1823
-
1824
-
1825
- // get data from xe.com
1826
- // ----------------
1827
- $ val = 0 ;
1828
- $ url = "https://www.xe.com/currencyconverter/convert/?Amount=1&From= " .$ from_Currency ."&To= " .$ to_Currency ;
1829
-
1830
- $ rawdata = get_url_contents ( $ url );
1831
-
1832
- if (strpos ($ rawdata , "1 " .$ from_Currency ." = " ))
1833
- {
1834
- $ data = explode ("uccResultAmount'> " , $ rawdata );
1835
- if (isset ($ data [1 ]))
1836
- {
1837
- $ pos = stripos ($ data [1 ], "</span> " );
1838
- if ($ pos )
1839
- {
1840
- $ data = substr ($ data [1 ], 0 , $ pos );
1841
- if (is_numeric ($ data ) && $ data > 0 ) $ val = floatval ($ data );
1842
- }
1843
- }
1844
- }
1845
-
1846
-
1847
-
1848
- // result
1849
- // ------------
1850
- if ($ val > 0 )
1851
- {
1852
- $ arr [$ key ] = $ val ;
1853
- $ total = $ val *$ amount ;
1854
- if ($ to_Currency =="XBT " || $ total <0.01 ) $ total = sprintf ('%.5f ' , round ($ total , 5 ));
1855
- else $ total = round ($ total , 2 );
1856
- if ($ total == 0 ) $ total = sprintf ('%.5f ' , 0.00001 );
1857
- return $ total ;
1858
- }
1859
- else
1860
- {
1861
- $ arr [$ key ] = -1 ;
1862
- return -1 ;
1863
- }
1791
+ $ from_Currency = trim (strtoupper (urlencode ($ from_Currency )));
1792
+ $ to_Currency = trim (strtoupper (urlencode ($ to_Currency )));
1793
+
1794
+ if ($ from_Currency == "TRL " ) $ from_Currency = "TRY " ; // fix for Turkish Lyra
1795
+ if ($ from_Currency == "ZWD " ) $ from_Currency = "ZWL " ; // fix for Zimbabwe Dollar
1796
+ if ($ from_Currency == "RM " ) $ from_Currency = "MYR " ; // fix for Malaysian Ringgit
1797
+ if ($ from_Currency == "XBT " ) $ from_Currency = "BTC " ; // fix for Bitcoin
1798
+ if ($ to_Currency == "XBT " ) $ to_Currency = "BTC " ; // fix for Bitcoin
1799
+
1800
+ if ($ from_Currency == "RIAL " ) $ from_Currency = "IRR " ; // fix for Iranian Rial
1801
+ if ($ from_Currency == "IRT " ) { $ from_Currency = "IRR " ; $ amount = $ amount * 10 ; } // fix for Iranian Toman; 1IRT = 10IRR
1802
+
1803
+ $ key = $ from_Currency ."_ " .$ to_Currency ;
1804
+
1805
+
1806
+
1807
+ // a. restore saved exchange rate
1808
+ // ----------------
1809
+ if (!isset ($ arr [$ key ]) && session_status () === PHP_SESSION_ACTIVE && isset ($ _SESSION ["exch_ " .$ key ]) && is_numeric ($ _SESSION ["exch_ " .$ key ]) && $ _SESSION ["exch_ " .$ key ] > 0 ) $ arr [$ key ] = $ _SESSION ["exch_ " .$ key ];
1810
+
1811
+ if (isset ($ arr [$ key ]))
1812
+ {
1813
+ if ($ arr [$ key ] > 0 )
1814
+ {
1815
+ $ val = $ arr [$ key ];
1816
+ $ total = $ val *$ amount ;
1817
+ if ($ to_Currency =="BTC " || $ total <0.01 ) $ total = sprintf ('%.5f ' , round ($ total , 5 ));
1818
+ else $ total = round ($ total , 2 );
1819
+ if ($ total == 0 ) $ total = sprintf ('%.5f ' , 0.00001 );
1820
+ return $ total ;
1821
+ }
1822
+ else return -1 ;
1823
+ }
1824
+
1825
+
1826
+ $ val = 0 ;
1827
+ if ($ from_Currency == $ to_Currency ) $ val = 1 ;
1828
+
1829
+
1830
+
1831
+ // b. get BTC rates
1832
+ // ----------------
1833
+ $ bitcoinUSD = 0 ;
1834
+ if (!$ val && ($ from_Currency == "BTC " || $ to_Currency == "BTC " ))
1835
+ {
1836
+ $ aval = array ('BTC ' , 'USD ' , 'AUD ' , 'BRL ' , 'CAD ' , 'CHF ' , 'CLP ' , 'CNY ' , 'DKK ' , 'EUR ' , 'GBP ' , 'HKD ' , 'INR ' , 'ISK ' , 'JPY ' , 'KRW ' , 'NZD ' , 'PLN ' , 'RUB ' , 'SEK ' , 'SGD ' , 'THB ' , 'TWD ' );
1837
+ if (in_array ($ from_Currency , $ aval ) && in_array ($ to_Currency , $ aval ))
1838
+ {
1839
+ $ data = json_decode (get_url_contents ("https://blockchain.info/ticker " ), true );
1840
+
1841
+ // rates BTC->...
1842
+ $ rates = array ("BTC " => 1 );
1843
+ if ($ data ) foreach ($ data as $ k => $ v ) $ rates [$ k ] = ($ v ["15m " ] > 1000 ) ? round ($ v ["15m " ]) : ($ v ["last " ] > 1000 ? round ($ v ["last " ]) : 0 );
1844
+ // convert BTC/USD, EUR/BTC, etc.
1845
+ if (isset ($ rates [$ to_Currency ]) && $ rates [$ to_Currency ] > 0 && isset ($ rates [$ from_Currency ]) && $ rates [$ from_Currency ] > 0 ) $ val = $ rates [$ to_Currency ] / $ rates [$ from_Currency ];
1846
+ if (isset ($ rates ["USD " ]) && $ rates ["USD " ] > 0 ) $ bitcoinUSD = $ rates ["USD " ];
1847
+ }
1848
+
1849
+ if (!$ val && $ bitcoinUSD < 1000 )
1850
+ {
1851
+ $ data = json_decode (get_url_contents ("https://www.bitstamp.net/api/ticker/ " ), true );
1852
+ if (isset ($ data ["last " ]) && isset ($ data ["volume " ]) && $ data ["last " ] > 1000 ) $ bitcoinUSD = round ($ data ["last " ]);
1853
+ }
1854
+
1855
+ if ($ from_Currency == "BTC " && $ to_Currency == "USD " && $ bitcoinUSD > 0 ) $ val = $ bitcoinUSD ;
1856
+ if ($ from_Currency == "USD " && $ to_Currency == "BTC " && $ bitcoinUSD > 0 ) $ val = 1 / $ bitcoinUSD ;
1857
+ }
1858
+
1859
+
1860
+
1861
+ // c. get rates from European Central Bank https://www.ecb.europa.eu
1862
+ // ----------------
1863
+ $ aval = array ('EUR ' , 'USD ' , 'JPY ' , 'BGN ' , 'CZK ' , 'DKK ' , 'GBP ' , 'HUF ' , 'PLN ' , 'RON ' , 'SEK ' , 'CHF ' , 'ISK ' , 'NOK ' , 'HRK ' , 'RUB ' , 'TRY ' , 'AUD ' , 'BRL ' , 'CAD ' , 'CNY ' , 'HKD ' , 'IDR ' , 'ILS ' , 'INR ' , 'KRW ' , 'MXN ' , 'MYR ' , 'NZD ' , 'PHP ' , 'SGD ' , 'THB ' , 'ZAR ' );
1864
+ if ($ bitcoinUSD > 0 ) $ aval [] = "BTC " ;
1865
+ if (!$ val && in_array ($ from_Currency , $ aval ) && in_array ($ to_Currency , $ aval ))
1866
+ {
1867
+ $ xml = simplexml_load_string (get_url_contents ("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml " ));
1868
+ $ json = json_encode ($ xml );
1869
+ $ data = json_decode ($ json ,TRUE );
1870
+
1871
+ if (isset ($ data ["Cube " ]["Cube " ]))
1872
+ {
1873
+ $ data = $ data ["Cube " ]["Cube " ];
1874
+ $ time = $ data ["@attributes " ]["time " ];
1875
+
1876
+ // rates EUR->...
1877
+ $ rates = array ("EUR " => 1 );
1878
+ foreach ($ data ["Cube " ] as $ v ) $ rates [$ v ["@attributes " ]["currency " ]] = floatval ($ v ["@attributes " ]["rate " ]);
1879
+ if ($ bitcoinUSD > 0 && $ rates ["USD " ] > 0 ) $ rates ["BTC " ] = $ rates ["USD " ] / $ bitcoinUSD ;
1880
+
1881
+ // convert USD/JPY, EUR/GBP, etc.
1882
+ if ($ rates [$ to_Currency ] > 0 && $ rates [$ from_Currency ] > 0 ) $ val = $ rates [$ to_Currency ] / $ rates [$ from_Currency ];
1883
+ }
1884
+ }
1885
+
1886
+
1887
+ // d. get rates from https://free.currencyconverterapi.com/api/v6/convert?q=BTC_EUR&compact=y
1888
+ // ----------------
1889
+ if (!$ val )
1890
+ {
1891
+ $ data = json_decode (get_url_contents ("https://free.currencyconverterapi.com/api/v6/convert?q= " .$ key ."&compact=y " ), TRUE );
1892
+ if (isset ($ data [$ key ]["val " ]) && $ data [$ key ]["val " ] > 0 ) $ val = $ data [$ key ]["val " ];
1893
+ }
1894
+
1895
+
1896
+ // e. result
1897
+ // ------------
1898
+ if ($ val > 0 )
1899
+ {
1900
+ if (session_status () === PHP_SESSION_ACTIVE ) $ _SESSION ["exch_ " .$ key ] = $ val ;
1901
+
1902
+ $ arr [$ key ] = $ val ;
1903
+ $ total = $ val *$ amount ;
1904
+ if ($ to_Currency =="BTC " || $ total <0.01 ) $ total = sprintf ('%.5f ' , round ($ total , 5 ));
1905
+ else $ total = round ($ total , 2 );
1906
+ if ($ total == 0 ) $ total = sprintf ('%.5f ' , 0.00001 );
1907
+ return $ total ;
1908
+ }
1909
+ else
1910
+ {
1911
+ $ arr [$ key ] = -1 ;
1912
+ return -1 ;
1913
+ }
1864
1914
}
1865
-
1866
1915
1867
1916
1868
1917
@@ -2225,6 +2274,6 @@ function run_sql($sql)
2225
2274
foreach ($ cryptobox_private_keys as $ v )
2226
2275
if (strpos ($ v , " " ) !== false || strpos ($ v , "PRV " ) === false || strpos ($ v , "AA " ) === false || strpos ($ v , "77 " ) === false ) die ("Invalid Private Key - " . (CRYPTOBOX_WORDPRESS ? "please setup it on your plugin settings page " : "$ v in variable \$cryptobox_private_keys, file cryptobox.config.php. " ));
2227
2276
2228
- unset($ v ); unset($ cryptobox_private_keys );
2277
+ unset($ v ); unset($ cryptobox_private_keys );
2229
2278
}
2230
2279
?>
0 commit comments