Class: Spree::PaymentMethod::BankSlip

Inherits:
Spree::PaymentMethod show all
Defined in:
app/models/spree/payment_method/bank_slip.rb

Instance Method Summary collapse

Instance Method Details

#authorize(amount, source, *args) ⇒ ActiveMerchant::Billing::Response

Autoriza o pagamento enviando as informacoes ao Iugu

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



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
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
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/spree/payment_method/bank_slip.rb', line 28

def authorize(amount, source, *args)
  gateway_options = args.first
  user = Spree::User.find gateway_options[:customer_id]
  doc_user = user.attributes[Spree::BankSlipConfig[:doc_customer_attr]] rescue ''
  billing_address = gateway_options[:billing_address]

  # Pegando o DDD e o telefone
  if billing_address[:phone].include?('(')
    phone_prefix = billing_address[:phone][1..2]  rescue ''
    phone = billing_address[:phone][5..-1] rescue ''
  else
    phone_prefix = nil
    phone = billing_address[:phone]
  end

  notification_url = Spree::BankSlipConfig[:store_url]
  notification_url << Spree::Core::Engine.routes.url_helpers.bank_slip_status_changed_path(source.id)

  due_date = Date.today + Spree::BankSlipConfig[:days_to_due_date].days

  iugu_params = {
      method: 'bank_slip',
      email: gateway_options[:email],
      notification_url: notification_url,
      due_date: due_date.strftime('%d/%m/%Y'),
      ignore_due_email: Spree::BankSlipConfig[:ignore_due_email],
      items: [],
      payer: {
          cpf_cnpj: doc_user,
          name: billing_address[:name],
          phone_prefix: phone_prefix,
          phone: phone,
          email: gateway_options[:email],
          address: {
              street: billing_address[:address1],
              city: billing_address[:city],
              state: billing_address[:state],
              country: 'Brasil',
              zip_code: billing_address[:zip]
          }
      }
  }

  source.order.line_items.each do |item|
    iugu_params[:items] << {
        description: item.variant.name,
        quantity: item.quantity,
        price_cents: item.single_money.cents
    }
  end

  # Cria um item para o frete a ser cobrado
  if source.order.shipment_total > 0
    iugu_params[:items] << {
        description: Spree.t(:shipment_total),
        quantity: 1,
        price_cents: source.order.display_ship_total.cents
    }
  end

  # Insere os adjustments na fatura
  source.order.all_adjustments.eligible.each do |adj|
    iugu_params[:items] << {
        description: adj.label,
        quantity: 1,
        price_cents: adj.display_amount.cents
    }
  end

  invoice = Iugu::Invoice.create(iugu_params)

  if Spree::BankSlipConfig[:log_requests]
    logger = Logger.new Rails.root.join('log', "#{Rails.env}.log")
    logger.debug "Request Iugu: #{iugu_params}"
    logger.debug "Response Iugu: #{invoice.attributes}" if invoice.respond_to? :attributes
  end

  if invoice.errors
    errors = invoice.errors.collect { |k, v| v.collect { |i| "#{k} #{i}" }.join(', ') }.join(', ')
    ActiveMerchant::Billing::Response.new(false, errors, {}, {})
  else
    source.amount = amount.to_d / 100
    source.invoice_id = invoice.attributes['id']
    source.payment_due = invoice.attributes['due_date']
    source.url = invoice.attributes['secure_url']
    source.pdf = "#{invoice.attributes['secure_url']}.pdf"

    if source.save
      ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_authorized'), {}, authorization: invoice.attributes['id'])
    else
      ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.source_fail'), {}, authorization: invoice.attributes['id'])
    end
  end
rescue
  ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.iugu_fail'), {}, {})
end

#auto_captureObject

Nao existe captura automatica para o metodo do tipo boleto



6
7
8
# File 'app/models/spree/payment_method/bank_slip.rb', line 6

def auto_capture
  false
end

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

Cancela o pagamento

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/spree/payment_method/bank_slip.rb', line 173

def cancel(response_code)
  bank_slip = Spree::BankSlip.find_by invoice_id: response_code
  return ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {}) if bank_slip.nil?

  # Verifica na Iugu se o boleto esta pendente
  # se tiver, faz o cancelamento
  invoice = Iugu::Invoice.fetch response_code
  if invoice.status == 'pending'
    invoice.cancel
  end
  bank_slip.update_attribute(:status, 'canceled')

  ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_voided'), {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {})
end

#capture(amount, response_code, gateway_options) ⇒ ActiveMerchant::Billing::Response

Captura o pagamento modificando o status e salvando a data

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



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

def capture(amount, response_code, gateway_options)
  bank_slip = Spree::BankSlip.find_by invoice_id: response_code
  bank_slip.amount = amount.to_d / 100
  bank_slip.paid_in = Date.today
  bank_slip.status = 'paid'
  bank_slip.save

  ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_captured'), {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.capture_fail'), {}, {})
end

#payment_source_classObject



10
11
12
# File 'app/models/spree/payment_method/bank_slip.rb', line 10

def payment_source_class
  Spree::BankSlip
end

#purchase(amount, source, *args) ⇒ Object

Redireciona para o metodo de autorizacao pois nao ha captura automatica



17
18
19
# File 'app/models/spree/payment_method/bank_slip.rb', line 17

def purchase(amount, source, *args)
  self.authorize(amount, source, args)
end

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

Cancela o pagamento

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/spree/payment_method/bank_slip.rb', line 150

def void(response_code, _gateway_options)
  bank_slip = Spree::BankSlip.find_by invoice_id: response_code
  return ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {}) if bank_slip.nil?

  # Verifica na Iugu se o boleto esta pendente
  # se tiver, faz o cancelamento
  invoice = Iugu::Invoice.fetch response_code
  if invoice.status == 'pending'
    invoice.cancel
  end
  bank_slip.update_attribute(:status, 'canceled')

  ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_voided'), {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {})
end