spree_zaez_clearsale 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +12 -0
  5. data/Gemfile +7 -0
  6. data/Guardfile +90 -0
  7. data/LICENSE +26 -0
  8. data/README.md +81 -0
  9. data/Rakefile +21 -0
  10. data/app/assets/javascripts/spree/backend/app/clear_sale_settings/clear_sale_add_provider.js.coffee +80 -0
  11. data/app/assets/javascripts/spree/backend/app/clear_sale_settings/clear_sale_provider.js.coffee +27 -0
  12. data/app/assets/javascripts/spree/backend/app/clear_sale_settings/clear_sale_settings.js.coffee +39 -0
  13. data/app/assets/javascripts/spree/backend/spree_zaez_clearsale.js +1 -0
  14. data/app/assets/javascripts/spree/frontend/app/addresses/phone_mask_address.js.coffee +26 -0
  15. data/app/assets/javascripts/spree/frontend/spree_zaez_clearsale.js +3 -0
  16. data/app/assets/stylesheets/spree/backend/spree_zaez_clearsale.css +18 -0
  17. data/app/assets/stylesheets/spree/frontend/spree_zaez_clearsale.css +4 -0
  18. data/app/controllers/spree/admin/clear_sale_settings_controller.rb +33 -0
  19. data/app/controllers/spree/admin/payments_controller_decorator.rb +9 -0
  20. data/app/helpers/orders_helper_decorator.rb +12 -0
  21. data/app/models/spree/clear_sale.rb +153 -0
  22. data/app/models/spree/credit_card_decorator.rb +10 -0
  23. data/app/models/spree/payment_decorator.rb +33 -0
  24. data/app/overrides/spree/address/_form/add_constructor_to_form.html.erb.deface +4 -0
  25. data/app/overrides/spree/admin/orders/index/replace_considered_risk_column.html.erb.deface +2 -0
  26. data/app/overrides/spree/admin/orders/index/replace_considered_risky_label.html.erb.deface +6 -0
  27. data/app/overrides/spree/admin/shared/_order_tabs/add_clear_sale_iframe_to_order_menu.html.erb.deface +6 -0
  28. data/app/overrides/spree/admin/shared/sub_menu/_configuration/add_clear_sale_settings_tab.html.erb.deface +2 -0
  29. data/app/views/spree/admin/clear_sale_settings/edit.html.erb +142 -0
  30. data/app/views/spree/admin/payments/clear_sale.html.erb +36 -0
  31. data/bin/rails +7 -0
  32. data/config/locales/en.yml +36 -0
  33. data/config/locales/pt-br.yml +36 -0
  34. data/config/routes.rb +9 -0
  35. data/db/migrate/20150608175547_add_bin_card_to_spree_credit_card.rb +5 -0
  36. data/db/migrate/20150611113444_add_clearsale_score_to_spree_payments.rb +5 -0
  37. data/lib/generators/spree_zaez_clearsale/install/install_generator.rb +31 -0
  38. data/lib/spree/clear_sale_configuration.rb +36 -0
  39. data/lib/spree_zaez_clearsale.rb +2 -0
  40. data/lib/spree_zaez_clearsale/engine.rb +27 -0
  41. data/lib/spree_zaez_clearsale/factories.rb +6 -0
  42. data/spec/features/admin/clear_sale_settings_spec.rb +145 -0
  43. data/spec/features/admin/orders/clearsale_spec.rb +60 -0
  44. data/spec/features/admin/orders/listing_spec.rb +47 -0
  45. data/spec/fixtures/clearsale_critical.txt +51 -0
  46. data/spec/fixtures/clearsale_high.txt +51 -0
  47. data/spec/fixtures/clearsale_low.txt +51 -0
  48. data/spec/fixtures/clearsale_medium.txt +51 -0
  49. data/spec/helpers/admin/orders_helper_decorator_spec.rb +32 -0
  50. data/spec/lib/spree/clear_sale_configuration_spec.rb +41 -0
  51. data/spec/models/spree/clear_sale_spec.rb +126 -0
  52. data/spec/models/spree/credit_card_decorator_spec.rb +11 -0
  53. data/spec/models/spree/payment_decorator_spec.rb +73 -0
  54. data/spec/spec_helper.rb +92 -0
  55. data/spec/support/capybara_login.rb +13 -0
  56. data/spec/support/verify_input_field.rb +16 -0
  57. data/spree_zaez_clearsale.gemspec +38 -0
  58. data/vendor/assets/javascripts/inputmask/inputmask.min.js +10 -0
  59. data/vendor/assets/javascripts/inputmask/jquery.inputmask.min.js +8 -0
  60. metadata +76 -3
@@ -0,0 +1,36 @@
1
+ <%= render partial: 'spree/admin/shared/order_tabs', locals: { current: 'Clear Sale' } %>
2
+
3
+ <% content_for :page_actions do %>
4
+ <% if @order.outstanding_balance? && can?(:create, Spree::Payment) %>
5
+ <span id="new_payment_section">
6
+ <%= button_link_to Spree.t(:new_payment), new_admin_order_payment_url(@order), class: 'btn-success', icon: 'add' %>
7
+ </span>
8
+ <% end %>
9
+ <% end %>
10
+
11
+ <% content_for :page_title do %>
12
+ / <%= Spree.t(:payments) %>
13
+ <% end %>
14
+
15
+ <% @order.payments.each do |payment| %>
16
+ <% if payment.clearsale_registered? %>
17
+ <div id="clear_sale_preferences" data-hook="clear_sale_preferences">
18
+ <div class="panel panel-default">
19
+ <div class="panel-heading">
20
+ <h1 class="panel-title">
21
+ <%= "#{Spree.t(:payment)} #{payment.number}" %>
22
+ </h1>
23
+ </div>
24
+ <div class="panel-body">
25
+ <div class="row">
26
+ <div class="col-sm-12">
27
+ <iframe id="iFrameStart" src="<%= "#{@integration_url}#{payment.number}" %>">
28
+ <p>Seu Browser não suporta iframes</p>
29
+ </iframe>
30
+ </div>
31
+ </div>
32
+ </div>
33
+ </div>
34
+ </div>
35
+ <% end %>
36
+ <% end %>
data/bin/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_zaez_clearsale/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,36 @@
1
+ en:
2
+ clear_sale: ClearSale
3
+ clear_sale_settings: ClearSale Settings
4
+ spree:
5
+ clear_sale: ClearSale
6
+ clear_sale_settings: ClearSale Settings
7
+ clearsale_score: ClearSale Score
8
+ clearsale_score_low: Low
9
+ clearsale_score_medium: Medium
10
+ clearsale_score_high: High
11
+ clearsale_score_critical: Critical
12
+ state: State
13
+ state_true: Enable
14
+ state_false: Disable
15
+ token: Token
16
+ test_mode: Active Test Mode
17
+ payment_methods: Payment Methods
18
+ doc_customer_attr: Document Customer Attribute
19
+ enable_birth_date: Enable Birth Date Customer Attribute
20
+ birth_date_customer_attr: Birth Date Customer Attribute
21
+ payment_type_1: Credit Card
22
+ payment_type_2: Bank Slip
23
+ payment_type_3: Debt Bank
24
+ payment_type_4: Debt Bank – Cash
25
+ payment_type_5: Debt Bank – Check
26
+ payment_type_6: Transfer Bank
27
+ payment_type_7: Sedex to Charge
28
+ payment_type_8: Check
29
+ payment_type_9: Cash
30
+ payment_type_10: Financing
31
+ payment_type_11: Order
32
+ payment_type_12: Coupon
33
+ payment_type_13: Multiple Checks
34
+ payment_type_14: Others
35
+ enable_category_taxonomy: There is a taxonomy which represents the categories
36
+ category_taxonomy_id: Taxonomy wich represents the categories
@@ -0,0 +1,36 @@
1
+ pt-BR:
2
+ clear_sale: ClearSale
3
+ clear_sale_settings: Configurações ClearSale
4
+ spree:
5
+ clear_sale: ClearSale
6
+ clear_sale_settings: Configurações ClearSale
7
+ clearsale_score: Risco ClearSale
8
+ clearsale_score_low: Baixo
9
+ clearsale_score_medium: Médio
10
+ clearsale_score_high: Alto
11
+ clearsale_score_critical: Crítico
12
+ state: Status
13
+ state_true: Habilitado
14
+ state_false: Desabilitado
15
+ token: Código de Integração
16
+ test_mode: Ativar Ambiente de Homologação
17
+ payment_methods: Tipos de Pagamento
18
+ doc_customer_attr: Atributo do Cliente que Armazena o Documento (CPF/CNPJ)
19
+ enable_birth_date: Habilitar atributo do cliente que armazena a data de nascimento
20
+ birth_date_customer_attr: Atributo do Cliente que Armazena a Data de Nascimento
21
+ payment_type_1: Cartão de Crédito
22
+ payment_type_2: Boleto Bancário
23
+ payment_type_3: Débito bancário
24
+ payment_type_4: Débito Bancário – Dinheiro
25
+ payment_type_5: Débito Bancário – Cheque
26
+ payment_type_6: Transferência Bancária
27
+ payment_type_7: Sedex a Cobrar
28
+ payment_type_8: Cheque
29
+ payment_type_9: Dinheiro
30
+ payment_type_10: Financiamento
31
+ payment_type_11: Fatura
32
+ payment_type_12: Cupom
33
+ payment_type_13: Multicheque
34
+ payment_type_14: Outros
35
+ enable_category_taxonomy: Existe um Taxonomy que representa as categorias
36
+ category_taxonomy_id: Taxonomy que representa as categorias
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ namespace :admin do
3
+ resource :clear_sale_settings, only: [:show, :edit, :update]
4
+
5
+ resources :orders, except: [:show] do
6
+ get 'payments/clear_sale', to: 'payments#clear_sale'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class AddBinCardToSpreeCreditCard < ActiveRecord::Migration
2
+ def change
3
+ add_column :spree_credit_cards, :bin_card, :string
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddClearsaleScoreToSpreePayments < ActiveRecord::Migration
2
+ def change
3
+ add_column :spree_payments, :clearsale_score, :string
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ module SpreeZaezClearsale
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ class_option :auto_run_migrations, :type => :boolean, :default => false
6
+
7
+ def add_javascripts
8
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/spree_zaez_clearsale\n"
9
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/spree_zaez_clearsale\n"
10
+ end
11
+
12
+ def add_stylesheets
13
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/spree_zaez_clearsale\n", :before => /\*\//, :verbose => true
14
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/spree_zaez_clearsale\n", :before => /\*\//, :verbose => true
15
+ end
16
+
17
+ def add_migrations
18
+ run 'bundle exec rake railties:install:migrations FROM=spree_zaez_clearsale'
19
+ end
20
+
21
+ def run_migrations
22
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
23
+ if run_migrations
24
+ run 'bundle exec rake db:migrate'
25
+ else
26
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ class Spree::ClearSaleConfiguration < Spree::Preferences::Configuration
2
+
3
+ preference :state, :boolean, default: true # modulo habilitado/desabilitado
4
+ preference :test_mode, :boolean, default: false # modo de teste
5
+ preference :token, :string, default: '' # codigo de integracao
6
+ preference :providers, :hash, default: {} # quais tipos de pagamento o clearsale sera utilizado
7
+ preference :doc_customer_attr, :string # campo que fica armazenado o CPF/CNPJ do cliente
8
+ preference :birth_date_customer_attr, :string # campo que fica armazenado a data de nascimento do cliente
9
+ preference :category_taxonomy_id, :integer, default: 0 # taxonmy que representa a categoria
10
+
11
+ # Retorna um json com as informacoes dos providers
12
+ # e seus tipos de pagamento (de acordo com a tabela da ClearSale) salvos
13
+ #
14
+ # @author Isabella Santos
15
+ #
16
+ # @return [String]
17
+ #
18
+ def get_providers
19
+ preferred_providers.collect { |key, value| {id: key, text: key, payment_type: {id: value, text: Spree.t("payment_type_#{value}")}} }.to_json
20
+ end
21
+
22
+ # Retorna a url de integracao
23
+ # de acordo com o modo selecionado (homologacao ou producao)
24
+ #
25
+ # @author Isabella Santos
26
+ #
27
+ # @return [String]
28
+ #
29
+ def integration_url
30
+ if preferred_test_mode
31
+ 'https://homolog.clearsale.com.br/start/Entrada/EnviarPedido.aspx'
32
+ else
33
+ 'https://www.clearsale.com.br/start/Entrada/EnviarPedido.aspx'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,2 @@
1
+ require 'spree_core'
2
+ require 'spree_zaez_clearsale/engine'
@@ -0,0 +1,27 @@
1
+ module SpreeZaezClearsale
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_zaez_clearsale'
6
+
7
+ initializer 'spree.zaez_clearsale.preferences', :before => :load_config_initializers do |app|
8
+ # require file with the preferences of the ClearSale
9
+ require 'spree/clear_sale_configuration'
10
+ Spree::ClearSaleConfig = Spree::ClearSaleConfiguration.new
11
+ #
12
+ end
13
+
14
+ # use rspec for tests
15
+ config.generators do |g|
16
+ g.test_framework :rspec
17
+ end
18
+
19
+ def self.activate
20
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
21
+ Rails.configuration.cache_classes ? require(c) : load(c)
22
+ end
23
+ end
24
+
25
+ config.to_prepare &method(:activate).to_proc
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ # Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
3
+ #
4
+ # Example adding this to your spec_helper will load these Factories for use:
5
+ # require 'spree_zaez_clearsale/factories'
6
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ClearSale Settings', type: :feature do
4
+ before { create_admin_in_sign_in }
5
+
6
+ context 'visit ClearSale settings' do
7
+ it 'should be a link to ClearSale settings' do
8
+ within('.sidebar') { page.find_link('ClearSale Settings')['/admin/clear_sale_settings/edit'] }
9
+ end
10
+ end
11
+
12
+ context 'show ClearSale settings' do
13
+
14
+ before { visit spree.edit_admin_clear_sale_settings_path }
15
+
16
+ it 'should show the preferences of ClearSale settings', js: true do
17
+ expect(page).to have_selector '[name=state]'
18
+ expect(page).to have_selector '#token'
19
+ expect(page).to have_selector '#test_mode'
20
+ expect(page).to have_selector '#doc_customer_attr'
21
+ expect(page).to have_selector '#provider'
22
+ expect(page).to have_selector '#payment_type'
23
+ expect(page).to have_selector '#enable_birth_date'
24
+ expect(page).to have_selector '#enable_category_taxonomy'
25
+ end
26
+
27
+ it 'should hide birth date customer attribute when checkbox enable_birth_date is disabled', js: true do
28
+ find(:css, '#enable_birth_date').set false
29
+
30
+ expect(page).not_to have_selector '#birth_date_customer_attr'
31
+ end
32
+
33
+ it 'should hide category taxonomy when checkbox enable_category_taxonomy is disabled', js: true do
34
+ find(:css, '#enable_category_taxonomy').set false
35
+
36
+ expect(page).not_to have_selector '#category_taxonomy_id'
37
+ end
38
+ end
39
+
40
+ context 'edit ClearSale settings' do
41
+
42
+ before do |example|
43
+ unless example.metadata[:skip_before]
44
+ visit spree.edit_admin_clear_sale_settings_path
45
+ end
46
+ end
47
+
48
+ it 'can edit state', js: true do
49
+ find(:css, '#state_false').set true
50
+ click_button 'Update'
51
+
52
+ expect(Spree::ClearSaleConfig.state).to be false
53
+ expect(find_field('state_false')).to be_checked
54
+
55
+ # set default
56
+ Spree::ClearSaleConfig.state = true
57
+ end
58
+
59
+ it 'can edit token', js: true do
60
+ fill_in 'Token', with: '123'
61
+ click_button 'Update'
62
+
63
+ verify_input_value 'token', Spree::ClearSaleConfig, '123', ''
64
+ end
65
+
66
+ it 'can edit test mode', js: true do
67
+ find(:css, '#test_mode').set true
68
+ click_button 'Update'
69
+
70
+ expect(Spree::ClearSaleConfig.test_mode).to be true
71
+ expect(find_field('test_mode')).to be_checked
72
+
73
+ # set default
74
+ Spree::ClearSaleConfig.test_mode = false
75
+ end
76
+
77
+ it 'can edit the providers', js: true do
78
+ Spree::ClearSaleConfig.providers = {}
79
+ # 1st provider
80
+ select 'Spree::Gateway::BogusSimple', from: 'provider'
81
+ select 'Bank Slip', from: 'payment_type'
82
+ click_button 'Add'
83
+ # 2nd provider
84
+ select 'Spree::PaymentMethod::Check', from: 'provider'
85
+ select 'Credit Card', from: 'payment_type'
86
+ click_button 'Add'
87
+ click_button 'Update'
88
+
89
+ # 1st provider
90
+ expect(Spree::ClearSaleConfig.providers.has_key?('Spree::Gateway::BogusSimple')).to be true
91
+ expect(Spree::ClearSaleConfig.providers['Spree::Gateway::BogusSimple']).to eq '2'
92
+ # 2nd provider
93
+ expect(Spree::ClearSaleConfig.providers.has_key?('Spree::PaymentMethod::Check')).to be true
94
+ expect(Spree::ClearSaleConfig.providers['Spree::PaymentMethod::Check']).to eq '1'
95
+
96
+ within_row(1) do
97
+ expect(column_text(1)).to eq('Spree::Gateway::BogusSimple')
98
+ expect(column_text(2)).to eq('Bank Slip')
99
+ end
100
+ within_row(2) do
101
+ expect(column_text(1)).to eq('Spree::PaymentMethod::Check')
102
+ expect(column_text(2)).to eq('Credit Card')
103
+ end
104
+
105
+ # set default
106
+ Spree::ClearSaleConfig.providers = {}
107
+ end
108
+
109
+ it 'can edit the document customer attribute', js: true do
110
+ select 'Authentication Token', from: 'doc_customer_attr'
111
+ click_button 'Update'
112
+
113
+ verify_input_value 'doc_customer_attr', Spree::ClearSaleConfig, 'authentication_token', ''
114
+ end
115
+
116
+ it 'can edit the birth date customer attribute', js: true do
117
+ find(:css, '#enable_birth_date').set true
118
+
119
+ select 'Created At', from: 'birth_date_customer_attr'
120
+ click_button 'Update'
121
+
122
+ expect(Spree::ClearSaleConfig.birth_date_customer_attr).to eq 'created_at'
123
+ expect(find_field('birth_date_customer_attr').value).to eq 'created_at'
124
+
125
+ # set default
126
+ Spree::ClearSaleConfig.birth_date_customer_attr = ''
127
+ end
128
+
129
+ it 'can edit the category taxonomy id', skip_before: true do
130
+ taxonomy = Spree::Taxonomy.create(name: 'Category')
131
+ visit spree.edit_admin_clear_sale_settings_path
132
+
133
+ find(:css, '#enable_category_taxonomy').set true
134
+
135
+ select 'Category', from: 'category_taxonomy_id'
136
+ click_button 'Update'
137
+
138
+ expect(Spree::ClearSaleConfig.category_taxonomy_id).to eq taxonomy.id
139
+ expect(find_field('category_taxonomy_id').value).to eq '1'
140
+
141
+ # set default
142
+ Spree::ClearSaleConfig.category_taxonomy_id = ''
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ClearSale', type: :feature do
4
+
5
+ def visit_order
6
+ create_admin_in_sign_in
7
+
8
+ visit spree.admin_path
9
+ within('.sidebar') do
10
+ click_link 'Orders'
11
+ end
12
+ within_row(1) do
13
+ click_link order.number
14
+ end
15
+ end
16
+
17
+ let!(:order) do
18
+ create(:completed_order_with_pending_payment)
19
+ end
20
+
21
+ context 'without payments registered in ClearSale' do
22
+ it 'should not show ClearSale tab when any payment is registered', js: true do
23
+ visit_order
24
+ expect(page).not_to have_content 'ClearSale'
25
+ end
26
+ end
27
+
28
+ context 'with payment registered in ClearSale' do
29
+ it 'should show ClearSale tab when a payment is registered', js: true do
30
+ Spree::ClearSaleConfig.providers = {'Spree::Gateway::Bogus' => 1}
31
+ Spree::ClearSaleConfig.test_mode = true
32
+
33
+ stub_request(:post, Spree::ClearSaleConfig.integration_url).
34
+ with(:headers => {'Accept'=>'*/*',
35
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
36
+ 'Content-Type'=>'application/x-www-form-urlencoded',
37
+ 'User-Agent'=>'Ruby'},
38
+ :body => hash_including({'CodigoIntegracao' => Spree::ClearSaleConfig.token})).
39
+ to_return(:body => File.read('spec/fixtures/clearsale_critical.txt'), :status => 200 )
40
+
41
+ visit_order
42
+ within('.main-right-sidebar') do
43
+ click_link 'Payments'
44
+ end
45
+ click_icon(:capture)
46
+
47
+ Spree::ClearSaleConfig.birth_date_customer_attr
48
+ expect(order.payments.first.reload.clearsale_score).to eq 'critical'
49
+ expect(page).to have_content 'ClearSale'
50
+
51
+ within('.main-right-sidebar') do
52
+ click_link 'ClearSale'
53
+ end
54
+
55
+ # set default
56
+ Spree::ClearSaleConfig.providers = {}
57
+ Spree::ClearSaleConfig.test_mode = false
58
+ end
59
+ end
60
+ end