2014-10-12 19:45:45 -04:00
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
_ = require("underscore")
|
2014-10-13 09:10:15 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-10-12 19:45:45 -04:00
|
|
|
|
|
|
|
currencyMappings = {
|
|
|
|
"GB":"GBP"
|
|
|
|
"US":"USD"
|
2014-11-21 08:13:53 -05:00
|
|
|
"CH":"CHF"
|
|
|
|
"NZ":"NZD"
|
|
|
|
"AU":"AUD"
|
|
|
|
"DK":"DKK"
|
|
|
|
"NO":"NOK"
|
|
|
|
"CA":"CAD"
|
|
|
|
"SE":"SEK"
|
2014-10-12 19:45:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Countries which would likely prefer Euro's
|
2014-11-21 08:13:53 -05:00
|
|
|
EuroCountries = ["AT", "BE", "BG", "HR", "CY", "CZ",
|
|
|
|
"EE", "FI", "FR", "DE", "EL", "HU", "IE",
|
2014-10-12 19:45:45 -04:00
|
|
|
"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT",
|
2014-11-21 08:13:53 -05:00
|
|
|
"RO", "SK", "SI", "ES"]
|
2014-10-12 19:45:45 -04:00
|
|
|
|
|
|
|
_.each EuroCountries, (country)-> currencyMappings[country] = "EUR"
|
|
|
|
|
|
|
|
module.exports = GeoIpLookup =
|
|
|
|
|
|
|
|
getDetails : (ip, callback)->
|
2014-10-13 08:04:20 -04:00
|
|
|
if !ip?
|
|
|
|
e = new Error("no ip passed")
|
|
|
|
return callback(e)
|
|
|
|
ip = ip.trim().split(" ")[0]
|
2014-10-12 19:45:45 -04:00
|
|
|
opts =
|
|
|
|
url: "#{settings.apis.geoIpLookup.url}/#{ip}"
|
2014-11-25 08:10:00 -05:00
|
|
|
timeout: 1000
|
2014-10-13 09:10:15 -04:00
|
|
|
json:true
|
|
|
|
logger.log ip:ip, opts:opts, "getting geo ip details"
|
|
|
|
request.get opts, (err, res, ipDetails)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, ip:ip, "error getting ip details"
|
2014-10-12 19:45:45 -04:00
|
|
|
callback(err, ipDetails)
|
|
|
|
|
|
|
|
getCurrencyCode : (ip, callback)->
|
|
|
|
GeoIpLookup.getDetails ip, (err, ipDetails)->
|
2014-10-13 08:08:11 -04:00
|
|
|
if err? or !ipDetails?
|
2014-10-13 09:10:15 -04:00
|
|
|
logger.err err:err, ip:ip, "problem getting currencyCode for ip, defaulting to USD"
|
2014-10-13 08:08:11 -04:00
|
|
|
return callback(null, "USD")
|
2014-10-13 09:10:15 -04:00
|
|
|
countryCode = ipDetails?.country_code?.toUpperCase()
|
2014-10-14 07:14:03 -04:00
|
|
|
currencyCode = currencyMappings[countryCode] || "USD"
|
2014-10-13 09:10:15 -04:00
|
|
|
logger.log ip:ip, currencyCode:currencyCode, ipDetails:ipDetails, "got currencyCode for ip"
|
2015-01-07 08:16:19 -05:00
|
|
|
callback(err, currencyCode, countryCode)
|