Class: PaymentMethod::CieloCredit

Inherits:
PaymentMethod
  • Object
show all
Defined in:
app/models/spree/payment_method/cielo_credit.rb

Instance Method Summary collapse

Instance Method Details

#authorize(_amount, source, gateway_options) ⇒ ActiveMerchant::Billing::Response

Authorizes the payment to Cielo

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/spree/payment_method/cielo_credit.rb', line 74

def authorize(_amount, source, gateway_options)
  if gateway_options[:portions].nil?
    return ActiveMerchant::Billing::Response.new(false, Spree.t('cielo.messages.invalid_portions'), {}, {})
  end

  order_number = gateway_options[:order_id].split('-').first
  order = Spree::Order.friendly.find order_number
  portion_value = Spree::CieloConfig.calculate_portion_value order, gateway_options[:portions]
  total_value = sprintf('%0.2f', portion_value * gateway_options[:portions])
  total_value.delete!('.')

  default_params = {
      parcelas: gateway_options[:portions],
      capturar: 'false'
  }

  if source.gateway_customer_profile_id?
    params = { token: CGI.escape(source.gateway_customer_profile_id) }
  else
    year = source.year.to_s.rjust(4, '0')
    month = source.month.to_s.rjust(2, '0')
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{year}#{month}",
        cartao_seguranca: source.verification_value,
        cartao_portador: source.name
    }

    if Spree::CieloConfig.generate_token
      params[:'gerar-token'] = 'true'
    end
  end

  transaction_params = mount_params(total_value, source, params.merge!(default_params))

  transaction = Cielo::Transaction.new
  response = transaction.create!(transaction_params, :store)

  if response[:transacao][:status] == '4'
    if Spree::CieloConfig.generate_token
      storage_token source, response[:transacao]
    end

    # Salva o valor do pagamento (e pedido) com o juros (se houver)
    update_payment_amount gateway_options

    ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.authorize_success'), {}, authorization: response[:transacao][:tid])
  else
    ActiveMerchant::Billing::Response.new(false, Spree.t('cielo.messages.authorize_fail'), {}, authorization: response[:transacao][:tid])
  end
rescue
  verify_error 'authorize', response
end

#cancel(response_code) ⇒ ActiveMerchant::Billing::Response

Cancel the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/spree/payment_method/cielo_credit.rb', line 164

def cancel(response_code)
  transaction = Cielo::Transaction.new
  response = transaction.verify!(response_code)

  if response[:transacao][:status] == '4' or response[:transacao][:status] == '6'
    response_cancel = transaction.cancel!(response_code)
    if response_cancel[:transacao].present?
      ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.cancel_success'), {}, authorization: response_cancel[:transacao][:tid])
    else
      verify_error 'cancel', response_cancel
    end
  else
    ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.cancel_success'), {}, authorization: response[:transacao][:tid])
  end
rescue
  verify_error 'cancel', response
end

#capture(_amount, response_code, _gateway_options) ⇒ ActiveMerchant::Billing::Response

Captures the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



134
135
136
137
138
139
140
141
# File 'app/models/spree/payment_method/cielo_credit.rb', line 134

def capture(_amount, response_code, _gateway_options)
  transaction = Cielo::Transaction.new
  ret = transaction.catch!(response_code)

  ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.capture_success'), {}, authorization: ret[:transacao][:tid])
rescue
  verify_error 'capture', ret
end

#payment_source_classObject



4
5
6
# File 'app/models/spree/payment_method/cielo_credit.rb', line 4

def payment_source_class
  Spree::CreditCard
end

#purchase(_amount, source, gateway_options) ⇒ ActiveMerchant::Billing::Response

Purchases the payment to Cielo

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
64
65
66
# File 'app/models/spree/payment_method/cielo_credit.rb', line 14

def purchase(_amount, source, gateway_options)
  if gateway_options[:portions].nil?
    return ActiveMerchant::Billing::Response.new(false, Spree.t('cielo.messages.invalid_portions'), {}, {})
  end

  order_number = gateway_options[:order_id].split('-').first
  order = Spree::Order.friendly.find order_number
  portion_value = Spree::CieloConfig.calculate_portion_value order, gateway_options[:portions]
  total_value = sprintf('%0.2f', portion_value * gateway_options[:portions])
  total_value.delete!('.')

  default_params = {
      parcelas: gateway_options[:portions],
      capturar: 'true'
  }

  if source.gateway_customer_profile_id?
    params = { token: CGI.escape(source.gateway_customer_profile_id) }
  else
    year = source.year.to_s.rjust(4, '0')
    month = source.month.to_s.rjust(2, '0')
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{year}#{month}",
        cartao_seguranca: source.verification_value,
        cartao_portador: source.name
    }

    if Spree::CieloConfig.generate_token
      params[:'gerar-token'] = 'true'
    end
  end

  transaction_params = mount_params(total_value, source, params.merge!(default_params))

  transaction = Cielo::Transaction.new
  response = transaction.create!(transaction_params, :store)

  if response[:transacao][:status] == '6'
    if Spree::CieloConfig.generate_token
      storage_token source, response[:transacao]
    end

    # Salva o valor do pagamento (e pedido) com o juros (se houver)
    update_payment_amount gateway_options

    ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.purchase_success'), {}, authorization: response[:transacao][:tid])
  else
    ActiveMerchant::Billing::Response.new(false, Spree.t('cielo.messages.purchase_fail'), {}, authorization: response[:transacao][:tid])
  end
rescue
  verify_error 'purchase', response
end

#void(response_code, _gateway_options) ⇒ ActiveMerchant::Billing::Response

Voids the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



149
150
151
152
153
154
155
156
# File 'app/models/spree/payment_method/cielo_credit.rb', line 149

def void(response_code, _gateway_options)
  transaction = Cielo::Transaction.new
  ret = transaction.cancel!(response_code)

  ActiveMerchant::Billing::Response.new(true, Spree.t('cielo.messages.void_success'), {}, authorization: ret[:transacao][:tid])
rescue
  verify_error 'void', ret
end