<commit_before>SecurityManager     = require '../../managers/SecurityManager'
SubscriptionHandler  = require './SubscriptionHandler'
PlansLocator = require("./PlansLocator")
SubscriptionFormatters = require("./SubscriptionFormatters")
SubscriptionViewModelBuilder = require('./SubscriptionViewModelBuilder')
LimitationsManager = require("./LimitationsManager")
RecurlyWrapper = require './RecurlyWrapper'
Settings   = require 'settings-sharelatex'
logger     = require('logger-sharelatex')
GeoIpLookup = require("../../infrastructure/GeoIpLookup")


module.exports = SubscriptionController =

	plansPage: (req, res, next) ->
		plans = SubscriptionViewModelBuilder.buildViewModel()
		if !req.session.user?
			baseUrl = "/register?redir="
		else
			baseUrl = ""
		viewName = "subscriptions/plans"
		if req.query.v?
			viewName = "#{viewName}_#{req.query.v}"
		logger.log viewName:viewName, "showing plans page"
		GeoIpLookup.getCurrencyCode req.ip, (err, recomendedCurrency)->
			res.render viewName,
				title: "plans_and_pricing"
				plans: plans
				baseUrl: baseUrl
				gaExperiments: Settings.gaExperiments.plansPage
				recomendedCurrency:recomendedCurrency

	#get to show the recurly.js page
	paymentPage: (req, res, next) ->
		SecurityManager.getCurrentUser req, (error, user) =>
			return next(error) if error?
			plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
			LimitationsManager.userHasSubscription user, (err, hasSubscription)->
				return next(err) if err?
				if hasSubscription or !plan?
					res.redirect "/user/subscription"
				else
					currency = req.query.currency?.toUpperCase()
					GeoIpLookup.getCurrencyCode req.ip, (err, recomendedCurrency)->
						return next(err) if err?
						if recomendedCurrency? and !currency?
							currency = recomendedCurrency
						RecurlyWrapper.sign {
							subscription:
								plan_code : req.query.planCode
								currency: currency
							account_code: user.id
						}, (error, signature) ->
							return next(error) if error?
							res.render "subscriptions/new",
								title      : "subscribe"
								plan_code: req.query.planCode
								currency: currency
								plan:plan
								recurlyConfig: JSON.stringify
									currency: currency<commit_msg>allow ip to set via query string for plans page<commit_after>SecurityManager     = require '../../managers/SecurityManager'
SubscriptionHandler  = require './SubscriptionHandler'
PlansLocator = require("./PlansLocator")
SubscriptionFormatters = require("./SubscriptionFormatters")
SubscriptionViewModelBuilder = require('./SubscriptionViewModelBuilder')
LimitationsManager = require("./LimitationsManager")
RecurlyWrapper = require './RecurlyWrapper'
Settings   = require 'settings-sharelatex'
logger     = require('logger-sharelatex')
GeoIpLookup = require("../../infrastructure/GeoIpLookup")


module.exports = SubscriptionController =

	plansPage: (req, res, next) ->
		plans = SubscriptionViewModelBuilder.buildViewModel()
		if !req.session.user?
			baseUrl = "/register?redir="
		else
			baseUrl = ""
		viewName = "subscriptions/plans"
		if req.query.v?
			viewName = "#{viewName}_#{req.query.v}"
		logger.log viewName:viewName, "showing plans page"
		GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)->
			res.render viewName,
				title: "plans_and_pricing"
				plans: plans
				baseUrl: baseUrl
				gaExperiments: Settings.gaExperiments.plansPage
				recomendedCurrency:recomendedCurrency

	#get to show the recurly.js page
	paymentPage: (req, res, next) ->
		SecurityManager.getCurrentUser req, (error, user) =>
			return next(error) if error?
			plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
			LimitationsManager.userHasSubscription user, (err, hasSubscription)->
				return next(err) if err?
				if hasSubscription or !plan?
					res.redirect "/user/subscription"
				else
					currency = req.query.currency?.toUpperCase()
					GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)->
						return next(err) if err?
						if recomendedCurrency? and !currency?
							currency = recomendedCurrency
						RecurlyWrapper.sign {
							subscription:
								plan_code : req.query.planCode
								currency: currency
							account_code: user.id
						}, (error, signature) ->
							return next(error) if error?
							res.render "subscriptions/new",
								title      : "subscribe"
								plan_code: req.query.planCode
								currency: currency
								plan:plan
								recurlyConfig: JSON.stringify
									currency: currency
{'fraction_non_alphanumeric': 0.10808435852372583, 'fraction_numerical': 0.0, 'mean_word_length': 3.7132505175983437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567857', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1331, 'n_words': 294}