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 17:19:15 -05:00
|
|
|
number: undefined
|
2014-12-28 16:53:12 -05:00
|
|
|
month: undefined
|
|
|
|
year: undefined
|
2014-12-28 17:19:15 -05:00
|
|
|
cvv: undefined
|
2014-12-28 12:16:21 -05:00
|
|
|
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 = ->
|
2014-12-28 17:19:15 -05:00
|
|
|
if $scope.data.number?
|
|
|
|
$scope.validation.correctCardNumber = recurly.validate.cardNumber($scope.data.number)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
|
|
|
$scope.validateExpiry = ->
|
2014-12-28 16:53:12 -05:00
|
|
|
if $scope.data.month? and $scope.data.year?
|
|
|
|
$scope.validation.correctExpiry = recurly.validate.expiry($scope.data.month, $scope.data.year)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
|
|
|
$scope.validateCvv = ->
|
2014-12-28 17:19:15 -05:00
|
|
|
if $scope.data.cvv?
|
|
|
|
$scope.validation.correctCvv = recurly.validate.cvv($scope.data.cvv)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
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
|
|
|
|