2014-10-13 12:28:00 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App)->
|
|
|
|
|
2015-11-16 11:40:14 -05:00
|
|
|
App.controller "NewSubscriptionController", ($scope, MultiCurrencyPricing, abTestManager, $http, sixpack)->
|
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 = ()->
|
2015-11-17 06:47:28 -05:00
|
|
|
window.location = "/user/subscription/new?planCode=student_free_trial_7_days¤cy=#{$scope.currencyCode}&cc=#{$scope.data.coupon}"
|
2014-12-18 12:48:23 -05:00
|
|
|
|
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 =
|
2015-01-07 11:43:20 -05:00
|
|
|
number: ""
|
|
|
|
month: ""
|
|
|
|
year: ""
|
|
|
|
cvv: ""
|
2014-12-28 12:16:21 -05:00
|
|
|
first_name: ""
|
|
|
|
last_name: ""
|
|
|
|
postal_code: ""
|
|
|
|
address1 : ""
|
|
|
|
address2 : ""
|
2015-01-19 15:27:01 -05:00
|
|
|
state:""
|
2014-12-28 12:16:21 -05:00
|
|
|
city:""
|
2015-01-07 08:16:19 -05:00
|
|
|
country:window.countryCode
|
2015-02-02 12:18:06 -05:00
|
|
|
coupon: window.couponCode
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2015-11-17 06:42:03 -05:00
|
|
|
|
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
|
|
|
|
2015-02-02 12:18:06 -05:00
|
|
|
initialPricing = pricing
|
|
|
|
.plan(window.plan_code, { quantity: 1 })
|
|
|
|
.address({country: $scope.data.country})
|
|
|
|
.tax({tax_code: 'digital', vat_number: ''})
|
|
|
|
.currency($scope.currencyCode)
|
|
|
|
.coupon($scope.data.coupon)
|
|
|
|
.done()
|
2014-12-22 10:51:50 -05:00
|
|
|
|
|
|
|
pricing.on "change", =>
|
|
|
|
$scope.planName = pricing.items.plan.name
|
2015-01-07 08:16:19 -05:00
|
|
|
$scope.price = pricing.price
|
2015-01-29 05:44:42 -05:00
|
|
|
$scope.trialLength = pricing.items.plan.trial?.length
|
2015-02-10 13:05:39 -05:00
|
|
|
$scope.monthlyBilling = pricing.items.plan.period.length == 1
|
2015-11-17 06:42:03 -05:00
|
|
|
if pricing.items?.coupon?.discount?.type == "percent"
|
|
|
|
basePrice = parseInt(pricing.price.base.plan.unit)
|
|
|
|
$scope.normalPrice = basePrice
|
2015-11-17 07:23:27 -05:00
|
|
|
if pricing.items.coupon.applies_for_months > 0 and pricing.items.coupon?.discount?.rate and pricing.items.coupon?.applies_for_months?
|
|
|
|
$scope.discountMonths = pricing.items.coupon?.applies_for_months
|
|
|
|
$scope.discountRate = pricing.items.coupon?.discount?.rate * 100
|
|
|
|
|
2015-11-17 06:42:03 -05:00
|
|
|
if pricing.price?.taxes[0]?.rate?
|
|
|
|
$scope.normalPrice += (basePrice * pricing.price.taxes[0].rate)
|
2014-12-22 10:51:50 -05:00
|
|
|
$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
|
|
|
|
2015-03-04 13:08:06 -05:00
|
|
|
$scope.applyVatNumber = ->
|
|
|
|
pricing.tax({tax_code: 'digital', vat_number: $scope.data.vat_number}).done()
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-01-07 11:43:20 -05:00
|
|
|
$scope.validateCardNumber = validateCardNumber = ->
|
|
|
|
if $scope.data.number?.length != 0
|
2014-12-28 17:19:15 -05:00
|
|
|
$scope.validation.correctCardNumber = recurly.validate.cardNumber($scope.data.number)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2015-01-07 11:43:20 -05:00
|
|
|
$scope.validateExpiry = validateExpiry = ->
|
|
|
|
if $scope.data.month?.length != 0 and $scope.data.year?.length != 0
|
2014-12-28 16:53:12 -05:00
|
|
|
$scope.validation.correctExpiry = recurly.validate.expiry($scope.data.month, $scope.data.year)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2015-01-07 11:43:20 -05:00
|
|
|
$scope.validateCvv = validateCvv = ->
|
|
|
|
if $scope.data.cvv?.length != 0
|
2014-12-28 17:19:15 -05:00
|
|
|
$scope.validation.correctCvv = recurly.validate.cvv($scope.data.cvv)
|
2014-12-22 10:07:55 -05:00
|
|
|
|
2015-01-07 08:16:19 -05:00
|
|
|
$scope.updateCountry = ->
|
|
|
|
pricing.address({country:$scope.data.country}).done()
|
|
|
|
|
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) ->
|
2015-01-07 11:43:20 -05:00
|
|
|
$scope.validation.errorFields = {}
|
2014-12-23 06:04:54 -05:00
|
|
|
if err?
|
2015-01-29 09:43:27 -05:00
|
|
|
# We may or may not be in a digest loop here depending on
|
|
|
|
# whether recurly could do validation locally, so do it async
|
|
|
|
$scope.$evalAsync () ->
|
2015-01-29 09:22:45 -05:00
|
|
|
$scope.processing = false
|
|
|
|
$scope.genericError = err.message
|
|
|
|
_.each err.fields, (field)-> $scope.validation.errorFields[field] = true
|
2014-12-23 06:04:54 -05:00
|
|
|
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
|
2015-03-04 12:50:24 -05:00
|
|
|
coupon_code:pricing.items?.coupon?.code || ""
|
2016-06-24 06:42:58 -04:00
|
|
|
isPaypal: $scope.paymentMethod == 'paypal'
|
|
|
|
address:
|
2016-06-27 04:44:40 -04:00
|
|
|
address1: $scope.data.address1
|
|
|
|
address2: $scope.data.address2
|
|
|
|
country: $scope.data.country
|
|
|
|
state: $scope.data.state
|
2016-06-27 05:38:10 -04:00
|
|
|
postal_code: $scope.data.postal_code
|
2014-12-23 06:04:54 -05:00
|
|
|
$http.post("/user/subscription/create", postData)
|
2015-01-29 09:22:45 -05:00
|
|
|
.success (data, status, headers)->
|
2015-11-17 10:54:59 -05:00
|
|
|
sixpack.convert "in-editor-free-trial-plan", pricing.items.plan.code, (err)->
|
2015-11-16 11:40:14 -05:00
|
|
|
window.location.href = "/user/subscription/thank-you"
|
2015-01-29 09:22:45 -05:00
|
|
|
.error (data, status, headers)->
|
|
|
|
$scope.processing = false
|
2015-02-03 05:41:58 -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
|
|
|
|