rbraspag 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbraspag (0.0.10)
4
+ rbraspag (0.0.11)
5
5
  cs-httpi (>= 0.9.5.2)
6
6
  json
7
7
  nokogiri
@@ -25,8 +25,8 @@ GEM
25
25
  json (1.5.3)
26
26
  linecache19 (0.5.12)
27
27
  ruby_core_source (>= 0.1.4)
28
- nokogiri (1.4.4)
29
- rack (1.2.3)
28
+ nokogiri (1.5.0)
29
+ rack (1.3.0)
30
30
  rspec (2.6.0)
31
31
  rspec-core (~> 2.6.0)
32
32
  rspec-expectations (~> 2.6.0)
@@ -0,0 +1,12 @@
1
+ development:
2
+ merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
3
+ braspag_url: "https://homologacao.pagador.com.br"
4
+ crypto_url: "http://localhost:9292"
5
+ crypto_key: "1234561246"
6
+
7
+ test:
8
+ merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
9
+ braspag_url: "https://homologacao.pagador.com.br"
10
+ crypto_url: "http://localhost:9292"
11
+ crypto_key: "1234561246"
12
+
data/lib/rbraspag/bill.rb CHANGED
@@ -27,70 +27,58 @@ module Braspag
27
27
  :emails => "emails"
28
28
  }
29
29
 
30
- def initialize(connection, params)
31
- raise InvalidConnection unless connection.is_a?(Braspag::Connection)
32
30
 
33
- @connection = connection
34
- @params = params
35
- @params[:merchant_id] = connection.merchant_id
31
+ def self.generate(params)
32
+ connection = Braspag::Connection.instance
33
+ params = params
34
+ params[:merchant_id] = connection.merchant_id
36
35
 
37
- if @params[:expiration_date].is_a?(Date)
38
- @params[:expiration_date] = @params[:expiration_date].strftime("%d/%m/%y")
36
+ if params[:expiration_date].is_a?(Date)
37
+ params[:expiration_date] = params[:expiration_date].strftime("%d/%m/%y")
39
38
  end
40
39
 
41
- if @params[:amount] && !@params[:amount].is_a?(BigDecimal)
42
- @params[:amount] = BigDecimal.new(@params[:amount].to_s)
40
+ if params[:amount] && !params[:amount].is_a?(BigDecimal)
41
+ params[:amount] = BigDecimal.new(params[:amount].to_s)
43
42
  end
44
43
 
45
- ok?
46
- end
47
-
48
- def [](key)
49
- @params[key]
50
- end
51
-
52
- def ok?
53
- raise IncompleteParams if @params[:order_id].nil? || @params[:amount].nil? || @params[:payment_method].nil?
44
+ raise IncompleteParams if params[:order_id].nil? || params[:amount].nil? || params[:payment_method].nil?
54
45
 
55
- raise InvalidOrderId unless @params[:order_id].is_a?(String) || @params[:order_id].is_a?(Fixnum)
56
- raise InvalidOrderId unless (1..50).include?(@params[:order_id].to_s.size)
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)
57
48
 
58
- unless @params[:customer_name].nil?
59
- raise InvalidCustomerName unless (1..255).include?(@params[:customer_name].to_s.size)
49
+ unless params[:customer_name].nil?
50
+ raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
60
51
  end
61
52
 
62
- unless @params[:customer_id].nil?
63
- raise InvalidCustomerId unless (11..18).include?(@params[:customer_id].to_s.size)
53
+ unless params[:customer_id].nil?
54
+ raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
64
55
  end
65
56
 
66
- unless @params[:number].nil?
67
- raise InvalidNumber unless (1..255).include?(@params[:number].to_s.size)
57
+ unless params[:number].nil?
58
+ raise InvalidNumber unless (1..255).include?(params[:number].to_s.size)
68
59
  end
69
60
 
70
- unless @params[:instructions].nil?
71
- raise InvalidInstructions unless (1..512).include?(@params[:instructions].to_s.size)
61
+ unless params[:instructions].nil?
62
+ raise InvalidInstructions unless (1..512).include?(params[:instructions].to_s.size)
72
63
  end
73
64
 
74
- unless @params[:expiration_date].nil?
65
+ unless params[:expiration_date].nil?
75
66
  date_regexp = /(0[1-9]|1[0-9]|2[0-9]|3[01])\/(0[1-9]|1[012])\/\d\d/
76
- raise InvalidExpirationDate unless @params[:expiration_date].to_s =~ date_regexp
67
+ raise InvalidExpirationDate unless params[:expiration_date].to_s =~ date_regexp
77
68
  end
78
69
 
79
- unless @params[:payment_method].is_a?(Symbol) && PAYMENT_METHODS[@params[:payment_method]]
70
+ unless params[:payment_method].is_a?(Symbol) && PAYMENT_METHODS[params[:payment_method]]
80
71
  raise InvalidPaymentMethod
81
72
  end
82
73
 
83
- true
84
- end
85
74
 
86
- def generate
87
75
  data = MAPPING.inject({}) do |memo, k|
88
76
  if k[0] == :payment_method
89
- memo[k[1]] = PAYMENT_METHODS[@params[:payment_method]]
77
+ memo[k[1]] = PAYMENT_METHODS[params[:payment_method]]
90
78
  elsif k[0] == :amount
91
- memo[k[1]] = Utils.convert_decimal_to_string(@params[:amount])
79
+ memo[k[1]] = Utils.convert_decimal_to_string(params[:amount])
92
80
  else
93
- memo[k[1]] = @params[k[0]] || "";
81
+ memo[k[1]] = params[k[0]] || "";
94
82
  end
95
83
 
96
84
  memo
@@ -100,8 +88,24 @@ module Braspag
100
88
  request.body = data
101
89
 
102
90
  response = ::HTTPI.post request
103
- response = convert_to_map response.body
104
-
91
+ response = Utils::convert_to_map(response.body,
92
+ {
93
+ :url => nil,
94
+ :amount => nil,
95
+ :number => "boletoNumber",
96
+ :expiration_date => Proc.new { |document|
97
+ begin
98
+ Date.parse(document.search("expirationDate").first.to_s)
99
+ rescue
100
+ nil
101
+ end
102
+ },
103
+ :return_code => "returnCode",
104
+ :status => nil,
105
+ :message => nil
106
+ })
107
+
108
+
105
109
  raise InvalidAmount if response[:message] == "Invalid purchase amount"
106
110
  raise InvalidMerchantId if response[:message] == "Invalid merchantId"
107
111
  raise InvalidPaymentMethod if response[:message] == "Invalid payment method"
@@ -113,49 +117,45 @@ module Braspag
113
117
  response
114
118
  end
115
119
 
116
- protected
117
-
118
- def uri
119
- "#{@connection.base_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
120
- end
121
120
 
122
- def convert_to_map(document)
123
- document = Nokogiri::XML(document)
124
-
125
- map = {
126
- :url => nil,
127
- :amount => nil,
128
- :number => "boletoNumber",
129
- :expiration_date => Proc.new {
130
- begin
131
- Date.parse(document.search("expirationDate").first.to_s)
132
- rescue
133
- nil
134
- end
135
- },
136
- :return_code => "returnCode",
137
- :status => nil,
138
- :message => nil
139
- }
140
-
141
- map.each do |keyForMap , keyValue|
142
- if keyValue.is_a?(String) || keyValue.nil?
143
- keyValue = keyForMap if keyValue.nil?
144
-
145
- value = document.search(keyValue).first
146
- if !value.nil?
147
- value = value.content.to_s
148
- map[keyForMap] = value unless value == ""
149
- end
150
-
151
- elsif keyValue.is_a?(Proc)
152
- map[keyForMap] = keyValue.call
153
- end
121
+ def self.info(order_id)
122
+ connection = Braspag::Connection.instance
123
+
124
+ raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
125
+ raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
126
+
127
+ request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosBoleto")
128
+ request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
129
+
130
+ response = ::HTTPI.post(request)
131
+
132
+ response = Utils::convert_to_map(response.body, {
133
+ :document_number => "NumeroDocumento",
134
+ :payer => "Sacado",
135
+ :our_number => "NossoNumero",
136
+ :bill_line => "LinhaDigitavel",
137
+ :document_date => "DataDocumento",
138
+ :expiration_date => "DataVencimento",
139
+ :receiver => "Cedente",
140
+ :bank => "Banco",
141
+ :agency => "Agencia",
142
+ :account => "Conta",
143
+ :wallet => "Carteira",
144
+ :amount => "ValorDocumento",
145
+ :amount_invoice => "ValorPago",
146
+ :invoice_date => "DataCredito"
147
+ })
148
+
149
+ raise UnknownError if response[:document_number].nil?
150
+ response
154
151
 
155
- map[keyForMap]
156
- end
152
+ end
157
153
 
158
- map
154
+ protected
155
+
156
+ def self.uri
157
+ connection = Braspag::Connection.instance
158
+ "#{connection.braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
159
159
  end
160
160
  end
161
161
  end
@@ -1,16 +1,26 @@
1
1
  module Braspag
2
2
  class Connection
3
+ include Singleton
3
4
  class InvalidMerchantId < Exception ; end
5
+ class InvalidEnv < Exception ; end
6
+ class InvalidBraspagUrl < Exception ; end
4
7
 
5
- attr_reader :base_url, :environment, :merchant_id
8
+ attr_reader :braspag_url, :merchant_id, :crypto_url, :crypto_key
9
+
10
+ def initialize
11
+ raise InvalidEnv if ENV["RACK_ENV"].nil? || ENV["RACK_ENV"].empty?
12
+ options = YAML.load_file("config/braspag.yml")
13
+ options = options[ENV["RACK_ENV"]]
14
+
15
+ merchant_id = options["merchant_id"]
6
16
 
7
- def initialize(merchant_id, environment = :production)
8
17
  raise InvalidMerchantId unless merchant_id.length == 38
9
18
  raise InvalidMerchantId unless merchant_id.match /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
19
+ raise InvalidBraspagUrl if options["braspag_url"].nil? || options["braspag_url"].empty?
10
20
 
11
- environment = :test unless environment.eql? :production
12
- @environment = eval(environment.to_s.capitalize)
13
- @base_url = @environment::BASE_URL
21
+ @crypto_key = options["crypto_key"]
22
+ @crypto_url = options["crypto_url"]
23
+ @braspag_url = options["braspag_url"]
14
24
  @merchant_id = merchant_id
15
25
  end
16
26
  end
@@ -44,21 +44,18 @@ module Braspag
44
44
  :type => "typePayment",
45
45
  }
46
46
 
47
- def initialize(connection)
48
- raise InvalidConnection unless connection.is_a?(Braspag::Connection)
49
- @connection = connection
50
- @merchant_id = connection.merchant_id
47
+ def self.uri_authorize
48
+ "#{Braspag::Connection.instance.braspag_url}/webservices/pagador/Pagador.asmx/Authorize"
51
49
  end
52
50
 
53
- def uri_authorize
54
- "#{@connection.base_url}/webservices/pagador/Pagador.asmx/Authorize"
51
+ def self.uri_capture
52
+ "#{Braspag::Connection.instance.braspag_url}/webservices/pagador/Pagador.asmx/Capture"
55
53
  end
56
54
 
57
- def uri_capture
58
- "#{@connection.base_url}/webservices/pagador/Pagador.asmx/Capture"
59
- end
55
+ def self.authorize(params = {})
56
+ connection = Braspag::Connection.instance
57
+ params[:merchant_id] = connection.merchant_id
60
58
 
61
- def authorize params
62
59
  [:order_id, :customer_name, :amount, :payment_method, :holder,
63
60
  :card_number, :expiration, :security_code, :number_payments, :type].each do |param|
64
61
  raise IncompleteParams unless params.include?(param)
@@ -87,51 +84,64 @@ module Braspag
87
84
  request = ::HTTPI::Request.new uri_authorize
88
85
  request.body = data
89
86
  response = ::HTTPI.post request
90
- response = convert_to_map response.body
87
+ Utils::convert_to_map(response.body, {
88
+ :amount => nil,
89
+ :number => "authorisationNumber",
90
+ :message => 'message',
91
+ :return_code => 'returnCode',
92
+ :status => 'status',
93
+ :transaction_id => "transactionId"
94
+ })
91
95
  end
92
96
 
93
- def capture(order_id)
97
+ def self.capture(order_id)
98
+ connection = Braspag::Connection.instance
99
+ merchant_id = connection.merchant_id
100
+
94
101
  raise InvalidOrderId unless (1..20).include?(order_id.to_s.size)
95
102
 
96
- data = {MAPPING[:order_id] => order_id, "merchantId" => @merchant_id }
103
+ data = {MAPPING[:order_id] => order_id, "merchantId" => merchant_id }
97
104
  request = ::HTTPI::Request.new uri_capture
98
105
 
99
106
  request.body = data
100
107
  response = ::HTTPI.post(request)
101
- convert_to_map(response.body)
108
+
109
+ Utils::convert_to_map(response.body, {
110
+ :amount => nil,
111
+ :number => "authorisationNumber",
112
+ :message => 'message',
113
+ :return_code => 'returnCode',
114
+ :status => 'status',
115
+ :transaction_id => "transactionId"
116
+ })
102
117
  end
103
118
 
104
- def convert_to_map(document)
105
- document = Nokogiri::XML(document)
106
-
107
- map = {
108
- :amount => nil,
109
- :number => "authorisationNumber",
110
- :message => 'message',
111
- :return_code => 'returnCode',
112
- :status => 'status',
113
- :transaction_id => "transactionId"
114
- }
115
-
116
- map.each do |keyForMap , keyValue|
117
- if keyValue.is_a?(String) || keyValue.nil?
118
- keyValue = keyForMap if keyValue.nil?
119
-
120
- value = document.search(keyValue).first
121
- if !value.nil?
122
- value = value.content.to_s
123
- map[keyForMap] = value unless value == ""
124
- end
125
-
126
- elsif keyValue.is_a?(Proc)
127
- map[keyForMap] = keyValue.call
128
- end
129
119
 
130
- map[keyForMap]
131
- end
120
+ def self.info(order_id)
121
+ connection = Braspag::Connection.instance
122
+
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)
125
+
126
+ request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosCartao")
127
+ request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
128
+
129
+ response = ::HTTPI.post(request)
130
+
131
+ response = Utils::convert_to_map(response.body, {
132
+ :checking_number => "NumeroComprovante",
133
+ :certified => "Autenticada",
134
+ :autorization_number => "NumeroAutorizacao",
135
+ :card_number => "NumeroCartao",
136
+ :transaction_number => "NumeroTransacao"
137
+ })
138
+
139
+ raise UnknownError if response[:checking_number].nil?
140
+ response
132
141
 
133
- map
134
142
  end
143
+
144
+
135
145
  end
136
146
  end
137
147
 
@@ -1,19 +1,14 @@
1
1
  module Braspag
2
2
  module Crypto
3
3
  class JarWebservice
4
-
5
- def initialize(crypto_key, connection_uri)
6
- @crypto_key = crypto_key
7
- @connection_uri = connection_uri
8
- end
9
-
10
- def encrypt(map)
4
+ def self.encrypt(map)
5
+ crypto_key = Braspag::Connection.instance.crypto_key
11
6
  raise Braspag::IncompleteParams if map.nil?
12
7
  raise Braspag::IncompleteParams unless map.is_a?(Hash)
13
8
 
14
9
  request = ::HTTPI::Request.new encrypt_uri
15
10
 
16
- data = {:key => @crypto_key, :fields => map}
11
+ data = {:key => crypto_key, :fields => map}
17
12
 
18
13
  request.headers["Content-Type"] = "application/json"
19
14
 
@@ -38,7 +33,8 @@ module Braspag
38
33
  response["encrypt"]
39
34
  end
40
35
 
41
- def decrypt(encrypted, fields)
36
+ def self.decrypt(encrypted, fields)
37
+ crypto_key = Braspag::Connection.instance.crypto_key
42
38
  raise Braspag::InvalidEncryptedKey if encrypted.nil?
43
39
  raise Braspag::InvalidEncryptedKey unless encrypted.is_a?(String)
44
40
 
@@ -48,7 +44,7 @@ module Braspag
48
44
 
49
45
  request = ::HTTPI::Request.new decrypt_uri
50
46
  request.body = {
51
- "key" => @crypto_key,
47
+ "key" => crypto_key,
52
48
  "encrypted" => encrypted,
53
49
  "fields" => fields
54
50
  }.to_json
@@ -80,12 +76,14 @@ module Braspag
80
76
  end
81
77
 
82
78
  protected
83
- def encrypt_uri
84
- "#{@connection_uri}/v1/encrypt.json"
79
+ def self.encrypt_uri
80
+ connection_uri = Braspag::Connection.instance.crypto_url
81
+ "#{connection_uri}/v1/encrypt.json"
85
82
  end
86
83
 
87
- def decrypt_uri
88
- "#{@connection_uri}/v1/decrypt.json"
84
+ def self.decrypt_uri
85
+ connection_uri = Braspag::Connection.instance.crypto_url
86
+ "#{connection_uri}/v1/decrypt.json"
89
87
  end
90
88
 
91
89
  end
@@ -1,15 +1,12 @@
1
1
  module Braspag
2
2
  module Crypto
3
3
  class Webservice
4
- def initialize(connection)
5
- @connection = connection
6
- end
7
-
8
- def encrypt(map)
4
+ def self.encrypt(map)
5
+ connection = Braspag::Connection.instance
9
6
  raise Braspag::IncompleteParams if map.nil?
10
7
  raise Braspag::IncompleteParams unless map.is_a?(Hash)
11
8
 
12
- request = ::HTTPI::Request.new uri
9
+ request = ::HTTPI::Request.new self.uri
13
10
 
14
11
  fields = "\n"
15
12
  map.each do |key, value|
@@ -22,7 +19,7 @@ module Braspag
22
19
  <env:Header />
23
20
  <env:Body>
24
21
  <tns:EncryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
25
- <tns:merchantId>#{@connection.merchant_id}</tns:merchantId>
22
+ <tns:merchantId>#{connection.merchant_id}</tns:merchantId>
26
23
  <tns:request>
27
24
  #{fields}
28
25
  </tns:request>
@@ -48,12 +45,13 @@ STRING
48
45
  response
49
46
  end
50
47
 
51
- def decrypt(encripted_text)
48
+ def self.decrypt(encripted_text)
49
+ connection = Braspag::Connection.instance
52
50
 
53
51
  raise Braspag::IncompleteParams if encripted_text.nil?
54
52
  raise Braspag::IncompleteParams unless encripted_text.is_a?(String)
55
53
 
56
- request = ::HTTPI::Request.new uri
54
+ request = ::HTTPI::Request.new self.uri
57
55
 
58
56
  request.body = <<-STRING
59
57
  <?xml version="1.0" encoding="utf-8"?>
@@ -61,7 +59,7 @@ STRING
61
59
  <env:Header />
62
60
  <env:Body>
63
61
  <tns:DecryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
64
- <tns:merchantId>#{@connection.merchant_id}</tns:merchantId>
62
+ <tns:merchantId>#{connection.merchant_id}</tns:merchantId>
65
63
  <tns:cryptString>#{encripted_text}</tns:cryptString>
66
64
  </tns:DecryptRequest>
67
65
  </env:Body>
@@ -80,16 +78,16 @@ STRING
80
78
  raise Braspag::InvalidMerchantId if (result_error == 'Erro BP 011' || result_error == 'Erro BP 012')
81
79
  raise Braspag::InvalidIP if (result_error == 'Erro BP 067' || result_error == 'Erro BP 068')
82
80
 
83
- convert_request_to_map document
81
+ self.convert_request_to_map document
84
82
  end
85
-
86
83
 
87
84
  protected
88
- def uri
89
- "#{@connection.base_url}/BraspagGeneralService/BraspagGeneralService.asmx"
85
+ def self.uri
86
+ connection = Braspag::Connection.instance
87
+ "#{connection.braspag_url}/BraspagGeneralService/BraspagGeneralService.asmx"
90
88
  end
91
89
 
92
- def convert_request_to_map(document)
90
+ def self.convert_request_to_map(document)
93
91
  map = {}
94
92
  document.children.children.children.children.children.each do |n|
95
93
  values = n.content.to_s.split("=")
@@ -97,7 +95,6 @@ STRING
97
95
  end
98
96
  map
99
97
  end
100
-
101
98
  end
102
99
  end
103
- end
100
+ end