2014-10-13 12:28:00 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App)->
|
|
|
|
|
2014-12-18 13:59:29 -05:00
|
|
|
App.controller "NewSubscriptionController", ($scope, MultiCurrencyPricing, abTestManager, $http)->
|
2014-12-22 07:55:29 -05:00
|
|
|
throw new Error("Recurly API Library Missing.") if typeof recurly is "undefined"
|
2014-10-13 12:28:00 -04:00
|
|
|
|
|
|
|
$scope.currencyCode = MultiCurrencyPricing.currencyCode
|
|
|
|
$scope.plans = MultiCurrencyPricing.plans
|
|
|
|
|
2014-11-24 11:21:03 -05:00
|
|
|
$scope.switchToStudent = ()->
|
2014-12-18 12:48:23 -05:00
|
|
|
window.location = "/user/subscription/new?planCode=student¤cy=#{$scope.currencyCode}"
|
|
|
|
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2014-12-22 10:51:50 -05:00
|
|
|
$scope.paymentMethod = "credit_card"
|
|
|
|
|
2014-12-18 12:48:23 -05:00
|
|
|
$scope.data =
|
2014-12-28 12:16:21 -05:00
|
|
|
number: ""
|
|
|
|
month: ""
|
|
|
|
year: ""
|
|
|
|
cvv: ""
|
|
|
|
first_name: ""
|
|
|
|
last_name: ""
|
|
|
|
postal_code: ""
|
|
|
|
address1 : ""
|
|
|
|
address2 : ""
|
|
|
|
city:""
|
|
|
|
country:""
|
2014-12-22 10:07:55 -05:00
|
|
|
|
|
|
|
$scope.validation =
|
|
|
|
correctCardNumber : true
|
|
|
|
correctExpiry: true
|
|
|
|
correctCvv:true
|
|
|
|
|
2014-12-28 12:16:21 -05:00
|
|
|
$scope.processing = false
|
|
|
|
|
2014-12-23 07:08:01 -05:00
|
|
|
recurly.configure window.recurlyApiKey
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2014-12-22 07:55:29 -05:00
|
|
|
pricing = recurly.Pricing()
|
|
|
|
window.pricing = pricing
|
2014-12-18 13:59:29 -05:00
|
|
|
|
2014-12-22 09:06:39 -05:00
|
|
|
pricing.plan(window.plan_code, { quantity: 1 }).currency($scope.currencyCode).done()
|
2014-12-18 13:59:29 -05:00
|
|
|
|
2014-12-22 10:51:50 -05:00
|
|
|
|
|
|
|
pricing.on "change", =>
|
|
|
|
$scope.planName = pricing.items.plan.name
|
|
|
|
$scope.price = pricing.price.currency.symbol+pricing.price.next.total
|
|
|
|
$scope.trialLength = pricing.items.plan.trial.length
|
|
|
|
$scope.billingCycleType = if pricing.items.plan.period.interval == "months" then "month" else "year"
|
|
|
|
$scope.$apply()
|
|
|
|
|
2014-12-22 09:06:39 -05:00
|
|
|
$scope.applyCoupon = ->
|
|
|
|
pricing.coupon($scope.data.coupon).done()
|
2014-12-22 07:55:29 -05:00
|
|
|
|
2014-12-22 09:06:39 -05:00
|
|
|
$scope.changeCurrency = (newCurrency)->
|
|
|
|
$scope.currencyCode = newCurrency
|
|
|
|
pricing.currency(newCurrency).done()
|
2014-12-22 07:55:29 -05:00
|
|
|
|
2014-12-22 10:07:55 -05:00
|
|
|
$scope.validateCardNumber = ->
|
|
|
|
$scope.validation.correctCardNumber = recurly.validate.cardNumber($scope.data.number)
|
|
|
|
|
|
|
|
$scope.validateExpiry = ->
|
|
|
|
$scope.validation.correctExpiry = recurly.validate.expiry($scope.data.month, $scope.data.year)
|
|
|
|
|
|
|
|
$scope.validateCvv = ->
|
|
|
|
$scope.validation.correctCvv = recurly.validate.cvv($scope.data.cvv)
|
|
|
|
|
2014-12-22 10:51:50 -05:00
|
|
|
$scope.changePaymentMethod = (paymentMethod)->
|
|
|
|
if paymentMethod == "paypal"
|
|
|
|
$scope.usePaypal = true
|
|
|
|
else
|
|
|
|
$scope.usePaypal = false
|
2014-12-18 12:48:23 -05:00
|
|
|
|
2014-12-22 10:51:50 -05:00
|
|
|
completeSubscription = (err, recurly_token_id) ->
|
2014-12-23 06:04:54 -05:00
|
|
|
if err?
|
|
|
|
$scope.genericError = err.message
|
|
|
|
else
|
|
|
|
postData =
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
recurly_token_id:recurly_token_id.id
|
|
|
|
subscriptionDetails:
|
2014-12-28 12:16:21 -05:00
|
|
|
currencyCode:pricing.items.currency
|
|
|
|
plan_code:pricing.items.plan.code
|
2014-12-23 06:04:54 -05:00
|
|
|
$http.post("/user/subscription/create", postData)
|
|
|
|
.success (data, status, headers)->
|
|
|
|
window.location.href = "/user/subscription/thank-you"
|
|
|
|
.error (data, status, headers)->
|
2014-12-28 12:16:21 -05:00
|
|
|
$scope.processing = false
|
2014-12-23 06:04:54 -05:00
|
|
|
$scope.genericError = "Something went wrong processing the request"
|
2014-12-18 13:59:29 -05:00
|
|
|
|
2014-12-22 10:51:50 -05:00
|
|
|
$scope.submit = ->
|
2014-12-28 12:16:21 -05:00
|
|
|
$scope.processing = true
|
2014-12-22 10:51:50 -05:00
|
|
|
if $scope.paymentMethod == 'paypal'
|
|
|
|
opts = { description: $scope.planName }
|
|
|
|
recurly.paypal opts, completeSubscription
|
|
|
|
else
|
|
|
|
recurly.token $scope.data, completeSubscription
|
|
|
|
|
|
|
|
|
2014-12-18 12:48:23 -05:00
|
|
|
|