<commit_before># frozen_string_literal: true

# == Schema Information
#
# Table name: payment_go_cardless_transactions
#
#  id                :integer          not null, primary key
#  go_cardless_id    :string
#  charge_date       :date
#  amount            :decimal(, )
#  description       :string
#  currency          :string
#  status            :integer
#  reference         :string
#  amount_refunded   :decimal(, )
#  page_id           :integer
#  payment_method_id :integer
#  customer_id       :integer
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  aasm_state        :string
#  subscription_id   :integer
#

class Payment::GoCardless::Transaction < ApplicationRecord
  include AASM

  belongs_to :page
  belongs_to :customer, class_name: 'Payment::GoCardless::Customer'
  belongs_to :payment_method, class_name: 'Payment::GoCardless::PaymentMethod'
  belongs_to :subscription, class_name:   'Payment::GoCardless::Subscription'

  after_create :increment_funding_counter

  validates :go_cardless_id, presence: true, allow_blank: false

  scope :one_off, -> { where(subscription_id: nil) }

  ACTION_FROM_STATE = {
    submitted:     :submit,
    confirmed:     :confirm,
    cancelled:     :cancel,
    failed:        :fail,
    charged_back:  :charge_back,
    paid_out:      :pay_out
  }.freeze

  aasm do
    state :created, initial: true
    state :submitted
    state :confirmed
    state :paid_out
    state :cancelled
    state :failed
    state :charged_back

    event :run_submit do
      transitions from: :created, to: :submitted
    end

    event :run_confirm do
      transitions from: %i[created submitted], to: :confirmed
    end

    event :run_payout do
      transitions from: %i[created submitted confirmed], to: :paid_out
    end

    event :run_cancel do
      transitions to: :cancelled
    end

    event :run_fail do
      transitions to: :failed, after: :publish_failed_subscription_charge
    end

    event :run_charge_back do
      transitions to: :charged_back
    end
  end

  def publish_failed_subscription_charge
    return if subscription.blank?
    ChampaignQueue.push({
      type: 'subscription-payment',
      params: {
        created_at: created_at.strftime('%Y-%m-%d %H:%M:%S'),
        recurring_id: subscription.go_cardless_id,
        success: 0,
        status: 'failed'
      }
    },
                        { group_id: "gocardless-subscription:#{id}" })
  end

  def increment_funding_counter
    FundingCounter.update(page: page, currency: currency, amount: amount * 100)
  end
end<commit_msg>GoCardless transaction: Rescue if amount.nil?<commit_after># frozen_string_literal: true

# == Schema Information
#
# Table name: payment_go_cardless_transactions
#
#  id                :integer          not null, primary key
#  go_cardless_id    :string
#  charge_date       :date
#  amount            :decimal(, )
#  description       :string
#  currency          :string
#  status            :integer
#  reference         :string
#  amount_refunded   :decimal(, )
#  page_id           :integer
#  payment_method_id :integer
#  customer_id       :integer
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  aasm_state        :string
#  subscription_id   :integer
#

class Payment::GoCardless::Transaction < ApplicationRecord
  include AASM

  belongs_to :page
  belongs_to :customer, class_name: 'Payment::GoCardless::Customer'
  belongs_to :payment_method, class_name: 'Payment::GoCardless::PaymentMethod'
  belongs_to :subscription, class_name:   'Payment::GoCardless::Subscription'

  after_create :increment_funding_counter

  validates :go_cardless_id, presence: true, allow_blank: false

  scope :one_off, -> { where(subscription_id: nil) }

  ACTION_FROM_STATE = {
    submitted:     :submit,
    confirmed:     :confirm,
    cancelled:     :cancel,
    failed:        :fail,
    charged_back:  :charge_back,
    paid_out:      :pay_out
  }.freeze

  aasm do
    state :created, initial: true
    state :submitted
    state :confirmed
    state :paid_out
    state :cancelled
    state :failed
    state :charged_back

    event :run_submit do
      transitions from: :created, to: :submitted
    end

    event :run_confirm do
      transitions from: %i[created submitted], to: :confirmed
    end

    event :run_payout do
      transitions from: %i[created submitted confirmed], to: :paid_out
    end

    event :run_cancel do
      transitions to: :cancelled
    end

    event :run_fail do
      transitions to: :failed, after: :publish_failed_subscription_charge
    end

    event :run_charge_back do
      transitions to: :charged_back
    end
  end

  def publish_failed_subscription_charge
    return if subscription.blank?
    ChampaignQueue.push({
      type: 'subscription-payment',
      params: {
        created_at: created_at.strftime('%Y-%m-%d %H:%M:%S'),
        recurring_id: subscription.go_cardless_id,
        success: 0,
        status: 'failed'
      }
    },
                        { group_id: "gocardless-subscription:#{id}" })
  end

  def increment_funding_counter
    FundingCounter.update(page: page, currency: currency, amount: (begin
                                                                     amount * 100
                                                                   rescue
                                                                     nil
                                                                   end))
  end
end
{'fraction_non_alphanumeric': 0.08175419599350298, 'fraction_numerical': 0.0014437827107020393, 'mean_word_length': 2.3145933014354068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5891100', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1619, 'n_words': 401}