catarse_pagarme 2.7.5 → 2.7.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6718ed84954953775a70a8f15d1dea5f9602c8a2
4
- data.tar.gz: 0785da5c0b96d09f67f3e39520821fbc0cae3161
3
+ metadata.gz: f6cf63973dd886ba8c5d27c0b298f30812ffe9e9
4
+ data.tar.gz: 3ffbcb1761d21d21cbc5ad41015d277d6af966f9
5
5
  SHA512:
6
- metadata.gz: 591d72d0f85ce7a0ea7722c9cc7a42c3ffa90aceb6919b4c1dd5ac91c00273c49474ba220ce5b00201155f8444fc2818791a5d061e4877403ab8b8c985f1be77
7
- data.tar.gz: 6f5637f671ad7739714da3c2314fc3d6a810672475901f59696dcfaaf41cf0c6730d96afbdb9af33f0dfd798023be6a7836ac4590e68d8d6b8ccb3cab84c5f7a
6
+ metadata.gz: ab663e3744a0c647dedf9aaf15e4be62174213a7d833b0625816871797e1c5d6235bec1c87061997d95d567ea4e347a59b613f5e60a4c055d198bf02f2fb6109
7
+ data.tar.gz: 02bdb171b420db9455e647dc4d522bc78f51cb8a645835540af593bc31d059e04af7ca9488845e50a10575ca33b39c4150f2a67fbc0e8f3e970ff64a1080973a
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- catarse_pagarme (2.7.5)
4
+ catarse_pagarme (2.7.6)
5
5
  pagarme (= 1.10.0)
6
6
  rails (~> 4.0)
7
+ sidekiq
7
8
  weekdays (>= 1.0.2)
8
9
 
9
10
  GEM
@@ -37,7 +38,10 @@ GEM
37
38
  tzinfo (~> 1.1)
38
39
  arel (5.0.1.20140414130214)
39
40
  builder (3.2.2)
41
+ celluloid (0.16.0)
42
+ timers (~> 4.0.0)
40
43
  coderay (1.1.0)
44
+ connection_pool (2.2.0)
41
45
  diff-lcs (1.2.5)
42
46
  domain_name (0.5.24)
43
47
  unf (>= 0.0.5, < 1.0.0)
@@ -47,6 +51,7 @@ GEM
47
51
  factory_girl_rails (4.4.1)
48
52
  factory_girl (~> 4.4.0)
49
53
  railties (>= 3.0.0)
54
+ hitimes (1.2.2)
50
55
  http-cookie (1.0.2)
51
56
  domain_name (~> 0.5)
52
57
  i18n (0.6.11)
@@ -87,6 +92,9 @@ GEM
87
92
  rake (>= 0.8.7)
88
93
  thor (>= 0.18.1, < 2.0)
89
94
  rake (10.3.2)
95
+ redis (3.2.1)
96
+ redis-namespace (1.5.2)
97
+ redis (~> 3.0, >= 3.0.4)
90
98
  rest-client (1.8.0)
91
99
  http-cookie (>= 1.0.2, < 2.0)
92
100
  mime-types (>= 1.16, < 3.0)
@@ -103,6 +111,12 @@ GEM
103
111
  rspec-core (~> 2.14.0)
104
112
  rspec-expectations (~> 2.14.0)
105
113
  rspec-mocks (~> 2.14.0)
114
+ sidekiq (3.4.1)
115
+ celluloid (~> 0.16.0)
116
+ connection_pool (>= 2.1.1)
117
+ json
118
+ redis (>= 3.0.6)
119
+ redis-namespace (>= 1.3.1)
106
120
  slop (3.6.0)
107
121
  sprockets (3.2.0)
108
122
  rack (~> 1.0)
@@ -112,6 +126,8 @@ GEM
112
126
  sprockets (>= 2.8, < 4.0)
113
127
  thor (0.19.1)
114
128
  thread_safe (0.3.4)
129
+ timers (4.0.1)
130
+ hitimes
115
131
  treetop (1.4.15)
116
132
  polyglot
117
133
  polyglot (>= 0.3.1)
@@ -7,11 +7,13 @@ module CatarsePagarme
7
7
  self.transaction = PagarMe::Transaction.new(self.attributes)
8
8
 
9
9
  payment.update_attributes({
10
- gateway: 'Pagarme',
10
+ gateway: 'Pagarme',
11
11
  payment_method: payment_method
12
12
  })
13
13
  payment.save!
14
14
 
15
+ VerifyPagarmeWorker.perform_in(5.minutes, payment.key)
16
+
15
17
  self.transaction.charge
16
18
 
17
19
  change_payment_state
@@ -0,0 +1,43 @@
1
+ class CatarsePagarme::VerifyPagarmeWorker
2
+ include Sidekiq::Worker
3
+ sidekiq_options retry: 5
4
+
5
+ def perform key
6
+ source = find_source_by_key key
7
+ payment = PaymentEngines.find_payment({ key: key })
8
+
9
+ raise "payment not found" unless payment.present?
10
+ raise "source not found" unless source.present? && source.try(:[], "metadata").try(:[], "key") == key
11
+
12
+ payment.update_attributes({gateway_id: source["id"]})
13
+ payment.pagarme_delegator.update_transaction
14
+ payment.pagarme_delegator.change_status_by_transaction source["status"]
15
+ end
16
+
17
+ private
18
+
19
+ def find_source_by_key key
20
+ ::PagarMe.api_key = CatarsePagarme.configuration.api_key
21
+ request = PagarMe::Request.new('/search', 'GET')
22
+ query = {
23
+ type: 'transaction',
24
+ query: {
25
+ from: 0,
26
+ size: 1,
27
+ query: {
28
+ bool: {
29
+ must: {
30
+ match: {
31
+ "metadata.key" => key
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }.to_json
37
+ }
38
+
39
+ request.parameters.merge!(query)
40
+ response = request.run
41
+ response.try(:[], "hits").try(:[], "hits").try(:[], 0).try(:[], "_source")
42
+ end
43
+ end
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.add_dependency "rails", "~> 4.0"
21
21
  s.add_dependency "pagarme", "1.10.0"
22
22
  s.add_dependency "weekdays", ">= 1.0.2"
23
+ s.add_dependency "sidekiq"
23
24
 
24
25
  s.add_development_dependency "rspec-rails", "~> 2.14.0"
25
26
  s.add_development_dependency "factory_girl_rails"
@@ -1,7 +1,8 @@
1
- require 'pagarme'
1
+ require "pagarme"
2
2
  require "catarse_pagarme/engine"
3
3
  require "catarse_pagarme/configuration"
4
4
  require "catarse_pagarme/payment_engine"
5
+ require "sidekiq"
5
6
 
6
7
 
7
8
  module CatarsePagarme
@@ -1,3 +1,3 @@
1
1
  module CatarsePagarme
2
- VERSION = "2.7.5"
2
+ VERSION = "2.7.6"
3
3
  end
@@ -6,4 +6,8 @@ class PaymentEngines
6
6
  def self.find_contribution(id)
7
7
  Contribution.find id
8
8
  end
9
+
10
+ def self.find_payment filter
11
+ Payment.where(filter).first
12
+ end
9
13
  end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'pagarme'
7
7
  require 'open-uri'
8
8
  require 'rspec/rails'
9
9
  require 'factory_girl'
10
+ require 'sidekiq/testing'
10
11
  # Requires supporting ruby files with custom matchers and macros, etc,
11
12
  # in spec/support/ and its subdirectories.
12
13
  Dir[ File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
@@ -33,6 +34,8 @@ RSpec.configure do |config|
33
34
  config.before(:each) do
34
35
  PagarMe.stub(:api_key).and_return('ak_test_XLoo19QDn9kg5JFGU70x12IA4NqbAv')
35
36
  PaymentEngines.stub(:configuration).and_return({})
37
+ Sidekiq::Testing.inline!
38
+ CatarsePagarme::VerifyPagarmeWorker.stub(:perform_in).and_return(true)
36
39
  end
37
40
  end
38
41
 
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe CatarsePagarme::VerifyPagarmeWorker do
4
+ let(:payment) { create(:contribution).payments.last }
5
+ let(:fake_transaction) do
6
+ {
7
+ "object"=>"transaction", "status"=>"paid", "refuse_reason"=>nil, "id"=>9999,
8
+ "status_reason"=>"acquirer", "acquirer_name" => 'stone', "tid" => "123123", "acquirer_response_code"=>"0000",
9
+ "payment_method"=>"credit_card", "antifraud_score"=>nil, "boleto_url"=>nil, "boleto_barcode"=>nil, "boleto_expiration_date"=>nil,
10
+ "metadata"=>{
11
+ "key"=> payment.key,
12
+ "contribution_id"=>"21313"}
13
+ }
14
+ end
15
+
16
+ before do
17
+ allow_any_instance_of(CatarsePagarme::VerifyPagarmeWorker).to receive(:find_source_by_key).and_return(fake_transaction)
18
+ end
19
+
20
+ context "When find a valid payment and source" do
21
+ before do
22
+ #allow(PaymentEngines).to receive(:find_payment).and_return(payment)
23
+ allow_any_instance_of(CatarsePagarme::PaymentDelegator).to receive(:update_transaction)
24
+ allow_any_instance_of(CatarsePagarme::PaymentDelegator).to receive(:change_status_by_transaction)
25
+
26
+ expect_any_instance_of(Payment).to receive(:update_attributes).with({gateway_id: fake_transaction["id"]})
27
+ expect_any_instance_of(CatarsePagarme::PaymentDelegator).to receive(:update_transaction)
28
+ expect_any_instance_of(CatarsePagarme::PaymentDelegator).to receive(:change_status_by_transaction).with(fake_transaction["status"])
29
+ end
30
+
31
+ it "should satisfy expectations" do
32
+ CatarsePagarme::VerifyPagarmeWorker.perform_async(payment.key)
33
+ end
34
+ end
35
+
36
+ context "When source is not found" do
37
+ before do
38
+ allow_any_instance_of(CatarsePagarme::VerifyPagarmeWorker).to receive(:find_source_by_key).and_return(nil)
39
+ end
40
+
41
+ it "should raise an error" do
42
+ expect {
43
+ CatarsePagarme::VerifyPagarmeWorker.perform_async(payment.key)
44
+ }.to raise_error("source not found")
45
+ end
46
+ end
47
+
48
+ context "When source keys not match" do
49
+ before do
50
+ allow_any_instance_of(CatarsePagarme::VerifyPagarmeWorker).to receive(:find_source_by_key).and_return(fake_transaction["metadata"].update({"key" => "AFSGSD"}))
51
+ end
52
+
53
+ it "should raise an error" do
54
+ expect {
55
+ CatarsePagarme::VerifyPagarmeWorker.perform_async(payment.key)
56
+ }.to raise_error("source not found")
57
+ end
58
+ end
59
+
60
+ context "When payment is not found" do
61
+ it "should raise an error" do
62
+ expect {
63
+ CatarsePagarme::VerifyPagarmeWorker.perform_async('INVALID KEY')
64
+ }.to raise_error("payment not found")
65
+ end
66
+ end
67
+ end
68
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catarse_pagarme
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.5
4
+ version: 2.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antônio Roberto Silva
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: 1.0.2
56
+ - !ruby/object:Gem::Dependency
57
+ name: sidekiq
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: rspec-rails
58
72
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +147,7 @@ files:
133
147
  - app/views/catarse_pagarme/pagarme/_terms.html.slim
134
148
  - app/views/catarse_pagarme/pagarme/review.html.slim
135
149
  - app/views/layouts/catarse_pagarme/application.html.erb
150
+ - app/workers/catarse_pagarme/verify_pagarme_worker.rb
136
151
  - bin/rails
137
152
  - catarse_pagarme.gemspec
138
153
  - config/initializers/register.rb
@@ -207,6 +222,7 @@ files:
207
222
  - spec/models/catarse_pagarme/slip_transaction_spec.rb
208
223
  - spec/spec_helper.rb
209
224
  - spec/support/factories.rb
225
+ - spec/workers/catarse_pagarme/verify_pagarme_worker_spec.rb
210
226
  homepage: http://github.com/catarse/catarse_pagarme
211
227
  licenses: []
212
228
  metadata: {}
@@ -295,3 +311,4 @@ test_files:
295
311
  - spec/models/catarse_pagarme/slip_transaction_spec.rb
296
312
  - spec/spec_helper.rb
297
313
  - spec/support/factories.rb
314
+ - spec/workers/catarse_pagarme/verify_pagarme_worker_spec.rb