diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionController.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionController.coffee index 2633944e74..104701fc1b 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionController.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionController.coffee @@ -39,33 +39,37 @@ module.exports = SubscriptionController = if hasSubscription or !plan? res.redirect "/user/subscription" else - currency = req.query.currency || "USD" - 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 - recurlyConfig: JSON.stringify + currency = req.query.currency?.toUpperCase() + GeoIpLookup.getCurrencyCode req.headers["x-forwarded-for"], (err, recomendedCurrency)-> + if recomendedCurrency? and !currency? + currency = recomendedCurrency + RecurlyWrapper.sign { + subscription: + plan_code : req.query.planCode currency: currency - subdomain: Settings.apis.recurly.subdomain - subscriptionFormOptions: JSON.stringify - acceptedCards: ['discover', 'mastercard', 'visa'] - target : "#subscribeForm" - signature : signature - planCode : req.query.planCode - successURL : "#{Settings.siteUrl}/user/subscription/create?_csrf=#{req.session._csrf}" - accountCode : user.id - enableCoupons: true - acceptPaypal: true - account : - firstName : user.first_name - lastName : user.last_name - email : user.email + account_code: user.id + }, (error, signature) -> + return next(error) if error? + res.render "subscriptions/new", + title : "subscribe" + plan_code: req.query.planCode + currency: currency + recurlyConfig: JSON.stringify + currency: currency + subdomain: Settings.apis.recurly.subdomain + subscriptionFormOptions: JSON.stringify + acceptedCards: ['discover', 'mastercard', 'visa'] + target : "#subscribeForm" + signature : signature + planCode : req.query.planCode + successURL : "#{Settings.siteUrl}/user/subscription/create?_csrf=#{req.session._csrf}" + accountCode : user.id + enableCoupons: true + acceptPaypal: true + account : + firstName : user.first_name + lastName : user.last_name + email : user.email userSubscriptionPage: (req, res, next) -> diff --git a/services/web/app/views/subscriptions/new.jade b/services/web/app/views/subscriptions/new.jade index 8bb9a106f6..aa0699b65a 100644 --- a/services/web/app/views/subscriptions/new.jade +++ b/services/web/app/views/subscriptions/new.jade @@ -18,7 +18,7 @@ block content .page-header h1.text-centered #{translate("new_subscription")} #subscribeForm #{translate("loading_billing_form")}... - .col-md-5(ng-controller="NewSubscriptionController") + .col-md-5(ng-controller="NewSubscriptionController" ng-cloak) span.dropdown.col-md-1.changePlanButton() a.btn.btn-primary.dropdown-toggle( href="#", diff --git a/services/web/app/views/subscriptions/plans.jade b/services/web/app/views/subscriptions/plans.jade index fa50eb5ac5..bd7ad28151 100644 --- a/services/web/app/views/subscriptions/plans.jade +++ b/services/web/app/views/subscriptions/plans.jade @@ -1,7 +1,6 @@ extends ../layout block scripts script(type='text/javascript'). - window.recurlyPublicToken = '#{settings.apis.recurly.publicKey}' window.recomendedCurrency = '#{recomendedCurrency}' block content .content-alt @@ -34,7 +33,7 @@ block content ng-click="switchToStudent()" ) #{translate("half_price_student")} .col-md-2 - .dropdown(style="display:none;") + .dropdown() a.btn.btn-primary.dropdown-toggle#currenyDropdown( href="#", data-toggle="dropdown" diff --git a/services/web/test/UnitTests/coffee/Subscription/SubscriptionControllerTests.coffee b/services/web/test/UnitTests/coffee/Subscription/SubscriptionControllerTests.coffee index 52ad068fd8..cc57b4ee7a 100644 --- a/services/web/test/UnitTests/coffee/Subscription/SubscriptionControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Subscription/SubscriptionControllerTests.coffee @@ -120,6 +120,10 @@ describe "SubscriptionController sanboxed", -> @res.redirectedTo.should.equal "/user/subscription" describe "paymentPage", -> + beforeEach -> + @req.headers = {} + @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode) + describe "with a user without a subscription", -> beforeEach -> @LimitationsManager.userHasSubscription.callsArgWith(1, null, false) @@ -158,6 +162,34 @@ describe "SubscriptionController sanboxed", -> done() @SubscriptionController.paymentPage(@req, @res) + describe "which currency to use", -> + beforeEach -> + @LimitationsManager.userHasSubscription.callsArgWith(1, null, false) + @PlansLocator.findLocalPlanInSettings.returns({}) + + it "should use the set currency from the query string", (done)-> + @req.query.currency = "EUR" + @res.render = (page, opts)=> + opts.currency.should.equal "EUR" + opts.currency.should.not.equal @stubbedCurrencyCode + done() + @SubscriptionController.paymentPage @req, @res + + it "should upercase the currency code", (done)-> + @req.query.currency = "eur" + @res.render = (page, opts)=> + opts.currency.should.equal "EUR" + done() + @SubscriptionController.paymentPage @req, @res + + + it "should use the geo ip currency if non is provided", (done)-> + @req.query.currency = null + @res.render = (page, opts)=> + opts.currency.should.equal @stubbedCurrencyCode + done() + @SubscriptionController.paymentPage @req, @res + describe "successful_subscription", -> beforeEach (done) -> @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {})