catarse_moip 2.0.1 → 2.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83e89215563372e631f8e7ed7b820debbc3ba7aa
4
- data.tar.gz: 41e6c16596b3ff5093e646f0e061bbd9efe84323
3
+ metadata.gz: c4b6b87c36747d772f7b9b7f7d3e3ea6227da474
4
+ data.tar.gz: e8964a173de2359f22ecc15de62a77edbb442a81
5
5
  SHA512:
6
- metadata.gz: 2cab1523a7fc3aa5bf0b5cb9c4e65fb55660f5db91870fa3463d1983a7b8b3e932fc3d126d9da48b183e44d091ddf1bc64d7d35b3edaf0433a17f1c3c4f8924f
7
- data.tar.gz: 992d107609239c1176c9743963d56cb99cba72b8e8abd7636e96fe4adc5afee85911ac899cd712a5f0b81918b10da7e1ff6c2bb1731cc603b637c8f625d1c050
6
+ metadata.gz: 7c88efa88b974a0c914f5152daa33ff6e093b7d4015ea213e3b11bf615ac113555f01cc6b7c722ed1533c481acdefb547601043b96b3e7443fbb02d318002116
7
+ data.tar.gz: d626a0c2b0db361985f8681167f556ee6047632b947f7297e71b5901853b8d3aa604108e5f0e8c0b8cd55b889901da20a9555641175321210a74aec2146695b8
data/Gemfile.lock CHANGED
@@ -10,7 +10,7 @@ GIT
10
10
  PATH
11
11
  remote: .
12
12
  specs:
13
- catarse_moip (2.0.1)
13
+ catarse_moip (2.0.2)
14
14
  libxml-ruby (~> 2.6.0)
15
15
  rails (~> 4.0)
16
16
 
@@ -63,7 +63,7 @@ GEM
63
63
  mail (2.5.4)
64
64
  mime-types (~> 1.16)
65
65
  treetop (~> 1.4.8)
66
- mime-types (1.24)
66
+ mime-types (1.25)
67
67
  minitest (4.7.5)
68
68
  multi_json (1.7.9)
69
69
  nokogiri (1.5.10)
@@ -22,7 +22,7 @@ module CatarseMoip
22
22
 
23
23
  def create_notification
24
24
  @backer = PaymentEngines.find_payment key: params[:id_transacao]
25
- process_moip_message(params)
25
+ process_moip_message
26
26
  return render :nothing => true, :status => 200
27
27
  rescue Exception => e
28
28
  return render :text => "#{e.inspect}: #{e.message} recebemos: #{params}", :status => 422
@@ -46,7 +46,7 @@ module CatarseMoip
46
46
 
47
47
  def moip_response
48
48
  @backer = PaymentEngines.find_payment id: params[:id], user_id: current_user.id
49
- process_moip_message params[:response] unless params[:response]['StatusPagamento'] == 'Falha'
49
+ first_update_backer params[:response] unless params[:response]['StatusPagamento'] == 'Falha'
50
50
  render nothing: true, status: 200
51
51
  end
52
52
 
@@ -97,37 +97,36 @@ module CatarseMoip
97
97
  }
98
98
  end
99
99
 
100
- def update_backer params = nil
101
- unless params && params["CodigoMoIP"] && params["TaxaMoIP"]
102
- response = ::MoIP.query(backer.payment_token)
103
- if response && response["Autorizacao"]
104
- params = response["Autorizacao"]["Pagamento"]
105
- params = params.first unless params.respond_to?(:key)
106
- end
107
- end
108
-
109
- if params
100
+ def first_update_backer
101
+ response = ::MoIP.query(backer.payment_token)
102
+ if response && response["Autorizacao"]
103
+ params = response["Autorizacao"]["Pagamento"]
104
+ params = params.first unless params.respond_to?(:key)
110
105
  backer.update_attributes({
111
106
  :payment_id => params["CodigoMoIP"],
112
107
  :payment_choice => params["FormaPagamento"],
113
108
  :payment_service_fee => params["TaxaMoIP"]
114
- })
109
+ }) if params
115
110
  end
116
111
  end
117
112
 
118
- def process_moip_message params
113
+ def process_moip_message
119
114
  PaymentEngines.create_payment_notification backer_id: backer.id, extra_data: JSON.parse(params.to_json.force_encoding('iso-8859-1').encode('utf-8'))
115
+
120
116
  backer.with_lock do
121
- update_backer if backer.payment_id.nil?
122
- case params[:status_pagamento].to_i
123
- when TransactionStatus::AUTHORIZED
124
- backer.confirm! unless backer.confirmed?
125
- when TransactionStatus::WRITTEN_BACK, TransactionStatus::REFUNDED
126
- backer.refund! unless backer.refunded?
127
- when TransactionStatus::CANCELED
128
- backer.cancel! unless backer.canceled?
129
- else
130
- backer.waiting! if backer.pending?
117
+ if backer.payment_id.gsub(".", "").to_i < params[:cod_moip].to_i
118
+ backer.update_attributes payment_id: params[:cod_moip]
119
+
120
+ case params[:status_pagamento].to_i
121
+ when TransactionStatus::AUTHORIZED
122
+ backer.confirm! unless backer.confirmed?
123
+ when TransactionStatus::WRITTEN_BACK, TransactionStatus::REFUNDED
124
+ backer.refund! unless backer.refunded?
125
+ when TransactionStatus::CANCELED
126
+ backer.cancel! unless backer.canceled?
127
+ else
128
+ backer.waiting! if backer.pending?
129
+ end
131
130
  end
132
131
  end
133
132
  end
@@ -1,3 +1,3 @@
1
1
  module CatarseMoip
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
@@ -6,13 +6,13 @@ describe CatarseMoip::MoipController do
6
6
 
7
7
  let(:get_token_response){{:status=>:fail, :code=>"171", :message=>"TelefoneFixo do endereço deverá ser enviado obrigatorio", :id=>"201210192052439150000024698931"}}
8
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,
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
16
  payer_name: 'foo',
17
17
  payer_email: 'foo@bar.com',
18
18
  address_street: 'test',
@@ -22,7 +22,13 @@ describe CatarseMoip::MoipController do
22
22
  address_city: '123',
23
23
  address_state: '123',
24
24
  address_zip_code: '123',
25
- address_phone_number: '123'
25
+ address_phone_number: '123',
26
+ confirmed?: true,
27
+ confirm!: true,
28
+ canceled?: true,
29
+ cancel!: true,
30
+ refunded?: true,
31
+ refund!: true
26
32
  }) }
27
33
 
28
34
  let(:user){ double('user', id: 1) }
@@ -53,8 +59,39 @@ describe CatarseMoip::MoipController do
53
59
 
54
60
  context "when we seach for an existing backer" do
55
61
  before do
62
+ controller.stub(:params).and_return({:id_transacao =>backer.key, :controller => "catarse_moip/moip", :action => "create_notification"})
56
63
  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"})
64
+ controller.should_receive(:process_moip_message)
65
+ post :create_notification, {:id_transacao => backer.key, :use_route => 'catarse_moip'}
66
+ end
67
+
68
+ its(:body){ should == ' ' }
69
+ its(:status){ should == 200 }
70
+ it("should assign backer"){ assigns(:backer).should == backer }
71
+ end
72
+
73
+ context "when receive ordered notification for backer" do
74
+ before do
75
+ controller.stub(:params).and_return({:cod_moip => 125, :id_transacao =>backer.key, :controller => "catarse_moip/moip", :action => "create_notification", :status_pagamento => 5})
76
+ backer.stub(:payment_id).and_return('123')
77
+
78
+ controller.should_receive(:process_moip_message).and_call_original
79
+ backer.should_receive(:update_attributes).with(payment_id: 125)
80
+ post :create_notification, {:id_transacao => backer.key, :use_route => 'catarse_moip'}
81
+ end
82
+
83
+ its(:body){ should == ' ' }
84
+ its(:status){ should == 200 }
85
+ it("should assign backer"){ assigns(:backer).should == backer }
86
+ end
87
+
88
+ context "when receive a unordered notification for backer" do
89
+ before do
90
+ controller.stub(:params).and_return({:cod_moip => 122, :id_transacao =>backer.key, :controller => "catarse_moip/moip", :action => "create_notification", :status_pagamento => 5})
91
+ backer.stub(:payment_id).and_return('123')
92
+
93
+ controller.should_receive(:process_moip_message).and_call_original
94
+ backer.should_not_receive(:update_attributes).with(payment_id: 122)
58
95
  post :create_notification, {:id_transacao => backer.key, :use_route => 'catarse_moip'}
59
96
  end
60
97
 
@@ -69,7 +106,7 @@ describe CatarseMoip::MoipController do
69
106
 
70
107
  context "when the content of get_javascript_url raises an error" do
71
108
  before do
72
- controller.should_receive(:open).at_least(3).times.and_raise('error')
109
+ controller.should_receive(:open).at_least(3).times.and_raise('error')
73
110
  ->{
74
111
  get :js, locale: :pt, use_route: 'catarse_moip'
75
112
  }.should raise_error('error')
@@ -79,7 +116,7 @@ describe CatarseMoip::MoipController do
79
116
 
80
117
  context "when the content of get_javascript_url is read without errors" do
81
118
  before do
82
- controller.should_receive(:open).and_return(file)
119
+ controller.should_receive(:open).and_return(file)
83
120
  file.should_receive(:set_encoding).and_return(file)
84
121
  file.should_receive(:read).and_return(file)
85
122
  file.should_receive(:encode).and_return('js content')
@@ -93,7 +130,7 @@ describe CatarseMoip::MoipController do
93
130
  describe "POST moip_response" do
94
131
  let(:processor){ double('moip processor') }
95
132
  before do
96
- controller.should_receive(:process_moip_message)
133
+ controller.should_receive(:first_update_backer)
97
134
  post :moip_response, id: backer.id, response: {StatusPagamento: 'Sucesso'}, use_route: 'catarse_moip'
98
135
  end
99
136
 
@@ -109,32 +146,19 @@ describe CatarseMoip::MoipController do
109
146
  its(:body){ should == "{\"get_token_response\":{\"status\":\"fail\",\"code\":\"171\",\"message\":\"TelefoneFixo do endereço deverá ser enviado obrigatorio\",\"id\":\"201210192052439150000024698931\"},\"moip\":\"{}\",\"widget_tag\":{\"tag_id\":\"MoipWidget\",\"token\":null,\"callback_success\":\"checkoutSuccessful\",\"callback_error\":\"checkoutFailure\"}}" }
110
147
  end
111
148
 
112
- describe "#update_backer" do
149
+ describe "#first_update_backer" do
113
150
  before do
114
151
  controller.stub(:backer).and_return(backer)
115
152
  backer.stub(:payment_token).and_return('token')
116
153
  end
117
154
 
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
155
  context "with no response from moip" do
132
156
  let(:moip_query_response) { nil }
133
157
  before do
134
158
  MoIP.should_receive(:query).with(backer.payment_token).and_return(moip_query_response)
135
159
  backer.should_not_receive(:update_attributes)
136
160
  end
137
- it("should never call update attributes"){ controller.update_backer }
161
+ it("should never call update attributes"){ controller.first_update_backer }
138
162
  end
139
163
 
140
164
  context "with an incomplete transaction" do
@@ -145,7 +169,7 @@ describe CatarseMoip::MoipController do
145
169
  MoIP.should_receive(:query).with(backer.payment_token).and_return(moip_query_response)
146
170
  backer.should_not_receive(:update_attributes)
147
171
  end
148
- it("should never call update attributes"){ controller.update_backer }
172
+ it("should never call update attributes"){ controller.first_update_backer }
149
173
  end
150
174
 
151
175
  context "with a real data set that works for some cases" do
@@ -161,7 +185,7 @@ describe CatarseMoip::MoipController do
161
185
  payment_service_fee: payment["TaxaMoIP"]
162
186
  })
163
187
  end
164
- it("should call update attributes"){ controller.update_backer }
188
+ it("should call update attributes"){ controller.first_update_backer }
165
189
  end
166
190
  end
167
191
 
@@ -173,57 +197,41 @@ describe CatarseMoip::MoipController do
173
197
  controller.stub(:update_backer)
174
198
  end
175
199
 
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
200
  context "when there is a written back request and backer is not refunded" do
199
201
  before do
202
+ controller.stub(:params).and_return(post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::WRITTEN_BACK}))
200
203
  backer.stub(:refunded?).and_return(false)
201
204
  backer.should_receive(:refund!)
205
+ backer.should_receive(:update_attributes)
202
206
  end
203
207
 
204
208
  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})
209
+ controller.process_moip_message
206
210
  end
207
211
  end
208
212
 
209
213
  context "when there is an authorized request" do
210
214
  before do
215
+ controller.stub(:params).and_return(post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::AUTHORIZED}))
211
216
  backer.should_receive(:confirm!)
217
+ backer.should_receive(:update_attributes)
212
218
  end
213
219
 
214
220
  it 'should call confirm!' do
215
- controller.process_moip_message post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::AUTHORIZED})
221
+ controller.process_moip_message
216
222
  end
217
223
  end
218
224
 
219
225
  context "when there is a refund request" do
220
226
  before do
227
+ controller.stub(:params).and_return(post_moip_params.merge!({:id_transacao => backer.key, :status_pagamento => CatarseMoip::MoipController::TransactionStatus::REFUNDED}))
221
228
  backer.stub(:refunded?).and_return(false)
222
229
  backer.should_receive(:refund!)
230
+ backer.should_receive(:update_attributes)
223
231
  end
224
232
 
225
233
  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})
234
+ controller.process_moip_message
227
235
  end
228
236
  end
229
237
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catarse_moip
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antônio Roberto Silva
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-08-21 00:00:00.000000000 Z
13
+ date: 2013-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  version: '0'
185
185
  requirements: []
186
186
  rubyforge_project:
187
- rubygems_version: 2.0.7
187
+ rubygems_version: 2.0.3
188
188
  signing_key:
189
189
  specification_version: 4
190
190
  summary: MoIP integration with Catarse