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