<commit_before># This class is responsible of showing the payment modal.
# This workflow will decide if what to do next.
# No matter where you are instantiating this class,
# as long as you pass the view instance to this class
# it will emit necessary events when a substantial thing
# happens in the work flow.
#
# Necessary options when you instantiate it.
#
# planTitle  : string (see PaymentWorkflow.plans)
# monthPrice : int (e.g 1900 for $19)
# yearPrice  : int (e.g 19000 for $190)
# view       : KDView
class PaymentWorkflow extends KDController

  @planInterval :
    MONTH       : 'month'
    YEAR        : 'year'

  @planTitle     :
    FREE         : 'free'
    HOBBYIST     : 'hobbyist'
    DEVELOPER    : 'developer'
    PROFESSIONAL : 'professional'

  @isUpgrade: (current, selected) ->

    arr = [
      PaymentWorkflow.planTitle.FREE
      PaymentWorkflow.planTitle.HOBBYIST
      PaymentWorkflow.planTitle.DEVELOPER
      PaymentWorkflow.planTitle.PROFESSIONAL
    ]

    (arr.indexOf selected) > (arr.indexOf current)


  initialState: {}


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

    super options, data

    @state = @utils.extend @initialState, options.state

    @initPaymentProvider()
    @start()


  initPaymentProvider: ->

    return  if window.Stripe?

    options = tagName: 'script', attributes: { src: 'https://js.stripe.com/v2/' }
    document.head.appendChild (@providerScript = new KDCustomHTMLView options).getElement()

    repeater = KD.utils.repeat 500, =>

      return  unless Stripe?

      Stripe.setPublishableKey KD.config.stripe.token

      @modal.emit 'PaymentProviderLoaded', { provider: Stripe }
      window.clearInterval repeater


  start: ->

    {
      planTitle, monthPrice, yearPrice, reducedMonth, discount
    } = @getOptions()

    @state = @utils.extend @state, {
      planTitle, monthPrice, yearPrice, reducedMonth, discount
    }

    @modal = new PaymentModal { @state }
    @modal.on 'PaymentWorkflowFinished', @bound 'finish'
    @modal.on "PaymentSubmitted",        @bound 'handlePaymentSubmit'


  handlePaymentSubmit: (formData) -><commit_msg>payment: read directly from state instead of options (workflow)<commit_after># This class is responsible of showing the payment modal.
# This workflow will decide if what to do next.
# No matter where you are instantiating this class,
# as long as you pass the view instance to this class
# it will emit necessary events when a substantial thing
# happens in the work flow.
#
# Necessary options when you instantiate it.
#
# planTitle  : string (see PaymentWorkflow.plans)
# monthPrice : int (e.g 1900 for $19)
# yearPrice  : int (e.g 19000 for $190)
# view       : KDView
class PaymentWorkflow extends KDController

  @planInterval :
    MONTH       : 'month'
    YEAR        : 'year'

  @planTitle     :
    FREE         : 'free'
    HOBBYIST     : 'hobbyist'
    DEVELOPER    : 'developer'
    PROFESSIONAL : 'professional'

  @isUpgrade: (current, selected) ->

    arr = [
      PaymentWorkflow.planTitle.FREE
      PaymentWorkflow.planTitle.HOBBYIST
      PaymentWorkflow.planTitle.DEVELOPER
      PaymentWorkflow.planTitle.PROFESSIONAL
    ]

    (arr.indexOf selected) > (arr.indexOf current)


  initialState: {}


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

    super options, data

    @state = @utils.extend @initialState, options.state

    @initPaymentProvider()
    @start()


  initPaymentProvider: ->

    return  if window.Stripe?

    options = tagName: 'script', attributes: { src: 'https://js.stripe.com/v2/' }
    document.head.appendChild (@providerScript = new KDCustomHTMLView options).getElement()

    repeater = KD.utils.repeat 500, =>

      return  unless Stripe?

      Stripe.setPublishableKey KD.config.stripe.token

      @modal.emit 'PaymentProviderLoaded', { provider: Stripe }
      window.clearInterval repeater


  start: ->

    @modal = new PaymentModal { @state }
    @modal.on 'PaymentWorkflowFinished', @bound 'finish'
    @modal.on "PaymentSubmitted",        @bound 'handlePaymentSubmit'


  handlePaymentSubmit: (formData) ->
{'fraction_non_alphanumeric': 0.09919177075679647, 'fraction_numerical': 0.00881704628949302, 'mean_word_length': 3.0677290836653386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '6521524', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1186, 'n_words': 348}