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/lib/rbraspag/eft.rb CHANGED
@@ -20,70 +20,59 @@ module Braspag
20
20
  :has_interest => "TIPOPARCELADO"
21
21
  }
22
22
 
23
- def initialize(connection, params, crypto_strategy = nil)
24
- raise InvalidConnection unless connection.is_a?(Braspag::Connection)
25
-
26
- @connection = connection
27
- @params = params
28
- @params[:merchant_id] = connection.merchant_id
29
- @crypto_strategy = crypto_strategy
30
-
31
- if @params[:amount] && !@params[:amount].is_a?(BigDecimal)
32
- @params[:amount] = BigDecimal.new(@params[:amount].to_s)
23
+ def self.generate(params, crypto_strategy = nil)
24
+ connection = Braspag::Connection.instance
25
+
26
+ params[:merchant_id] = connection.merchant_id
27
+
28
+ if params[:amount] && !params[:amount].is_a?(BigDecimal)
29
+ params[:amount] = BigDecimal.new(params[:amount].to_s)
33
30
  end
34
-
35
- ok?
36
- end
37
31
 
38
- def ok?
39
- raise IncompleteParams if @params[:order_id].nil? || @params[:amount].nil? || @params[:payment_method].nil?
32
+ raise IncompleteParams if params[:order_id].nil? || params[:amount].nil? || params[:payment_method].nil?
40
33
 
41
- raise InvalidOrderId unless @params[:order_id].is_a?(String) || @params[:order_id].is_a?(Fixnum)
42
- raise InvalidOrderId unless (1..50).include?(@params[:order_id].to_s.size)
34
+ raise InvalidOrderId unless params[:order_id].is_a?(String) || params[:order_id].is_a?(Fixnum)
35
+ raise InvalidOrderId unless (1..50).include?(params[:order_id].to_s.size)
43
36
 
44
- unless @params[:customer_name].nil?
45
- raise InvalidCustomerName unless (1..255).include?(@params[:customer_name].to_s.size)
37
+ unless params[:customer_name].nil?
38
+ raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
46
39
  end
47
40
 
48
- unless @params[:customer_id].nil?
49
- raise InvalidCustomerId unless (11..18).include?(@params[:customer_id].to_s.size)
41
+ unless params[:customer_id].nil?
42
+ raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
50
43
  end
51
44
 
52
- unless @params[:installments].nil?
53
- raise InvalidInstallments unless (1..2).include?(@params[:installments].to_s.size)
45
+ unless params[:installments].nil?
46
+ raise InvalidInstallments unless (1..2).include?(params[:installments].to_s.size)
54
47
  begin
55
- @params[:installments] = Integer(@params[:installments]) unless @params[:installments].is_a?(Integer)
48
+ params[:installments] = Integer(params[:installments]) unless params[:installments].is_a?(Integer)
56
49
  rescue Exception => e
57
50
  raise InvalidInstallments
58
51
  end
59
52
  end
60
53
 
61
- unless @params[:has_interest].nil?
62
- raise InvalidHasInterest unless (@params[:has_interest].is_a?(TrueClass) || @params[:has_interest].is_a?(FalseClass))
54
+ unless params[:has_interest].nil?
55
+ raise InvalidHasInterest unless (params[:has_interest].is_a?(TrueClass) || params[:has_interest].is_a?(FalseClass))
63
56
  end
64
57
 
65
- true
66
- end
67
-
68
- def generate
69
- data = create_data_from_params
58
+ data = create_data(params)
70
59
 
71
- html = "<form id=\"form_tef_#{@params[:order_id]}\" name=\"form_tef_#{@params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
60
+ html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
72
61
 
73
- if @crypto_strategy.nil?
62
+ if crypto_strategy.nil?
74
63
  data.each do |key, value|
75
64
  html.concat " <input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
76
65
  end
77
66
  else
78
67
  data.delete("Id_Loja")
79
- html.concat " <input type=\"text\" name=\"crypt\" value=\"#{@crypto_strategy.encrypt(data)}\" />\n"
80
- html.concat " <input type=\"text\" name=\"Id_Loja\" value=\"#{@params[:merchant_id]}\" />\n"
68
+ html.concat " <input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />\n"
69
+ html.concat " <input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />\n"
81
70
  end
82
71
 
83
72
  html.concat <<-EOHTML
84
73
  </form>
85
74
  <script type="text/javascript" charset="utf-8">
86
- document.forms["form_tef_#{@params[:order_id]}"].submit();
75
+ document.forms["form_tef_#{params[:order_id]}"].submit();
87
76
  </script>
88
77
  EOHTML
89
78
 
@@ -91,28 +80,23 @@ module Braspag
91
80
  end
92
81
 
93
82
  protected
94
- def create_data_from_params
83
+ def self.create_data(params)
95
84
  MAPPING.inject({}) do |memo, k|
96
85
  if k[0] == :payment_method
97
- memo[k[1]] = PAYMENT_METHODS[@params[:payment_method]]
86
+ memo[k[1]] = PAYMENT_METHODS[params[:payment_method]]
98
87
  elsif k[0] == :amount
99
- memo[k[1]] = convert_decimal_to_string(@params[:amount])
88
+ memo[k[1]] = Utils.convert_decimal_to_string(params[:amount])
100
89
  else
101
- memo[k[1]] = @params[k[0]] || "";
90
+ memo[k[1]] = params[k[0]] || "";
102
91
  end
103
92
 
104
93
  memo
105
94
  end
106
95
  end
107
96
 
108
- def convert_decimal_to_string(value)
109
- cents = "0#{((value - value.to_i) * 100).to_i}".slice(-2,2)
110
- integer = (value - (value - value.to_i)).to_i
111
- "#{integer},#{cents}"
112
- end
113
-
114
- def uri
115
- "#{@connection.base_url}/pagador/passthru.asp"
97
+ def self.uri
98
+ connection = Braspag::Connection.instance
99
+ "#{connection.braspag_url}/pagador/passthru.asp"
116
100
  end
117
101
  end
118
102
  end
@@ -1,30 +1,19 @@
1
1
  module Braspag
2
2
  class Order
3
-
4
3
  class InvalidData < Exception; end
5
4
 
6
- def self.status(connection, order_id)
7
- raise InvalidConnection unless connection.is_a?(Braspag::Connection)
5
+ def self.status(order_id)
6
+ connection = Braspag::Connection.instance
8
7
 
9
8
  raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
10
9
  raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
11
10
 
12
- request = ::HTTPI::Request.new("#{connection.base_url}/pagador/webservice/pedido.asmx/GetDadosPedido")
11
+ request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosPedido")
13
12
  request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
14
13
 
15
14
  response = ::HTTPI.post(request)
16
15
 
17
- response = convert_to_map response.body
18
-
19
- raise InvalidData if response[:authorization].nil?
20
- response
21
-
22
- end
23
-
24
- def self.convert_to_map(document)
25
- document = Nokogiri::XML(document)
26
-
27
- map = {
16
+ response = Utils::convert_to_map(response.body, {
28
17
  :authorization => "CodigoAutorizacao",
29
18
  :error_code => "CodigoErro",
30
19
  :error_message => "MensagemErro",
@@ -38,30 +27,11 @@ module Braspag
38
27
  :order_date => "DataPedido",
39
28
  :transaction_id => "TransId",
40
29
  :tid => "BraspagTid"
41
- }
42
-
43
- map.each do |keyForMap , keyValue|
44
- if keyValue.is_a?(String) || keyValue.nil?
45
- keyValue = keyForMap if keyValue.nil?
46
-
47
- value = document.search(keyValue).first
48
- if !value.nil?
49
- value = value.content.to_s
50
- map[keyForMap] = value unless value == ""
51
- map[keyForMap] = nil if value == ""
52
- else
53
- map[keyForMap] = nil
54
- end
55
-
56
- elsif keyValue.is_a?(Proc)
57
- map[keyForMap] = keyValue.call
58
- end
30
+ })
59
31
 
60
- map[keyForMap]
61
- end
32
+ raise InvalidData if response[:authorization].nil?
33
+ response
62
34
 
63
- map
64
35
  end
65
-
66
36
  end
67
37
  end
@@ -1,11 +1,35 @@
1
1
  module Braspag
2
-
3
2
  class Utils
4
3
  def self.convert_decimal_to_string(value)
5
4
  cents = "0#{((value - value.to_i) * 100).to_i}".slice(-2,2)
6
5
  integer = (value - (value - value.to_i)).to_i
7
6
  "#{integer},#{cents}"
8
7
  end
8
+
9
+ def self.convert_to_map(document, map = {})
10
+ document = Nokogiri::XML(document)
11
+
12
+ map.each do |keyForMap , keyValue|
13
+ if keyValue.is_a?(String) || keyValue.nil?
14
+ keyValue = keyForMap if keyValue.nil?
15
+
16
+ value = document.search(keyValue).first
17
+ if !value.nil?
18
+ value = value.content.to_s
19
+ map[keyForMap] = value unless value == ""
20
+ map[keyForMap] = nil if value == ""
21
+ else
22
+ map[keyForMap] = nil
23
+ end
24
+
25
+ elsif keyValue.is_a?(Proc)
26
+ map[keyForMap] = keyValue.call(document)
27
+ end
28
+
29
+ map[keyForMap]
30
+ end
31
+
32
+ map
33
+ end
9
34
  end
10
-
11
- end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Braspag
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
data/lib/rbraspag.rb CHANGED
@@ -24,11 +24,4 @@ require 'rbraspag/utils'
24
24
  require 'rbraspag/order'
25
25
 
26
26
  module Braspag
27
- class Production
28
- BASE_URL = 'https://www.pagador.com.br'
29
- end
30
-
31
- class Test
32
- BASE_URL = 'https://homologacao.pagador.com.br'
33
- end
34
27
  end
Binary file