catarse_moip 2.3.6 → 2.4.0

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: 292517b36fffd4d8d67041e5a76bd6c8ad8194ba
4
- data.tar.gz: 9bff0bb7d02d42ad8db94c400bde5e2e7dc1051d
3
+ metadata.gz: 854311602c06cc63096b63f16afcd185651f9785
4
+ data.tar.gz: 408660bff34b36cd055506a66b41ab06c41a4c0f
5
5
  SHA512:
6
- metadata.gz: 098817dba512473eabd06ed765a7b7fc1e545312f7f93af322fed241a3dc5f45337f55485548dacce203c213792a2ffa37d2ef90a43b8d5b57662fe171917f17
7
- data.tar.gz: a0a9d44b71900a81a5552c9d443d3a595504e2b4145e05df26252d5bfb8a91855be1320fbbe072772f7891903d40a10f2fdcc5eaa6ee7c82d4e2ee7e88878201
6
+ metadata.gz: fb65373e9e9a4833fe388782bc5a54d32a714db952b6c7a360bf4101d1d30af47c8b3fdf1e410f4e642b5d32525f84f802b868a8ba70af5ac66993984636be8c
7
+ data.tar.gz: 8d5da792104b78a35ca04b81644a5c22ec0189173ce3cc92e24069380146e746b07ec718f524b2bea2110229f229eb86296c315a4c003ccced3b748da6b00d59
data/Gemfile.lock CHANGED
@@ -10,8 +10,9 @@ GIT
10
10
  PATH
11
11
  remote: .
12
12
  specs:
13
- catarse_moip (2.3.6)
13
+ catarse_moip (2.4.0)
14
14
  enumerate_it
15
+ httparty
15
16
  libxml-ruby (~> 2.6.0)
16
17
  rails (~> 4.0)
17
18
 
@@ -98,7 +99,7 @@ GEM
98
99
  rspec-core (~> 2.14.0)
99
100
  rspec-expectations (~> 2.14.0)
100
101
  rspec-mocks (~> 2.14.0)
101
- sprockets (2.12.2)
102
+ sprockets (2.12.3)
102
103
  hike (~> 1.2)
103
104
  multi_json (~> 1.0)
104
105
  rack (~> 1.0)
@@ -137,7 +137,7 @@ module CatarseMoip
137
137
  case params[:status_pagamento].to_i
138
138
  when TransactionStatus::PROCESS
139
139
  payment_notification.deliver_process_notification
140
- when TransactionStatus::AUTHORIZED
140
+ when TransactionStatus::AUTHORIZED, TransactionStatus::FINISHED
141
141
  contribution.confirm! unless contribution.confirmed?
142
142
  when TransactionStatus::WRITTEN_BACK, TransactionStatus::REFUNDED
143
143
  contribution.refund! unless contribution.refunded?
@@ -0,0 +1,121 @@
1
+ module CatarseMoip
2
+ module V2
3
+ class Refund
4
+ include ::HTTParty
5
+
6
+ base_uri ::MOIP_V2_ENDPOINT
7
+
8
+ attr_accessor :contribution, :refund_options
9
+
10
+ def self.start(contribution)
11
+ _instance = new(contribution)
12
+ _instance.refund
13
+ end
14
+
15
+ def initialize(contribution)
16
+ self.contribution = contribution
17
+ self.refund_options = define_refund_options
18
+ end
19
+
20
+ def refund
21
+ self.class.post("/v2/payments/#{parsed_payment_id}/refunds", {
22
+ body: self.refund_options.to_json
23
+ }.merge!(basic_auth_params))
24
+ end
25
+
26
+ def define_refund_options
27
+ if refund_method == 'BANK_ACCOUNT'
28
+ check_bank_account!
29
+ {
30
+ amount: amount_to_refund,
31
+ method: refund_method,
32
+ refundingInstrument: refund_instrument
33
+ }
34
+ else
35
+ {}
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def check_bank_account!
42
+ unless bank_account.valid?
43
+ raise "Invalid bank account"
44
+ end
45
+ end
46
+
47
+ def refund_instrument
48
+ {
49
+ method: refund_method,
50
+ bankAccount: {
51
+ type: 'CHECKING',
52
+ bankNumber: bank_account.bank_code,
53
+ agencyNumber: bank_account.agency.to_i,
54
+ agencyCheckNumber: bank_account.agency_digit.to_i,
55
+ accountNumber: bank_account.account.to_i,
56
+ accountCheckNumber: bank_account.account_digit.to_i,
57
+ holder: {
58
+ fullname: bank_account.owner_name,
59
+ taxDocument: {
60
+ type: bank_account_document_type,
61
+ number: parsed_holder_document
62
+ }
63
+ }
64
+ }
65
+ }
66
+ end
67
+
68
+ def bank_account
69
+ self.contribution.user.bank_account
70
+ end
71
+
72
+ def bank_account_document_type
73
+ if parsed_holder_document.size > 11
74
+ 'CNPJ'
75
+ else
76
+ 'CPF'
77
+ end
78
+ end
79
+
80
+ def parsed_holder_document
81
+ bank_account.owner_document.gsub(/[\.\-\/\s]/,'')
82
+ end
83
+
84
+ def amount_to_refund
85
+ if already_expired_refund_deadline?
86
+ ((self.contribution.value - self.contribution.payment_service_fee.to_f) * 100).to_i
87
+ else
88
+ self.contribution.price_in_cents
89
+ end
90
+ end
91
+
92
+ def refund_method
93
+ if already_expired_refund_deadline? || !self.contribution.is_credit_card?
94
+ 'BANK_ACCOUNT'
95
+ else
96
+ 'CREDIT_CARD'
97
+ end
98
+ end
99
+
100
+ def already_expired_refund_deadline?
101
+ self.contribution.confirmed_at < 170.days.ago
102
+ end
103
+
104
+ def basic_auth_params
105
+ {
106
+ basic_auth: {
107
+ username: PaymentEngines.configuration[:moip_token],
108
+ password: PaymentEngines.configuration[:moip_key],
109
+ },
110
+ headers: {
111
+ 'Content-Type' => 'application/json'
112
+ }
113
+ }
114
+ end
115
+
116
+ def parsed_payment_id
117
+ contribution.payment_id.gsub('.', '')
118
+ end
119
+ end
120
+ end
121
+ end
data/catarse_moip.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
19
 
20
20
  s.add_dependency "rails", "~> 4.0"
21
+ s.add_dependency "httparty"
21
22
  s.add_dependency('libxml-ruby', '~> 2.6.0')
22
23
  s.add_dependency "enumerate_it"
23
24
 
@@ -3,3 +3,13 @@
3
3
  config.token = (PaymentEngines.configuration.get_without_cache(:moip_token) rescue nil) || ''
4
4
  config.key = (PaymentEngines.configuration.get_without_cache(:moip_key) rescue nil) || ''
5
5
  end
6
+
7
+ MOIP_V2_ENDPOINT = begin
8
+ if PaymentEngines.configuration.get_without_cache(:moip_test) == 'true'
9
+ 'https://test.moip.com.br'
10
+ else
11
+ 'https://api.moip.com.br'
12
+ end
13
+ rescue Exception => e
14
+ 'https://test.moip.com.br'
15
+ end
data/lib/catarse_moip.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require "httparty"
1
2
  require "catarse_moip/engine"
2
3
  require "catarse_moip/payment_engine"
4
+
3
5
  #require "moip"
4
6
 
5
7
  module CatarseMoip
@@ -14,11 +14,11 @@ module CatarseMoip
14
14
  end
15
15
 
16
16
  def can_do_refund?
17
- false
17
+ true
18
18
  end
19
19
 
20
- def direct_refund
21
- false
20
+ def direct_refund contribution
21
+ ::CatarseMoip::V2::Refund.start(contribution)
22
22
  end
23
23
 
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module CatarseMoip
2
- VERSION = "2.3.6"
2
+ VERSION = "2.4.0"
3
3
  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.3.6
4
+ version: 2.4.0
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: 2014-09-18 00:00:00.000000000 Z
13
+ date: 2014-11-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -26,6 +26,20 @@ dependencies:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
28
  version: '4.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: httparty
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
29
43
  - !ruby/object:Gem::Dependency
30
44
  name: libxml-ruby
31
45
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +135,7 @@ files:
121
135
  - app/assets/javascripts/catarse_moip/payment_slip.js
122
136
  - app/assets/javascripts/catarse_moip/user_document.js
123
137
  - app/controllers/catarse_moip/moip_controller.rb
138
+ - app/models/catarse_moip/v2/refund.rb
124
139
  - app/views/catarse_moip/moip/review.html.slim
125
140
  - catarse_moip.gemspec
126
141
  - config/initializers/moip.rb