class ActiveMerchant::Billing::WorldpayUsGateway

Constants

ACTIONS

Public Class Methods

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

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 31
def authorize(money, payment, options={})
  post = {}
  add_invoice(post, money, options)
  add_payment_method(post, payment)
  add_customer_data(post, options)

  commit('authorize', post)
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 40
def capture(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit('capture', post)
end
purchase(money, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 22
def purchase(money, payment_method, options={})
  post = {}
  add_invoice(post, money, options)
  add_payment_method(post, payment_method)
  add_customer_data(post, options)

  commit('purchase', post)
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 49
def refund(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit("refund", post)
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 65
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/worldpay_us.rb, line 58
def void(authorization, options={})
  post = {}
  add_reference(post, authorization)

  commit('void', post)
end

Private Instance Methods

add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 74
def add_customer_data(post, options)
  if(billing_address = (options[:billing_address] || options[:address]))
    post[:ci_companyname] = billing_address[:company]
    post[:ci_billaddr1]   = billing_address[:address1]
    post[:ci_billaddr2]   = billing_address[:address2]
    post[:ci_billcity]    = billing_address[:city]
    post[:ci_billstate]   = billing_address[:state]
    post[:ci_billzip]     = billing_address[:zip]
    post[:ci_billcountry] = billing_address[:country]

    post[:ci_phone]       = billing_address[:phone]
    post[:ci_email]       = billing_address[:email]
    post[:ci_ipaddress]   = billing_address[:ip]
  end

  if(shipping_address = options[:shipping_address])
    post[:ci_shipaddr1] = shipping_address[:address1]
    post[:ci_shipaddr2] = shipping_address[:address2]
    post[:ci_shipcity] = shipping_address[:city]
    post[:ci_shipstate] = shipping_address[:state]
    post[:ci_shipzip]    = shipping_address[:zip]
    post[:ci_shipcountry]    = shipping_address[:country]
  end
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 99
def add_invoice(post, money, options)
  post[:amount] = amount(money)
  post[:currencycode] = (options[:currency] || currency(money))
  post[:merchantordernumber] = options[:order_id] if options[:order_id]
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 105
def add_payment_method(post, payment_method)
  post[:ccname] = payment_method.name
  post[:ccnum] = payment_method.number
  post[:cvv2] = payment_method.verification_value
  post[:expyear] = format(payment_method.year, :four_digits)
  post[:expmon] = format(payment_method.month, :two_digits)
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 118
def add_reference(post, authorization)
  historyid, orderid = split_authorization(authorization)
  post[:postonly] = historyid
  post[:historykeyid] = historyid
  post[:orderkeyid] = orderid
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 176
def authorization_from(response)
  [response['historyid'], response['orderid']].join("|")
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 144
def commit(action, post)
  post[:action] = ACTIONS[action] if ACTIONS[action]
  post[:acctid] = @options[:acctid]
  post[:subid] = @options[:subid]
  post[:merchantpin] = @options[:merchantpin]

  post[:authonly] = '1' if action == 'authorize'

  raw = parse(ssl_post(live_url, post.to_query))

  succeeded = success_from(raw['transresult'])
  Response.new(
    succeeded,
    message_from(succeeded, raw),
    raw,
    :authorization => authorization_from(raw),
    :test => test?
  )
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 168
def message_from(succeeded, response)
  if succeeded
    "Succeeded"
  else
    (response['transresult'] || response['Reason'] || "Unable to read error message")
  end
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 125
def parse(xml)
  response = {}
  doc = Nokogiri::XML(xml)
  message = doc.xpath("//plaintext")
  message.text.split(/\r?\n/).each do |line|
    key, value = line.split(%r{=})
    response[key] = value if key
  end
  response
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 113
def split_authorization(authorization)
  historyid, orderid = authorization.split("|")
  [historyid, orderid]
end
success_from(result) click to toggle source
# File lib/active_merchant/billing/gateways/worldpay_us.rb, line 164
def success_from(result)
  result == 'APPROVED'
end