Class: CatarsePagarme::CreditCardTransaction

Inherits:
TransactionBase show all
Defined in:
app/models/catarse_pagarme/credit_card_transaction.rb

Instance Attribute Summary

Attributes inherited from TransactionBase

#attributes, #payment, #transaction, #user

Instance Method Summary collapse

Methods inherited from TransactionBase

#attributes_to_payment, #change_payment_state, #default_installments, #delegator, #initialize, #payment_method

Constructor Details

This class inherits a constructor from CatarsePagarme::TransactionBase

Instance Method Details

#antifraud_wrapperObject



72
73
74
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 72

def antifraud_wrapper
  @antifraud_wrapper ||= AntifraudOrderWrapper.new(self.attributes, self.transaction)
end

#authorize!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 28

def authorize!
  save_card = self.attributes.delete(:save_card)

  self.transaction = PagarMe::Transaction.new(
    {
      amount: self.attributes[:amount],
      capture: false,
      async: false,
      postback_url: self.attributes[:postback_url],
      installments: self.attributes[:installments],
      soft_descriptor: self.attributes[:soft_descriptor],
      metadata: self.attributes[:metadata]
    }.merge(credit_card_identifier)
  )

  unless payment.update(gateway: 'Pagarme', payment_method: payment_method)
    raise ::PagarMe::PagarMeError.new(payment.errors.messages.values.flatten.to_sentence)
  end

  self.transaction.charge

  change_payment_state

  if self.transaction.status == 'refused'
    antifraud_wrapper.send(analyze: false)
    raise ::PagarMe::PagarMeError.new(I18n.t('projects.contributions.edit.transaction_error'))
  end

  save_user_credit_card if save_card
end

#credit_card_identifierObject



76
77
78
79
80
81
82
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 76

def credit_card_identifier
  if self.attributes[:card_hash].present?
    { card_hash: self.attributes[:card_hash] }
  else
    { card_id: self.attributes[:card_id] }
  end
end

#process!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 3

def process!
  authorize!
  if self.transaction.status == 'authorized'
    if was_credit_card_used_before?
      self.transaction.capture
    else
      antifraud_outcome = process_antifraud

      self.payment.create_antifraud_analysis!(cost: CatarsePagarme.configuration.antifraud_tax)

      if antifraud_outcome.recommendation == :APPROVE
        self.transaction.capture
      elsif antifraud_outcome.recommendation == :DECLINE
        self.transaction.refund
      elsif antifraud_outcome.recommendation == :REVIEW
        self.payment.try(:notify_about_pending_review)
      end
    end
  end

  change_payment_state

  self.transaction
end

#process_antifraudObject



63
64
65
66
67
68
69
70
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 63

def process_antifraud
  begin
    antifraud_wrapper.send(analyze: true)
  rescue RuntimeError => e
    ::Raven.capture_exception(e, level: 'fatal')
    OpenStruct.new(recommendation: :DECLINE)
  end
end

#save_user_credit_cardObject



84
85
86
87
88
89
90
91
92
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 84

def save_user_credit_card
  card = self.transaction.card

  credit_card = self.user.credit_cards.find_or_initialize_by(card_key: card.id)
  credit_card.last_digits = card.last_digits
  credit_card.card_brand = card.brand

  credit_card.save!
end

#was_credit_card_used_before?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/catarse_pagarme/credit_card_transaction.rb', line 59

def was_credit_card_used_before?
  PaymentEngines.was_credit_card_used_before?(self.transaction.card.id, self.attributes.dig(:customer, :id))
end