Class: Braspag::Eft

Inherits:
PaymentMethod show all
Defined in:
lib/rbraspag/eft.rb

Constant Summary collapse

PAYMENT_METHODS =
{
  :bradesco => 11,
  :itau => 12,
  :banco_do_brasil => 15,
  :banco_real => 16,
  :banrisul => 30,
  :unibanco => 31
}
MAPPING =
{
  :merchant_id => "Id_Loja",
  :order_id => "VENDAID",
  :customer_name => "nome",
  :customer_id => "CPF",
  :amount => "VALOR",
  :payment_method => "CODPAGAMENTO",
  :installments => "PARCELAS",
  :has_interest => "TIPOPARCELADO"
}

Class Method Summary collapse

Methods inherited from PaymentMethod

payment_method_from_id

Class Method Details

.generate(params, crypto_strategy = nil) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
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
84
85
86
87
88
89
# File 'lib/rbraspag/eft.rb', line 23

def self.generate(params, crypto_strategy = nil)
  connection = Braspag::Connection.instance

  params[:merchant_id] = connection.merchant_id

  if params[:amount] && !params[:amount].is_a?(BigDecimal)
    params[:amount] = BigDecimal.new(params[:amount].to_s)
  end

  raise IncompleteParams if params[:order_id].nil? || params[:amount].nil? || params[:payment_method].nil?

  raise InvalidOrderId unless params[:order_id].is_a?(String) || params[:order_id].is_a?(Fixnum)
  raise InvalidOrderId unless (1..50).include?(params[:order_id].to_s.size)

  params[:installments] = "1" if params[:installments].nil?
  params[:installments] = "1" if params[:installments].nil?

  unless params[:customer_name].nil?
    raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
  end

  unless params[:customer_id].nil?
    raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
  end

  unless params[:installments].nil?
    raise InvalidInstallments unless (1..2).include?(params[:installments].to_s.size)
    begin
      params[:installments] = Integer(params[:installments]) unless params[:installments].is_a?(Integer)
    rescue Exception => e
      raise InvalidInstallments
    end
  end

  unless params[:has_interest].nil?
    raise InvalidHasInterest unless (params[:has_interest].is_a?(TrueClass) || params[:has_interest].is_a?(FalseClass))
  end

  if params[:has_interest]
     params[:has_interest] = "1"
  else
     params[:has_interest] = "0"
  end

  data =  create_data(params)

  html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"

  if crypto_strategy.nil?
    data.each do |key, value|
      html.concat "  <input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
    end
  else
    data.delete("Id_Loja")
    html.concat "  <input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />\n"
    html.concat "  <input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />\n"
  end

  html.concat "</form>\n<script type=\"text/javascript\" charset=\"utf-8\">\n  document.forms[\"form_tef_\#{params[:order_id]}\"].submit();\n</script>\n  EOHTML\n\n  html\nend\n"