rbraspag 0.0.19 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,4 +5,5 @@ tags
5
5
  *.swp
6
6
  .bundle
7
7
  .rvmrc
8
+ Gemfile.lock
8
9
 
@@ -27,67 +27,34 @@ module Braspag
27
27
  :emails => "emails"
28
28
  }
29
29
 
30
- # (my face when I saw this method ---------> D:)
30
+ PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosBoleto"
31
+ HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosBoleto"
32
+ CREATION_URI = "/webservices/pagador/Boleto.asmx/CreateBoleto"
33
+
31
34
  def self.generate(params)
32
35
  connection = Braspag::Connection.instance
33
- params = params
34
36
  params[:merchant_id] = connection.merchant_id
35
37
 
36
- if params[:expiration_date].is_a?(Date)
37
- params[:expiration_date] = params[:expiration_date].strftime("%d/%m/%y")
38
- end
39
-
40
- if params[:amount] && !params[:amount].is_a?(BigDecimal)
41
- params[:amount] = BigDecimal.new(params[:amount].to_s)
42
- end
43
-
44
- raise IncompleteParams if params[:order_id].nil? || params[:amount].nil? || params[:payment_method].nil?
45
-
46
- raise InvalidOrderId unless params[:order_id].is_a?(String) || params[:order_id].is_a?(Fixnum)
47
- raise InvalidOrderId unless (1..50).include?(params[:order_id].to_s.size)
38
+ params = self.normalize_params(params)
39
+ self.check_params(params)
48
40
 
49
- unless params[:customer_name].nil?
50
- raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
51
- end
52
-
53
- unless params[:customer_id].nil?
54
- raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
55
- end
56
-
57
- unless params[:number].nil?
58
- raise InvalidNumber unless (1..255).include?(params[:number].to_s.size)
59
- end
41
+ data = {}
60
42
 
61
- unless params[:instructions].nil?
62
- raise InvalidInstructions unless (1..512).include?(params[:instructions].to_s.size)
63
- end
64
-
65
- unless params[:expiration_date].nil?
66
- date_regexp = /(0[1-9]|1[0-9]|2[0-9]|3[01])\/(0[1-9]|1[012])\/\d\d/
67
- raise InvalidExpirationDate unless params[:expiration_date].to_s =~ date_regexp
68
- end
69
-
70
- unless params[:payment_method].is_a?(Symbol) && PAYMENT_METHODS[params[:payment_method]]
71
- raise InvalidPaymentMethod
72
- end
73
-
74
- data = MAPPING.inject({}) do |memo, k|
75
- if k[0] == :payment_method
76
- memo[k[1]] = PAYMENT_METHODS[params[:payment_method]]
77
- elsif k[0] == :amount
78
- memo[k[1]] = Utils.convert_decimal_to_string(params[:amount])
43
+ MAPPING.each do |k, v|
44
+ case k
45
+ when :payment_method
46
+ data[v] = PAYMENT_METHODS[params[:payment_method]]
47
+ when :amount
48
+ data[v] = Utils.convert_decimal_to_string(params[:amount])
79
49
  else
80
- memo[k[1]] = params[k[0]] || "";
50
+ data[v] = params[k] || ""
81
51
  end
82
-
83
- memo
84
52
  end
85
53
 
86
- request = ::HTTPI::Request.new uri
54
+ request = ::HTTPI::Request.new(self.creation_url)
87
55
  request.body = data
88
56
 
89
- response = ::HTTPI.post request
90
- response = Utils::convert_to_map(response.body,
57
+ response = Utils::convert_to_map(::HTTPI.post(request).body,
91
58
  {
92
59
  :url => nil,
93
60
  :amount => nil,
@@ -104,9 +71,8 @@ module Braspag
104
71
  :message => nil
105
72
  })
106
73
 
107
-
108
- raise InvalidAmount if response[:message] == "Invalid purchase amount"
109
74
  raise InvalidMerchantId if response[:message] == "Invalid merchantId"
75
+ raise InvalidAmount if response[:message] == "Invalid purchase amount"
110
76
  raise InvalidPaymentMethod if response[:message] == "Invalid payment method"
111
77
  raise InvalidStringFormat if response[:message] == "Input string was not in a correct format."
112
78
  raise UnknownError if response[:status].nil?
@@ -116,17 +82,60 @@ module Braspag
116
82
  response
117
83
  end
118
84
 
85
+ def self.normalize_params(params)
86
+ params = super
87
+
88
+ if params[:expiration_date].respond_to?(:strftime)
89
+ params[:expiration_date] = params[:expiration_date].strftime("%d/%m/%y")
90
+ end
91
+
92
+ params
93
+ end
94
+
95
+ def self.check_params(params)
96
+ super
97
+
98
+ if params[:number]
99
+ raise InvalidNumber unless (1..255).include?(params[:number].to_s.size)
100
+ end
101
+
102
+ if params[:instructions]
103
+ raise InvalidInstructions unless (1..512).include?(params[:instructions].to_s.size)
104
+ end
105
+
106
+ if params[:expiration_date]
107
+ matches = params[:expiration_date].to_s.match /(\d{2})\/(\d{2})\/(\d{2})/
108
+ raise InvalidExpirationDate unless matches
109
+ begin
110
+ Date.new(matches[3].to_i, matches[2].to_i, matches[1].to_i)
111
+ rescue ArgumentError
112
+ raise InvalidExpirationDate
113
+ end
114
+ end
115
+ end
116
+
117
+ def self.info_url
118
+ connection = Braspag::Connection.instance
119
+ connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
120
+ end
121
+
122
+ def self.creation_url
123
+ Braspag::Connection.instance.braspag_url + CREATION_URI
124
+ end
125
+
119
126
  def self.info(order_id)
120
127
  connection = Braspag::Connection.instance
121
128
 
122
- raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
123
- raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
129
+ raise InvalidOrderId unless self.valid_order_id?(order_id)
124
130
 
125
- request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosBoleto")
126
- request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
131
+ request = ::HTTPI::Request.new(self.info_url)
132
+ request.body = {
133
+ :loja => connection.merchant_id,
134
+ :numeroPedido => order_id.to_s
135
+ }
127
136
 
128
137
  response = ::HTTPI.post(request)
129
-
138
+
130
139
  response = Utils::convert_to_map(response.body, {
131
140
  :document_number => "NumeroDocumento",
132
141
  :payer => "Sacado",
@@ -147,12 +156,5 @@ module Braspag
147
156
  raise UnknownError if response[:document_number].nil?
148
157
  response
149
158
  end
150
-
151
- protected
152
-
153
- def self.uri
154
- connection = Braspag::Connection.instance
155
- "#{connection.braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
156
- end
157
159
  end
158
160
  end
@@ -2,17 +2,10 @@ module Braspag
2
2
  class Connection
3
3
  include Singleton
4
4
 
5
- class InvalidMerchantId < Exception ; end
6
- class InvalidEnv < Exception ; end
7
- class InvalidBraspagUrl < Exception ; end
8
-
9
5
  PRODUCTION_URL = "https://transaction.pagador.com.br"
10
6
  HOMOLOGATION_URL = "https://homologacao.pagador.com.br"
11
7
 
12
- PRODUCTION_QUERY_URL = "https://query.pagador.com.br/webservices/pagador/pedido.asmx"
13
- HOMOLOGATION_QUERY_URL = "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx"
14
-
15
- attr_reader :braspag_url, :braspag_query_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
8
+ attr_reader :braspag_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
16
9
 
17
10
  def initialize
18
11
  raise InvalidEnv if ENV["RACK_ENV"].nil? || ENV["RACK_ENV"].empty?
@@ -24,11 +17,9 @@ module Braspag
24
17
 
25
18
  @crypto_key = @options["crypto_key"]
26
19
  @crypto_url = @options["crypto_url"]
27
-
28
20
  @environment = @options["environment"] == 'production' ? 'production' : 'homologation'
29
21
 
30
- @braspag_url = self.production? ? PRODUCTION_URL : HOMOLOGATION_URL
31
- @braspag_query_url = self.production? ? PRODUCTION_QUERY_URL : HOMOLOGATION_QUERY_URL
22
+ @braspag_url = self.production? ? PRODUCTION_URL : HOMOLOGATION_URL
32
23
  end
33
24
 
34
25
  def production?
@@ -44,45 +44,33 @@ module Braspag
44
44
  :type => "typePayment",
45
45
  }
46
46
 
47
- def self.uri_authorize
48
- "#{Braspag::Connection.instance.braspag_url}/webservices/pagador/Pagador.asmx/Authorize"
49
- end
47
+ AUTHORIZE_URI = "/webservices/pagador/Pagador.asmx/Authorize"
48
+ CAPTURE_URI = "/webservices/pagador/Pagador.asmx/Capture"
50
49
 
51
- def self.uri_capture
52
- "#{Braspag::Connection.instance.braspag_url}/webservices/pagador/Pagador.asmx/Capture"
53
- end
50
+ PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosCartao"
51
+ HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosCartao"
54
52
 
55
53
  def self.authorize(params = {})
56
54
  connection = Braspag::Connection.instance
57
55
  params[:merchant_id] = connection.merchant_id
58
56
 
59
- [:order_id, :customer_name, :amount, :payment_method, :holder,
60
- :card_number, :expiration, :security_code, :number_payments, :type].each do |param|
61
- raise IncompleteParams unless params.include?(param)
62
- end
63
-
64
- raise InvalidOrderId unless (1..20).include?(params[:order_id].to_s.size)
65
- raise InvalidCustomerName unless (1..100).include?(params[:customer_name].to_s.size)
66
- raise InvalidAmount unless (1..10).include?(params[:amount].to_s.size)
67
- raise InvalidHolder unless (1..100).include?(params[:holder].to_s.size)
68
- raise InvalidExpirationDate unless (1..7).include?(params[:expiration].to_s.size)
69
- raise InvalidSecurityCode unless (1..4).include?(params[:security_code].to_s.size)
70
- raise InvalidNumberPayments unless (1..2).include?(params[:number_payments].to_s.size)
71
- raise InvalidType unless (1..2).include?(params[:type].to_s.size)
72
-
73
- params[:payment_method] = PAYMENT_METHODS[params[:payment_method]] if params[:payment_method].is_a?(Symbol)
57
+ self.check_params(params)
74
58
 
75
- data = MAPPING.inject({}) do |memo, k|
76
- if k[0] == :amount
77
- memo[k[1]] = Utils.convert_decimal_to_string(params[:amount])
59
+ data = {}
60
+ MAPPING.each do |k, v|
61
+ case k
62
+ when :payment_method
63
+ data[v] = PAYMENT_METHODS[params[:payment_method]]
64
+ when :amount
65
+ data[v] = Utils.convert_decimal_to_string(params[:amount])
78
66
  else
79
- memo[k[1]] = params[k[0]] || "";
67
+ data[v] = params[k] || ""
80
68
  end
81
- memo
82
69
  end
83
70
 
84
- request = ::HTTPI::Request.new uri_authorize
71
+ request = ::HTTPI::Request.new self.authorize_url
85
72
  request.body = data
73
+
86
74
  response = ::HTTPI.post request
87
75
  Utils::convert_to_map(response.body, {
88
76
  :amount => nil,
@@ -98,14 +86,17 @@ module Braspag
98
86
  connection = Braspag::Connection.instance
99
87
  merchant_id = connection.merchant_id
100
88
 
101
- raise InvalidOrderId unless (1..20).include?(order_id.to_s.size)
89
+ raise InvalidOrderId unless self.valid_order_id?(order_id)
102
90
 
103
- data = {MAPPING[:order_id] => order_id, "merchantId" => merchant_id }
104
- request = ::HTTPI::Request.new uri_capture
91
+ data = {
92
+ MAPPING[:order_id] => order_id,
93
+ MAPPING[:merchant_id] => merchant_id
94
+ }
105
95
 
96
+ request = ::HTTPI::Request.new(self.capture_url)
106
97
  request.body = data
107
- response = ::HTTPI.post(request)
108
98
 
99
+ response = ::HTTPI.post(request)
109
100
  Utils::convert_to_map(response.body, {
110
101
  :amount => nil,
111
102
  :number => "authorisationNumber",
@@ -116,14 +107,12 @@ module Braspag
116
107
  })
117
108
  end
118
109
 
119
-
120
110
  def self.info(order_id)
121
111
  connection = Braspag::Connection.instance
122
112
 
123
- raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
124
- raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
113
+ raise InvalidOrderId unless self.valid_order_id?(order_id)
125
114
 
126
- request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosCartao")
115
+ request = ::HTTPI::Request.new(self.info_url)
127
116
  request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
128
117
 
129
118
  response = ::HTTPI.post(request)
@@ -138,10 +127,44 @@ module Braspag
138
127
 
139
128
  raise UnknownError if response[:checking_number].nil?
140
129
  response
130
+ end
131
+
132
+ def self.check_params(params)
133
+ super
134
+
135
+ [:customer_name, :holder, :card_number, :expiration, :security_code, :number_payments, :type].each do |param|
136
+ raise IncompleteParams if params[param].nil?
137
+ end
138
+
139
+ raise InvalidHolder if params[:holder].to_s.size < 1 || params[:holder].to_s.size > 100
140
+
141
+ matches = params[:expiration].to_s.match /^(\d{2})\/(\d{2}|\d{4})$/
142
+ raise InvalidExpirationDate unless matches
143
+ begin
144
+ year = matches[2].to_i
145
+ year = "20#{year}" if year.size == 2
146
+
147
+ Date.new(year.to_i, matches[1].to_i)
148
+ rescue ArgumentError
149
+ raise InvalidExpirationDate
150
+ end
151
+
152
+ raise InvalidSecurityCode if params[:security_code].to_s.size < 1 || params[:security_code].to_s.size > 4
141
153
 
154
+ raise InvalidNumberPayments if params[:number_payments].to_i < 1 || params[:number_payments].to_i > 99
142
155
  end
143
156
 
157
+ def self.info_url
158
+ connection = Braspag::Connection.instance
159
+ connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
160
+ end
161
+
162
+ def self.authorize_url
163
+ Braspag::Connection.instance.braspag_url + AUTHORIZE_URI
164
+ end
144
165
 
166
+ def self.capture_url
167
+ Braspag::Connection.instance.braspag_url + CAPTURE_URI
168
+ end
145
169
  end
146
170
  end
147
-
@@ -5,31 +5,31 @@ module Braspag
5
5
  crypto_key = Braspag::Connection.instance.crypto_key
6
6
  raise Braspag::IncompleteParams if map.nil?
7
7
  raise Braspag::IncompleteParams unless map.is_a?(Hash)
8
-
8
+
9
9
  request = ::HTTPI::Request.new encrypt_uri
10
-
10
+
11
11
  data = {:key => crypto_key, :fields => map}
12
-
12
+
13
13
  request.headers["Content-Type"] = "application/json"
14
-
14
+
15
15
  request.body = data.to_json
16
-
16
+
17
17
  response = ::HTTPI.post request
18
-
18
+
19
19
  begin
20
20
  response = JSON.parse(response.body)
21
21
  rescue Exception => e
22
22
  raise UnknownError
23
23
  end
24
-
24
+
25
25
  raise IncompleteParams if (
26
- response["msg"] == "INVALID FORMAT" ||
27
- response["msg"] == "INVALID FIELDS"
26
+ response["msg"] == "INVALID FORMAT" ||
27
+ response["msg"] == "INVALID FIELDS"
28
28
  )
29
-
29
+
30
30
  raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"
31
31
  raise InvalidCryptKey if response["msg"] == "INVALID KEY"
32
-
32
+
33
33
  response["encrypt"]
34
34
  end
35
35
 
@@ -37,11 +37,11 @@ module Braspag
37
37
  crypto_key = Braspag::Connection.instance.crypto_key
38
38
  raise Braspag::InvalidEncryptedKey if encrypted.nil?
39
39
  raise Braspag::InvalidEncryptedKey unless encrypted.is_a?(String)
40
-
40
+
41
41
  raise Braspag::IncompleteParams if fields.nil?
42
42
  raise Braspag::IncompleteParams unless fields.is_a?(Array)
43
-
44
-
43
+
44
+
45
45
  request = ::HTTPI::Request.new decrypt_uri
46
46
  request.body = {
47
47
  "key" => crypto_key,
@@ -58,16 +58,16 @@ module Braspag
58
58
  rescue Exception => e
59
59
  raise UnknownError
60
60
  end
61
-
61
+
62
62
  raise IncompleteParams if (
63
- response["msg"] == "INVALID FORMAT" ||
64
- response["msg"] == "INVALID FIELDS"
63
+ response["msg"] == "INVALID FORMAT" ||
64
+ response["msg"] == "INVALID FIELDS"
65
65
  )
66
-
66
+
67
67
  raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"
68
68
 
69
69
  raise InvalidCryptKey if response["msg"] == "INVALID KEY"
70
-
70
+
71
71
  map = {}
72
72
  response["fields"].each do |key,value|
73
73
  map[key.downcase.to_sym] = value
@@ -80,12 +80,12 @@ module Braspag
80
80
  connection_uri = Braspag::Connection.instance.crypto_url
81
81
  "#{connection_uri}/v1/encrypt.json"
82
82
  end
83
-
83
+
84
84
  def self.decrypt_uri
85
85
  connection_uri = Braspag::Connection.instance.crypto_url
86
86
  "#{connection_uri}/v1/decrypt.json"
87
87
  end
88
-
88
+
89
89
  end
90
90
  end
91
91
  end
@@ -86,7 +86,7 @@ STRING
86
86
  connection = Braspag::Connection.instance
87
87
  "#{connection.braspag_url}/BraspagGeneralService/BraspagGeneralService.asmx"
88
88
  end
89
-
89
+
90
90
  def self.convert_request_to_map(document)
91
91
  map = {}
92
92
  document.children.children.children.children.children.each do |n|