2019-05-29 05:21:06 -04:00
|
|
|
const request = require('request')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const URL = require('url')
|
2021-06-23 09:14:16 -04:00
|
|
|
const { promisify, promisifyMultiResult } = require('../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const currencyMappings = {
|
|
|
|
GB: 'GBP',
|
|
|
|
US: 'USD',
|
|
|
|
CH: 'CHF',
|
|
|
|
NZ: 'NZD',
|
|
|
|
AU: 'AUD',
|
|
|
|
DK: 'DKK',
|
|
|
|
NO: 'NOK',
|
|
|
|
CA: 'CAD',
|
2021-04-27 03:52:58 -04:00
|
|
|
SE: 'SEK',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Countries which would likely prefer Euro's
|
|
|
|
const EuroCountries = [
|
|
|
|
'AT',
|
|
|
|
'BE',
|
|
|
|
'BG',
|
|
|
|
'HR',
|
|
|
|
'CY',
|
|
|
|
'CZ',
|
|
|
|
'EE',
|
|
|
|
'FI',
|
|
|
|
'FR',
|
|
|
|
'DE',
|
|
|
|
'EL',
|
|
|
|
'HU',
|
|
|
|
'IE',
|
|
|
|
'IT',
|
|
|
|
'LV',
|
|
|
|
'LT',
|
|
|
|
'LU',
|
|
|
|
'MT',
|
|
|
|
'NL',
|
|
|
|
'PL',
|
|
|
|
'PT',
|
|
|
|
'RO',
|
|
|
|
'SK',
|
|
|
|
'SI',
|
2021-04-27 03:52:58 -04:00
|
|
|
'ES',
|
2019-05-29 05:21:06 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
_.each(EuroCountries, country => (currencyMappings[country] = 'EUR'))
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function getDetails(ip, callback) {
|
2021-06-01 09:52:26 -04:00
|
|
|
if (!ip) {
|
|
|
|
return callback(new Error('no ip passed'))
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
ip = ip.trim().split(' ')[0]
|
|
|
|
const opts = {
|
|
|
|
url: URL.resolve(settings.apis.geoIpLookup.url, ip),
|
|
|
|
timeout: 1000,
|
|
|
|
json: true,
|
|
|
|
}
|
|
|
|
logger.log({ ip, opts }, 'getting geo ip details')
|
|
|
|
request.get(opts, function (err, res, ipDetails) {
|
|
|
|
if (err) {
|
|
|
|
logger.warn({ err, ip }, 'error getting ip details')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
callback(err, ipDetails)
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function getCurrencyCode(ip, callback) {
|
|
|
|
getDetails(ip, function (err, ipDetails) {
|
|
|
|
if (err || !ipDetails) {
|
|
|
|
logger.err(
|
|
|
|
{ err, ip },
|
|
|
|
'problem getting currencyCode for ip, defaulting to USD'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
return callback(null, 'USD')
|
|
|
|
}
|
|
|
|
const countryCode =
|
2021-06-01 09:52:26 -04:00
|
|
|
ipDetails && ipDetails.country_code
|
|
|
|
? ipDetails.country_code.toUpperCase()
|
2021-05-26 08:26:59 -04:00
|
|
|
: undefined
|
|
|
|
const currencyCode = currencyMappings[countryCode] || 'USD'
|
|
|
|
logger.log({ ip, currencyCode, ipDetails }, 'got currencyCode for ip')
|
2021-06-01 09:52:26 -04:00
|
|
|
callback(err, currencyCode, countryCode)
|
2021-05-26 08:26:59 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
module.exports = {
|
|
|
|
getDetails,
|
|
|
|
getCurrencyCode,
|
|
|
|
promises: {
|
|
|
|
getDetails: promisify(getDetails),
|
2021-06-23 09:14:16 -04:00
|
|
|
getCurrencyCode: promisifyMultiResult(getCurrencyCode, [
|
|
|
|
'currencyCode',
|
|
|
|
'countryCode',
|
|
|
|
]),
|
2021-05-26 08:26:59 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|