class WebpayInterswitch::FormBuilder

Public Class Methods

new(txn_ref, amount, cust_name, cust_id, html_options, optional_parameters) click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 6
def initialize(txn_ref, amount, cust_name, cust_id, html_options, optional_parameters)
  @txn_ref = txn_ref
  @amount = amount
  @html_options = html_options
  @cust_name = cust_name
  @cust_id = cust_id
  @optional_parameters = optional_parameters
  sanitize_options
end

Public Instance Methods

generate_webpay_form() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 16
def generate_webpay_form
  form_html = "<form action=#{ WebpayInterswitch::Gateway.url } method=post>"

  form_html += generate_webpay_elements

  form_html += generate_transaction_data_elements

  form_html += generate_optional_parameter_elements

  # This generates the submit button alongwith the @submit_button_text
  # If submit_button_text is not provided, it defaults to 'Make Payment'
  form_html += generate_input_field('commit', @submit_button_text, 'submit', @html_options)

  form_html += '</form>'
end
valid?() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 32
def valid?
  @txn_ref.present? && Integer(@amount.to_s) > 0 rescue false
end

Private Instance Methods

generate_input_field(name, value, type = 'hidden', html_options={}) click to toggle source

Generates the input tag alongwith the provided name, value, type and html_options if any.

# File lib/webpay_interswitch/form_builder.rb, line 61
def generate_input_field(name, value, type = 'hidden', html_options={})
  html_string = string_for_html_options(html_options)
  "<input type=#{ type } name=#{ name } value='#{ value }' #{html_string}>"
end
generate_optional_parameter_elements() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 54
def generate_optional_parameter_elements
  @optional_parameters.collect do |field_name, field_value|
    generate_input_field(field_name, field_value)
  end.join('<br />')
end
generate_transaction_data_elements() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 46
def generate_transaction_data_elements
  txn_elem_html  = generate_input_field('txn_ref', @txn_ref)
  txn_elem_html += generate_input_field('amount', @amount)
  txn_elem_html += generate_input_field('hash', sha_hash(string_for_hash_param))
  txn_elem_html += generate_input_field('cust_name', @cust_name)
  txn_elem_html += generate_input_field('cust_id', @cust_id)
end
generate_webpay_elements() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 38
def generate_webpay_elements
  webpay_elem_html = ''
  %w(product_id pay_item_id currency site_redirect_url).each do |field_name|
    webpay_elem_html += generate_input_field(field_name, WebpayInterswitch::Gateway.public_send("#{ field_name }"))
  end
  webpay_elem_html
end
sanitize_options() click to toggle source
# File lib/webpay_interswitch/form_builder.rb, line 87
def sanitize_options
  @html_options.symbolize_keys!
  @submit_button_text = @html_options.delete(:submit_button_text) || 'Make Payment'
end
string_for_hash_param() click to toggle source

Returns a string that is used to compute the sha hash for POST request on webpay.

# File lib/webpay_interswitch/form_builder.rb, line 67
def string_for_hash_param
  @txn_ref.to_s +
  WebpayInterswitch::Gateway.product_id +
  WebpayInterswitch::Gateway.pay_item_id +
  @amount.to_s +
  WebpayInterswitch::Gateway.site_redirect_url +
  WebpayInterswitch::Gateway.mac_key
end
string_for_html_options(html_options) click to toggle source

Returns a string from html_options that is embedded into the submit button. e.g: html_options = {id: 'hi', class: 'c1 c2'} output: “id='hi' class='c1 c2'”

# File lib/webpay_interswitch/form_builder.rb, line 79
def string_for_html_options(html_options)
  html_string = ''
  html_options.each do |attr, val|
    html_string << " #{attr}='#{val}'"
  end
  html_string
end