class ActiveMerchant::Billing::TransactProGateway

For more information visit Transact Pro Services

This gateway was formerly associated with www.1stpayments.net

Written by Piers Chambers (Varyonic.com)

Public Class Methods

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

Public Instance Methods

authorize(amount, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 47
def authorize(amount, payment, options={})
  post = PostData.new
  add_invoice(post, amount, options)
  add_payment(post, payment)
  add_address(post, payment, options)
  add_customer_data(post, options)
  add_credentials(post)
  post[:rs] = @options[:terminal]

  MultiResponse.run do |r|
    r.process { commit('init_dms', post) }
    r.process do
      post = PostData.new
      post[:init_transaction_id] = r.authorization
      add_payment_cc(post, payment)
      post[:f_extended] = '4'

      commit('make_hold', post, amount)
    end
  end
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 69
def capture(amount, authorization, options={})
  identifier, original_amount = split_authorization(authorization)
  if amount && (amount != original_amount)
    raise ArgumentError.new("Partial capture is not supported, and #{amount.inspect} != #{original_amount.inspect}")
  end

  post = PostData.new
  add_credentials(post)
  post[:init_transaction_id] = identifier
  post[:f_extended] = '4'

  commit('charge_hold', post, original_amount)
end
purchase(amount, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 25
def purchase(amount, payment, options={})
  post = PostData.new
  add_invoice(post, amount, options)
  add_payment(post, payment)
  add_address(post, payment, options)
  add_customer_data(post, options)
  add_credentials(post)
  post[:rs] = @options[:terminal]

  MultiResponse.run do |r|
    r.process { commit('init', post) }
    r.process do
      post = PostData.new
      post[:init_transaction_id] = r.authorization
      add_payment_cc(post, payment)
      post[:f_extended] = '4'

      commit('charge', post, amount)
    end
  end
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 83
def refund(amount, authorization, options={})
  identifier, original_amount = split_authorization(authorization)

  post = PostData.new
  add_credentials(post, :account_guid)
  post[:init_transaction_id] = identifier
  post[:amount_to_refund] = amount(amount || original_amount)

  commit('refund', post)
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 104
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 94
def void(authorization, options={})
  identifier, amount = split_authorization(authorization)

  post = PostData.new
  add_credentials(post, :account_guid)
  post[:init_transaction_id] = identifier
  post[:amount_to_refund] = amount(amount)
  commit('cancel_dms', post)
end

Private Instance Methods

add_address(post, creditcard, options) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 118
def add_address(post, creditcard, options)
  if address = options[:billing_address]
    post[:street]  = address[:address1].to_s
    post[:city]    = address[:city].to_s
    post[:state]   = (address[:state].blank? ? 'NA' : address[:state].to_s)
    post[:zip]     = address[:zip].to_s
    post[:country] = address[:country].to_s
    post[:phone]   = (address[:phone].to_s.gsub(/[^0-9]/, '') || '0000000')
  end

  if address = options[:shipping_address]
    post[:shipping_name]    = "#{address.first_name} #{address.last_name}"
    post[:shipping_street]  = address[:address1].to_s
    post[:shipping_phone]   = address[:phone].to_s
    post[:shipping_zip]     = address[:zip].to_s
    post[:shipping_city]    = address[:city].to_s
    post[:shipping_country] = address[:country].to_s
    post[:shipping_state]   = (address[:state].blank? ? 'NA' : address[:state].to_s)
    post[:shipping_email]   = (options[:email] || 'noone@example.com')
  end
end
add_credentials(post, key=:guid) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 161
def add_credentials(post, key=:guid)
  post[key] = @options[:guid]
  post[:pwd] = Digest::SHA1.hexdigest(@options[:password])
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 113
def add_customer_data(post, options)
  post[:email] = (options[:email] || 'noone@example.com')
  post[:user_ip] = (options[:ip] || '127.0.0.1')
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 140
def add_invoice(post, money, options)
  post[:merchant_transaction_id] = options[:order_id] if options[:order_id]
  post[:amount] = amount(money)
  post[:currency] = (options[:currency] || currency(money))
  post[:description] = options[:description]
  post[:merchant_site_url] = options[:merchant]
end
add_payment(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 148
def add_payment(post, payment)
  post[:name_on_card] = "#{payment.first_name} #{payment.last_name}"
  post[:card_bin] = payment.first_digits
end
add_payment_cc(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 153
def add_payment_cc(post, credit_card)
  post[:cc] = credit_card.number
  post[:cvv] = credit_card.verification_value if credit_card.verification_value?
  year  = sprintf('%.4i', credit_card.year)
  month = sprintf('%.2i', credit_card.month)
  post[:expire] = "#{month}/#{year[2..3]}"
end
authorization_from(parameters, response, amount) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 194
def authorization_from(parameters, response, amount)
  identifier = (response[:id] || parameters[:init_transaction_id])
  authorization = [identifier]
  authorization << amount if amount
  authorization.join('|')
end
commit(action, parameters, amount=nil) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 181
def commit(action, parameters, amount=nil)
  url = (test? ? test_url : live_url)
  response = parse(ssl_post(url, post_data(action, parameters)))

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(parameters, response, amount),
    test: test?
  )
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 214
def message_from(response)
  (response[:message] || response[:status])
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 166
def parse(body)
  if body =~ /^ID:/
    body.split('~').reduce(Hash.new) { |h, v|
      m = v.match('(.*?):(.*)')
      h.merge!(m[1].underscore.to_sym => m[2])
    }
  elsif (m = body.match('(.*?):(.*)'))
    m[1] == 'OK' ?
      { status: 'success', id: m[2] } :
      { status: 'failure', message: m[2] }
  else
    Hash[status: body]
  end
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 218
def post_data(action, parameters = {})
  parameters[:a] = action
  parameters.to_s
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 201
def split_authorization(authorization)
  if authorization =~ /|/
    identifier, amount = authorization.split('|')
    [identifier, amount.to_i]
  else
    authorization
  end
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/transact_pro.rb, line 210
def success_from(response)
  (response[:status] =~ /success/i || response[:status] =~ /ok/i)
end