class ActiveMerchant::Billing::TransFirstGateway

Constants

ACTIONS
DECLINED
ENDPOINTS
UNUSED_CREDIT_CARD_FIELDS

Public Class Methods

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

Public Instance Methods

purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 37
def purchase(money, payment, options = {})
  post = {}

  add_amount(post, money)
  add_payment(post, payment)
  add_address(post, options)
  add_invoice(post, options) if payment.credit_card?
  add_pair(post, :RefID, options[:order_id], required: true)

  commit((payment.is_a?(Check) ? :purchase_echeck : :purchase), post)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 49
def refund(money, authorization, options={})
  post = {}

  transaction_id, payment_type = split_authorization(authorization)
  add_amount(post, money)
  add_pair(post, :TransID, transaction_id)
  add_pair(post, :RefID, options[:order_id], required: true)

  commit((payment_type == 'check' ? :refund_echeck : :refund), post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 73
def scrub(transcript)
  transcript.
    gsub(%r((&?RegKey=)\w*(&?)), '\1[FILTERED]\2').
    gsub(%r((&?CardNumber=)\d*(&?)), '\1[FILTERED]\2').
    gsub(%r((&?CVV2=)\d*(&?)), '\1[FILTERED]\2').
    gsub(%r((&?TransRoute=)\d*(&?)), '\1[FILTERED]\2').
    gsub(%r((&?BankAccountNo=)\d*(&?)), '\1[FILTERED]\2')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 69
def supports_scrubbing?
  true
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 60
def void(authorization, options={})
  post = {}

  transaction_id, _ = split_authorization(authorization)
  add_pair(post, :TransID, transaction_id)

  commit(:void, post)
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 88
def add_address(post, options)
  address = options[:billing_address] || options[:address]

  if address
    add_pair(post, :Address, address[:address1], required: true)
    add_pair(post, :ZipCode, address[:zip], required: true)
  else
    add_pair(post, :Address, '', required: true)
    add_pair(post, :ZipCode, '', required: true)
  end
end
add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 84
def add_amount(post, money)
  add_pair(post, :Amount, amount(money), required: true)
end
add_credit_card(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 117
def add_credit_card(post, payment)
  add_pair(post, :CardHolderName, payment.name, required: true)
  add_pair(post, :CardNumber, payment.number, required: true)
  add_pair(post, :Expiration, expdate(payment), required: true)
  add_pair(post, :CVV2, payment.verification_value, required: true)
end
add_echeck(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 124
def add_echeck(post, payment)
  add_pair(post, :TransRoute, payment.routing_number, required: true)
  add_pair(post, :BankAccountNo, payment.account_number, required: true)
  add_pair(post, :BankAccountType, add_or_use_default(payment.account_type, 'Checking'), required: true)
  add_pair(post, :CheckType, add_or_use_default(payment.account_holder_type, 'Personal'), required: true)
  add_pair(post, :Name, payment.name, required: true)
  add_pair(post, :ProcessDate, Time.now.strftime('%m%d%y'), required: true)
  add_pair(post, :Description, '', required: true)
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 100
def add_invoice(post, options)
  add_pair(post, :SECCCode, options[:invoice], required: true)
  add_pair(post, :PONumber, options[:invoice], required: true)
  add_pair(post, :SaleTaxAmount, amount(options[:tax] || 0))
  add_pair(post, :TaxIndicator, 0)
  add_pair(post, :PaymentDesc, options[:description] || '', required: true)
  add_pair(post, :CompanyName, options[:company_name] || '', required: true)
end
add_or_use_default(payment_data, default_value) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 134
def add_or_use_default(payment_data, default_value)
  return payment_data.capitalize if payment_data
  return default_value
end
add_pair(post, key, value, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 225
def add_pair(post, key, value, options = {})
  post[key] = value if !value.blank? || options[:required]
end
add_payment(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 109
def add_payment(post, payment)
  if payment.is_a?(Check)
    add_echeck(post, payment)
  else
    add_credit_card(post, payment)
  end
end
add_unused_fields(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 139
def add_unused_fields(action, post)
  return unless action == :purchase

  UNUSED_CREDIT_CARD_FIELDS.each do |f|
    post[f] = ''
  end
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 184
def authorization_from(response)
  if response[:status] == 'APPROVED'
    "#{response[:trans_id]}|check"
  else
    "#{response[:trans_id]}|creditcard"
  end
end
commit(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 171
def commit(action, params)
  response = parse(ssl_post(url(action), post_data(action, params)))
  Response.new(
    success_from(response),
    message_from(response),
    response,
    :test => test?,
    :authorization => authorization_from(response),
    :avs_result => { :code => response[:avs_code] },
    :cvv_result => response[:cvv2_code]
  )
end
expdate(credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 147
def expdate(credit_card)
  year  = format(credit_card.year, :two_digits)
  month = format(credit_card.month, :two_digits)

  "#{month}#{year}"
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 207
def message_from(response)
  case response[:message]
  when 'Call Voice Center'
    DECLINED
  else
    response[:message]
  end
end
parse(data) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 154
def parse(data)
  response = {}

  xml = REXML::Document.new(data)
  root = REXML::XPath.first(xml, '*')

  if root.nil?
    response[:message] = data.to_s.strip
  else
    root.elements.to_a.each do |node|
      response[node.name.underscore.to_sym] = node.text
    end
  end

  response
end
post_data(action, params = {}) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 216
def post_data(action, params = {})
  add_unused_fields(action, params)
  params[:MerchantID] = @options[:login]
  params[:RegKey] = @options[:password]

  request = params.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
  request
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 234
def split_authorization(authorization)
  authorization.split('|')
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 192
def success_from(response)
  case response[:status]
  when 'Authorized'
    true
  when 'Voided'
    true
  when 'APPROVED'
    true
  when 'VOIDED'
    true
  else
    false
  end
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/trans_first.rb, line 229
def url(action)
  base_url = test? ? test_url : live_url
  "#{base_url}/#{ENDPOINTS[action]}/#{ACTIONS[action]}"
end