Created
September 21, 2012 15:54
-
-
Save chico/3762293 to your computer and use it in GitHub Desktop.
currency and price based on IP address
This file contains 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
var DOLLAR = "DOLLAR"; | |
var POUND = "POUND"; | |
var EURO = "EURO"; | |
function getCurrency(callback) { | |
$.ajax({ | |
url: "http://api.hostip.info/get_json.php", | |
timeout:1000 // timeout after 1 second and default to dollars | |
}).done(function(json) { | |
var country = ""; | |
try { | |
country = json["country_code"]; | |
} catch(e) { | |
// whoops | |
} | |
if ("GB" == country) { // UK | |
doCallback(callback, POUND); | |
} else if ( | |
("AD" == country) // Andorra | |
|| ("AT" == country) // Austria | |
|| ("BE" == country) // Belgium | |
|| ("CY" == country) // Cyprus | |
|| ("EE" == country) // Estonia | |
|| ("FI" == country) // Finland | |
|| ("FR" == country) // France | |
|| ("DE" == country) // Germany | |
|| ("GR" == country) // Greece | |
|| ("IE" == country) // Ireland | |
|| ("IT" == country) // Italy | |
|| ("LU" == country) // Luxembourg | |
|| ("MT" == country) // Malta | |
|| ("MC" == country) // Monaco | |
|| ("ME" == country) // Montenegro | |
|| ("NL" == country) // Netherlands | |
|| ("PT" == country) // Portugal | |
|| ("SM" == country) // San Marino | |
|| ("SK" == country) // Slovakia | |
|| ("SI" == country) // Slovenia | |
|| ("ES" == country) // Spain | |
|| ("VA" == country) // Vatican City | |
) { | |
doCallback(callback, EURO); | |
} else { | |
doCallback(callback, DOLLAR); | |
} | |
}).fail(function() { | |
doCallback(callback, DOLLAR); | |
}); | |
} | |
function doCallback(callback, currency) { | |
if (typeof callback == 'function') { | |
callback.call(this, currency); | |
} | |
} | |
getCurrency(function(currency) { | |
var price = ""; | |
if (DOLLAR == currency) { | |
price = "$25"; | |
} else if (POUND == currency) { | |
price = "£15"; | |
} else if (EURO == currency) { | |
price = "€19"; | |
} | |
console.log("Price is " + price); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment