Class: CatarsePagarme::PaymentDelegator

Inherits:
Object
  • Object
show all
Includes:
FeeCalculatorConcern
Defined in:
app/models/catarse_pagarme/payment_delegator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payment) ⇒ PaymentDelegator

Returns a new instance of PaymentDelegator.



6
7
8
9
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 6

def initialize(payment)
  configure_pagarme
  self.payment = payment
end

Instance Attribute Details

#paymentObject

Returns the value of attribute payment.



3
4
5
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 3

def payment
  @payment
end

#transactionObject

Returns the value of attribute transaction.



3
4
5
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 3

def transaction
  @transaction
end

Instance Method Details

#change_status_by_transaction(transactin_status) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 11

def change_status_by_transaction(transactin_status)
  case transactin_status
  when 'paid', 'authorized' then
    self.payment.pay unless self.payment.paid?
  when 'pending_refund' then
    self.payment.request_refund unless self.payment.pending_refund?
  when 'refunded' then
    self.payment.refund unless self.payment.refunded?
  when 'refused' then
    self.payment.refuse unless self.payment.refused?
  when 'chargedback' then
    self.payment.chargeback unless self.payment.chargeback?
  end
end

#fill_acquirer_dataObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 33

def fill_acquirer_data
  if payment.gateway_data.nil? || payment.gateway_data["acquirer_name"].nil? || payment.gateway_data["acquirer_tid"].nil?
    data = payment.gateway_data || {}
    payment.gateway_data = data.merge({
      acquirer_name: transaction.acquirer_name,
      acquirer_tid: transaction.tid,
      card_brand: transaction.try(:card_brand)
    })
    payment.save
  end
end

#get_installment(installment_number) ⇒ Object



83
84
85
86
87
88
89
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 83

def get_installment(installment_number)
  installment = get_installments['installments'].select do |_installment|
    !_installment[installment_number.to_s].nil?
  end

  installment[installment_number.to_s]
end

#get_installmentsObject



91
92
93
94
95
96
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 91

def get_installments
  @installments ||= PagarMe::Transaction.calculate_installments({
    amount: self.value_for_transaction,
    interest_rate: CatarsePagarme.configuration.interest_rate
  })
end

#refundObject



45
46
47
48
49
50
51
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 45

def refund
  if payment.is_credit_card?
    transaction.refund
  else
    transaction.refund()
  end
end

#transfer_fundsObject

Transfer payment amount to payer bank account via transfers API



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 99

def transfer_funds
  raise "payment must be paid" if !payment.paid?

   = PagarMe::BankAccount.new(.delete(:bank_account))
  .create
  raise "unable to create an bank account" unless .id.present?

  transfer = PagarMe::Transfer.new({
    bank_account_id: .id,
    amount: value_for_transaction
  })
  transfer.create
  raise "unable to create a transfer" unless transfer.id.present?

  #avoid sending notification
  payment.update_attributes(state: 'pending_refund')
  payment.payment_transfers.create!({
    user: payment.user,
    transfer_id: transfer.id,
    transfer_data: transfer.to_json
  })
end

#update_transactionObject



26
27
28
29
30
31
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 26

def update_transaction
  fill_acquirer_data
  payment.installment_value = (value_for_installment / 100.0).to_f
  payment.gateway_fee = get_fee
  payment.save!
end

#value_for_installment(installment = transaction.installments || 0) ⇒ Object



67
68
69
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 67

def value_for_installment(installment = transaction.installments || 0)
  get_installment(installment).try(:[], "installment_amount")
end

#value_for_transactionObject



53
54
55
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 53

def value_for_transaction
  (self.payment.value * 100).to_i
end

#value_with_installment_tax(installment) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'app/models/catarse_pagarme/payment_delegator.rb', line 57

def value_with_installment_tax(installment)
  current_installment = get_installment(installment)

  if current_installment.present?
    current_installment['amount']
  else
    value_for_transaction
  end
end