Class: Braspag::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/cbraspag/core/converter.rb

Class Method Summary collapse

Class Method Details

.decimal_to_string(value) ⇒ Object



3
4
5
# File 'lib/cbraspag/core/converter.rb', line 3

def self.decimal_to_string(value)
  ("%.2f" % value.to_f).gsub('.', ',')
end

.from(method, data) ⇒ Object



106
107
108
# File 'lib/cbraspag/core/converter.rb', line 106

def self.from(method, data)
  self.send("from_#{method}", data)
end

.from_authorize(data) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/cbraspag/core/converter.rb', line 110

def self.from_authorize(data)
  to_map(data, {
    :amount => nil,
    :number => "authorisationNumber",
    :message => 'message',
    :return_code => 'returnCode',
    :status => 'status',
    :transaction_id => "transactionId"
  })
end

.from_capture(data) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/cbraspag/core/converter.rb', line 121

def self.from_capture(data)
  to_map(data, {
    :amount => nil,
    :message => 'message',
    :return_code => 'returnCode',
    :status => 'status',
    :transaction_id => "transactionId"
  })
end

.from_generate_billet(data) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cbraspag/core/converter.rb', line 142

def self.from_generate_billet(data)
  to_map(data, {
    :url => nil,
    :amount => nil,
    :number => "boletoNumber",
    :expiration_date => Proc.new { |document|
      begin
        Date.parse(document.search("expirationDate").first.to_s)
      rescue
        nil
      end
    },
    :return_code => "returnCode",
    :status => nil,
    :message => nil
  })
end

.from_void(data) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/cbraspag/core/converter.rb', line 131

def self.from_void(data)
  to_map(data, {
    :order_id => "orderId",
    :amount => nil,
    :message => 'message',
    :return_code => 'returnCode',
    :status => 'status',
    :transaction_id => "transactionId"
  })
end

.payment_method_name?(code) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cbraspag/core/converter.rb', line 36

def self.payment_method_name?(code)
  Braspag::PAYMENT_METHOD.key(code.to_s.to_i)
end

.status_name?(code) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/cbraspag/core/converter.rb', line 40

def self.status_name?(code)
  Braspag::STATUS_PAYMENT.key(code.to_s.to_i)
end

.string_to_decimal(value) ⇒ Object



7
8
9
# File 'lib/cbraspag/core/converter.rb', line 7

def self.string_to_decimal(value)
  BigDecimal.new(value.to_s.gsub(".",""))
end

.to(method, data) ⇒ Object



57
58
59
# File 'lib/cbraspag/core/converter.rb', line 57

def self.to(method, data)
  self.send("to_#{method}", data)
end

.to_authorize(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cbraspag/core/converter.rb', line 61

def self.to_authorize(params)
  self.to_hash({
    :merchant_id => "merchantId",
    :order_id => "orderId",
    :customer_name => "customerName",
    :amount => "amount",
    :payment_method => "paymentMethod",
    :holder => "holder",
    :card_number => "cardNumber",
    :expiration => "expiration",
    :security_code => "securityCode",
    :number_payments => "numberPayments",
    :type => "typePayment",
  }, params)
end

.to_capture(params) ⇒ Object



77
78
79
80
81
82
# File 'lib/cbraspag/core/converter.rb', line 77

def self.to_capture(params)
  self.to_hash({
    :merchant_id => "merchantId",
    :order_id => "orderId"
  }, params)
end

.to_generate_billet(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cbraspag/core/converter.rb', line 91

def self.to_generate_billet(params)
  self.to_hash({
    :merchant_id => "merchantId",
    :order_id => "orderId",
    :customer_name => "customerName",
    :customer_id => "customerIdNumber",
    :amount => "amount",
    :payment_method => "paymentMethod",
    :number => "boletoNumber",
    :instructions => "instructions",
    :expiration_date => "expirationDate",
    :emails => "emails"
  }, params)
end

.to_hash(format, params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cbraspag/core/converter.rb', line 44

def self.to_hash(format, params)
  data = {}
  format.each do |k, v|
    case k
    when :amount
      data[v] = self.decimal_to_string(params[:amount])
    else
      data[v] = params[k] || ""
    end
  end
  data
end

.to_map(document, map = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cbraspag/core/converter.rb', line 11

def self.to_map(document, map = {})
  document = Nokogiri::XML(document)

  map.each do |key, value|
    if value.is_a?(String) || value.nil?
      value = key if value.nil?

      new_value = document.search(value).first

      if new_value.nil?
        map[key] = nil
      else
        new_value = new_value.content.to_s
        map[key] = new_value unless new_value == ""
        map[key] = nil if new_value == ""
      end

    elsif value.is_a?(Proc)
      map[key] = value.call(document)
    end
  end

  map
end

.to_void(params) ⇒ Object



84
85
86
87
88
89
# File 'lib/cbraspag/core/converter.rb', line 84

def self.to_void(params)
  self.to_hash({
    :merchant_id => "merchantId",
    :order_id => "order"
  }, params)
end