pagarme 1.9.2 → 1.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,10 +19,10 @@ module PagarMe
19
19
 
20
20
  def format_amount(amount)
21
21
  if amount.kind_of?(String)
22
- value = amount.gsub(/\./, "")
23
- value = value.strip
24
- value = value.match(/\d+/)[0]
25
- amount = value
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
@@ -6,73 +6,73 @@ require File.join(File.dirname(__FILE__), '.', 'errors')
6
6
 
7
7
  module PagarMe
8
8
  class Request
9
- attr_accessor :path, :method, :parameters, :headers
9
+ attr_accessor :path, :method, :parameters, :headers
10
10
 
11
- def initialize(path, method)
12
- self.path = path
13
- self.method = method
14
- self.parameters = {}
15
- self.headers = {}
16
- end
11
+ def initialize(path, method)
12
+ self.path = path
13
+ self.method = method
14
+ self.parameters = {}
15
+ self.headers = {}
16
+ end
17
17
 
18
- def self.encode(params)
19
- Util.normalize_params(params).to_params
20
- end
18
+ def self.encode(params)
19
+ Util.normalize_params(params).to_params
20
+ end
21
21
 
22
- def run
23
- unless PagarMe.api_key
24
- raise PagarMeError.new("You need to configure a API key before performing requests.")
25
- end
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
- self.headers = {}
27
+ self.headers = {}
28
28
 
29
- parameters = self.parameters.merge({
30
- :api_key => PagarMe.api_key
31
- })
32
- error = nil
29
+ parameters = self.parameters.merge({
30
+ :api_key => PagarMe.api_key
31
+ })
32
+ error = nil
33
33
 
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
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
- raise ConnectionError.new(error) if error
63
+ raise ConnectionError.new(error) if error
64
64
 
65
- parse_json_response(response.body)
66
- end
65
+ parse_json_response(response.body)
66
+ end
67
67
 
68
- private
68
+ private
69
69
 
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
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
@@ -5,40 +5,40 @@ module PagarMe
5
5
  class Subscription < TransactionCommon
6
6
 
7
7
 
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
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
@@ -5,14 +5,21 @@ require File.join(File.dirname(__FILE__), '..', 'pagarme')
5
5
 
6
6
  module PagarMe
7
7
  class Transaction < TransactionCommon
8
- def charge
9
- create
10
- end
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
- def refund
13
- request = PagarMe::Request.new(self.url + '/refund', 'POST')
14
- response = request.run
15
- update(response)
16
- end
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
- 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
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
- 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
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
- 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
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
- double = even.to_i * 2
36
- double -= 9 if double >= 10
37
- s2 += double
38
- end
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
- 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
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
- 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
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
- 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
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
- def generate_card_hash
90
- request = PagarMe::Request.new("/transactions/card_hash_key", 'GET')
91
- response = request.run
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
- 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
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
@@ -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
- 'transaction' => Transaction,
6
- 'plan' => Plan,
7
- 'customer' => Customer,
8
- 'subscription' => Subscription,
9
- 'address' => Address,
10
- 'phone' => Phone,
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
- response.map{ |i| convert_to_pagarme_object(i)}
20
+ response.map{ |i| convert_to_pagarme_object(i)}
18
21
  when Hash
19
- self.pagarme_classes.fetch(response['object'], PagarMeObject).build(response)
22
+ self.pagarme_classes.fetch(response['object'], PagarMeObject).build(response)
20
23
  else
21
- response
24
+ response
22
25
  end
23
26
  end
24
27