class ActiveMerchant::Billing::CashnetGateway

Constants

CASHNET_CODES

Public Class Methods

new(options = {}) click to toggle source

Creates a new CashnetGateway

Options

  • :merchantGateway Merchant (REQUIRED)

  • :operator – Operator (REQUIRED)

  • :password – Password (REQUIRED)

  • :merchant_gateway_name – Site name (REQUIRED)

  • :station – Station (defaults to “WEB”)

  • :custcode – Customer code (defaults to “ActiveMerchant/#{ActiveMerchant::VERSION}”)

  • :default_item_code – Default item code (defaults to “FEE”, can be overridden on a per-transaction basis with options)

Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/cashnet.rb, line 29
def initialize(options = {})
  requires!(
    options,
    :merchant,
    :operator,
    :password,
    :merchant_gateway_name
  )
  options[:default_item_code] ||= 'FEE'
  super
end

Public Instance Methods

purchase(money, payment_object, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 41
def purchase(money, payment_object, options = {})
  post = {}
  add_creditcard(post, payment_object)
  add_invoice(post, options)
  add_address(post, options)
  add_customer_data(post, options)
  commit('SALE', money, post)
end
refund(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 50
def refund(money, identification, options = {})
  post = {}
  post[:origtx]  = identification
  add_invoice(post, options)
  add_customer_data(post, options)
  commit('REFUND', money, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 62
def scrub(transcript)
  transcript.
    gsub(%r{(password=)[^&]+}, '\1[FILTERED]').
    gsub(%r{(cardno=)[^&]+}, '\1[FILTERED]').
    gsub(%r{(cid=)[^&]+}, '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 58
def supports_scrubbing?
  true
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 114
def add_address(post, options)
  if address = (options[:shipping_address] || options[:billing_address] || options[:address])
    post[:addr_g]       = String(address[:address1]) + ',' + String(address[:address2])
    post[:city_g]       = address[:city]
    post[:state_g]      = address[:state]
    post[:zip_g]        = address[:zip]
  end
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 100
def add_creditcard(post, creditcard)
  post[:cardno]          = creditcard.number
  post[:cid]             = creditcard.verification_value
  post[:expdate]         = expdate(creditcard)
  post[:card_name_g]     = creditcard.name
  post[:fname]           = creditcard.first_name
  post[:lname]           = creditcard.last_name
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 123
def add_customer_data(post, options)
  post[:email_g]  = options[:email]
  post[:custcode]  = options[:custcode] unless empty?(options[:custcode])
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 109
def add_invoice(post, options)
  post[:order_number]    = options[:order_id] if options[:order_id].present?
  post[:itemcode]       = (options[:item_code] || @options[:default_item_code])
end
commit(action, money, fields) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 71
def commit(action, money, fields)
  fields[:amount] = amount(money)
  url = (test? ? test_url : live_url) + CGI.escape(@options[:merchant_gateway_name])
  raw_response = ssl_post(url, post_data(action, fields))
  parsed_response = parse(raw_response)

  return unparsable_response(raw_response) unless parsed_response

  success = (parsed_response[:result] == '0')
  Response.new(
    success,
    CASHNET_CODES[parsed_response[:result]],
    parsed_response,
    test:          test?,
    authorization: (success ? parsed_response[:tx] : '')
  )
end
expdate(creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 128
def expdate(creditcard)
  year  = format(creditcard.year, :two_digits)
  month = format(creditcard.month, :two_digits)

  "#{month}#{year}"
end
handle_response(response) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 142
def handle_response(response)
  if (200...300).cover?(response.code.to_i)
    return response.body
  elsif response.code.to_i == 302
    return ssl_get(URI.parse(response['location']))
  end
  raise ResponseError.new(response)
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 135
def parse(body)
  match = body.match(/<cngateway>(.*)<\/cngateway>/)
  return nil unless match

  Hash[CGI::parse(match[1]).map { |k, v| [k.to_sym, v.first] }]
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 89
def post_data(action, parameters = {})
  post = {}
  post[:command]        = action
  post[:merchant]       = @options[:merchant]
  post[:operator]       = @options[:operator]
  post[:password]       = @options[:password]
  post[:station]        = (@options[:station] || 'WEB')
  post[:custcode]       = (@options[:custcode] || "ActiveMerchant/#{ActiveMerchant::VERSION}")
  post.merge(parameters).collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
end
unparsable_response(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/cashnet.rb, line 151
def unparsable_response(raw_response)
  message = 'Unparsable response received from Cashnet. Please contact Cashnet if you continue to receive this message.'
  message += " (The raw response returned by the API was #{raw_response.inspect})"
  return Response.new(false, message)
end