class ActiveMerchant::Billing::MerchantWareGateway

Constants

ACTIONS
ENV_NAMESPACES
ENV_NAMESPACES_V4
TX_NAMESPACE
TX_NAMESPACE_V4

Public Class Methods

new(options = {}) click to toggle source

Creates a new MerchantWareGateway

The gateway requires that a valid login, password, and name be passed in the options hash.

Options

  • :login - The MerchantWARE SiteID.

  • :password - The MerchantWARE Key.

  • :name - The MerchantWARE Name.

Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 43
def initialize(options = {})
  requires!(options, :login, :password, :name)
  super
end

Public Instance Methods

authorize(money, credit_card, options = {}) click to toggle source

Authorize a credit card for a given amount.

Parameters

  • money - The amount to be authorized as an Integer value in cents.

  • credit_card - The CreditCard details for the transaction.

  • options

    • :order_id - A unique reference for this order (required).

    • :billing_address - The billing address for the cardholder.

# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 56
def authorize(money, credit_card, options = {})
  request = build_purchase_request(:authorize, money, credit_card, options)
  commit(:authorize, request)
end
capture(money, authorization, options = {}) click to toggle source

Capture authorized funds from a credit card.

Parameters

  • money - The amount to be captured as anInteger value in cents.

  • authorization - The authorization string returned from the initial authorization.

# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 79
def capture(money, authorization, options = {})
  request = build_capture_request(:capture, money, authorization, options)
  commit(:capture, request)
end
credit(money, identification, options = {}) click to toggle source

Refund an amount back a cardholder

Parameters

  • money - The amount to be refunded as an Integer value in cents.

  • identification - The credit card you want to refund or the authorization for the existing transaction you are refunding.

  • options

    • :order_id - A unique reference for this order (required when performing a non-referenced credit)

# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 104
def credit(money, identification, options = {})
  if identification.is_a?(String)
    ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
    refund(money, identification, options)
  else
    perform_credit(money, identification, options)
  end
end
purchase(money, credit_card, options = {}) click to toggle source

Authorize and immediately capture funds from a credit card.

Parameters

  • money - The amount to be authorized as anInteger value in cents.

  • credit_card - The CreditCard details for the transaction.

  • options

    • :order_id - A unique reference for this order (required).

    • :billing_address - The billing address for the cardholder.

# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 69
def purchase(money, credit_card, options = {})
  request = build_purchase_request(:purchase, money, credit_card, options)
  commit(:purchase, request)
end
refund(money, reference, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 113
def refund(money, reference, options = {})
  perform_reference_credit(money, reference, options)
end
void(authorization, options = {}) click to toggle source

Void a transaction.

Parameters

  • authorization - The authorization string returned from the initial authorization or purchase.

# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 88
def void(authorization, options = {})
  reference, options[:order_id] = split_reference(authorization)
  request = v4_soap_request(:void) do |xml|
    add_reference_token(xml, reference)
  end
  commit(:void, request, true)
end

Private Instance Methods

add_address(xml, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 216
def add_address(xml, options)
  if address = options[:billing_address] || options[:address]
    xml.tag! 'strAVSStreetAddress', address[:address1]
    xml.tag! 'strAVSZipCode', address[:zip]
  end
end
add_amount(xml, money, tag = 'strAmount') click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 204
def add_amount(xml, money, tag = 'strAmount')
  xml.tag! tag, amount(money)
end
add_credentials(xml) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 194
def add_credentials(xml)
  xml.tag! 'strSiteId', @options[:login]
  xml.tag! 'strKey', @options[:password]
  xml.tag! 'strName', @options[:name]
end
add_credit_card(xml, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 223
def add_credit_card(xml, credit_card)
  if credit_card.respond_to?(:track_data) && credit_card.track_data.present?
    xml.tag! 'trackData', credit_card.track_data
  else
    xml.tag! 'strPAN', credit_card.number
    xml.tag! 'strExpDate', expdate(credit_card)
    xml.tag! 'strCardHolder', credit_card.name
    xml.tag! 'strCVCode', credit_card.verification_value if credit_card.verification_value?
  end
end
add_invoice(xml, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 200
def add_invoice(xml, options)
  xml.tag! 'strOrderNumber', options[:order_id].to_s.gsub(/[^\w]/, '').slice(0, 25)
end
add_reference(xml, reference) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 208
def add_reference(xml, reference)
  xml.tag! 'strReferenceCode', reference
end
add_reference_token(xml, reference) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 212
def add_reference_token(xml, reference)
  xml.tag! 'token', reference
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 315
def authorization_from(response)
  [response['ReferenceID'], response['OrderNumber']].join(';') if response[:success]
end
build_capture_request(action, money, identification, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 160
def build_capture_request(action, money, identification, options)
  reference, options[:order_id] = split_reference(identification)

  soap_request(action) do |xml|
    add_reference(xml, reference)
    add_invoice(xml, options)
    add_amount(xml, money)
  end
end
build_purchase_request(action, money, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 149
def build_purchase_request(action, money, credit_card, options)
  requires!(options, :order_id)

  soap_request(action) do |xml|
    add_invoice(xml, options)
    add_amount(xml, money)
    add_credit_card(xml, credit_card)
    add_address(xml, options)
  end
end
commit(action, request, v4 = false) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 291
def commit(action, request, v4 = false)
  begin
    data = ssl_post(
      url(v4),
      request,
      'Content-Type' => 'text/xml; charset=utf-8',
      'SOAPAction'   => soap_action(action, v4)
    )
    response = parse(action, data)
  rescue ActiveMerchant::ResponseError => e
    response = parse_error(e.response)
  end

  Response.new(
    response[:success],
    response[:message],
    response,
    test: test?,
    authorization: authorization_from(response),
    avs_result: { code: response['AVSResponse'] },
    cvv_result: response['CVResponse']
  )
end
parse(action, data) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 238
def parse(action, data)
  response = {}
  xml = REXML::Document.new(data)

  root = REXML::XPath.first(xml, "//#{ACTIONS[action]}Response/#{ACTIONS[action]}Result")

  root.elements.each do |element|
    response[element.name] = element.text
  end

  status, code, message = response['ApprovalStatus'].split(';')
  response[:status] = status

  if response[:success] = status == 'APPROVED'
    response[:message] = status
  else
    response[:message] = message
    response[:failure_code] = code
  end

  response
end
parse_error(http_response) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 261
def parse_error(http_response)
  response = {}
  response[:http_code] = http_response.code
  response[:http_message] = http_response.message
  response[:success] = false

  document = REXML::Document.new(http_response.body)

  node     = REXML::XPath.first(document, '//soap:Fault')

  node.elements.each do |element|
    response[element.name] = element.text
  end

  response[:message] = response['faultstring'].to_s.tr("\n", ' ')
  response
rescue REXML::ParseException
  response[:http_body]        = http_response.body
  response[:message]          = 'Failed to parse the failed response'
  response
end
perform_credit(money, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 182
def perform_credit(money, credit_card, options)
  requires!(options, :order_id)

  request = soap_request(:credit) do |xml|
    add_invoice(xml, options)
    add_amount(xml, money)
    add_credit_card(xml, credit_card)
  end

  commit(:credit, request)
end
perform_reference_credit(money, identification, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 170
def perform_reference_credit(money, identification, options)
  reference, options[:order_id] = split_reference(identification)

  request = soap_request(:reference_credit) do |xml|
    add_reference(xml, reference)
    add_invoice(xml, options)
    add_amount(xml, money, 'strOverrideAmount')
  end

  commit(:reference_credit, request)
end
soap_action(action, v4 = false) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 283
def soap_action(action, v4 = false)
  v4 ? "#{TX_NAMESPACE_V4}#{ACTIONS[action]}" : "#{TX_NAMESPACE}/#{ACTIONS[action]}"
end
soap_request(action) { |xml| ... } click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 119
def soap_request(action)
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  xml.tag! 'env:Envelope', ENV_NAMESPACES do
    xml.tag! 'env:Body' do
      xml.tag! ACTIONS[action], 'xmlns' => TX_NAMESPACE do
        add_credentials(xml)
        yield xml
      end
    end
  end
  xml.target!
end
split_reference(reference) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 234
def split_reference(reference)
  reference.to_s.split(';')
end
url(v4 = false) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 287
def url(v4 = false)
  v4 ? v4_live_url : live_url
end
v4_soap_request(action) { |xml| ... } click to toggle source
# File lib/active_merchant/billing/gateways/merchant_ware.rb, line 133
def v4_soap_request(action)
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  xml.tag! 'soap:Envelope', ENV_NAMESPACES_V4 do
    xml.tag! 'soap:Body' do
      xml.tag! ACTIONS[:void], 'xmlns' => TX_NAMESPACE_V4 do
        xml.tag! 'merchantName', @options[:name]
        xml.tag! 'merchantSiteId', @options[:login]
        xml.tag! 'merchantKey', @options[:password]
        yield xml
      end
    end
  end
  xml.target!
end