pagarme 1.9.2 → 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile.lock +1 -1
- data/lib/pagarme.rb +11 -4
- data/lib/pagarme/errors.rb +23 -23
- data/lib/pagarme/model.rb +50 -50
- data/lib/pagarme/object.rb +135 -135
- data/lib/pagarme/plan.rb +4 -4
- data/lib/pagarme/request.rb +58 -58
- data/lib/pagarme/subscription.rb +34 -34
- data/lib/pagarme/transaction.rb +15 -8
- data/lib/pagarme/transaction_common.rb +81 -81
- data/lib/pagarme/util.rb +12 -9
- data/pagarme.gemspec +1 -1
- data/test/pagarme/object.rb +17 -17
- data/test/pagarme/plan.rb +9 -9
- data/test/pagarme/subscription.rb +61 -61
- data/test/pagarme/transaction.rb +158 -148
- data/test/test_helper.rb +76 -76
- metadata +2 -2
data/lib/pagarme/plan.rb
CHANGED
@@ -19,10 +19,10 @@ module PagarMe
|
|
19
19
|
|
20
20
|
def format_amount(amount)
|
21
21
|
if amount.kind_of?(String)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
value = amount.gsub(/\./, "")
|
23
|
+
value = value.strip
|
24
|
+
value = value.match(/\d+/)[0]
|
25
|
+
amount = value
|
26
26
|
end
|
27
27
|
amount
|
28
28
|
end
|
data/lib/pagarme/request.rb
CHANGED
@@ -6,73 +6,73 @@ require File.join(File.dirname(__FILE__), '.', 'errors')
|
|
6
6
|
|
7
7
|
module PagarMe
|
8
8
|
class Request
|
9
|
-
|
9
|
+
attr_accessor :path, :method, :parameters, :headers
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
def initialize(path, method)
|
12
|
+
self.path = path
|
13
|
+
self.method = method
|
14
|
+
self.parameters = {}
|
15
|
+
self.headers = {}
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def self.encode(params)
|
19
|
+
Util.normalize_params(params).to_params
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def run
|
23
|
+
unless PagarMe.api_key
|
24
|
+
raise PagarMeError.new("You need to configure a API key before performing requests.")
|
25
|
+
end
|
26
26
|
|
27
|
-
|
27
|
+
self.headers = {}
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
parameters = self.parameters.merge({
|
30
|
+
:api_key => PagarMe.api_key
|
31
|
+
})
|
32
|
+
error = nil
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
34
|
+
begin
|
35
|
+
response = RestClient::Request.execute({
|
36
|
+
:method => self.method,
|
37
|
+
:url => PagarMe.full_api_url(self.path),
|
38
|
+
:headers => self.headers,
|
39
|
+
:open_timeout => 30,
|
40
|
+
:payload => self.class.encode(parameters),
|
41
|
+
:timeout => 90
|
42
|
+
})
|
43
|
+
rescue SocketError => e
|
44
|
+
error = "Error connecting to server (#{e.message})."
|
45
|
+
rescue NoMethodError => e
|
46
|
+
if e.message =~ /\WRequestFailed\W/
|
47
|
+
raise ResponseError.new("Unexpected response code (#{e.inspect}).")
|
48
|
+
else
|
49
|
+
raise
|
50
|
+
end
|
51
|
+
rescue RestClient::ExceptionWithResponse => e
|
52
|
+
parsed_error = parse_json_response(e.http_body)
|
53
|
+
if parsed_error['errors']
|
54
|
+
error = parsed_error
|
55
|
+
raise PagarMeError.initFromServerResponse(error)
|
56
|
+
else
|
57
|
+
raise PagarMeError.new(e.http_body)
|
58
|
+
end
|
59
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
60
|
+
error = "Error connecting to server: connection refused"
|
61
|
+
end
|
62
62
|
|
63
|
-
|
63
|
+
raise ConnectionError.new(error) if error
|
64
64
|
|
65
|
-
|
66
|
-
|
65
|
+
parse_json_response(response.body)
|
66
|
+
end
|
67
67
|
|
68
|
-
|
68
|
+
private
|
69
69
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
70
|
+
def parse_json_response(response)
|
71
|
+
begin
|
72
|
+
MultiJson.load(response)
|
73
|
+
rescue MultiJson::LoadError => e
|
74
|
+
raise PagarMeError.new("Server response is not a valid JSON.")
|
75
|
+
end
|
76
|
+
end
|
77
77
|
end
|
78
78
|
end
|
data/lib/pagarme/subscription.rb
CHANGED
@@ -5,40 +5,40 @@ module PagarMe
|
|
5
5
|
class Subscription < TransactionCommon
|
6
6
|
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
8
|
+
def create
|
9
|
+
if self.plan
|
10
|
+
self.plan_id = plan.id
|
11
|
+
end
|
12
|
+
|
13
|
+
self.plan = nil
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def save
|
18
|
+
if self.plan
|
19
|
+
self.plan_id = plan.id
|
20
|
+
end
|
21
|
+
|
22
|
+
self.plan = nil
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
def cancel
|
27
|
+
request = PagarMe::Request.new(self.url + '/cancel', 'POST')
|
28
|
+
response = request.run
|
29
|
+
update(response)
|
30
|
+
end
|
31
|
+
|
32
|
+
def charge(amount)
|
33
|
+
request = PagarMe::Request.new(self.url + '/transactions', 'POST')
|
34
|
+
request.parameters = {
|
35
|
+
:amount => amount,
|
36
|
+
}
|
37
|
+
response = request.run
|
38
|
+
|
39
|
+
request = PagarMe::Request.new(self.url, 'GET')
|
40
|
+
update(request.run)
|
41
|
+
end
|
42
42
|
|
43
43
|
end
|
44
44
|
end
|
data/lib/pagarme/transaction.rb
CHANGED
@@ -5,14 +5,21 @@ require File.join(File.dirname(__FILE__), '..', 'pagarme')
|
|
5
5
|
|
6
6
|
module PagarMe
|
7
7
|
class Transaction < TransactionCommon
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def self.calculate_installments(params)
|
9
|
+
request = PagarMe::Request.new('/transactions/calculate_installments_amount', 'GET')
|
10
|
+
request.parameters.merge!(params)
|
11
|
+
response = request.run
|
12
|
+
response
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def charge
|
16
|
+
create
|
17
|
+
end
|
18
|
+
|
19
|
+
def refund
|
20
|
+
request = PagarMe::Request.new(self.url + '/refund', 'POST')
|
21
|
+
response = request.run
|
22
|
+
update(response)
|
23
|
+
end
|
17
24
|
end
|
18
25
|
end
|
@@ -4,94 +4,94 @@ require File.join(File.dirname(__FILE__), '..', 'pagarme')
|
|
4
4
|
module PagarMe
|
5
5
|
class TransactionCommon < Model
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
def initialize(response = {})
|
8
|
+
super(response)
|
9
|
+
self.payment_method = 'credit_card' unless self.payment_method
|
10
|
+
self.installments = 1 unless self.installments
|
11
|
+
self.status = 'local' unless self.status
|
12
|
+
before_set_filter :amount, :format_amount
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
def unset_creditcard_information
|
23
|
-
self.card_number = nil
|
24
|
-
self.card_holder_name = nil
|
25
|
-
self.card_expiration_year = nil
|
26
|
-
self.card_expiration_month = nil
|
27
|
-
self.card_cvv = nil
|
28
|
-
end
|
15
|
+
def create
|
16
|
+
validation_error = self.card_hash ? nil : validate
|
17
|
+
self.card_hash = generate_card_hash unless self.card_hash
|
18
|
+
unset_creditcard_information
|
19
|
+
super
|
20
|
+
end
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
22
|
+
def unset_creditcard_information
|
23
|
+
self.card_number = nil
|
24
|
+
self.card_holder_name = nil
|
25
|
+
self.card_expiration_year = nil
|
26
|
+
self.card_expiration_month = nil
|
27
|
+
self.card_cvv = nil
|
28
|
+
end
|
34
29
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
(s1 + s2) % 10 == 0
|
40
|
-
end
|
30
|
+
def is_valid_credit_card(card)
|
31
|
+
s1 = s2 = 0
|
32
|
+
card.to_s.reverse.chars.each_slice(2) do |odd, even|
|
33
|
+
s1 += odd.to_i
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
if !self.card_holder_name || !self.card_holder_name || self.card_holder_name.length == 0
|
49
|
-
error.errors << PagarMeError.new("Nome do portador inválido.", 'card_holder_name')
|
50
|
-
end
|
51
|
-
if !self.card_expiration_month || self.card_expiration_month.to_i <= 0 || self.card_expiration_month.to_i > 12
|
52
|
-
error.errors << PagarMeError.new("Mês de expiração inválido.", 'card_expiration_date')
|
53
|
-
end
|
54
|
-
if !self.card_expiration_year || self.card_expiration_year.to_i <= 0
|
55
|
-
error.errors << PagarMeError.new("Ano de expiração inválido.", 'card_expiration_date')
|
56
|
-
end
|
57
|
-
if !self.card_cvv || self.card_cvv.to_s.length < 3 || self.card_cvv.to_s.length > 4
|
58
|
-
error.errors << PagarMeError.new("Código de segurança inválido.", 'card_cvv')
|
59
|
-
end
|
60
|
-
end
|
61
|
-
if(error.errors.any?)
|
62
|
-
error.message = error.errors.map {|e| e.message}
|
63
|
-
error.message = error.message.join(',')
|
64
|
-
raise error
|
65
|
-
else
|
66
|
-
nil
|
67
|
-
end
|
68
|
-
end
|
35
|
+
double = even.to_i * 2
|
36
|
+
double -= 9 if double >= 10
|
37
|
+
s2 += double
|
38
|
+
end
|
39
|
+
(s1 + s2) % 10 == 0
|
40
|
+
end
|
69
41
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
42
|
+
def validate
|
43
|
+
error = PagarMeError.new
|
44
|
+
if self.payment_method == 'credit_card'
|
45
|
+
if !self.card_number || self.card_number.to_s.length > 20 || !is_valid_credit_card(self.card_number.to_s)
|
46
|
+
error.errors << PagarMeError.new("Número do cartão inválido.", 'card_number')
|
47
|
+
end
|
48
|
+
if !self.card_holder_name || !self.card_holder_name || self.card_holder_name.length == 0
|
49
|
+
error.errors << PagarMeError.new("Nome do portador inválido.", 'card_holder_name')
|
50
|
+
end
|
51
|
+
if !self.card_expiration_month || self.card_expiration_month.to_i <= 0 || self.card_expiration_month.to_i > 12
|
52
|
+
error.errors << PagarMeError.new("Mês de expiração inválido.", 'card_expiration_date')
|
53
|
+
end
|
54
|
+
if !self.card_expiration_year || self.card_expiration_year.to_i <= 0
|
55
|
+
error.errors << PagarMeError.new("Ano de expiração inválido.", 'card_expiration_date')
|
56
|
+
end
|
57
|
+
if !self.card_cvv || self.card_cvv.to_s.length < 3 || self.card_cvv.to_s.length > 4
|
58
|
+
error.errors << PagarMeError.new("Código de segurança inválido.", 'card_cvv')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
if(error.errors.any?)
|
62
|
+
error.message = error.errors.map {|e| e.message}
|
63
|
+
error.message = error.message.join(',')
|
64
|
+
raise error
|
65
|
+
else
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
end
|
79
69
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
70
|
+
def format_amount(amount)
|
71
|
+
if amount.kind_of?(String)
|
72
|
+
value = amount.gsub(/\./, "")
|
73
|
+
value = value.strip
|
74
|
+
value = value.match(/\d+/)[0]
|
75
|
+
amount = value
|
76
|
+
end
|
77
|
+
amount
|
78
|
+
end
|
88
79
|
|
89
|
-
|
90
|
-
|
91
|
-
|
80
|
+
def card_data_parameters
|
81
|
+
{
|
82
|
+
:card_number => self.card_number,
|
83
|
+
:card_holder_name => self.card_holder_name,
|
84
|
+
:card_expiration_date => "#{self.card_expiration_month}#{self.card_expiration_year}",
|
85
|
+
:card_cvv => self.card_cvv
|
86
|
+
}
|
87
|
+
end
|
92
88
|
|
93
|
-
|
94
|
-
|
95
|
-
|
89
|
+
def generate_card_hash
|
90
|
+
request = PagarMe::Request.new("/transactions/card_hash_key", 'GET')
|
91
|
+
response = request.run
|
92
|
+
|
93
|
+
public_key = OpenSSL::PKey::RSA.new(response['public_key'])
|
94
|
+
ret = "#{response['id']}_#{Base64.strict_encode64(public_key.public_encrypt(card_data_parameters.to_params))}"
|
95
|
+
end
|
96
96
|
end
|
97
97
|
end
|
data/lib/pagarme/util.rb
CHANGED
@@ -1,24 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'pagarme')
|
3
|
+
|
1
4
|
module PagarMe
|
2
5
|
class Util
|
3
6
|
def self.pagarme_classes
|
4
7
|
return {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
'transaction' => Transaction,
|
9
|
+
'plan' => Plan,
|
10
|
+
'customer' => Customer,
|
11
|
+
'subscription' => Subscription,
|
12
|
+
'address' => Address,
|
13
|
+
'phone' => Phone,
|
11
14
|
}
|
12
15
|
end
|
13
16
|
|
14
17
|
def self.convert_to_pagarme_object(response)
|
15
18
|
case response
|
16
19
|
when Array
|
17
|
-
|
20
|
+
response.map{ |i| convert_to_pagarme_object(i)}
|
18
21
|
when Hash
|
19
|
-
|
22
|
+
self.pagarme_classes.fetch(response['object'], PagarMeObject).build(response)
|
20
23
|
else
|
21
|
-
|
24
|
+
response
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|