Class: Braspag::Bill

Inherits:
PaymentMethod show all
Defined in:
lib/baby-braspag/bill.rb

Constant Summary collapse

PAYMENT_METHODS =
{
  :bradesco => "06",
  :cef => "07",
  :hsbc => "08",
  :bb => "09",
  :real => "10",
  :citibank => "13",
  :itau => "14",
  :unibanco => "26"
}
MAPPING =
{
  :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"
}
PRODUCTION_INFO_URI =
"/webservices/pagador/pedido.asmx/GetDadosBoleto"
HOMOLOGATION_INFO_URI =
"/pagador/webservice/pedido.asmx/GetDadosBoleto"
CREATION_URI =
"/webservices/pagador/Boleto.asmx/CreateBoleto"

Class Method Summary collapse

Methods inherited from PaymentMethod

payment_method_from_id, valid_order_id?

Class Method Details

.check_params(params) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/baby-braspag/bill.rb', line 95

def self.check_params(params)
  super

  if params[:number]
    raise InvalidNumber unless (1..255).include?(params[:number].to_s.size)
  end

  if params[:instructions]
    raise InvalidInstructions unless (1..512).include?(params[:instructions].to_s.size)
  end

  if params[:expiration_date]
    matches = params[:expiration_date].to_s.match /(\d{2})\/(\d{2})\/(\d{2})/
    raise InvalidExpirationDate unless matches
    begin
      Date.new(matches[3].to_i, matches[2].to_i, matches[1].to_i)
    rescue ArgumentError
      raise InvalidExpirationDate
    end
  end
end

.creation_urlObject



122
123
124
# File 'lib/baby-braspag/bill.rb', line 122

def self.creation_url
  Braspag::Connection.instance.braspag_url + CREATION_URI
end

.generate(params) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/baby-braspag/bill.rb', line 34

def self.generate(params)
  connection = Braspag::Connection.instance
  params[:merchant_id] = connection.merchant_id

  params = self.normalize_params(params)
  self.check_params(params)

  data = {}

  MAPPING.each do |k, v|
    case k
    when :payment_method
      data[v] = PAYMENT_METHODS[params[:payment_method]]
    when :amount
      data[v] = Utils.convert_decimal_to_string(params[:amount])
    else
      data[v] = params[k] || ""
    end
  end

  request = ::HTTPI::Request.new(self.creation_url)
  request.body = data

  response = Utils::convert_to_map(::HTTPI.post(request).body,
    {
      :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
    })

  raise InvalidMerchantId if response[:message] == "Invalid merchantId"
  raise InvalidAmount if response[:message] == "Invalid purchase amount"
  raise InvalidPaymentMethod if response[:message] == "Invalid payment method"
  raise InvalidStringFormat if response[:message] == "Input string was not in a correct format."
  raise UnknownError if response[:status].nil?

  response[:amount] = BigDecimal.new(response[:amount])

  response
end

.info(order_id) ⇒ Object

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/baby-braspag/bill.rb', line 126

def self.info(order_id)
  connection = Braspag::Connection.instance

  raise InvalidOrderId unless self.valid_order_id?(order_id)

  request = ::HTTPI::Request.new(self.info_url)
  request.body = {
    :loja => connection.merchant_id,
    :numeroPedido => order_id.to_s
  }

  response = ::HTTPI.post(request)

  response = Utils::convert_to_map(response.body, {
      :document_number => "NumeroDocumento",
      :payer => "Sacado",
      :our_number => "NossoNumero",
      :bill_line => "LinhaDigitavel",
      :document_date => "DataDocumento",
      :expiration_date => "DataVencimento",
      :receiver => "Cedente",
      :bank => "Banco",
      :agency => "Agencia",
      :account => "Conta",
      :wallet => "Carteira",
      :amount => "ValorDocumento",
      :amount_invoice => "ValorPago",
      :invoice_date => "DataCredito"
    })

  raise UnknownError if response[:document_number].nil?
  response
end

.info_urlObject



117
118
119
120
# File 'lib/baby-braspag/bill.rb', line 117

def self.info_url
  connection = Braspag::Connection.instance
  connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
end

.normalize_params(params) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/baby-braspag/bill.rb', line 85

def self.normalize_params(params)
  params = super

  if params[:expiration_date].respond_to?(:strftime)
    params[:expiration_date] = params[:expiration_date].strftime("%d/%m/%y")
  end

  params
end