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



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
109
110
111
112
# File 'app/models/spree/payment_method/cielo_credit.rb', line 67

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, source, gateway_options
  total_value.delete!('.')
  default_params = {
      parcelas: gateway_options[:portions],
      capturar: 'false'
  }

  if source.gateway_customer_profile_id.present?
    params = { token: CGI.escape(source.gateway_customer_profile_id) }
  elsif Spree::CieloConfig.generate_token
    params = generate_token source

    if params[:token].nil?
      params = {
          cartao_numero: source.number,
          cartao_validade: "#{source.year}#{source.month}",
          cartao_seguranca: source.verification_value,
          cartao_portador: source.name
      }
    end
  else
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{source.year}#{source.month}",
        cartao_seguranca: source.verification_value,
        cartao_portador: source.name
    }
  end
  transaction_params = mount_params(total_value, source, params.merge!(default_params))

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

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

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

Captures the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



120
121
122
123
124
125
126
127
# File 'app/models/spree/payment_method/cielo_credit.rb', line 120

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
# 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, source, gateway_options
  total_value.delete!('.')
  default_params = {
      parcelas: gateway_options[:portions],
      capturar: 'true'
  }

  if source.gateway_customer_profile_id.present?
    params = { token: CGI.escape(source.gateway_customer_profile_id) }
  elsif Spree::CieloConfig.generate_token
    params = generate_token source

    if params[:token].nil?
      params = {
          cartao_numero: source.number,
          cartao_validade: "#{source.year}#{source.month}",
          cartao_seguranca: source.verification_value,
          cartao_portador: source.name
      }
    end
  else
    params = {
        cartao_numero: source.number,
        cartao_validade: "#{source.year}#{source.month}",
        cartao_seguranca: source.verification_value,
        cartao_portador: source.name
    }
  end
  transaction_params = mount_params(total_value, source, params.merge!(default_params))

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

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

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

Voids the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



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

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