spree_iugu_bank_slip 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +9 -0
- data/Guardfile +90 -0
- data/LICENSE +26 -0
- data/README.md +48 -0
- data/Rakefile +21 -0
- data/app/assets/javascripts/spree/backend/spree_iugu_bank_slip.js +2 -0
- data/app/assets/javascripts/spree/frontend/spree_iugu_bank_slip.js +2 -0
- data/app/assets/stylesheets/spree/backend/spree_iugu_bank_slip.css +3 -0
- data/app/assets/stylesheets/spree/frontend/spree_iugu_bank_slip.css +4 -0
- data/app/controllers/spree/admin/bank_slip_settings_controller.rb +27 -0
- data/app/controllers/spree/bank_slips_controller.rb +32 -0
- data/app/models/spree/bank_slip.rb +109 -0
- data/app/models/spree/payment_method/bank_slip.rb +175 -0
- data/app/overrides/spree/admin/shared/sub_menu/_configuration/add_bank_slip_settings_tab.html.erb.deface +2 -0
- data/app/overrides/spree/payments/_payment/add_bank_slip_info_to_details.html.erb.deface +8 -0
- data/app/views/spree/admin/bank_slip_settings/edit.html.erb +45 -0
- data/app/views/spree/admin/payments/source_forms/_bankslip.html.erb +3 -0
- data/app/views/spree/admin/payments/source_views/_bankslip.html.erb +39 -0
- data/app/views/spree/checkout/payment/_bankslip.html.erb +3 -0
- data/bin/rails +7 -0
- data/config/locales/en.yml +29 -0
- data/config/locales/pt-br.yml +29 -0
- data/config/routes.rb +13 -0
- data/db/migrate/20160105163131_create_spree_bank_slips.rb +17 -0
- data/lib/generators/spree_iugu_bank_slip/install/install_generator.rb +31 -0
- data/lib/spree/bank_slip_configuration.rb +11 -0
- data/lib/spree_iugu_bank_slip.rb +4 -0
- data/lib/spree_iugu_bank_slip/engine.rb +33 -0
- data/lib/spree_iugu_bank_slip/factories.rb +32 -0
- data/spec/controllers/spree/bank_slips_controller_spec.rb +39 -0
- data/spec/features/admin/bank_slip_settings_spec.rb +63 -0
- data/spec/fixtures/iugu_responses/create.json +131 -0
- data/spec/fixtures/iugu_responses/create_error.json +136 -0
- data/spec/fixtures/iugu_responses/fetch.json +131 -0
- data/spec/models/spree/bank_slip_spec.rb +56 -0
- data/spec/models/spree/payment_method/bank_slip_spec.rb +90 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/support/capybara_login.rb +13 -0
- data/spree_iugu_bank_slip.gemspec +42 -0
- metadata +402 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55d5c5052105be048ea2404dd76796739cdf62b4
|
4
|
+
data.tar.gz: 0b078e3dacc30f21a4f798cc913090b88a772460
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac874f019fa58beb187d425e249d22cb073050d0d252d9ed1bcf91393e6d1efb5dc585cb3142aa9311e3e8351f67ad5bf43acaf145761be4685d7516ebabc1d3
|
7
|
+
data.tar.gz: 883586b33693148ac27bd9c236cd7cd1ea36824416b3bb1d94d07e269a5fca041f149f85bce63148d4a3796beeb23f9509ce705757b23bcb42ba32b9f98c50a1
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format documentation --color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'spree', github: 'spree/spree', branch: '3-0-stable'
|
4
|
+
# Provides basic authentication functionality for testing parts of your engine
|
5
|
+
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'
|
6
|
+
|
7
|
+
gem 'iugu'
|
8
|
+
|
9
|
+
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec feature)
|
6
|
+
|
7
|
+
## Uncomment to clear the screen before every task
|
8
|
+
# clearing :on
|
9
|
+
|
10
|
+
## Guard internally checks for changes in the Guardfile and exits.
|
11
|
+
## If you want Guard to automatically start up again, run guard in a
|
12
|
+
## shell loop, e.g.:
|
13
|
+
##
|
14
|
+
## $ while bundle exec guard; do echo "Restarting Guard..."; done
|
15
|
+
##
|
16
|
+
## Note: if you are using the `directories` clause above and you are not
|
17
|
+
## watching the project directory ('.'), the you will want to move the Guardfile
|
18
|
+
## to a watched dir and symlink it back, e.g.
|
19
|
+
#
|
20
|
+
# $ mkdir config
|
21
|
+
# $ mv Guardfile config/
|
22
|
+
# $ ln -s config/Guardfile .
|
23
|
+
#
|
24
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
25
|
+
|
26
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
27
|
+
# rspec may be run, below are examples of the most common uses.
|
28
|
+
# * bundler: 'bundle exec rspec'
|
29
|
+
# * bundler binstubs: 'bin/rspec'
|
30
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
31
|
+
# installed the spring binstubs per the docs)
|
32
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
33
|
+
# * 'just' rspec: 'rspec'
|
34
|
+
|
35
|
+
guard :shell do
|
36
|
+
watch(/^db\/migrate\/.*\.rb/) do |m|
|
37
|
+
puts "#{m[0]} Changed"
|
38
|
+
`bundle exec rake test_app`
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
guard :bundler do
|
43
|
+
watch('Gemfile')
|
44
|
+
# Uncomment next line if Gemfile contain `gemspec' command
|
45
|
+
watch(/^.+\.gemspec/)
|
46
|
+
end
|
47
|
+
|
48
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
49
|
+
require "guard/rspec/dsl"
|
50
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
51
|
+
|
52
|
+
# Feel free to open issues for suggestions and improvements
|
53
|
+
|
54
|
+
# RSpec files
|
55
|
+
rspec = dsl.rspec
|
56
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
57
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
58
|
+
watch(rspec.spec_files)
|
59
|
+
|
60
|
+
# Ruby files
|
61
|
+
ruby = dsl.ruby
|
62
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
63
|
+
|
64
|
+
# Rails files
|
65
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
66
|
+
dsl.watch_spec_files_for(rails.app_files)
|
67
|
+
dsl.watch_spec_files_for(rails.views)
|
68
|
+
|
69
|
+
watch(rails.controllers) do |m|
|
70
|
+
[
|
71
|
+
rspec.spec.("routing/#{m[1]}_routing"),
|
72
|
+
rspec.spec.("controllers/#{m[1]}_controller"),
|
73
|
+
rspec.spec.("acceptance/#{m[1]}")
|
74
|
+
]
|
75
|
+
end
|
76
|
+
|
77
|
+
# Rails config changes
|
78
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
79
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
80
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
81
|
+
|
82
|
+
# Capybara features specs
|
83
|
+
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
|
84
|
+
|
85
|
+
# Turnip features and steps
|
86
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
87
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
88
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
89
|
+
end
|
90
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2016 [Zaez Comunicação Digital]
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name Spree nor the names of its contributors may be used to
|
13
|
+
endorse or promote products derived from this software without specific
|
14
|
+
prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
Spree Iugu BankSlip [](https://travis-ci.org/zaeznet/spree_iugu_bank_slip)
|
2
|
+
=================
|
3
|
+
|
4
|
+
Add to Spree Commerce the payment by [Iugu](http://c.iugu.com/) Bank Slip.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Add spree_iugu_bank_slip to your Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'spree_iugu_bank_slip'
|
13
|
+
```
|
14
|
+
|
15
|
+
Bundle your dependencies and run the installation generator:
|
16
|
+
|
17
|
+
```shell
|
18
|
+
bundle
|
19
|
+
bundle exec rails g spree_iugu_bank_slip:install
|
20
|
+
```
|
21
|
+
|
22
|
+
Settings
|
23
|
+
------------
|
24
|
+
|
25
|
+
To configure the settings, you can go to admin/bank_slip_settings page, or in an initialize file you can modify the settings like that:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Spree::BankSlipConfig[:iugu_api_token] = 'ABC1234'
|
29
|
+
```
|
30
|
+
|
31
|
+
Testing
|
32
|
+
-------
|
33
|
+
|
34
|
+
First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using `rake test_app`.
|
35
|
+
|
36
|
+
```shell
|
37
|
+
bundle
|
38
|
+
bundle exec rake
|
39
|
+
```
|
40
|
+
|
41
|
+
When testing your applications integration with this extension you may use it's factories.
|
42
|
+
Simply add this require statement to your spec_helper:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require 'spree_iugu_bank_slip/factories'
|
46
|
+
```
|
47
|
+
|
48
|
+
Copyright (c) 2016 [Zaez Comunicação Digital], released under the New BSD License
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'spree/testing_support/extension_rake'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new
|
8
|
+
|
9
|
+
task :default do
|
10
|
+
if Dir["spec/dummy"].empty?
|
11
|
+
Rake::Task[:test_app].invoke
|
12
|
+
Dir.chdir("../../")
|
13
|
+
end
|
14
|
+
Rake::Task[:spec].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generates a dummy app for testing'
|
18
|
+
task :test_app do
|
19
|
+
ENV['LIB_NAME'] = 'spree_iugu_bank_slip'
|
20
|
+
Rake::Task['extension:test_app'].invoke
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Spree
|
2
|
+
module Admin
|
3
|
+
class BankSlipSettingsController< BaseController
|
4
|
+
|
5
|
+
def edit
|
6
|
+
@config = Spree::BankSlipConfiguration.new
|
7
|
+
@user_attr = Spree::User.new.attribute_names.sort_by { |item| item }
|
8
|
+
end
|
9
|
+
|
10
|
+
def update
|
11
|
+
config = Spree::BankSlipConfiguration.new
|
12
|
+
|
13
|
+
params.each do |name, value|
|
14
|
+
next unless config.has_preference?(name)
|
15
|
+
config[name] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
# Atualiza o token do Iugu
|
19
|
+
Iugu.api_key = params[:iugu_api_token] if params[:iugu_api_token].present?
|
20
|
+
|
21
|
+
flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:bank_slip_settings))
|
22
|
+
redirect_to edit_admin_bank_slip_settings_path
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Spree
|
2
|
+
class BankSlipsController < Spree::StoreController
|
3
|
+
|
4
|
+
before_action :load_object, only: [:show, :status_changed]
|
5
|
+
skip_before_action :set_current_order, only: :status_changed
|
6
|
+
skip_before_action :verify_authenticity_token, only: [:status_changed]
|
7
|
+
|
8
|
+
def show
|
9
|
+
data = open(@slip.pdf)
|
10
|
+
send_data data.read, filename: "boleto_#{@slip.id}.pdf", type: 'application/pdf'
|
11
|
+
end
|
12
|
+
|
13
|
+
def status_changed
|
14
|
+
if params[:data]
|
15
|
+
case params[:data][:status].to_sym
|
16
|
+
# Captura o pagamento
|
17
|
+
when :paid then @slip.payment.capture!
|
18
|
+
# Cancela o pagamento
|
19
|
+
when :canceled, :expired then @slip.payment.void_transaction!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
render nothing: true, status: 200
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def load_object
|
28
|
+
@slip = Spree::BankSlip.find params[:id] || params[:bank_slip_id]
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Spree
|
2
|
+
class BankSlip < ActiveRecord::Base
|
3
|
+
|
4
|
+
has_one :payment, as: :source
|
5
|
+
has_one :order, through: :payment
|
6
|
+
belongs_to :user
|
7
|
+
belongs_to :payment_method
|
8
|
+
|
9
|
+
# Displays what actions can be done according to payments method
|
10
|
+
#
|
11
|
+
# @author Isabella Santos
|
12
|
+
#
|
13
|
+
# @return [Array]
|
14
|
+
#
|
15
|
+
def actions
|
16
|
+
act = []
|
17
|
+
act << 'capture' if can_capture? payment
|
18
|
+
act << 'void' if can_void? payment
|
19
|
+
act
|
20
|
+
end
|
21
|
+
|
22
|
+
# Save the amount of the billet
|
23
|
+
# if amount is a string, convert to a BigDecimal
|
24
|
+
#
|
25
|
+
# copy of Spree::Payment.amount
|
26
|
+
#
|
27
|
+
def amount=(amount)
|
28
|
+
self[:amount] =
|
29
|
+
case amount
|
30
|
+
when String
|
31
|
+
separator = I18n.t('number.currency.format.separator')
|
32
|
+
number = amount.delete("^0-9-#{separator}\.").tr(separator, '.')
|
33
|
+
number.to_d if number.present?
|
34
|
+
end || amount
|
35
|
+
end
|
36
|
+
|
37
|
+
# Determines whether can capture the payments
|
38
|
+
# (only can capture when the state is checkout or pending)
|
39
|
+
#
|
40
|
+
# @author Isabella Santos
|
41
|
+
#
|
42
|
+
# @return [Boolean]
|
43
|
+
#
|
44
|
+
def can_capture?(payment)
|
45
|
+
payment.pending? || payment.checkout?
|
46
|
+
end
|
47
|
+
|
48
|
+
# Determines whether can void the payments
|
49
|
+
# (only can void when the state is different of void, failure or invalid)
|
50
|
+
#
|
51
|
+
# @author Isabella Santos
|
52
|
+
#
|
53
|
+
# @return [Boolean]
|
54
|
+
#
|
55
|
+
def can_void?(payment)
|
56
|
+
!%w(void failure invalid canceled).include?(payment.state)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Defines the currency of the billet
|
60
|
+
# based in te currency of the order
|
61
|
+
#
|
62
|
+
# copy of Spree::Payment.currency
|
63
|
+
#
|
64
|
+
def currency
|
65
|
+
order.currency
|
66
|
+
end
|
67
|
+
|
68
|
+
# Return the amount converted to Money
|
69
|
+
# according to currency
|
70
|
+
#
|
71
|
+
# copy of Spree::Payment.money
|
72
|
+
#
|
73
|
+
def money
|
74
|
+
Spree::Money.new(amount, { currency: currency })
|
75
|
+
end
|
76
|
+
alias display_amount money
|
77
|
+
|
78
|
+
# Returns if billet is paid
|
79
|
+
#
|
80
|
+
# @author Isabella Santos
|
81
|
+
#
|
82
|
+
# @return [Boolean]
|
83
|
+
#
|
84
|
+
def paid?
|
85
|
+
status == 'paid'
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns if billet is pending
|
89
|
+
#
|
90
|
+
# @author Isabella Santos
|
91
|
+
#
|
92
|
+
# @return [Boolean]
|
93
|
+
#
|
94
|
+
def pending?
|
95
|
+
status == 'pending'
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns if slip is canceled
|
99
|
+
#
|
100
|
+
# @author Isabella Santos
|
101
|
+
#
|
102
|
+
# @return [Boolean]
|
103
|
+
#
|
104
|
+
def canceled?
|
105
|
+
status == 'canceled'
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
module Spree
|
2
|
+
class PaymentMethod
|
3
|
+
class PaymentMethod::BankSlip < PaymentMethod
|
4
|
+
|
5
|
+
# Nao existe captura automatica para o metodo do tipo boleto
|
6
|
+
def auto_capture
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def payment_source_class
|
11
|
+
Spree::BankSlip
|
12
|
+
end
|
13
|
+
|
14
|
+
# Redireciona para o metodo de autorizacao
|
15
|
+
# pois nao ha captura automatica
|
16
|
+
#
|
17
|
+
def purchase(amount, source, *args)
|
18
|
+
self.authorize(amount, source, args)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Autoriza o pagamento
|
22
|
+
# enviando as informacoes ao Iugu
|
23
|
+
#
|
24
|
+
# @author Isabella Santos
|
25
|
+
#
|
26
|
+
# @return [ActiveMerchant::Billing::Response]
|
27
|
+
#
|
28
|
+
def authorize(amount, source, *args)
|
29
|
+
gateway_options = args.first
|
30
|
+
user = Spree::User.find gateway_options[:customer_id]
|
31
|
+
doc_user = user.attributes[Spree::BankSlipConfig[:doc_customer_attr]] rescue ''
|
32
|
+
billing_address = gateway_options[:billing_address]
|
33
|
+
|
34
|
+
# Pegando o DDD e o telefone
|
35
|
+
if billing_address[:phone].include?('(')
|
36
|
+
phone_prefix = billing_address[:phone][1..2] rescue ''
|
37
|
+
phone = billing_address[:phone][5..-1] rescue ''
|
38
|
+
else
|
39
|
+
phone_prefix = nil
|
40
|
+
phone = billing_address[:phone]
|
41
|
+
end
|
42
|
+
|
43
|
+
notification_url = Spree::Store.current.url
|
44
|
+
notification_url << Spree::Core::Engine.routes.url_helpers.bank_slip_status_changed_path(source.id)
|
45
|
+
|
46
|
+
due_date = Date.today + Spree::BankSlipConfig[:days_to_due_date].days
|
47
|
+
|
48
|
+
iugu_params = {
|
49
|
+
method: 'bank_slip',
|
50
|
+
email: gateway_options[:email],
|
51
|
+
notification_url: notification_url,
|
52
|
+
due_date: due_date.strftime('%d/%m/%Y'),
|
53
|
+
ignore_due_email: Spree::BankSlipConfig[:ignore_due_email],
|
54
|
+
items: [],
|
55
|
+
payer: {
|
56
|
+
cpf_cnpj: doc_user,
|
57
|
+
name: billing_address[:name],
|
58
|
+
phone_prefix: phone_prefix,
|
59
|
+
phone: phone,
|
60
|
+
email: gateway_options[:email],
|
61
|
+
address: {
|
62
|
+
street: billing_address[:address1],
|
63
|
+
city: billing_address[:city],
|
64
|
+
state: billing_address[:state],
|
65
|
+
country: 'Brasil',
|
66
|
+
zip_code: billing_address[:zip]
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
source.order.line_items.each do |item|
|
72
|
+
iugu_params[:items] << {
|
73
|
+
description: item.variant.name,
|
74
|
+
quantity: item.quantity,
|
75
|
+
price_cents: item.single_money.cents
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Cria um item para o frete a ser cobrado
|
80
|
+
if source.order.shipment_total > 0
|
81
|
+
iugu_params[:items] << {
|
82
|
+
description: Spree.t(:shipment_total),
|
83
|
+
quantity: 1,
|
84
|
+
price_cents: source.order.display_ship_total.cents
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
# Insere os adjustments na fatura
|
89
|
+
source.order.all_adjustments.eligible.each do |adj|
|
90
|
+
iugu_params[:items] << {
|
91
|
+
description: adj.label,
|
92
|
+
quantity: 1,
|
93
|
+
price_cents: adj.display_amount.cents
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
invoice = Iugu::Invoice.create(iugu_params)
|
98
|
+
|
99
|
+
if Spree::BankSlipConfig[:log_requests]
|
100
|
+
logger = Logger.new Rails.root.join('log', "#{Rails.env}.log")
|
101
|
+
logger.debug "Request Iugu: #{iugu_params}"
|
102
|
+
logger.debug "Response Iugu: #{invoice.attributes}" if invoice.respond_to? :attributes
|
103
|
+
end
|
104
|
+
|
105
|
+
if invoice.errors
|
106
|
+
errors = invoice.errors.collect { |k, v| v.collect { |i| "#{k} #{i}" }.join(', ') }.join(', ')
|
107
|
+
ActiveMerchant::Billing::Response.new(false, errors, {}, {})
|
108
|
+
else
|
109
|
+
source.amount = amount.to_d / 100
|
110
|
+
source.invoice_id = invoice.attributes['id']
|
111
|
+
source.payment_due = invoice.attributes['due_date']
|
112
|
+
source.url = invoice.attributes['secure_url']
|
113
|
+
source.pdf = "#{invoice.attributes['secure_url']}.pdf"
|
114
|
+
|
115
|
+
if source.save
|
116
|
+
ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_authorized'), {}, authorization: invoice.attributes['id'])
|
117
|
+
else
|
118
|
+
ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.source_fail'), {}, authorization: invoice.attributes['id'])
|
119
|
+
end
|
120
|
+
end
|
121
|
+
rescue
|
122
|
+
ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.iugu_fail'), {}, {})
|
123
|
+
end
|
124
|
+
|
125
|
+
# Captura o pagamento
|
126
|
+
# modificando o status e salvando a data
|
127
|
+
#
|
128
|
+
# @author Isabella Santos
|
129
|
+
#
|
130
|
+
# @return [ActiveMerchant::Billing::Response]
|
131
|
+
#
|
132
|
+
def capture(amount, response_code, gateway_options)
|
133
|
+
bank_slip = Spree::BankSlip.find_by invoice_id: response_code
|
134
|
+
bank_slip.amount = amount.to_d / 100
|
135
|
+
bank_slip.paid_in = Date.today
|
136
|
+
bank_slip.status = 'paid'
|
137
|
+
bank_slip.save
|
138
|
+
|
139
|
+
ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_captured'), {}, authorization: response_code)
|
140
|
+
rescue
|
141
|
+
ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.capture_fail'), {}, {})
|
142
|
+
end
|
143
|
+
|
144
|
+
# Cancela o pagamento
|
145
|
+
#
|
146
|
+
# @author Isabella Santos
|
147
|
+
#
|
148
|
+
# @return [ActiveMerchant::Billing::Response]
|
149
|
+
#
|
150
|
+
def void(response_code, gateway_options)
|
151
|
+
bank_slip = Spree::BankSlip.find_by invoice_id: response_code
|
152
|
+
bank_slip.update_attribute(:status, 'canceled')
|
153
|
+
|
154
|
+
ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_voided'), {}, authorization: response_code)
|
155
|
+
rescue
|
156
|
+
ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {})
|
157
|
+
end
|
158
|
+
|
159
|
+
# Cancela o pagamento
|
160
|
+
#
|
161
|
+
# @author Isabella Santos
|
162
|
+
#
|
163
|
+
# @return [ActiveMerchant::Billing::Response]
|
164
|
+
#
|
165
|
+
def cancel(response_code)
|
166
|
+
bank_slip = Spree::BankSlip.find_by invoice_id: response_code
|
167
|
+
bank_slip.update_attribute(:status, 'canceled')
|
168
|
+
|
169
|
+
ActiveMerchant::Billing::Response.new(true, Spree.t('bank_slip.messages.successfully_voided'), {}, authorization: response_code)
|
170
|
+
rescue
|
171
|
+
ActiveMerchant::Billing::Response.new(false, Spree.t('bank_slip.messages.void_fail'), {}, {})
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|