class ActiveMerchant::Billing::HdfcGateway

Constants

ACTIONS
CURRENCY_CODES

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/hdfc.rb, line 17
def initialize(options = {})
  requires!(options, :login, :password)
  super
end

Public Instance Methods

authorize(amount, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 31
def authorize(amount, payment_method, options = {})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method)
  add_customer_data(post, options)

  commit('authorize', post)
end
capture(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 40
def capture(amount, authorization, options = {})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit('capture', post)
end
purchase(amount, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 22
def purchase(amount, payment_method, options = {})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method)
  add_customer_data(post, options)

  commit('purchase', post)
end
refund(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 49
def refund(amount, authorization, options = {})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit('refund', post)
end

Private Instance Methods

add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 80
      def add_customer_data(post, options)
        post[:udf2] = escape(options[:email]) if options[:email]
        if address = (options[:billing_address] || options[:address])
          post[:udf3] = escape(address[:phone]) if address[:phone]
          post[:udf4] = escape(<<~ADDRESS)
            #{address[:name]}
            #{address[:company]}
            #{address[:address1]}
            #{address[:address2]}
            #{address[:city]} #{address[:state]} #{address[:zip]}
            #{address[:country]}
          ADDRESS
        end
      end
add_invoice(post, amount, options) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 72
def add_invoice(post, amount, options)
  post[:amt] = amount(amount)
  post[:currencycode] = CURRENCY_CODES[options[:currency] || currency(amount)]
  post[:trackid] = escape(options[:order_id], 40) if options[:order_id]
  post[:udf1] = escape(options[:description]) if options[:description]
  post[:eci] = options[:eci] if options[:eci]
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 95
def add_payment_method(post, payment_method)
  post[:member] = escape(payment_method.name, 30)
  post[:card] = escape(payment_method.number)
  post[:cvv2] = escape(payment_method.verification_value)
  post[:expyear] = format(payment_method.year, :four_digits)
  post[:expmonth] = format(payment_method.month, :two_digits)
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 103
def add_reference(post, authorization)
  tranid, member = split_authorization(authorization)
  post[:transid] = tranid
  post[:member] = member
end
authorization_from(request, response) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 188
def authorization_from(request, response)
  [response[:tranid], request[:member]].join('|')
end
build_request(post) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 157
def build_request(post)
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  post.each do |field, value|
    xml.tag!(field, value)
  end
  xml.target!
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 140
def commit(action, post)
  post[:id] = @options[:login]
  post[:password] = @options[:password]
  post[:action] = ACTIONS[action] if ACTIONS[action]

  raw = parse(ssl_post(url(action), build_request(post)))

  succeeded = success_from(raw[:result])
  Response.new(
    succeeded,
    message_from(succeeded, raw),
    raw,
    authorization: authorization_from(post, raw),
    test: test?
  )
end
escape(string, max_length = 250) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 197
def escape(string, max_length = 250)
  return '' unless string

  string = string[0...max_length] if max_length
  string.gsub(/[^A-Za-z0-9 \-_@\.\n]/, '')
end
fix_xml(xml) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 129
def fix_xml(xml)
  xml.gsub(/&(?!(?:amp|quot|apos|lt|gt);)/, '&amp;')
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 180
def message_from(succeeded, response)
  if succeeded
    'Succeeded'
  else
    (response[:error_text] || response[:result] || 'Unable to read error message').split('-').last
  end
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 109
def parse(xml)
  response = {}

  doc = Nokogiri::XML.fragment(fix_xml(xml))
  doc.children.each do |node|
    if node.text?
      next
    elsif node.elements.size == 0
      response[node.name.downcase.to_sym] = node.text
    else
      node.elements.each do |childnode|
        name = "#{node.name.downcase}_#{childnode.name.downcase}"
        response[name.to_sym] = childnode.text
      end
    end
  end

  response
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 192
def split_authorization(authorization)
  tranid, member = authorization.split('|')
  [tranid, member]
end
success_from(result) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 171
def success_from(result)
  case result
  when 'CAPTURED', 'APPROVED', 'NOT ENROLLED', 'ENROLLED'
    true
  else
    false
  end
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/hdfc.rb, line 166
def url(action)
  endpoint = 'TranPortalXMLServlet'
  (test? ? test_url : live_url) + endpoint
end