Class: PaymentMethod::CieloDebt

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

Instance Method Summary collapse

Instance Method Details

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

Authorizes the payment to Cielo

Parameters:

  • _amount (Integer)

    amount of the payment (not used for debt)

  • source (Spree::CreditCard)

    source of the payment (object which contains the credit card information)

  • _gateway_options (Hash)

    collection of information of the payment (not used for debt)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/spree/payment_method/cielo_debt.rb', line 106

def authorize(_amount, source, _gateway_options)
  transaction = Cielo::Transaction.new
  ret = transaction.verify!(source.gateway_payment_profile_id)

  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

#auto_capture?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/models/spree/payment_method/cielo_debt.rb', line 185

def auto_capture?
  true
end

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

Cancel the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/spree/payment_method/cielo_debt.rb', line 167

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

Authorizes the payment to Cielo

Parameters:

  • _amount (Integer)

    amount of the payment (not used for debt)

  • response_code (String)

    response code of transaction

  • _gateway_options (Hash)

    collection of information of the payment (not used for debt)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



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

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

#create(amount, source) ⇒ Hash

Creates the transaction to Cielo and return the url of authentication

Parameters:

  • amount (Integer)

    amount of the payment

  • source (Spree::CreditCard)

    source of the payment (object which contains the credit card information)

Returns:

  • (Hash)

Author:

  • Isabella Santos



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
# File 'app/models/spree/payment_method/cielo_debt.rb', line 20

def create(amount, source)
  cc_type = source.cc_type
  cc_type = 'mastercard' if cc_type == 'master'

  return_url = Spree::Store.current.url
  return_url << Spree::Core::Engine.routes.url_helpers.cielo_debt_confirm_path(source.id, self.id)

  year = source.year.to_s.rjust(4, '0')
  month = source.month.to_s.rjust(2, '0')

  params = { numero: source.id,
    valor: amount,
    moeda: '986',
    bandeira: cc_type,
    parcelas: '1',
    cartao_numero: source.number,
    :'url-retorno' => return_url,
    cartao_validade: "#{year}#{month}",
    cartao_seguranca: source.verification_value,
    cartao_portador: source.name,
    autorizar: '2',
    produto: 'A',
    capturar: 'true'
  }

  params[:'soft-descriptor'] = Spree::CieloConfig.soft_descriptor if Spree::CieloConfig.soft_descriptor.present?

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

  if ret[:transacao][:status] == '0'
    {url_auth: ret[:transacao][:'url-autenticacao'], tid: ret[:transacao][:tid]}
  else
    {error: Spree.t('cielo.messages.authorize_fail')}
  end
rescue
  if !ret.nil? and ret.has_key? :erro
    if ret[:erro][:codigo] == '001'
      {error: Spree.t('cielo.messages.invalid_message')}
    else
      {error: "Cielo: #{ret[:erro][:codigo]} - #{ret[:erro][:mensagem]}"}
    end
  else
    {error: Spree.t('cielo.messages.create_rescue')}
  end
end

#payment_source_classObject



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

def payment_source_class
  Spree::CreditCard
end

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

Purchases the payment to Cielo

Parameters:

  • _amount (Integer)

    amount of the payment (not used for debt)

  • source (Spree::CreditCard)

    source of the payment (object which contains the credit card information)

  • _gateway_options (Hash)

    collection of information of the payment (not used for debt)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/spree/payment_method/cielo_debt.rb', line 80

def purchase(_amount, source, _gateway_options)
  transaction = Cielo::Transaction.new
  ret = transaction.verify!(source.gateway_payment_profile_id)

  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

Authorizes the payment to Cielo

Parameters:

  • response_code (String)

    response code of transaction

  • _gateway_options (Hash)

    collection of information of the payment (not used for debt)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



152
153
154
155
156
157
158
159
# File 'app/models/spree/payment_method/cielo_debt.rb', line 152

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