class ActiveMerchant::Billing::LitleGateway

Constants

AVS_RESPONSE_CODE
CARD_TYPE
SCHEMA_VERSION

Public Class Methods

new(options={}) click to toggle source

Public: Create a new Litle gateway.

options - A hash of options:

:login         - The user.
:password      - The password.
:merchant_id   - The merchant id.
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/litle.rb, line 24
def initialize(options={})
  requires!(options, :login, :password, :merchant_id)
  super
end

Public Instance Methods

authorize(money, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 40
def authorize(money, payment_method, options={})
  request = build_xml_request do |doc|
    add_authentication(doc)
    doc.authorization(transaction_attributes(options)) do
      add_auth_purchase_params(doc, money, payment_method, options)
    end
  end

  commit(:authorization, request)
end
capture(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 51
def capture(money, authorization, options={})
  transaction_id, _ = split_authorization(authorization)

  request = build_xml_request do |doc|
    add_authentication(doc)
    add_descriptor(doc, options)
    doc.capture_(transaction_attributes(options)) do
      doc.litleTxnId(transaction_id)
      doc.amount(money) if money
    end
  end

  commit(:capture, request)
end
credit(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 66
def credit(money, authorization, options = {})
  ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
  refund(money, authorization, options)
end
purchase(money, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 29
def purchase(money, payment_method, options={})
  request = build_xml_request do |doc|
    add_authentication(doc)
    doc.sale(transaction_attributes(options)) do
      add_auth_purchase_params(doc, money, payment_method, options)
    end
  end

  commit(:sale, request)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 71
def refund(money, authorization, options={})
  transaction_id, _ = split_authorization(authorization)

  request = build_xml_request do |doc|
    add_authentication(doc)
    add_descriptor(doc, options)
    doc.credit(transaction_attributes(options)) do
      doc.litleTxnId(transaction_id)
      doc.amount(money) if money
    end
  end

  commit(:credit, request)
end
store(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 106
def store(creditcard, options = {})
  request = build_xml_request do |doc|
    add_authentication(doc)
    doc.registerTokenRequest(transaction_attributes(options)) do
      doc.orderId(truncated(options[:order_id]))
      doc.accountNumber(creditcard.number)
    end
  end

  commit(:registerToken, request)
end
verify(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 86
def verify(creditcard, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(0, creditcard, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 93
def void(authorization, options={})
  transaction_id, kind = split_authorization(authorization)

  request = build_xml_request do |doc|
    add_authentication(doc)
    doc.send(void_type(kind), transaction_attributes(options)) do
      doc.litleTxnId(transaction_id)
    end
  end

  commit(void_type(kind), request)
end

Private Instance Methods

add_address(doc, address) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 215
def add_address(doc, address)
  return unless address

  doc.companyName(address[:company]) unless address[:company].blank?
  doc.addressLine1(address[:address1]) unless address[:address1].blank?
  doc.addressLine2(address[:address2]) unless address[:address2].blank?
  doc.city(address[:city]) unless address[:city].blank?
  doc.state(address[:state]) unless address[:state].blank?
  doc.zip(address[:zip]) unless address[:zip].blank?
  doc.country(address[:country]) unless address[:country].blank?
  doc.phone(address[:phone]) unless address[:phone].blank?
end
add_auth_purchase_params(doc, money, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 157
def add_auth_purchase_params(doc, money, payment_method, options)
  doc.orderId(truncated(options[:order_id]))
  doc.amount(money)
  add_order_source(doc, payment_method)
  add_billing_address(doc, payment_method, options)
  add_shipping_address(doc, payment_method, options)
  add_payment_method(doc, payment_method)
  add_pos(doc, payment_method)
  add_descriptor(doc, options)
end
add_authentication(doc) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 150
def add_authentication(doc)
  doc.authentication do
    doc.user(@options[:login])
    doc.password(@options[:password])
  end
end
add_billing_address(doc, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 196
def add_billing_address(doc, payment_method, options)
  return if payment_method.is_a?(String)

  doc.billToAddress do
    doc.name(payment_method.name)
    doc.email(options[:email]) if options[:email]

    add_address(doc, options[:billing_address])
  end
end
add_descriptor(doc, options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 168
def add_descriptor(doc, options)
  if options[:descriptor_name] || options[:descriptor_phone]
    doc.customBilling do
      doc.phone(options[:descriptor_phone]) if options[:descriptor_phone]
      doc.descriptor(options[:descriptor_name]) if options[:descriptor_name]
    end
  end
end
add_order_source(doc, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 228
def add_order_source(doc, payment_method)
  if payment_method.respond_to?(:track_data) && payment_method.track_data.present?
    doc.orderSource('retail')
  else
    doc.orderSource('ecommerce')
  end
end
add_payment_method(doc, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 177
def add_payment_method(doc, payment_method)
  if payment_method.is_a?(String)
    doc.token do
      doc.litleToken(payment_method)
    end
  elsif payment_method.respond_to?(:track_data) && payment_method.track_data.present?
    doc.card do
      doc.track(payment_method.track_data)
    end
  else
    doc.card do
      doc.type_(CARD_TYPE[payment_method.brand])
      doc.number(payment_method.number)
      doc.expDate(exp_date(payment_method))
      doc.cardValidationNum(payment_method.verification_value)
    end
  end
end
add_pos(doc, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 236
def add_pos(doc, payment_method)
  return unless payment_method.respond_to?(:track_data) && payment_method.track_data.present?

  doc.pos do
    doc.capability('magstripe')
    doc.entryMode('completeread')
    doc.cardholderId('signature')
  end
end
add_shipping_address(doc, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 207
def add_shipping_address(doc, payment_method, options)
  return if payment_method.is_a?(String)

  doc.shipToAddress do
    add_address(doc, options[:shipping_address])
  end
end
authorization_from(kind, parsed) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 286
def authorization_from(kind, parsed)
  (kind == :registerToken) ? parsed[:litleToken] : "#{parsed[:litleTxnId]};#{kind}"
end
build_xml_request() { |doc| ... } click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 312
def build_xml_request
  builder = Nokogiri::XML::Builder.new
  builder.__send__('litleOnlineRequest', root_attributes) do |doc|
    yield(doc)
  end
  builder.doc.root.to_xml
end
commit(kind, request) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 268
def commit(kind, request)
  parsed = parse(kind, ssl_post(url, request, headers))

  options = {
    authorization: authorization_from(kind, parsed),
    test: test?,
    :avs_result => { :code => AVS_RESPONSE_CODE[parsed[:fraudResult_avsResult]] },
    :cvv_result => parsed[:fraudResult_cardValidationResult]
  }

  Response.new(success_from(kind, parsed), parsed[:message], parsed, options)
end
exp_date(payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 246
def exp_date(payment_method)
  "#{format(payment_method.month, :two_digits)}#{format(payment_method.year, :two_digits)}"
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 339
def headers
  {
    'Content-Type' => 'text/xml'
  }
end
parse(kind, xml) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 250
def parse(kind, xml)
  parsed = {}

  doc = Nokogiri::XML(xml).remove_namespaces!
  doc.xpath("//litleOnlineResponse/#{kind}Response/*").each do |node|
    if (node.elements.empty?)
      parsed[node.name.to_sym] = node.text
    else
      node.elements.each do |childnode|
        name = "#{node.name}_#{childnode.name}"
        parsed[name.to_sym] = childnode.text
      end
    end
  end

  parsed
end
root_attributes() click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 304
def root_attributes
  {
    merchantId: @options[:merchant_id],
    version: SCHEMA_VERSION,
    xmlns: "http://www.litle.com/schema"
  }
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 290
def split_authorization(authorization)
  transaction_id, kind = authorization.to_s.split(';')
  [transaction_id, kind]
end
success_from(kind, parsed) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 281
def success_from(kind, parsed)
  return (parsed[:response] == '000') unless kind == :registerToken
  %w(000 801 802).include?(parsed[:response])
end
transaction_attributes(options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 295
def transaction_attributes(options)
  attributes = {}
  attributes[:id] = truncated(options[:id] || options[:order_id])
  attributes[:reportGroup] = options[:merchant] || 'Default Report Group'
  attributes[:customerId] = options[:customer]
  attributes.delete_if { |key, value| value == nil }
  attributes
end
truncated(value) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 324
def truncated(value)
  return unless value
  value[0..24]
end
truncated_id(options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 334
def truncated_id(options)
  return unless options[:id]
  options[:id][0..24]
end
truncated_order_id(options) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 329
def truncated_order_id(options)
  return unless options[:order_id]
  options[:order_id][0..24]
end
url() click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 320
def url
  test? ? test_url : live_url
end
void_type(kind) click to toggle source
# File lib/active_merchant/billing/gateways/litle.rb, line 146
def void_type(kind)
  (kind == 'authorization') ? :authReversal : :void
end