<commit_before>class PaymentController extends KDController

  getGroup = ->
    KD.getSingleton('groupsController').getCurrentGroup()

  getBalance: (type, callback)->

    { JPaymentPlan } = KD.remote.api

    if type is 'user'
      JPaymentPlan.getUserBalance callback
    else
      JPaymentPlan.getGroupBalance callback

  updateCreditCard: (type, callback = (->)) ->

    { JPaymentPlan } = KD.remote.api

    @updateCreditCardModal {}, (newData) =>
      @modal.buttons.Save.hideLoader()
      if type in ['group', 'expensed']
        getGroup().setBillingInfo newData, callback
      else
        JPaymentPlan.setUserAccount newData, callback


  removePaymentMethod: (accountCode, callback) ->
    { JPayment } = KD.remote.api
    JPayment.removePaymentMethod accountCode, callback

  getSubscription: do ->
    findActiveSubscription = (subs, planCode, callback) ->
      subs.reverse().forEach (sub) ->
        if sub.planCode is planCode and sub.status in ['canceled', 'active']
          return callback sub

      callback 'none'

    getSubscription = (type, planCode, callback) ->
      { JPaymentSubscription } = KD.remote.api

      if type is 'group'
        getGroup().checkPayment (err, subs) =>
          findActiveSubscription subs, planCode, callback
      else
        JPaymentSubscription.getUserSubscriptions (err, subs) ->
          findActiveSubscription subs, planCode, callback

  confirmPayment: (type, plan, callback = (->)) ->
    getGroup().canCreateVM { type, planCode: plan.code }, (err, status) =>
      @getSubscription type, plan.code, (subscription) =>
        cb = (needBilling, balance, amount) =>
          @createPaymentConfirmationModal {
            needBilling, balance, amount, type, group, plan, subscription
          }, callback

        if status
          cb no, 0, 0
        else
          @fetchBillingInfo type, group, (err, billing) =>
            needBilling = err or not billing?.cardNumber?

            @getBalance type, group, (err, balance) =>
              balance = 0  if err
              cb needBilling, balance, plan.feeMonthly

  makePayment: (type, plan, amount) ->
    vmController = KD.getSingleton('vmController')

    if amount is 0
      vmController.createGroupVM type, plan.code
    else if type in ['group', 'expensed']
      paymentInfo = { plan: plan.code, multiple: yes }
      getGroup().makePayment paymentInfo, (err, result)->
        return KD.showError err  if err
        vmController.createGroupVM type, plan.code
    else
      plan.subscribe multiple: yes, (err, result)->
        return KD.showError err  if err
        vmController.createGroupVM type, plan.code

  deleteVM: (vmInfo, callback) ->
    type  =
      if (vmInfo.planOwner.indexOf 'user_') > -1 then 'user'
      else if vmInfo.type is 'expensed'          then 'expensed'
      else 'group'

    @getSubscription getGroup(), type, vmInfo.planCode,\
      @createDeleteConfirmationModal.bind this, type, callback

  # views

  updateCreditCardModal: (data, callback) ->
    @modal = new PaymentFormModal { callback }

    form = @modal.modalTabs.forms['Billing Info']
    form.inputs[k]?.setValue v  for k, v of data

    @modal.on 'KDObjectWillBeDestroyed', => delete @modal
    return @modal

  fetchBillingInfo: (type, callback) ->

    { JPaymentPlan } = KD.remote.api

    switch type
      when 'group', 'expensed'
        getGroup().fetchBillingInfo callback
      when 'user'
        JPaymentPlan.fetchAccountDetails callback

  updateBillingInfo: (billingInfo, callback) ->

    { JPayment } = KD.remote.api

    JPayment.setBillingInfo billingInfo, (err, result)->


  createBillingInfoModal:(type, billingInfo) ->

    modal = new BillingFormModal { type }, billingInfo

    @fetchCountryData (err, countries, countryOfIp) =>
      modal.setCountryData { countries, countryOfIp }

    return modal

  fetchCountryData:(callback)->

    { JPayment } = KD.remote.api

    if @countries or @countryOfIp
      return @utils.defer => callback null, @countries, @countryOfIp

    ip = $.cookie 'clientIPAddress'

    JPayment.fetchCountryDataByIp ip, (err, @countries, @countryOfIp) =>
      callback err, @countries, @countryOfIp

  createPaymentConfirmationModal: (options, callback)->
    options.callback or= callback
    return new PaymentConfirmationModal options

  createDeleteConfirmationModal: (type, callback, subscription)->
    return new PaymentDeleteConfirmationModal { subscription, type, callback }<commit_msg>remove no-longer-used methods, and forward back to the correct callback<commit_after>class PaymentController extends KDController

  getGroup = ->
    KD.getSingleton('groupsController').getCurrentGroup()

  getBalance: (type, callback)->

    { JPaymentPlan } = KD.remote.api

    if type is 'user'
      JPaymentPlan.getUserBalance callback
    else
      JPaymentPlan.getGroupBalance callback


  removePaymentMethod: (accountCode, callback) ->
    { JPayment } = KD.remote.api
    JPayment.removePaymentMethod accountCode, callback

  getSubscription: do ->
    findActiveSubscription = (subs, planCode, callback) ->
      subs.reverse().forEach (sub) ->
        if sub.planCode is planCode and sub.status in ['canceled', 'active']
          return callback sub

      callback 'none'

    getSubscription = (type, planCode, callback) ->
      { JPaymentSubscription } = KD.remote.api

      if type is 'group'
        getGroup().checkPayment (err, subs) =>
          findActiveSubscription subs, planCode, callback
      else
        JPaymentSubscription.getUserSubscriptions (err, subs) ->
          findActiveSubscription subs, planCode, callback

  confirmPayment: (type, plan, callback = (->)) ->
    getGroup().canCreateVM { type, planCode: plan.code }, (err, status) =>
      @getSubscription type, plan.code, (subscription) =>
        cb = (needBilling, balance, amount) =>
          @createPaymentConfirmationModal {
            needBilling, balance, amount, type, group, plan, subscription
          }, callback

        if status
          cb no, 0, 0
        else
          @fetchBillingInfo type, group, (err, billing) =>
            needBilling = err or not billing?.cardNumber?

            @getBalance type, group, (err, balance) =>
              balance = 0  if err
              cb needBilling, balance, plan.feeMonthly

  makePayment: (type, plan, amount) ->
    vmController = KD.getSingleton('vmController')

    if amount is 0
      vmController.createGroupVM type, plan.code
    else if type in ['group', 'expensed']
      paymentInfo = { plan: plan.code, multiple: yes }
      getGroup().makePayment paymentInfo, (err, result)->
        return KD.showError err  if err
        vmController.createGroupVM type, plan.code
    else
      plan.subscribe multiple: yes, (err, result)->
        return KD.showError err  if err
        vmController.createGroupVM type, plan.code

  deleteVM: (vmInfo, callback) ->
    type  =
      if (vmInfo.planOwner.indexOf 'user_') > -1 then 'user'
      else if vmInfo.type is 'expensed'          then 'expensed'
      else 'group'

    @getSubscription getGroup(), type, vmInfo.planCode,\
      @createDeleteConfirmationModal.bind this, type, callback

  # views

  fetchBillingInfo: (type, callback) ->

    { JPaymentPlan } = KD.remote.api

    switch type
      when 'group', 'expensed'
        getGroup().fetchBillingInfo callback
      when 'user'
        JPaymentPlan.fetchAccountDetails callback

  updateBillingInfo: (billingInfo, callback) ->

    { JPayment } = KD.remote.api

    JPayment.setBillingInfo billingInfo, callback


  createBillingInfoModal:(type, billingInfo) ->

    modal = new BillingFormModal { type }, billingInfo

    @fetchCountryData (err, countries, countryOfIp) =>
      modal.setCountryData { countries, countryOfIp }

    return modal

  fetchCountryData:(callback)->

    { JPayment } = KD.remote.api

    if @countries or @countryOfIp
      return @utils.defer => callback null, @countries, @countryOfIp

    ip = $.cookie 'clientIPAddress'

    JPayment.fetchCountryDataByIp ip, (err, @countries, @countryOfIp) =>
      callback err, @countries, @countryOfIp

  createPaymentConfirmationModal: (options, callback)->
    options.callback or= callback
    return new PaymentConfirmationModal options

  createDeleteConfirmationModal: (type, callback, subscription)->
    return new PaymentDeleteConfirmationModal { subscription, type, callback }
{'fraction_non_alphanumeric': 0.10153006760763848, 'fraction_numerical': 0.0011860989206499823, 'mean_word_length': 2.9605448567402535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '6718175', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2321, 'n_words': 643}