<commit_before>Bongo = require 'bongo'
{ secure, signature, Base } = Bongo
KodingError = require '../error'
{ extend } = require 'underscore'

TEAM_PLANS = require '../models/computeproviders/teamplans'

module.exports = class Payment extends Base
  @share()

  ERR_USER_NOT_CONFIRMED = 'ERR_USER_NOT_CONFIRMED'

  @set
    sharedMethods     :
      static          :
        subscribe         :
          (signature Object, Function)
        subscriptions     :
          (signature Object, Function)
        invoices          :
          (signature Object, Function)
        creditCard        :
          (signature Object, Function)
        updateCreditCard  :
          (signature Object, Function)
        canChangePlan     :
          (signature Object, Function)
        logOrder          :
          (signature Object, Function)
        getToken          :
          (signature Object, Function)
        canUserPurchase   :
          (signature Function)
        fetchGroupPlan    :
          (signature Function)


  { get, post, deleteReq } = require './socialapi/requests'

  socialProxyUrl = '/api/social'


  @subscribe = secure (client, data, callback) ->
    requiredParams = [
      'token', 'email', 'planTitle', 'planInterval', 'provider'
    ]

    @canUserPurchase client, (err, confirmed) ->
      return callback err  if err
      return callback new KodingError ERR_USER_NOT_CONFIRMED  unless confirmed

      validateParams requiredParams, data, (err) ->
        return callback err  if err

        canChangePlan client, data.planTitle, (err) ->
          return callback err  if err

          data.accountId = getAccountId client
          url = "#{socialProxyUrl}/payments/subscribe"

          post url, data, (err, response) ->
            callback err, response

            data.status = if err then '$failed' else '$success'

            SiftScience = require './siftscience'
            SiftScience.transaction client, data, (err) ->
              log 'logging to SiftScience failed', err  if err


  @fetchGroupPlan = (group, callback) ->

    return callback new KodingError 'No such group'   unless group
    return callback null, { planTitle: 'unlimited' }  if group.slug is 'koding'

    url = "#{socialProxyUrl}/payments/group/subscriptions?group_id=#{group._id}"
    get url, {}, (err, subscription) ->
      return callback err  if err

      unless isSubscriptionOk group, subscription
        return callback new KodingError 'Trial period exceeded'

      subscription = sanitizeSubscription subscription

      callback null, subscription<commit_msg>Payment: disable subscription date check for now<commit_after>Bongo = require 'bongo'
{ secure, signature, Base } = Bongo
KodingError = require '../error'
{ extend } = require 'underscore'

TEAM_PLANS = require '../models/computeproviders/teamplans'

module.exports = class Payment extends Base
  @share()

  ERR_USER_NOT_CONFIRMED = 'ERR_USER_NOT_CONFIRMED'

  @set
    sharedMethods     :
      static          :
        subscribe         :
          (signature Object, Function)
        subscriptions     :
          (signature Object, Function)
        invoices          :
          (signature Object, Function)
        creditCard        :
          (signature Object, Function)
        updateCreditCard  :
          (signature Object, Function)
        canChangePlan     :
          (signature Object, Function)
        logOrder          :
          (signature Object, Function)
        getToken          :
          (signature Object, Function)
        canUserPurchase   :
          (signature Function)
        fetchGroupPlan    :
          (signature Function)


  { get, post, deleteReq } = require './socialapi/requests'

  socialProxyUrl = '/api/social'


  @subscribe = secure (client, data, callback) ->
    requiredParams = [
      'token', 'email', 'planTitle', 'planInterval', 'provider'
    ]

    @canUserPurchase client, (err, confirmed) ->
      return callback err  if err
      return callback new KodingError ERR_USER_NOT_CONFIRMED  unless confirmed

      validateParams requiredParams, data, (err) ->
        return callback err  if err

        canChangePlan client, data.planTitle, (err) ->
          return callback err  if err

          data.accountId = getAccountId client
          url = "#{socialProxyUrl}/payments/subscribe"

          post url, data, (err, response) ->
            callback err, response

            data.status = if err then '$failed' else '$success'

            SiftScience = require './siftscience'
            SiftScience.transaction client, data, (err) ->
              log 'logging to SiftScience failed', err  if err


  @fetchGroupPlan = (group, callback) ->

    return callback new KodingError 'No such group'   unless group
    return callback null, { planTitle: 'unlimited' }  if group.slug is 'koding'

    url = "#{socialProxyUrl}/payments/group/subscriptions?group_id=#{group._id}"
    get url, {}, (err, subscription) ->
      return callback err  if err

      # unless isSubscriptionOk group, subscription
      #   return callback new KodingError 'Trial period exceeded'

      callback null, sanitizeSubscription subscription
{'fraction_non_alphanumeric': 0.08779360800924144, 'fraction_numerical': 0.0, 'mean_word_length': 2.2858950031625556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2972521', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1332, 'n_words': 405}