Class: Braspag::Bill

Inherits:
PaymentMethod show all
Defined in:
lib/rbraspag/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"
}

Instance Method Summary collapse

Methods inherited from PaymentMethod

payment_method_from_id

Constructor Details

#initialize(connection, params) ⇒ Bill

Returns a new instance of Bill.

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbraspag/bill.rb', line 30

def initialize(connection, params)
  raise InvalidConnection unless connection.is_a?(Braspag::Connection)

  @connection = connection
  @params = params
  @params[:merchant_id] = connection.merchant_id

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

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

  ok?
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
# File 'lib/rbraspag/bill.rb', line 48

def [](key)
  @params[key]
end

#generateObject

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbraspag/bill.rb', line 86

def generate
  data =  MAPPING.inject({}) do |memo, k|
    if k[0] == :payment_method
      memo[k[1]] = PAYMENT_METHODS[@params[:payment_method]]
    elsif k[0] == :amount
      memo[k[1]] = Utils.convert_decimal_to_string(@params[:amount])
    else
      memo[k[1]] = @params[k[0]] || "";
    end
    
    memo
  end

  request = ::HTTPI::Request.new uri
  request.body = data

  response = ::HTTPI.post request
  response = convert_to_map response.body

  raise InvalidAmount if response[:message] == "Invalid purchase amount"
  raise InvalidMerchantId if response[:message] == "Invalid merchantId"
  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

#ok?Boolean

Returns:

  • (Boolean)

Raises:



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
# File 'lib/rbraspag/bill.rb', line 52

def ok?
  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)

  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[:number].nil?
    raise InvalidNumber unless (1..255).include?(@params[:number].to_s.size)
  end

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

  unless @params[:expiration_date].nil?
    date_regexp = /(0[1-9]|1[0-9]|2[0-9]|3[01])\/(0[1-9]|1[012])\/\d\d/
    raise InvalidExpirationDate unless @params[:expiration_date].to_s =~ date_regexp
  end

  unless @params[:payment_method].is_a?(Symbol) && PAYMENT_METHODS[@params[:payment_method]]
    raise InvalidPaymentMethod
  end

  true
end