catarse_moip 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -6
  3. data/.rspec +2 -0
  4. data/.travis.yml +17 -0
  5. data/Gemfile +2 -109
  6. data/Gemfile.lock +73 -476
  7. data/README.md +2 -26
  8. data/Rakefile +7 -2
  9. data/app/assets/javascripts/catarse_moip/moip_form.js +80 -0
  10. data/app/assets/javascripts/catarse_moip/payment_account.js +37 -0
  11. data/app/assets/javascripts/catarse_moip/payment_card.js +89 -0
  12. data/app/assets/javascripts/catarse_moip/payment_choice.js +19 -0
  13. data/app/assets/javascripts/catarse_moip/payment_slip.js +35 -0
  14. data/app/assets/javascripts/catarse_moip/user_document.js +111 -0
  15. data/app/assets/javascripts/catarse_moip.js +7 -0
  16. data/app/controllers/catarse_moip/moip_controller.rb +126 -0
  17. data/app/views/catarse_moip/moip/review.html.slim +105 -0
  18. data/catarse_moip.gemspec +5 -5
  19. data/config/initializers/moip.rb +4 -7
  20. data/config/initializers/register.rb +5 -0
  21. data/config/locales/en.yml +2 -0
  22. data/config/locales/pt.yml +19 -0
  23. data/config/routes.rb +9 -5
  24. data/lib/catarse_moip/engine.rb +7 -0
  25. data/lib/catarse_moip/version.rb +1 -1
  26. data/lib/catarse_moip.rb +1 -1
  27. data/lib/moip_transparente/checkout.rb +218 -0
  28. data/lib/moip_transparente/version.rb +3 -0
  29. data/lib/moip_transparente.rb +32 -0
  30. data/spec/controllers/catarse_moip/moip_controller_spec.rb +230 -0
  31. data/spec/fixtures/ipn_data.txt +1 -0
  32. data/spec/javascripts/moip_form_spec.js +25 -0
  33. data/spec/javascripts/support/app.js +1 -0
  34. data/spec/javascripts/support/backbone.js +1571 -0
  35. data/spec/javascripts/support/jquery.js +8829 -0
  36. data/spec/javascripts/support/skull.js +64 -0
  37. data/spec/javascripts/support/underscore.js +1227 -0
  38. data/spec/spec_helper.rb +2 -23
  39. data/spec/support/payment_engines.rb +3 -0
  40. data/test/dummy/README.rdoc +261 -0
  41. data/test/dummy/Rakefile +7 -0
  42. data/test/dummy/app/assets/javascripts/application.js +15 -0
  43. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  44. data/test/dummy/app/controllers/application_controller.rb +3 -0
  45. data/test/dummy/app/helpers/application_helper.rb +2 -0
  46. data/test/dummy/app/mailers/.gitkeep +0 -0
  47. data/test/dummy/app/models/.gitkeep +0 -0
  48. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  49. data/test/dummy/config/application.rb +59 -0
  50. data/test/dummy/config/boot.rb +10 -0
  51. data/test/dummy/config/database.yml +50 -0
  52. data/test/dummy/config/environment.rb +5 -0
  53. data/test/dummy/config/environments/development.rb +37 -0
  54. data/test/dummy/config/environments/production.rb +67 -0
  55. data/test/dummy/config/environments/test.rb +37 -0
  56. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/test/dummy/config/initializers/inflections.rb +15 -0
  58. data/test/dummy/config/initializers/mime_types.rb +5 -0
  59. data/test/dummy/config/initializers/secret_token.rb +7 -0
  60. data/test/dummy/config/initializers/session_store.rb +8 -0
  61. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/test/dummy/config/locales/en.yml +5 -0
  63. data/test/dummy/config/routes.rb +4 -0
  64. data/test/dummy/config.ru +4 -0
  65. data/test/dummy/lib/assets/.gitkeep +0 -0
  66. data/test/dummy/log/.gitkeep +0 -0
  67. data/test/dummy/public/404.html +26 -0
  68. data/test/dummy/public/422.html +26 -0
  69. data/test/dummy/public/500.html +25 -0
  70. data/test/dummy/public/favicon.ico +0 -0
  71. data/test/dummy/script/rails +6 -0
  72. metadata +120 -35
  73. data/.gitmodules +0 -3
  74. data/app/controllers/catarse_moip/payment/moip_controller.rb +0 -49
  75. data/spec/controllers/catarse_moip/payment/moip_controller_spec.rb +0 -81
@@ -1,6 +1,25 @@
1
1
  pt:
2
2
  projects:
3
3
  backers:
4
+ review:
5
+ moip: 'Pagamento nacional'
6
+ form:
7
+ labels:
8
+ payment_account: 'Debito em Conta'
9
+ document: 'CPF/CNPJ (somente números)'
10
+ payment_card: 'Pagamento com Cartão'
11
+ owner_card: "Dados do titular do cartão"
12
+ number_card: "Dados do cartão"
13
+ payment_boleto: "Pagamento com Boleto"
14
+ payment_boleto_subtitle: "Clique no link abaixo para ver o boleto:"
15
+ select_boleto: "Para prosseguir, por favor selecione:"
16
+ select_account: "Para prosseguir, por favor selecione o banco:"
17
+ payment_account: "Pagamento com Débito em Conta"
18
+ select_account: "Você será redirecionado para o site do seu banco:"
19
+ you_are_about_html: "Você está prestes a fazer parte do grupo que faz de tudo para o %{link} acontecer."
20
+ pay_with: "Pagar com"
21
+ submit: "Efetuar pagamento"
22
+ build_boleto: 'Gerar boleto'
4
23
  checkout:
5
24
  moip_error: "Ooops. Ocorreu um erro ao enviar seu pagamento para o MoIP. Por favor, tente novamente."
6
25
  success: "Seu apoio foi realizado com sucesso. Muito obrigado!"
data/config/routes.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  CatarseMoip::Engine.routes.draw do
2
- namespace :payment do
3
- resources :moip, only: [] do
4
- member do
5
- match :pay
6
- end
2
+ resources :moip, only: [], path: 'payment/moip' do
3
+ collection do
4
+ post 'notifications' => "moip#create_notification"
5
+ get 'js'
6
+ end
7
+ member do
8
+ match :moip_response
9
+ match :review
10
+ match :get_moip_token
7
11
  end
8
12
  end
9
13
  end
@@ -1,5 +1,12 @@
1
1
  module CatarseMoip
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace CatarseMoip
4
+ initializer "pandemic.asset_addition", :group => :all do |app|
5
+ # Enabling assets precompiling under rails 3.1
6
+ if Rails.version >= '3.1'
7
+ app.config.assets.precompile += %w( catarse_moip.js )
8
+ end
9
+ end
10
+
4
11
  end
5
12
  end
@@ -1,3 +1,3 @@
1
1
  module CatarseMoip
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/catarse_moip.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "catarse_moip/engine"
2
- require "moip"
2
+ #require "moip"
3
3
 
4
4
  module CatarseMoip
5
5
  end
@@ -0,0 +1,218 @@
1
+ require 'xml'
2
+ require 'base64'
3
+
4
+ class MoipTransparente::Checkout
5
+
6
+ def javascript_tag
7
+ "<script type='text/javascript' src='#{get_javascript_url}' charset='ISO-8859-1'></script>"
8
+ end
9
+
10
+ def widget_tag(success, fail)
11
+ "<div id='MoipWidget'
12
+ data-token='#{@token}'
13
+ callback-method-success='#{success}'
14
+ callback-method-error='#{fail}'>
15
+ </div>"
16
+ end
17
+
18
+ def get_token(invoice)
19
+ doc = XML::Document.new
20
+ doc.root = XML::Node.new('EnviarInstrucao')
21
+
22
+ unica = XML::Node.new('InstrucaoUnica')
23
+ unica['TipoValidacao'] = 'Transparente'
24
+
25
+
26
+ description = XML::Node.new('Razao')
27
+ description << invoice[:razao]
28
+ unica << description
29
+
30
+ id_invoice = XML::Node.new('IdProprio')
31
+ id_invoice << invoice[:id]
32
+ unica << id_invoice
33
+
34
+ valores = XML::Node.new('Valores')
35
+
36
+ valor = XML::Node.new('Valor')
37
+ valor['moeda'] = 'BRL'
38
+ valor << invoice[:total]
39
+
40
+ valores << valor
41
+
42
+
43
+ valor = XML::Node.new('Acrescimo')
44
+ valor['moeda'] = 'BRL'
45
+ valores << invoice[:acrescimo] || '0.00'
46
+
47
+
48
+ valor = XML::Node.new('Deducao')
49
+ valor['moeda'] = 'BRL'
50
+ valores << invoice[:desconto] || '0.00'
51
+
52
+ unica << valores
53
+
54
+ pagador = XML::Node.new('Pagador')
55
+ nome = XML::Node.new('Nome')
56
+ nome << invoice[:cliente][:nome]
57
+ pagador << nome
58
+
59
+ email = XML::Node.new('Email')
60
+ email << invoice[:cliente][:email]
61
+ pagador << email
62
+
63
+ id = XML::Node.new('IdPagador')
64
+ id << invoice[:cliente][:id]
65
+ pagador << id
66
+
67
+ endereco = XML::Node.new('EnderecoCobranca')
68
+
69
+ logradouro = XML::Node.new('Logradouro')
70
+ logradouro << invoice[:cliente][:logradouro]
71
+ endereco << logradouro
72
+
73
+ numero = XML::Node.new('Numero')
74
+ numero_or_default = invoice[:cliente][:numero] || '0'
75
+ numero << numero_or_default
76
+ endereco << numero
77
+
78
+ complemento = XML::Node.new('Complemento')
79
+ complemento << invoice[:cliente][:complemento]
80
+ endereco << complemento
81
+
82
+ bairro = XML::Node.new('Bairro')
83
+ bairro << invoice[:cliente][:bairro]
84
+ endereco << bairro
85
+
86
+ cidade = XML::Node.new('Cidade')
87
+ cidade << invoice[:cliente][:cidade]
88
+ endereco << cidade
89
+
90
+ estado = XML::Node.new('Estado')
91
+ estado << invoice[:cliente][:uf]
92
+ endereco << estado
93
+
94
+ pais = XML::Node.new('Pais')
95
+ pais_or_default = invoice[:cliente][:pais] || 'BRA'
96
+ pais << pais_or_default
97
+ endereco << pais
98
+
99
+ cep = XML::Node.new('CEP')
100
+ cep << invoice[:cliente][:cep]
101
+ endereco << cep
102
+
103
+ telefone = XML::Node.new('TelefoneFixo')
104
+ telefone << invoice[:cliente][:telefone]
105
+ endereco << telefone
106
+
107
+ pagador << endereco
108
+ unica << pagador
109
+
110
+ if invoice[:parcelamentos]
111
+ parcelamentos = XML::Node.new('Parcelamentos')
112
+ invoice[:parcelamentos].each do |parcelamento_item|
113
+ parcelamento = XML::Node.new('Parcelamento')
114
+ minimo = XML::Node.new('MinimoParcelas')
115
+ minimo << parcelamento_item[:minimo]
116
+ parcelamento << minimo
117
+
118
+ maximo = XML::Node.new('MaximoParcelas')
119
+ maximo << parcelamento_item[:maximo]
120
+ parcelamento << maximo
121
+
122
+ if parcelamento_item[:repassar]
123
+ repassar = XML::Node.new('Repassar')
124
+ repassar << '1'
125
+ parcelamento << repassar
126
+ else
127
+ juros = XML::Node.new('Juros')
128
+ juros << parcelamento_item[:juros]
129
+ parcelamento << juros
130
+ end
131
+ parcelamentos << parcelamento
132
+ end
133
+
134
+ unica << parcelamentos
135
+ end
136
+
137
+ comissoes = XML::Node.new('Comissoes')
138
+ if invoice[:comissoes]
139
+ invoice[:comissoes].each do |comissao_item|
140
+ comissionamento = XML::Node.new('Comissionamento')
141
+ razao = XML::Node.new('Razao')
142
+ razao << comissao_item[:razao] || invoice[:razao]
143
+ comissionamento << razao
144
+
145
+ comissionado = XML::Node.new("Comissionado")
146
+ login_moip = XML::Node.new("LoginMoIP")
147
+ login_moip << comissao_item[:login_moip]
148
+ comissionado << login_moip
149
+ comissionamento << comissionado
150
+
151
+ valor = XML::Node.new("ValorFixo")
152
+ valor << comissao_item[:valor]
153
+ comissionamento << valor
154
+ comissoes << comissionamento
155
+ end
156
+ end
157
+
158
+ if invoice[:pagador_taxas]
159
+ taxas = XML::Node.new('PagadorTaxa')
160
+ login_moip = XML::Node.new("LoginMoIP")
161
+ login_moip << invoice[:pagador_taxas]
162
+ taxas << login_moip
163
+ comissoes << taxas
164
+ end
165
+
166
+ unica << comissoes
167
+
168
+ doc.root << unica
169
+ begin
170
+ response = post_data(doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
171
+ parser = XML::Parser.string(response)
172
+ dom = parser.parse
173
+ rescue LibXML::XML::Error => e
174
+ puts response.inspect
175
+ puts parser.inspect
176
+ Rails.logger.info response.inspect
177
+ Rails.logger.info parser.inspect
178
+ end
179
+ resposta = dom.find('./Resposta').first
180
+ if resposta.find('Status')[0].content == 'Sucesso'
181
+ @token = resposta.find('Token')[0].content
182
+ return {:status => :ok, :token => resposta.find('Token')[0].content, :id => resposta.find('ID')[0].content}
183
+ elsif resposta.find('Status')[0].content == 'Falha'
184
+ return {:status => :fail, :code => resposta.find('Erro')[0]['Codigo'], :message => resposta.find('Erro')[0].content, :id => resposta.find('ID')[0].content }
185
+ end
186
+ end
187
+
188
+ def get_token_url
189
+ if ::MoipTransparente::Config.test?
190
+ return "https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica"
191
+ else
192
+ return "https://www.moip.com.br/ws/alpha/EnviarInstrucao/Unica"
193
+ end
194
+ end
195
+
196
+ def get_javascript_url
197
+ if ::MoipTransparente::Config.test?
198
+ return "https://desenvolvedor.moip.com.br/sandbox/transparente/MoipWidget-v2.js"
199
+ else
200
+ return "https://www.moip.com.br/transparente/MoipWidget-v2.js"
201
+ end
202
+ end
203
+
204
+ def post_data(xml)
205
+ uri = URI.parse(get_token_url)
206
+ http = Net::HTTP.new(uri.host, uri.port)
207
+ http.use_ssl = true
208
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
209
+ #http.set_debug_output $stderr if Moip::Config.test?
210
+
211
+ request = Net::HTTP::Post.new(uri.path)
212
+ request.basic_auth ::MoipTransparente::Config.access_token, ::MoipTransparente::Config.access_key
213
+
214
+ request.body = xml
215
+ response = http.start {|r| r.request request }
216
+ response.body
217
+ end
218
+ end
@@ -0,0 +1,3 @@
1
+ module MoipTransparente
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,32 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require "moip_transparente/version"
4
+ require "moip_transparente/checkout"
5
+
6
+ module MoipTransparente
7
+ class Config
8
+ def self.access_token
9
+ @access_token
10
+ end
11
+
12
+ def self.access_token=(value)
13
+ @access_token = value
14
+ end
15
+
16
+ def self.access_key
17
+ @access_key
18
+ end
19
+
20
+ def self.access_key=(value)
21
+ @access_key = value
22
+ end
23
+
24
+ def self.test?
25
+ @test || false
26
+ end
27
+
28
+ def self.test=(test)
29
+ @test = test
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,230 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe CatarseMoip::MoipController do
5
+ subject{ response }
6
+
7
+ let(:get_token_response){{:status=>:fail, :code=>"171", :message=>"TelefoneFixo do endereço deverá ser enviado obrigatorio", :id=>"201210192052439150000024698931"}}
8
+ let(:backer){ double('backer', {
9
+ id: 1,
10
+ key: 'backer key',
11
+ payment_id: 'payment id',
12
+ project: project,
13
+ pending?: false,
14
+ value: 10,
15
+ user: user,
16
+ payer_name: 'foo',
17
+ payer_email: 'foo@bar.com',
18
+ address_street: 'test',
19
+ address_number: '123',
20
+ address_complement: '123',
21
+ address_neighbourhood: '123',
22
+ address_city: '123',
23
+ address_state: '123',
24
+ address_zip_code: '123',
25
+ address_phone_number: '123'
26
+ }) }
27
+
28
+ let(:user){ double('user', id: 1) }
29
+ let(:project){ double('project', id: 1, name: 'test project') }
30
+ let(:extra_data){ {"id_transacao"=>backer.key, "valor"=>"2190", "cod_moip"=>"12345123", "forma_pagamento"=>"1", "tipo_pagamento"=>"CartaoDeCredito", "email_consumidor"=>"some@email.com", "controller"=>"catarse_moip/payment/notifications", "action"=>"create"} }
31
+
32
+ before do
33
+ controller.stub(:current_user).and_return(user)
34
+ ::MoipTransparente::Checkout.any_instance.stub(:get_token).and_return(get_token_response)
35
+ ::MoipTransparente::Checkout.any_instance.stub(:moip_widget_tag).and_return('<div>')
36
+ ::MoipTransparente::Checkout.any_instance.stub(:moip_javascript_tag).and_return('<script>')
37
+ ::MoipTransparente::Checkout.any_instance.stub(:as_json).and_return('{}')
38
+ PaymentEngines.stub(:find_payment).and_return(backer)
39
+ PaymentEngines.stub(:create_payment_notification)
40
+ backer.stub(:with_lock).and_yield
41
+ end
42
+
43
+ describe "POST create_notification" do
44
+ context "when we search for a non-existant backer" do
45
+ before do
46
+ PaymentEngines.should_receive(:find_payment).with(key: "non-existant backer key").and_raise('error')
47
+ post :create_notification, {:id_transacao => "non-existant backer key", :use_route => 'catarse_moip'}
48
+ end
49
+
50
+ its(:status){ should == 422 }
51
+ its(:body){ should == "#<RuntimeError: error>: error recebemos: {\"id_transacao\"=>\"non-existant backer key\", \"controller\"=>\"catarse_moip/moip\", \"action\"=>\"create_notification\"}" }
52
+ end
53
+
54
+ context "when we seach for an existing backer" do
55
+ before do
56
+ PaymentEngines.should_receive(:find_payment).with(key: backer.key).and_return(backer)
57
+ controller.should_receive(:process_moip_message).with({"id_transacao"=>backer.key, "controller"=>"catarse_moip/moip", "action"=>"create_notification"})
58
+ post :create_notification, {:id_transacao => backer.key, :use_route => 'catarse_moip'}
59
+ end
60
+
61
+ its(:body){ should == ' ' }
62
+ its(:status){ should == 200 }
63
+ it("should assign backer"){ assigns(:backer).should == backer }
64
+ end
65
+ end
66
+
67
+ describe "GET js" do
68
+ let(:file){ double('js_file') }
69
+
70
+ context "when the content of get_javascript_url raises an error" do
71
+ before do
72
+ controller.should_receive(:open).at_least(3).times.and_raise('error')
73
+ ->{
74
+ get :js, locale: :pt, use_route: 'catarse_moip'
75
+ }.should raise_error('error')
76
+ end
77
+ its(:status){ should == 200 }
78
+ end
79
+
80
+ context "when the content of get_javascript_url is read without errors" do
81
+ before do
82
+ controller.should_receive(:open).and_return(file)
83
+ file.should_receive(:set_encoding).and_return(file)
84
+ file.should_receive(:read).and_return(file)
85
+ file.should_receive(:encode).and_return('js content')
86
+ get :js, locale: :pt, use_route: 'catarse_moip'
87
+ end
88
+ its(:status){ should == 200 }
89
+ its(:body){ should == 'js content' }
90
+ end
91
+ end
92
+
93
+ describe "POST moip_response" do
94
+ let(:processor){ double('moip processor') }
95
+ before do
96
+ controller.should_receive(:process_moip_message)
97
+ post :moip_response, id: backer.id, response: {StatusPagamento: 'Sucesso'}, use_route: 'catarse_moip'
98
+ end
99
+
100
+ its(:status){ should == 200 }
101
+ end
102
+
103
+ describe "POST get_moip_token" do
104
+ before do
105
+ post :get_moip_token, :id => backer.id, :use_route => 'catarse_moip'
106
+ end
107
+
108
+ its(:status){ should == 200 }
109
+ its(:body){ should == "{\"get_token_response\":{\"status\":\"fail\",\"code\":\"171\",\"message\":\"TelefoneFixo do endereço deverá ser enviado obrigatorio\",\"id\":\"201210192052439150000024698931\"},\"moip\":\"{}\",\"widget_tag\":\"<div id='MoipWidget'\\n data-token=''\\n callback-method-success='checkoutSuccessful' \\n callback-method-error='checkoutFailure'>\\n </div>\",\"javascript_tag\":\"<script type='text/javascript' src='https://www.moip.com.br/transparente/MoipWidget-v2.js' charset='ISO-8859-1'></script>\"}" }
110
+ end
111
+
112
+ describe "#update_backer" do
113
+ before do
114
+ controller.stub(:backer).and_return(backer)
115
+ backer.stub(:payment_token).and_return('token')
116
+ end
117
+
118
+ context "with parameters containing CodigoMoIP and TaxaMoIP" do
119
+ let(:payment){ {"Status" => "Autorizado","Codigo" => "0","CodigoRetorno" => "0","TaxaMoIP" => "1.54","StatusPagamento" => "Sucesso","CodigoMoIP" => "18093844","Mensagem" => "Requisição processada com sucesso","TotalPago" => "25.00","url" => "https => //www.moip.com.br/Instrucao.do?token=R2W0N123E005F2A911V6O2I0Y3S7M4J853H0S0F0T0D044T8F4H4E9G0I3W8"} }
120
+ before do
121
+ MoIP.should_not_receive(:query)
122
+ backer.should_receive(:update_attributes).with({
123
+ payment_id: payment["CodigoMoIP"],
124
+ payment_choice: payment["FormaPagamento"],
125
+ payment_service_fee: payment["TaxaMoIP"]
126
+ })
127
+ end
128
+ it("should call update attributes but not call MoIP.query"){ controller.update_backer payment }
129
+ end
130
+
131
+ context "with no response from moip" do
132
+ let(:moip_query_response) { nil }
133
+ before do
134
+ MoIP.should_receive(:query).with(backer.payment_token).and_return(moip_query_response)
135
+ backer.should_not_receive(:update_attributes)
136
+ end
137
+ it("should never call update attributes"){ controller.update_backer }
138
+ end
139
+
140
+ context "with an incomplete transaction" do
141
+ let(:moip_query_response) do
142
+ {"ID"=>"201210191926185570000024694351", "Status"=>"Sucesso"}
143
+ end
144
+ before do
145
+ MoIP.should_receive(:query).with(backer.payment_token).and_return(moip_query_response)
146
+ backer.should_not_receive(:update_attributes)
147
+ end
148
+ it("should never call update attributes"){ controller.update_backer }
149
+ end
150
+
151
+ context "with a real data set that works for some cases" do
152
+ let(:moip_query_response) do
153
+ {"ID"=>"201210191926185570000024694351", "Status"=>"Sucesso", "Autorizacao"=>{"Pagador"=>{"Nome"=>"juliana.giopato@hotmail.com", "Email"=>"juliana.giopato@hotmail.com"}, "EnderecoCobranca"=>{"Logradouro"=>"Rua sócrates abraão ", "Numero"=>"16.0", "Complemento"=>"casa 19", "Bairro"=>"Campo Limpo", "CEP"=>"05782-470", "Cidade"=>"São Paulo", "Estado"=>"SP", "Pais"=>"BRA", "TelefoneFixo"=>"1184719963"}, "Recebedor"=>{"Nome"=>"Catarse", "Email"=>"financeiro@catarse.me"}, "Pagamento"=>[{"Data"=>"2012-10-17T13:06:07.000-03:00", "DataCredito"=>"2012-10-19T00:00:00.000-03:00", "TotalPago"=>"50.00", "TaxaParaPagador"=>"0.00", "TaxaMoIP"=>"1.34", "ValorLiquido"=>"48.66", "FormaPagamento"=>"BoletoBancario", "InstituicaoPagamento"=>"Bradesco", "Status"=>"Autorizado", "Parcela"=>{"TotalParcelas"=>"1"}, "CodigoMoIP"=>"0000.1325.5258"}, {"Data"=>"2012-10-17T13:05:49.000-03:00", "TotalPago"=>"50.00", "TaxaParaPagador"=>"0.00", "TaxaMoIP"=>"3.09", "ValorLiquido"=>"46.91", "FormaPagamento"=>"CartaoDebito", "InstituicaoPagamento"=>"Visa", "Status"=>"Iniciado", "Parcela"=>{"TotalParcelas"=>"1"}, "CodigoMoIP"=>"0000.1325.5248"}]}}
154
+ end
155
+ before do
156
+ MoIP.should_receive(:query).with(backer.payment_token).and_return(moip_query_response)
157
+ payment = moip_query_response["Autorizacao"]["Pagamento"].first
158
+ backer.should_receive(:update_attributes).with({
159
+ payment_id: payment["CodigoMoIP"],
160
+ payment_choice: payment["FormaPagamento"],
161
+ payment_service_fee: payment["TaxaMoIP"]
162
+ })
163
+ end
164
+ it("should call update attributes"){ controller.update_backer }
165
+ end
166
+ end
167
+
168
+ describe "#process_moip_message" do
169
+ before do
170
+ controller.stub(:backer).and_return(backer)
171
+ backer.stub(:confirmed?).and_return(false)
172
+ backer.stub(:confirm!)
173
+ controller.stub(:update_backer)
174
+ end
175
+
176
+ context "when we already have the payment_id in backers table" do
177
+ before do
178
+ backer.stub(:payment_id).and_return('test')
179
+ controller.should_not_receive(:update_backer)
180
+ end
181
+
182
+ it 'should never call update_backer' do
183
+ controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::AUTHORIZED})
184
+ end
185
+ end
186
+
187
+ context "when we still do not have the payment_id in backers table" do
188
+ before do
189
+ backer.stub(:payment_id).and_return(nil)
190
+ controller.should_receive(:update_backer)
191
+ end
192
+
193
+ it 'should call update_backer on the processor' do
194
+ controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::AUTHORIZED})
195
+ end
196
+ end
197
+
198
+ context "when there is a written back request and backer is not refunded" do
199
+ before do
200
+ backer.stub(:refunded?).and_return(false)
201
+ backer.should_receive(:refund!)
202
+ end
203
+
204
+ it 'should call refund!' do
205
+ controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::WRITTEN_BACK})
206
+ end
207
+ end
208
+
209
+ context "when there is an authorized request" do
210
+ before do
211
+ backer.should_receive(:confirm!)
212
+ end
213
+
214
+ it 'should call confirm!' do
215
+ controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::AUTHORIZED})
216
+ end
217
+ end
218
+
219
+ context "when there is a refund request" do
220
+ before do
221
+ backer.stub(:refunded?).and_return(false)
222
+ backer.should_receive(:refund!)
223
+ end
224
+
225
+ it 'should mark refunded to true' do
226
+ controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::REFUNDED})
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1 @@
1
+ id_transacao=928a335e1756249b5e3bee2d3cdf9942&valor=4000&status_pagamento=2&cod_moip=13847342&forma_pagamento=5&tipo_pagamento=CartaoDeCredito&parcelas=1&recebedor_login=softa&email_consumidor=murilo.morelli@gmail.com&cartao_bin=539028&cartao_final=0004&cartao_bandeira=Mastercard&cofre=cec1b177-102d-4320-a9ee-8f523ed1a310&classificacao=Falha na comunica��o com o Banco Emissor&action=create&controller=catarse_moip/payment/notifications
@@ -0,0 +1,25 @@
1
+ global.window = require("jsdom")
2
+ .jsdom()
3
+ .createWindow();
4
+ global.jQuery = global.$ = require("jquery");
5
+
6
+ global._ = global.underscore = require("./support/underscore");
7
+ global.Backbone = require("./support/backbone");
8
+ Backbone.$ = $;
9
+
10
+ global.Skull = require('./support/skull');
11
+ require('./support/app');
12
+ require("../../app/assets/javascripts/catarse_moip/moip_form");
13
+
14
+ describe("MoipForm", function() {
15
+ var view;
16
+
17
+ beforeEach(function() {
18
+ view = new App.views.MoipForm();
19
+ });
20
+
21
+ it("should be true", function() {
22
+ expect(true).toEqual(true);
23
+ });
24
+ });
25
+
@@ -0,0 +1 @@
1
+ global.App = window.App = Skull.View.extend({});