Sha256: b55b08dcb8744ed5e74f924dd366999e4ba086705b877a93b6c19667439ad6c8

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module CatarsePagarme
  class TransactionBase
    attr_accessor :attributes, :payment,
      :transaction, :user

    def initialize(attributes, payment)
      self.attributes = attributes
      self.payment = payment
      self.user = payment.user
    end

    def change_payment_state
      self.payment.update_attributes(attributes_to_payment)
      self.payment.save!
      delegator.update_fee
      self.payment.payment_notifications.create(contribution_id: self.payment.contribution_id, extra_data: self.transaction.to_json)
      delegator.change_status_by_transaction(self.transaction.status)
    end

    def payment_method
      PaymentType::CREDIT_CARD
    end

    def attributes_to_payment
      {
        payment_method: payment_method,
        gateway_id: self.transaction.id,
        gateway: 'Pagarme',
        gateway_data: self.transaction.to_json,
        installments: default_installments,
        installment_value: (delegator.value_for_installment(self.transaction.installments || 0) / 100.0).to_f
      }
    end

    def default_installments
      (self.transaction.installments || 1)
    end

    def default_installment_value
      if default_installments == 1
        self.payment.value
      else
        (delegator.value_for_installment(self.transaction.installments || 0) / 100.0).to_f
      end
    end

    def delegator
      self.payment.pagarme_delegator
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catarse_pagarme-2.6.5 app/models/catarse_pagarme/transaction_base.rb