catarse_pagarme 2.13.0 → 2.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/Gemfile.lock +28 -16
- data/app/controllers/catarse_pagarme/credit_cards_controller.rb +2 -3
- data/app/models/catarse_pagarme/antifraud_order_wrapper.rb +148 -0
- data/app/models/catarse_pagarme/balance_transfer_delegator.rb +5 -1
- data/app/models/catarse_pagarme/credit_card_transaction.rb +48 -11
- data/app/models/catarse_pagarme/payment_delegator.rb +7 -3
- data/catarse_pagarme.gemspec +2 -1
- data/lib/catarse_pagarme.rb +2 -1
- data/lib/catarse_pagarme/configuration.rb +2 -1
- data/lib/catarse_pagarme/version.rb +1 -1
- data/spec/dummy/app/models/payment.rb +4 -0
- data/spec/dummy/app/models/payment_engines.rb +4 -0
- data/spec/dummy/config/initializers/pagarme.rb +1 -0
- data/spec/models/catarse_pagarme/antifraud_order_wrapper_spec.rb +366 -0
- data/spec/models/catarse_pagarme/credit_card_transaction_spec.rb +225 -37
- data/spec/models/catarse_pagarme/payment_delegator_spec.rb +33 -15
- data/spec/support/factories.rb +5 -5
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 759be41182016de2403b4d8dda77eb30691dbd1f
|
4
|
+
data.tar.gz: f161dd3654541d5ced3e5c0a7dd07dfb08fb82d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0c95c06bde334b295b76b41c5f739b9ddcd4e5c67a0267160e2ba52f0f68f88ccb046c1e7c52cc5f9604585d5bed4c4b79281428d0eb3839cb3dc0dd8735435
|
7
|
+
data.tar.gz: a30bcc3414beebaf09ee900ffb777fbd7b16094bcd0188cac420b65b756b1dcbee22201e341d3ea45e787381f1b84d7369b3c378bba9032e2952b59453bdcf84
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
catarse_pagarme
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
catarse_pagarme (2.
|
4
|
+
catarse_pagarme (2.14.0)
|
5
|
+
countries (= 3.0.0)
|
6
|
+
konduto-ruby (= 2.1.0)
|
5
7
|
pagarme (= 2.1.4)
|
6
8
|
rails (~> 4.0)
|
7
9
|
sentry-raven
|
@@ -50,10 +52,14 @@ GEM
|
|
50
52
|
coderay (1.1.2)
|
51
53
|
concurrent-ruby (1.0.5)
|
52
54
|
connection_pool (2.2.2)
|
55
|
+
countries (3.0.0)
|
56
|
+
i18n_data (~> 0.8.0)
|
57
|
+
sixarm_ruby_unaccent (~> 1.1)
|
58
|
+
unicode_utils (~> 1.4)
|
53
59
|
crass (1.0.4)
|
54
60
|
database_cleaner (1.7.0)
|
55
61
|
diff-lcs (1.3)
|
56
|
-
domain_name (0.5.
|
62
|
+
domain_name (0.5.20190701)
|
57
63
|
unf (>= 0.0.5, < 1.0.0)
|
58
64
|
erubis (2.7.0)
|
59
65
|
factory_girl (4.9.0)
|
@@ -61,40 +67,43 @@ GEM
|
|
61
67
|
factory_girl_rails (4.9.0)
|
62
68
|
factory_girl (~> 4.9.0)
|
63
69
|
railties (>= 3.0.0)
|
64
|
-
faraday (0.
|
70
|
+
faraday (0.17.0)
|
65
71
|
multipart-post (>= 1.2, < 3)
|
66
72
|
globalid (0.4.2)
|
67
73
|
activesupport (>= 4.2.0)
|
74
|
+
http-accept (1.7.0)
|
68
75
|
http-cookie (1.0.3)
|
69
76
|
domain_name (~> 0.5)
|
70
77
|
i18n (0.9.5)
|
71
78
|
concurrent-ruby (~> 1.0)
|
79
|
+
i18n_data (0.8.0)
|
80
|
+
konduto-ruby (2.1.0)
|
72
81
|
loofah (2.2.2)
|
73
82
|
crass (~> 1.0.2)
|
74
83
|
nokogiri (>= 1.5.9)
|
75
84
|
mail (2.7.1)
|
76
85
|
mini_mime (>= 0.1.1)
|
77
86
|
method_source (0.9.0)
|
78
|
-
mime-types (3.
|
87
|
+
mime-types (3.3)
|
79
88
|
mime-types-data (~> 3.2015)
|
80
|
-
mime-types-data (3.
|
81
|
-
mini_mime (1.0.
|
89
|
+
mime-types-data (3.2019.1009)
|
90
|
+
mini_mime (1.0.2)
|
82
91
|
mini_portile2 (2.3.0)
|
83
92
|
minitest (5.11.3)
|
84
|
-
multi_json (1.
|
85
|
-
multipart-post (2.
|
93
|
+
multi_json (1.14.1)
|
94
|
+
multipart-post (2.1.1)
|
86
95
|
netrc (0.11.0)
|
87
96
|
nokogiri (1.8.4)
|
88
97
|
mini_portile2 (~> 2.3.0)
|
89
98
|
pagarme (2.1.4)
|
90
99
|
multi_json
|
91
100
|
rest-client
|
92
|
-
pg (
|
101
|
+
pg (0.15.0)
|
93
102
|
pry (0.11.3)
|
94
103
|
coderay (~> 1.1.0)
|
95
104
|
method_source (~> 0.9.0)
|
96
105
|
rack (1.6.10)
|
97
|
-
rack-protection (2.0.
|
106
|
+
rack-protection (2.0.7)
|
98
107
|
rack
|
99
108
|
rack-test (0.6.3)
|
100
109
|
rack (>= 1.0)
|
@@ -123,8 +132,9 @@ GEM
|
|
123
132
|
rake (>= 0.8.7)
|
124
133
|
thor (>= 0.18.1, < 2.0)
|
125
134
|
rake (12.3.1)
|
126
|
-
redis (4.1.
|
127
|
-
rest-client (2.0
|
135
|
+
redis (4.1.3)
|
136
|
+
rest-client (2.1.0)
|
137
|
+
http-accept (>= 1.7.0, < 2.0)
|
128
138
|
http-cookie (>= 1.0.2, < 2.0)
|
129
139
|
mime-types (>= 1.16, < 4.0)
|
130
140
|
netrc (~> 0.8)
|
@@ -145,13 +155,14 @@ GEM
|
|
145
155
|
rspec-mocks (~> 3.8.0)
|
146
156
|
rspec-support (~> 3.8.0)
|
147
157
|
rspec-support (3.8.0)
|
148
|
-
sentry-raven (2.
|
158
|
+
sentry-raven (2.12.3)
|
149
159
|
faraday (>= 0.7.6, < 1.0)
|
150
|
-
sidekiq (5.2.
|
160
|
+
sidekiq (5.2.7)
|
151
161
|
connection_pool (~> 2.2, >= 2.2.2)
|
152
162
|
rack (>= 1.5.0)
|
153
163
|
rack-protection (>= 1.5.0)
|
154
164
|
redis (>= 3.3.5, < 5)
|
165
|
+
sixarm_ruby_unaccent (1.2.0)
|
155
166
|
sprockets (3.7.2)
|
156
167
|
concurrent-ruby (~> 1.0)
|
157
168
|
rack (> 1, < 3)
|
@@ -165,7 +176,8 @@ GEM
|
|
165
176
|
thread_safe (~> 0.1)
|
166
177
|
unf (0.1.4)
|
167
178
|
unf_ext
|
168
|
-
unf_ext (0.0.7.
|
179
|
+
unf_ext (0.0.7.6)
|
180
|
+
unicode_utils (1.4.0)
|
169
181
|
weekdays (1.0.2)
|
170
182
|
|
171
183
|
PLATFORMS
|
@@ -180,4 +192,4 @@ DEPENDENCIES
|
|
180
192
|
rspec-rails (~> 3.3)
|
181
193
|
|
182
194
|
BUNDLED WITH
|
183
|
-
1.
|
195
|
+
1.17.3
|
@@ -4,7 +4,7 @@ module CatarsePagarme
|
|
4
4
|
MAX_SOFT_DESCRIPTOR_LENGTH = 13
|
5
5
|
|
6
6
|
def create
|
7
|
-
transaction = CreditCardTransaction.new(credit_card_attributes, payment).
|
7
|
+
transaction = CreditCardTransaction.new(credit_card_attributes, payment).process!
|
8
8
|
|
9
9
|
render json: { payment_status: transaction.status }
|
10
10
|
rescue PagarMe::PagarMeError, PagarMe::ValidationError => e
|
@@ -142,7 +142,6 @@ module CatarsePagarme
|
|
142
142
|
end
|
143
143
|
|
144
144
|
def af_metadata
|
145
|
-
return {} unless CatarsePagarme.configuration.use_simility
|
146
145
|
project = contribution.project
|
147
146
|
user = contribution.user
|
148
147
|
|
@@ -235,7 +234,7 @@ module CatarsePagarme
|
|
235
234
|
id: contribution.project_id.to_s,
|
236
235
|
name: contribution.project.name,
|
237
236
|
type: contribution.project.mode == 'flex' ? 'flex' : 'full',
|
238
|
-
date: contribution.project.
|
237
|
+
date: contribution.project.created_at.to_s,
|
239
238
|
venue_name: project.user.name,
|
240
239
|
address: {
|
241
240
|
country: "Brasil",
|
@@ -0,0 +1,148 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
class AntifraudOrderWrapper
|
3
|
+
attr_accessor :attributes, :transaction
|
4
|
+
|
5
|
+
def initialize(attributes, transaction)
|
6
|
+
self.attributes = attributes
|
7
|
+
self.transaction = transaction
|
8
|
+
end
|
9
|
+
|
10
|
+
def send(analyze:)
|
11
|
+
order = build_order(analyze: analyze)
|
12
|
+
client.analyze(order)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def client
|
18
|
+
konduto_api_key = CatarsePagarme.configuration.konduto_api_key
|
19
|
+
@client ||= KondutoRuby.new(konduto_api_key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def build_order(analyze:)
|
23
|
+
KondutoOrder.new(
|
24
|
+
order_attributes.merge({
|
25
|
+
analyze: analyze,
|
26
|
+
customer: build_customer,
|
27
|
+
payment: build_payment,
|
28
|
+
billing: build_billing_address,
|
29
|
+
shipping: build_shipping_address,
|
30
|
+
shopping_cart: build_shopping_cart,
|
31
|
+
seller: build_seller
|
32
|
+
})
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_customer
|
37
|
+
KondutoCustomer.new(customer_attributes)
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_payment
|
41
|
+
[KondutoPayment.new(payment_attributes)]
|
42
|
+
end
|
43
|
+
|
44
|
+
def build_billing_address
|
45
|
+
KondutoAddress.new(billing_address_attributes)
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_shipping_address
|
49
|
+
KondutoAddress.new(shipping_address_attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_shopping_cart
|
53
|
+
[KondutoItem.new(item_attributes)]
|
54
|
+
end
|
55
|
+
|
56
|
+
def build_seller
|
57
|
+
KondutoSeller.new(seller_attributes)
|
58
|
+
end
|
59
|
+
|
60
|
+
def order_attributes
|
61
|
+
{
|
62
|
+
id: self.transaction.id.to_s,
|
63
|
+
total_amount: self.attributes[:amount] / 100.0,
|
64
|
+
visitor: self.attributes.dig(:metadata, :contribution_id).to_s,
|
65
|
+
currency: 'BRL',
|
66
|
+
installments: self.attributes[:installments],
|
67
|
+
purchased_at: self.transaction.date_created,
|
68
|
+
ip: self.attributes.dig(:antifraud_metadata, :ip)
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
def customer_attributes
|
73
|
+
customer = self.attributes.dig(:customer)
|
74
|
+
{
|
75
|
+
id: customer[:document_number],
|
76
|
+
name: customer[:name],
|
77
|
+
email: customer[:email],
|
78
|
+
phone1: customer[:phone].to_h.values.join,
|
79
|
+
created_at: self.attributes.dig(:antifraud_metadata, :register, :registered_at)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
def payment_attributes
|
84
|
+
{
|
85
|
+
type: 'credit',
|
86
|
+
status: self.transaction.status == 'authorized' ? 'approved' : 'declined',
|
87
|
+
bin: self.transaction.card.first_digits,
|
88
|
+
last4: self.transaction.card.last_digits,
|
89
|
+
expiration_date: card_expiration_date
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def billing_address_attributes
|
94
|
+
billing_data = self.attributes.dig(:antifraud_metadata, :billing)
|
95
|
+
{
|
96
|
+
name: self.attributes.dig(:customer, :name),
|
97
|
+
address1: billing_data.dig(:address, :street),
|
98
|
+
city: billing_data.dig(:address, :city),
|
99
|
+
state: billing_data.dig(:address, :state),
|
100
|
+
zip: billing_data.dig(:address, :zipcode),
|
101
|
+
country: card_country_code
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
def shipping_address_attributes
|
106
|
+
shipping_data = self.attributes.dig(:antifraud_metadata, :shipping)
|
107
|
+
{
|
108
|
+
name: shipping_data.dig(:customer, :name),
|
109
|
+
address1: shipping_data.dig(:address, :street),
|
110
|
+
city: shipping_data.dig(:address, :city),
|
111
|
+
state: shipping_data.dig(:address, :state),
|
112
|
+
zip: shipping_data.dig(:address, :zipcode)
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
def item_attributes
|
117
|
+
shopping_cart_data = self.attributes.dig(:antifraud_metadata, :shopping_cart).first
|
118
|
+
{
|
119
|
+
sku: self.attributes.dig(:metadata, :contribution_id).to_s,
|
120
|
+
product_code: self.attributes.dig(:metadata, :contribution_id).to_s,
|
121
|
+
category: 9999,
|
122
|
+
name: shopping_cart_data[:name],
|
123
|
+
unit_cost: self.attributes[:amount] / 100.0,
|
124
|
+
quantity: 1,
|
125
|
+
created_at: self.attributes.dig(:metadata, :project_online).to_s[0..9]
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
def seller_attributes
|
130
|
+
event_data = self.attributes.dig(:antifraud_metadata, :events).first
|
131
|
+
{
|
132
|
+
id: event_data[:id],
|
133
|
+
name: event_data[:venue_name],
|
134
|
+
created_at: event_data[:date]
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
def card_expiration_date
|
139
|
+
expiration_date = self.transaction.card.expiration_date
|
140
|
+
"#{expiration_date[0..1]}20#{expiration_date[2..3]}"
|
141
|
+
end
|
142
|
+
|
143
|
+
def card_country_code
|
144
|
+
country = ::ISO3166::Country.find_country_by_name(self.transaction.card.country)
|
145
|
+
country.alpha2
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -17,7 +17,11 @@ module CatarsePagarme
|
|
17
17
|
|
18
18
|
transfer = PagarMe::Transfer.new({
|
19
19
|
bank_account_id: bank_account.id,
|
20
|
-
amount: value_for_transaction
|
20
|
+
amount: value_for_transaction,
|
21
|
+
metadata: {
|
22
|
+
balance_transfer_id: balance_transfer.id,
|
23
|
+
seed: SecureRandom.hex(4)
|
24
|
+
}
|
21
25
|
})
|
22
26
|
transfer.create
|
23
27
|
raise "unable to create a transfer" unless transfer.id.present?
|
@@ -1,31 +1,68 @@
|
|
1
1
|
module CatarsePagarme
|
2
2
|
class CreditCardTransaction < TransactionBase
|
3
|
+
def process!
|
4
|
+
authorize!
|
5
|
+
if self.transaction.status == 'authorized'
|
6
|
+
if was_credit_card_used_before?
|
7
|
+
self.transaction.capture
|
8
|
+
else
|
9
|
+
antifraud_outcome = process_antifraud
|
3
10
|
|
4
|
-
|
5
|
-
|
11
|
+
if antifraud_outcome.recommendation == :APPROVE
|
12
|
+
self.transaction.capture
|
13
|
+
elsif antifraud_outcome.recommendation == :DECLINE
|
14
|
+
self.transaction.refund
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
6
18
|
|
7
|
-
|
19
|
+
change_payment_state
|
8
20
|
|
9
|
-
|
10
|
-
|
11
|
-
payment_method: payment_method })
|
21
|
+
self.transaction
|
22
|
+
end
|
12
23
|
|
13
|
-
|
14
|
-
|
24
|
+
def authorize!
|
25
|
+
save_card = self.attributes.delete(:save_card)
|
26
|
+
|
27
|
+
self.transaction = PagarMe::Transaction.new(
|
28
|
+
amount: self.attributes[:amount],
|
29
|
+
card_hash: self.attributes[:card_hash],
|
30
|
+
capture: false,
|
31
|
+
async: false,
|
32
|
+
postback_url: self.attributes[:postback_url]
|
33
|
+
)
|
34
|
+
|
35
|
+
unless payment.update_attributes(gateway: 'Pagarme', payment_method: payment_method)
|
36
|
+
raise ::PagarMe::PagarMeError.new(payment.errors.messages.values.flatten.to_sentence)
|
15
37
|
end
|
16
38
|
|
17
39
|
self.transaction.charge
|
18
40
|
|
19
|
-
#VerifyPagarmeWorker.perform_in(5.minutes, payment.key)
|
20
|
-
|
21
41
|
change_payment_state
|
22
42
|
|
23
43
|
if self.transaction.status == 'refused'
|
44
|
+
antifraud_wrapper.send(analyze: false)
|
24
45
|
raise ::PagarMe::PagarMeError.new(I18n.t('projects.contributions.edit.transaction_error'))
|
25
46
|
end
|
26
47
|
|
27
48
|
save_user_credit_card if save_card
|
28
|
-
|
49
|
+
end
|
50
|
+
|
51
|
+
def was_credit_card_used_before?
|
52
|
+
PaymentEngines.was_credit_card_used_before?(self.transaction.card.id)
|
53
|
+
end
|
54
|
+
|
55
|
+
def process_antifraud
|
56
|
+
begin
|
57
|
+
antifraud_wrapper.send(analyze: true)
|
58
|
+
rescue RuntimeError => e
|
59
|
+
::Raven.capture_exception(e)
|
60
|
+
OpenStruct.new(recommendation: :DECLINE)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def antifraud_wrapper
|
65
|
+
@antifraud_wrapper ||= AntifraudOrderWrapper.new(self.attributes, self.transaction)
|
29
66
|
end
|
30
67
|
|
31
68
|
def save_user_credit_card
|
@@ -8,16 +8,20 @@ module CatarsePagarme
|
|
8
8
|
self.payment = payment
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
def change_status_by_transaction(transaction_status)
|
12
12
|
case transaction_status
|
13
13
|
when 'pending_review' then
|
14
14
|
self.payment.try(:notify_about_pending_review)
|
15
|
-
when 'paid'
|
15
|
+
when 'paid' then
|
16
16
|
self.payment.pay unless self.payment.paid?
|
17
17
|
when 'pending_refund' then
|
18
18
|
self.payment.request_refund unless self.payment.pending_refund?
|
19
19
|
when 'refunded' then
|
20
|
-
|
20
|
+
if self.payment.pending?
|
21
|
+
self.payment.refuse
|
22
|
+
else
|
23
|
+
self.payment.refund unless self.payment.refunded?
|
24
|
+
end
|
21
25
|
when 'refused' then
|
22
26
|
self.payment.refuse unless self.payment.refused?
|
23
27
|
when 'chargedback' then
|
data/catarse_pagarme.gemspec
CHANGED
@@ -17,9 +17,10 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.files = `git ls-files`.split($\)
|
18
18
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
19
|
|
20
|
+
s.add_dependency "countries", "3.0.0"
|
20
21
|
s.add_dependency "rails", "~> 4.0"
|
21
22
|
s.add_dependency "pagarme", "2.1.4"
|
22
|
-
|
23
|
+
s.add_dependency "konduto-ruby", "2.1.0"
|
23
24
|
s.add_dependency "weekdays", ">= 1.0.2"
|
24
25
|
s.add_dependency "sidekiq"
|
25
26
|
s.add_dependency "sentry-raven"
|
data/lib/catarse_pagarme.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require "pagarme"
|
2
|
+
require "konduto-ruby"
|
3
|
+
require "countries"
|
2
4
|
require "catarse_pagarme/engine"
|
3
5
|
require "catarse_pagarme/configuration"
|
4
6
|
require "catarse_pagarme/payment_engine"
|
5
7
|
require "sidekiq"
|
6
8
|
|
7
|
-
|
8
9
|
module CatarsePagarme
|
9
10
|
class << self
|
10
11
|
attr_writer :configuration
|