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



65
66
67
68
69
70
71
72
73
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
# File 'app/models/spree/payment_method/cielo_credit.rb', line 65

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

  total_value = update_payment_amount amount, gateway_options
  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
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{source.year}#{source.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

    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

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

Captures the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



116
117
118
119
120
121
122
123
# File 'app/models/spree/payment_method/cielo_credit.rb', line 116

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
# 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

  total_value = update_payment_amount amount, gateway_options
  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
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{source.year}#{source.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

    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



131
132
133
134
135
136
137
138
# File 'app/models/spree/payment_method/cielo_credit.rb', line 131

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