class ActiveMerchant::Billing::NetbillingGateway

To perform PCI Compliant Repeat Billing

 Ensure that PCI Compliant Repeat Billing is enabled on your merchant account:
  "Enable PCI Compliant Repeat Billing, Up-selling and Cross-selling" in Step 6 of the Credit Cards setup page

Instead of passing a credit_card to authorize or purchase, pass the transaction id (res.authorization)
of a past Netbilling transaction

To store billing information without performing an operation, use the 'store' method which invokes the tran_type 'Q' (Quasi) operation and returns a transaction id to use in future Repeat Billing operations

Constants

FAILURE_MESSAGE
SUCCESS_CODES
SUCCESS_MESSAGE
TEST_LOGIN
TRANSACTIONS

Public Class Methods

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

Public Instance Methods

authorize(money, payment_source, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 42
def authorize(money, payment_source, options = {})
  post = {}
  add_amount(post, money)
  add_invoice(post, options)
  add_payment_source(post, payment_source)
  add_address(post, payment_source, options)
  add_customer_data(post, options)

  commit(:authorization, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 64
def capture(money, authorization, options = {})
  post = {}
  add_reference(post, authorization)
  commit(:capture, post)
end
credit(money, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 77
def credit(money, credit_card, options = {})
  post = {}
  add_amount(post, money)
  add_invoice(post, options)
  add_credit_card(post, credit_card)
  add_address(post, credit_card, options)
  add_customer_data(post, options)

  commit(:credit, post)
end
purchase(money, payment_source, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 53
def purchase(money, payment_source, options = {})
  post = {}
  add_amount(post, money)
  add_invoice(post, options)
  add_payment_source(post, payment_source)
  add_address(post, payment_source, options)
  add_customer_data(post, options)

  commit(:purchase, post)
end
refund(money, source, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 70
def refund(money, source, options = {})
  post = {}
  add_amount(post, money)
  add_reference(post, source)
  commit(:refund, post)
end
store(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 94
def store(credit_card, options = {})
  post = {}
  add_amount(post, 0)
  add_payment_source(post, credit_card)
  add_address(post, credit_card, options)
  add_customer_data(post, options)

  commit(:quasi, post)
end
test?() click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway#test?
# File lib/active_merchant/billing/gateways/netbilling.rb, line 104
def test?
  (@options[:login] == TEST_LOGIN || super)
end
void(source, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 88
def void(source, options = {})
  post = {}
  add_reference(post, source)
  commit(:void, post)
end

Private Instance Methods

add_address(post, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 123
def add_address(post, credit_card, options)
  if billing_address = options[:billing_address] || options[:address]
    post[:bill_street]     = billing_address[:address1]
    post[:cust_phone]      = billing_address[:phone]
    post[:bill_zip]        = billing_address[:zip]
    post[:bill_city]       = billing_address[:city]
    post[:bill_country]    = billing_address[:country]
    post[:bill_state]      = billing_address[:state]
  end

 if shipping_address = options[:shipping_address]
   first_name, last_name = parse_first_and_last_name(shipping_address[:name])

   post[:ship_name1]      = first_name
   post[:ship_name2]      = last_name
   post[:ship_street]     = shipping_address[:address1]
   post[:ship_zip]        = shipping_address[:zip]
   post[:ship_city]       = shipping_address[:city]
   post[:ship_country]    = shipping_address[:country]
   post[:ship_state]      = shipping_address[:state]
 end
end
add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 110
def add_amount(post, money)
  post[:amount] = amount(money)
end
add_credit_card(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 162
def add_credit_card(post, credit_card)
  post[:bill_name1] = credit_card.first_name
  post[:bill_name2] = credit_card.last_name
  post[:card_number] = credit_card.number
  post[:card_expire] = expdate(credit_card)
  post[:card_cvv2] = credit_card.verification_value
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 118
def add_customer_data(post, options)
  post[:cust_email] = options[:email]
  post[:cust_ip] = options[:ip]
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 146
def add_invoice(post, options)
  post[:description] = options[:description]
end
add_payment_source(params, source) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 150
def add_payment_source(params, source)
  if source.is_a?(String)
    add_transaction_id(params, source)
  else
    add_credit_card(params, source)
  end
end
add_reference(post, reference) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 114
def add_reference(post, reference)
  post[:orig_id] = reference
end
add_transaction_id(post, transaction_id) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 158
def add_transaction_id(post, transaction_id)
  post[:card_number] = 'CS:' + transaction_id
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 179
def commit(action, parameters)
  response = parse(ssl_post(self.live_url, post_data(action, parameters)))

  Response.new(success?(response), message_from(response), response,
    :test => test_response?(response),
    :authorization => response[:trans_id],
    :avs_result => { :code => response[:avs_code]},
    :cvv_result => response[:cvv2_code]
  )
rescue ActiveMerchant::ResponseError => e
  raise unless(e.response.code =~ /^[67]\d\d$/)
  return Response.new(false, e.response.message, {:status_code => e.response.code}, :test => test?)
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 201
def message_from(response)
  success?(response) ? SUCCESS_MESSAGE : (response[:auth_msg] || FAILURE_MESSAGE)
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 170
def parse(body)
  results = {}
  body.split(/&/).each do |pair|
    key,val = pair.split(/\=/)
    results[key.to_sym] = CGI.unescape(val)
  end
  results
end
parse_first_and_last_name(value) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 214
def parse_first_and_last_name(value)
  name = value.to_s.split(' ')

  last_name = name.pop || ''
  first_name = name.join(' ')
  [ first_name, last_name ]
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 205
def post_data(action, parameters = {})
  parameters[:account_id] = @options[:login]
  parameters[:site_tag] = @options[:site_tag] if @options[:site_tag].present?
  parameters[:pay_type] = 'C'
  parameters[:tran_type] = TRANSACTIONS[action]

  parameters.reject{|k,v| v.blank?}.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 197
def success?(response)
  SUCCESS_CODES.include?(response[:status_code])
end
test_response?(response) click to toggle source
# File lib/active_merchant/billing/gateways/netbilling.rb, line 193
def test_response?(response)
  !!(test? || response[:auth_msg] =~ /TEST/)
end