<commit_before>kd = require 'kd'
KDCustomHTMLView = kd.CustomHTMLView
PaymentBaseModal = require './paymentbasemodal'
PaymentForm = require './paymentform'
PaymentConstants = require './paymentconstants'
showError = require '../util/showError'
trackEvent = require 'app/util/trackEvent'


# This class is the modal view.
# Shows the payment form and the result of
# the process, (e.g validation errors etc)
module.exports = class PaymentModal extends PaymentBaseModal

  { UPGRADE, DOWNGRADE, INTERVAL_CHANGE } = PaymentConstants.operation

  getInitialState: ->
    planInterval : PaymentConstants.planInterval.MONTH
    planTitle    : PaymentConstants.planTitle.HOBBYIST
    provider     : PaymentConstants.provider.KODING
    operation    : PaymentConstants.operation.UPGRADE


  constructor: (options = {}, data) ->

    { state } = options

    @state = kd.utils.extend @getInitialState(), state

    operation = PaymentConstants.getOperation @state.currentPlan, @state.planTitle

    if @state.subscriptionState is 'expired'
      options.title = 'Reactivate your account'
    else
      options.title = switch operation
        when UPGRADE         then 'Upgrades are awesome. Let\'s do this!'
        when INTERVAL_CHANGE then 'Change your billing cycle'
        when DOWNGRADE       then 'Downgrade your plan'

    options.subtitle = ''

    super options, data

    trackEvent 'Viewed Product',
      interval : @state.planInterval
      name     : @state.planTitle


  initViews: ->

    { provider, planTitle } = @state
    { PAYPAL } = PaymentConstants.provider
    { FREE }   = PaymentConstants.planTitle

    @addSubView @errors = new KDCustomHTMLView {cssClass : 'errors hidden'}
    @addSubView @form   = new PaymentForm {@state}

    @handlePaypalNotAllowed()  if provider is PAYPAL and planTitle isnt FREE


  initEvents: ->

    @forwardEvent @form, 'PaymentSubmitted'
    @forwardEvent @form, 'PaymentWorkflowFinished'
    @forwardEvent @form, 'PaypalButtonClicked'

    @form.forwardEvent this, 'PaymentProviderLoaded'

    @on 'StripeRequestValidationFailed', @bound 'handleStripeFail'
    @on 'FailedAttemptLimitReached',     @bound 'handleLimitReached'
    @on 'PaymentFailed',                 @bound 'handleError'
    @on 'PaymentSucceeded',              @bound 'handleSuccess'

    { paymentController } = kd.singletons

    paymentController.on 'PaypalRequestFinished', @bound 'handlePaypalResponse'


  handlePaypalNotAllowed: ->

    @setTitle 'Not allowed'
    @form.showPaypalNotAllowedStage()


  handlePaypalResponse: (err) ->

    @form.paypalForm.buttons['paypal'].hideLoader()

    return showError err  if err

    { paymentController } = kd.singletons

    paymentController.subscriptions (err, subscription) =>

      return showError err  if err

      @state = kd.utils.extend @state, subscription

      @handleSuccess()


  handleStripeFail: (error) ->

    @form.submitButton.hideLoader()
    @form.showValidationErrorsOnInputs error


  handleLimitReached: ->

    @setTitle 'Too many failed attempts'
    @setSubtitle ''
    @setClass 'has-problem'

    @form.submitButton.hideLoader()
    @form.showMaximumAttemptFailed()

    @once 'KDModalViewDestroyed', =>
      @emit 'PaymentWorkflowFinishedWithError', @state


  handleError: (error) ->

    msg = error?.description or error?.message or "Something went wrong."
    showError msg


  handleSuccess: ->

    { currentPlan, planTitle } = @state

    operation = PaymentConstants.getOperation currentPlan, planTitle

    switch operation
      when UPGRADE
        @setTitle 'Congratulations! Upgrade successful'
        @setSubtitle 'Your account has been upgraded to the plan below.'
      when INTERVAL_CHANGE
        @setTitle 'Billing cycle changed'
      when DOWNGRADE
        @setTitle 'Downgrade complete.'

    @form.showSuccess operation

    @once 'KDModalViewDestroyed', =>
      @emit 'PaymentWorkflowFinished', @state<commit_msg>client: remove duplicate payment success event<commit_after>kd = require 'kd'
KDCustomHTMLView = kd.CustomHTMLView
PaymentBaseModal = require './paymentbasemodal'
PaymentForm = require './paymentform'
PaymentConstants = require './paymentconstants'
showError = require '../util/showError'
trackEvent = require 'app/util/trackEvent'


# This class is the modal view.
# Shows the payment form and the result of
# the process, (e.g validation errors etc)
module.exports = class PaymentModal extends PaymentBaseModal

  { UPGRADE, DOWNGRADE, INTERVAL_CHANGE } = PaymentConstants.operation

  getInitialState: ->
    planInterval : PaymentConstants.planInterval.MONTH
    planTitle    : PaymentConstants.planTitle.HOBBYIST
    provider     : PaymentConstants.provider.KODING
    operation    : PaymentConstants.operation.UPGRADE


  constructor: (options = {}, data) ->

    { state } = options

    @state = kd.utils.extend @getInitialState(), state

    operation = PaymentConstants.getOperation @state.currentPlan, @state.planTitle

    if @state.subscriptionState is 'expired'
      options.title = 'Reactivate your account'
    else
      options.title = switch operation
        when UPGRADE         then 'Upgrades are awesome. Let\'s do this!'
        when INTERVAL_CHANGE then 'Change your billing cycle'
        when DOWNGRADE       then 'Downgrade your plan'

    options.subtitle = ''

    super options, data

    trackEvent 'Viewed Product',
      interval : @state.planInterval
      name     : @state.planTitle


  initViews: ->

    { provider, planTitle } = @state
    { PAYPAL } = PaymentConstants.provider
    { FREE }   = PaymentConstants.planTitle

    @addSubView @errors = new KDCustomHTMLView {cssClass : 'errors hidden'}
    @addSubView @form   = new PaymentForm {@state}

    @handlePaypalNotAllowed()  if provider is PAYPAL and planTitle isnt FREE


  initEvents: ->

    @forwardEvent @form, 'PaymentSubmitted'
    @forwardEvent @form, 'PaymentWorkflowFinished'
    @forwardEvent @form, 'PaypalButtonClicked'

    @form.forwardEvent this, 'PaymentProviderLoaded'

    @on 'StripeRequestValidationFailed', @bound 'handleStripeFail'
    @on 'FailedAttemptLimitReached',     @bound 'handleLimitReached'
    @on 'PaymentFailed',                 @bound 'handleError'
    @on 'PaymentSucceeded',              @bound 'handleSuccess'

    { paymentController } = kd.singletons

    paymentController.on 'PaypalRequestFinished', @bound 'handlePaypalResponse'


  handlePaypalNotAllowed: ->

    @setTitle 'Not allowed'
    @form.showPaypalNotAllowedStage()


  handlePaypalResponse: (err) ->

    @form.paypalForm.buttons['paypal'].hideLoader()

    return showError err  if err

    { paymentController } = kd.singletons

    paymentController.subscriptions (err, subscription) =>

      return showError err  if err

      @state = kd.utils.extend @state, subscription

      @handleSuccess()


  handleStripeFail: (error) ->

    @form.submitButton.hideLoader()
    @form.showValidationErrorsOnInputs error


  handleLimitReached: ->

    @setTitle 'Too many failed attempts'
    @setSubtitle ''
    @setClass 'has-problem'

    @form.submitButton.hideLoader()
    @form.showMaximumAttemptFailed()

    @once 'KDModalViewDestroyed', =>
      @emit 'PaymentWorkflowFinishedWithError', @state


  handleError: (error) ->

    msg = error?.description or error?.message or "Something went wrong."
    showError msg


  handleSuccess: ->

    { currentPlan, planTitle } = @state

    operation = PaymentConstants.getOperation currentPlan, planTitle

    switch operation
      when UPGRADE
        @setTitle 'Congratulations! Upgrade successful'
        @setSubtitle 'Your account has been upgraded to the plan below.'
      when INTERVAL_CHANGE
        @setTitle 'Billing cycle changed'
      when DOWNGRADE
        @setTitle 'Downgrade complete.'

    @form.showSuccess operation

{'fraction_non_alphanumeric': 0.0913265306122449, 'fraction_numerical': 0.0, 'mean_word_length': 3.3951793721973096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '3421879', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2144, 'n_words': 569}