spree_zaez_komerci 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +11 -0
  5. data/Gemfile +9 -0
  6. data/Guardfile +90 -0
  7. data/LICENSE +26 -0
  8. data/README.md +52 -0
  9. data/Rakefile +21 -0
  10. data/app/assets/images/rede-credit-cards/rede-dinersclub.png +0 -0
  11. data/app/assets/images/rede-credit-cards/rede-hipercard.png +0 -0
  12. data/app/assets/images/rede-credit-cards/rede-jcb.png +0 -0
  13. data/app/assets/images/rede-credit-cards/rede-mastercard.png +0 -0
  14. data/app/assets/images/rede-credit-cards/rede-visa.png +0 -0
  15. data/app/assets/javascripts/spree/backend/spree_zaez_komerci.js +1 -0
  16. data/app/assets/javascripts/spree/frontend/app/komerci_credit_card.js.coffee +26 -0
  17. data/app/assets/javascripts/spree/frontend/spree_zaez_komerci.js +1 -0
  18. data/app/assets/stylesheets/spree/frontend/spree_zaez_komerci.css +7 -0
  19. data/app/controllers/spree/admin/komerci_settings_controller.rb +20 -0
  20. data/app/models/spree/credit_card_decorator.rb +7 -0
  21. data/app/models/spree/komerci_transaction.rb +8 -0
  22. data/app/models/spree/payment/gateway_options_decorator.rb +14 -0
  23. data/app/models/spree/payment_method/komerci.rb +250 -0
  24. data/app/overrides/spree/admin/shared/sub_menu/_configuration/add_komerci_settings_to_configuration_tab.html.erb.deface +2 -0
  25. data/app/views/spree/admin/komerci_settings/edit.html.erb +67 -0
  26. data/app/views/spree/admin/payments/source_forms/_komerci.html.erb +62 -0
  27. data/app/views/spree/admin/payments/source_views/_komerci.html.erb +33 -0
  28. data/app/views/spree/checkout/payment/_komerci.html.erb +62 -0
  29. data/bin/rails +7 -0
  30. data/config/locales/en.yml +31 -0
  31. data/config/locales/pt-br.yml +32 -0
  32. data/config/routes.rb +5 -0
  33. data/db/migrate/20160630132716_add_portions_to_spree_payments.rb +5 -0
  34. data/db/migrate/20160630161016_create_spree_komerci_transaction.rb +14 -0
  35. data/db/migrate/20160701184408_add_payment_id_to_spree_komerci_transactions.rb +5 -0
  36. data/db/migrate/20160704120859_add_total_to_spree_komerci_transactions.rb +5 -0
  37. data/lib/generators/spree_zaez_komerci/install/install_generator.rb +30 -0
  38. data/lib/spree/komerci_configuration.rb +190 -0
  39. data/lib/spree_zaez_komerci/engine.rb +30 -0
  40. data/lib/spree_zaez_komerci/factories.rb +30 -0
  41. data/lib/spree_zaez_komerci.rb +5 -0
  42. data/spec/features/admin/komerci_actions_spec.rb +80 -0
  43. data/spec/features/admin/komerci_settings_spec.rb +77 -0
  44. data/spec/features/checkout_with_komerci_spec.rb +75 -0
  45. data/spec/fixtures/authorize_error.txt +14 -0
  46. data/spec/fixtures/authorize_success.txt +14 -0
  47. data/spec/fixtures/conf_authorize_error.txt +7 -0
  48. data/spec/fixtures/conf_authorize_success.txt +7 -0
  49. data/spec/fixtures/error.txt +1 -0
  50. data/spec/fixtures/void_error.txt +7 -0
  51. data/spec/fixtures/void_success.txt +7 -0
  52. data/spec/lib/spree/komerci_configuration_spec.rb +82 -0
  53. data/spec/models/spree/payment_method/komerci_spec.rb +206 -0
  54. data/spec/spec_helper.rb +99 -0
  55. data/spec/support/capybara_login.rb +11 -0
  56. data/spec/support/shared_contexts/checkout_setup.rb +9 -0
  57. data/spree_zaez_komerci.gemspec +42 -0
  58. data/vendor/assets/javascripts/jquery.payment.js +612 -0
  59. metadata +423 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18b494e5288460ddac18898245a151588640da7f
4
+ data.tar.gz: 660ac6bed659eba6b196650a8c1ce6224059a305
5
+ SHA512:
6
+ metadata.gz: bf033ea0c92515c1e54a75777375a4ad0879f86b3541e9e7527f4a40872ed53096a9800928e847f463f4f25490192cb84c8d252b1d5c31654ac5fc475b0f3bff
7
+ data.tar.gz: 2b848e7d4cea532345ecf5b858ccb2586356b87ee11c0a1e2200be826f2eb5191aa43a68bba868409330f78cc95963ca0318afc55502cab2ff2ee144e1e76f92
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ .sass-cache
8
+ coverage
9
+ Gemfile.lock
10
+ tmp
11
+ nbproject
12
+ pkg
13
+ *.swp
14
+ spec/dummy
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ before_script:
2
+ - sh -e /etc/init.d/xvfb start
3
+ - export DISPLAY=:99.0
4
+ bundler_args: --quiet
5
+ script:
6
+ - bundle exec rake test_app
7
+ - bundle exec rspec
8
+ language: ruby
9
+ rvm:
10
+ - 2.1.5
11
+ - 2.2.3
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 'sprockets-rails', '2.3.3'
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 Inovaçã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,52 @@
1
+ Spree Zaez Komerci
2
+ ==============
3
+
4
+ This gem implements some funcionalities of Komerci (a product of [Rede](https://userede.com.br)) to Spree Commerce.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add spree_zaez_komerci to your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'spree_zaez_komerci'
13
+ ```
14
+
15
+ Bundle your dependencies and run the installation generator:
16
+
17
+ ```shell
18
+ bundle
19
+ bundle exec rails g spree_zaez_komerci:install
20
+ ```
21
+
22
+ Settings
23
+ -------
24
+
25
+ To configure the Komerci settings, go to /admin/komerci_settings/edit (or access by the menu in configurations > Komerci Settings).
26
+ To set the settings through a config file, you can assign values to the settings like so:
27
+
28
+ ```ruby
29
+ Spree::KomerciConfig[:afiliation_key] = '12345'
30
+ ```
31
+
32
+ See all settings in /lib/spree/komerci_configuration.rb.
33
+
34
+
35
+ Testing
36
+ -------
37
+
38
+ 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`.
39
+
40
+ ```shell
41
+ bundle
42
+ bundle exec rake
43
+ ```
44
+
45
+ When testing your applications integration with this extension you may use it's factories.
46
+ Simply add this require statement to your spec_helper:
47
+
48
+ ```ruby
49
+ require 'spree_zaez_komerci/factories'
50
+ ```
51
+
52
+ Copyright (c) 2016 Zaez Inovaçã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_zaez_komerci'
20
+ Rake::Task['extension:test_app'].invoke
21
+ end
@@ -0,0 +1 @@
1
+ //= require ../frontend/app/komerci_credit_card
@@ -0,0 +1,26 @@
1
+ #= require_self
2
+ class window.KomerciCreditCard
3
+
4
+ afterConstructor: ->
5
+
6
+ beforeConstructor: ->
7
+
8
+ constructor: (@available_cc, defaultExecution = true) ->
9
+ do @beforeConstructor
10
+ do @defaultExecution if defaultExecution
11
+ do @afterConstructor
12
+
13
+ defaultExecution: ->
14
+ $('.komerci-cc').on 'input', @setCreditCard
15
+
16
+ # Verifica se o cartao que esta sendo digitado esta disponivel
17
+ # para a loja
18
+ setCreditCard: =>
19
+ $('#komerci_unrecognized').hide()
20
+ if $('.komerci-cc').val().length > 10
21
+ type = $.payment.cardType($('.komerci-cc').val())
22
+ if type == null or !@available_cc.hasOwnProperty(type)
23
+ $('#komerci_unrecognized').show()
24
+ else
25
+ $('#komerci_cc_type').html("<img src='#{@available_cc[type]}' class='cc-icon pull-right' />")
26
+ $('#komerci_unrecognized').hide()
@@ -0,0 +1 @@
1
+ //= require ./app/komerci_credit_card
@@ -0,0 +1,7 @@
1
+ .komerci-info {
2
+ margin-top: 15px;
3
+ }
4
+
5
+ .cc-icon {
6
+ height: 35px;
7
+ }
@@ -0,0 +1,20 @@
1
+ class Spree::Admin::KomerciSettingsController < Spree::Admin::BaseController
2
+
3
+ def edit
4
+ @config = Spree::KomerciConfiguration.new
5
+ end
6
+
7
+ def update
8
+ config = Spree::KomerciConfiguration.new
9
+
10
+ params.each do |name, value|
11
+ next if !config.has_preference?(name)
12
+ config[name] = value
13
+ end
14
+
15
+ config.test_mode = false unless params.include?(:test_mode)
16
+
17
+ flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:komerci_settings))
18
+ redirect_to edit_admin_komerci_settings_path
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Spree
2
+ CreditCard.class_eval do
3
+
4
+ has_one :komerci_transaction, class_name: 'Spree::KomerciTransaction'
5
+
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Spree
2
+ class KomerciTransaction < Base
3
+
4
+ belongs_to :credit_card, class_name: 'Spree::CreditCard'
5
+ belongs_to :payment, class_name: 'Spree::Payment'
6
+
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module Spree
2
+ Payment::GatewayOptions.class_eval do
3
+ def portions
4
+ @payment.portions
5
+ end
6
+
7
+ alias_method :hash_methods_old, :hash_methods
8
+ def hash_methods
9
+ ret = hash_methods_old
10
+ ret << :portions
11
+ ret
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,250 @@
1
+ module Spree
2
+ class PaymentMethod::Komerci < PaymentMethod
3
+
4
+ def payment_source_class
5
+ Spree::CreditCard
6
+ end
7
+
8
+ # Purchases the payment to Komerci
9
+ #
10
+ # @author Isabella Santos
11
+ #
12
+ # @return [ActiveMerchant::Billing::Response]
13
+ #
14
+ def purchase(_amount, source, gateway_options)
15
+ if gateway_options[:portions].nil?
16
+ return ActiveMerchant::Billing::Response.new(false, Spree.t('komerci.messages.invalid_portions'), {}, {})
17
+ end
18
+
19
+ if gateway_options[:portions] == 1
20
+ transaction = '04'
21
+ portions = '00'
22
+ else
23
+ transaction = Spree::KomerciConfig[:portions_type]
24
+ portions = gateway_options[:portions]
25
+ end
26
+
27
+ params = prepare_authorize_params source, gateway_options
28
+ params[:transacao] = transaction
29
+ params[:parcelas] = portions
30
+
31
+ response = Spree::KomerciConfig.authorize params
32
+
33
+ verify_authorize_response(source, gateway_options, response, 'purchase')
34
+ end
35
+
36
+ # Authorizes the payment to Cielo
37
+ #
38
+ # @author Isabella Santos
39
+ #
40
+ # @return [ActiveMerchant::Billing::Response]
41
+ #
42
+ def authorize(_amount, source, gateway_options)
43
+ if gateway_options[:portions].nil?
44
+ return ActiveMerchant::Billing::Response.new(false, Spree.t('komerci.messages.invalid_portions'), {}, {})
45
+ end
46
+
47
+ params = prepare_authorize_params source, gateway_options
48
+ params[:transacao] = '73'
49
+
50
+ if gateway_options[:portions] == 1
51
+ params[:parcelas] = '00'
52
+ else
53
+ params[:parcelas] = gateway_options[:portions]
54
+ end
55
+
56
+ response = Spree::KomerciConfig.authorize params
57
+
58
+ verify_authorize_response(source, gateway_options, response, 'authorize')
59
+ end
60
+
61
+ # Captures the payment
62
+ #
63
+ # @author Isabella Santos
64
+ #
65
+ # @return [ActiveMerchant::Billing::Response]
66
+ #
67
+ def capture(amount, response_code, _gateway_options)
68
+ total = sprintf('%0.2f', amount / 100)
69
+ transaction = Spree::KomerciTransaction.find_by order_number: response_code
70
+
71
+ if transaction.payment.portions == 1
72
+ transaction_option = '04'
73
+ portions = '00'
74
+ else
75
+ transaction_option = Spree::KomerciConfig[:portions_type]
76
+ portions = transaction.payment.portions
77
+ end
78
+
79
+ params = {
80
+ filiacao: Spree::KomerciConfig[:afiliation_key],
81
+ total: total,
82
+ transorig: transaction_option,
83
+ parcelas: portions,
84
+ data: transaction.created_at.strftime('%Y%m%d'),
85
+ numautor: transaction.authorization_number,
86
+ numcv: transaction.order_number,
87
+ usr: Spree::KomerciConfig[:komerci_user],
88
+ pwd: Spree::KomerciConfig[:komerci_password],
89
+ distribuidor: nil,
90
+ concentrador: nil
91
+ }
92
+
93
+ response = Spree::KomerciConfig.conf_authorize params
94
+
95
+ if response[:success] == true
96
+ ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.capture_success'), {}, authorization: response_code)
97
+ else
98
+ message = response[:data][:error_number] ? "#{response[:data][:error_number]}: " : ''
99
+ message << response[:data][:error_message]
100
+ ActiveMerchant::Billing::Response.new(false, message, {}, authorization: response_code)
101
+ end
102
+ end
103
+
104
+ # Voids the payment
105
+ #
106
+ # @author Isabella Santos
107
+ #
108
+ # @return [ActiveMerchant::Billing::Response]
109
+ #
110
+ def void(response_code, _gateway_options)
111
+ transaction = Spree::KomerciTransaction.find_by order_number: response_code
112
+
113
+ params = {
114
+ total: transaction.total,
115
+ filiacao: Spree::KomerciConfig[:afiliation_key],
116
+ numcv: transaction.order_number,
117
+ numautor: transaction.authorization_number,
118
+ usr: Spree::KomerciConfig[:komerci_user],
119
+ pwd: Spree::KomerciConfig[:komerci_password],
120
+ concentrador: nil
121
+ }
122
+
123
+ response = Spree::KomerciConfig.void_transaction params
124
+
125
+ if response[:success] == true
126
+ ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.void_success'), {}, authorization: response_code)
127
+ else
128
+ message = response[:data][:error_number] ? "#{response[:data][:error_number]}: " : ''
129
+ message << response[:data][:error_message]
130
+ ActiveMerchant::Billing::Response.new(false, message, {}, authorization: response_code)
131
+ end
132
+ end
133
+
134
+ # Cancel the payment
135
+ #
136
+ # @author Isabella Santos
137
+ #
138
+ # @return [ActiveMerchant::Billing::Response]
139
+ #
140
+ def cancel(response_code)
141
+ transaction = Spree::KomerciTransaction.find_by order_number: response_code
142
+
143
+ params = {
144
+ total: transaction.total,
145
+ filiacao: Spree::KomerciConfig[:afiliation_key],
146
+ numcv: transaction.order_number,
147
+ numautor: transaction.authorization_number,
148
+ usr: Spree::KomerciConfig[:komerci_user],
149
+ pwd: Spree::KomerciConfig[:komerci_password],
150
+ concentrador: nil
151
+ }
152
+
153
+ response = Spree::KomerciConfig.void_transaction params
154
+
155
+ if response[:success] == true
156
+ ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.cancel_success'), {}, authorization: response_code)
157
+ else
158
+ # Retorna como cancelado, mas avisando que o cancelamento deve ser feito no Portal
159
+ ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.cancel_error_date'), {}, authorization: response_code)
160
+ end
161
+ end
162
+
163
+ private
164
+
165
+ # Returns the params to Komerci
166
+ #
167
+ # @author Isabella Santos
168
+ #
169
+ # @return [Hash]
170
+ #
171
+ def prepare_authorize_params(source, gateway_options)
172
+ order_number, payment_number = gateway_options[:order_id].split('-')
173
+ order = Spree::Order.friendly.find order_number
174
+ payment = Spree::Payment.friendly.find payment_number
175
+ portion_value = Spree::KomerciConfig.calculate_portion_value order, gateway_options[:portions]
176
+ total_value = sprintf('%0.2f', portion_value * gateway_options[:portions])
177
+
178
+ year = source.year.to_s.last(2)
179
+ month = source.month.to_s.rjust(2, '0')
180
+
181
+ params = {
182
+ total: total_value,
183
+ filiacao: Spree::KomerciConfig[:afiliation_key],
184
+ numpedido: "#{order_number}-#{payment.id}",
185
+ nrcartao: source.number,
186
+ cvc2: source.verification_value,
187
+ mes: month,
188
+ ano: year,
189
+ portador: source.name,
190
+ conftxn: 'S'
191
+ }
192
+
193
+ [:iata, :distribuidor, :concentrador, :taxaembarque, :entrada, :adddata, :add_data,
194
+ :numdoc1, :numdoc2, :numdoc3, :numdoc4, :pax1, :pax2, :pax3, :pax4].each { |p| params[p] = nil }
195
+
196
+ params
197
+ end
198
+
199
+ # Update the value of the payment with the tax of the portions
200
+ #
201
+ # @author Isabella Santos
202
+ #
203
+ # @param gateway_options [Hash]
204
+ #
205
+ def update_payment_amount(gateway_options)
206
+ order_number, payment_number = gateway_options[:order_id].split('-')
207
+ order = Spree::Order.friendly.find order_number
208
+ total = Spree::KomerciConfig.calculate_portion_value(order, gateway_options[:portions]) * gateway_options[:portions]
209
+
210
+ if total > order.total
211
+ Spree::Adjustment.create(adjustable: order,
212
+ amount: (total - order.total),
213
+ label: Spree.t(:komerci_adjustment_tax),
214
+ eligible: true,
215
+ order: order)
216
+ order.updater.update
217
+
218
+ payment = Spree::Payment.friendly.find payment_number
219
+ payment.update_attributes(amount: order.total)
220
+
221
+ end
222
+ end
223
+
224
+ # Verify if the response was authorized or not
225
+ #
226
+ # @author Isabella Santos
227
+ #
228
+ def verify_authorize_response(source, gateway_options, response, type)
229
+ if response[:success]
230
+ _order_number, payment_number = gateway_options[:order_id].split('-')
231
+ payment = Spree::Payment.friendly.find payment_number
232
+
233
+ update_payment_amount(gateway_options)
234
+
235
+ komerci_attributes = response[:data].merge(credit_card_id: source.id,
236
+ payment_id: payment.id,
237
+ total: payment.reload.amount)
238
+ Spree::KomerciTransaction.create(komerci_attributes)
239
+
240
+ return ActiveMerchant::Billing::Response.new(true, Spree.t("komerci.messages.#{type}_success"), {}, authorization: response[:data][:order_number])
241
+ else
242
+ message = response[:data][:error_number] ? "#{response[:data][:error_number]}: " : ''
243
+ message << response[:data][:error_message]
244
+
245
+ return ActiveMerchant::Billing::Response.new(false, message, {}, authorization: '')
246
+ end
247
+ end
248
+
249
+ end
250
+ end
@@ -0,0 +1,2 @@
1
+ <!-- insert_bottom "[data-hook='admin_configurations_sidebar_menu'], #admin_configurations_sidebar_menu[data-hook]" -->
2
+ <%= configurations_sidebar_menu_item(t(:komerci_settings), edit_admin_komerci_settings_path) if can? :manage, :komerci_settings %>
@@ -0,0 +1,67 @@
1
+ <%= render partial: 'spree/admin/shared/sub_menu/configuration' %>
2
+
3
+ <% content_for :page_title do %>
4
+ <%= Spree.t(:komerci_settings) %>
5
+ <% end %>
6
+
7
+ <%= form_tag admin_komerci_settings_path, method: :put do |form| %>
8
+ <div id="komerci_preferences" data-hook="komerci_preferences" xmlns="http://www.w3.org/1999/html">
9
+ <div class="panel panel-default">
10
+ <div class="panel-heading">
11
+ <h1 class="panel-title">
12
+ <%= Spree.t(:settings) %>
13
+ </h1>
14
+ </div>
15
+ <div class="panel-body">
16
+ <div class="row">
17
+ <div class="col-sm-4">
18
+ <%= label_tag(:afiliation_key, Spree.t(:afiliation_key) + ': ') + tag(:br) %>
19
+ <%= text_field_tag :afiliation_key, @config.afiliation_key, class: 'form-control' %>
20
+ </div>
21
+ <div class="col-sm-4">
22
+ <%= label_tag(:komerci_user, Spree.t(:komerci_user) + ': ') + tag(:br) %>
23
+ <%= text_field_tag :komerci_user, @config.komerci_user, class: 'form-control' %>
24
+ </div>
25
+ <div class="col-sm-4">
26
+ <%= label_tag(:komerci_password, Spree.t(:komerci_password) + ': ') + tag(:br) %>
27
+ <%= text_field_tag :komerci_password, @config.komerci_password, class: 'form-control' %>
28
+ </div>
29
+ </div>
30
+ <div class="row">
31
+ <div class="col-sm-6">
32
+ <%= check_box_tag :test_mode, true, @config.test_mode %>
33
+ <%= label_tag(:test_mode) %>
34
+ </div>
35
+ <div class="col-sm-6">
36
+ <%= label_tag(:portions_type, Spree.t(:portions_type) + ': ') + tag(:br) %>
37
+ <%= radio_button_tag :portions_type, '06', @config.portions_type == '06' %>
38
+ <%= label_tag :portions_type_06, Spree.t(:portions_type_06) %>
39
+ <%= radio_button_tag :portions_type, '08', @config.portions_type == '08' %>
40
+ <%= label_tag :portions_type_08, Spree.t(:portions_type_08) %>
41
+ </div>
42
+ </div>
43
+ <div class="row">
44
+ <div class="col-sm-6">
45
+ <%= label_tag(:minimum_value, Spree.t(:minimum_value) + ': ') + tag(:br) %>
46
+ <%= text_field_tag :minimum_value, @config.minimum_value, class: 'form-control' %>
47
+ </div>
48
+ <div class="col-sm-6">
49
+ <%= label_tag(:tax_value, Spree.t(:tax_value) + ': ') + tag(:br) %>
50
+ <%= text_field_tag :tax_value, @config.tax_value, class: 'form-control' %>
51
+ </div>
52
+ </div>
53
+ <div class="row">
54
+ <div class="col-sm-6">
55
+ <%= label_tag(:portion_without_tax, Spree.t(:portion_without_tax) + ': ') + tag(:br) %>
56
+ <%= number_field_tag :portion_without_tax, @config.portion_without_tax, class: 'form-control' %>
57
+ </div>
58
+ <div class="col-sm-6">
59
+ <%= label_tag(:max_portion, Spree.t(:max_portion) + ': ') + tag(:br) %>
60
+ <%= number_field_tag :max_portion, @config.max_portion, class: 'form-control' %>
61
+ </div>
62
+ </div>
63
+ </div>
64
+ </div>
65
+ </div>
66
+ <%= render partial: 'spree/admin/shared/edit_resource_links', locals: { collection_url: edit_admin_komerci_settings_url } %>
67
+ <% end %>