class ActiveMerchant::Billing::MerchantWarriorGateway

Constants

POST_LIVE_URL
POST_TEST_URL
TOKEN_LIVE_URL
TOKEN_TEST_URL

Public Class Methods

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

Public Instance Methods

authorize(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 27
def authorize(money, payment_method, options = {})
  post = {}
  add_amount(post, money, options)
  add_order_id(post, options)
  add_address(post, options)
  add_payment_method(post, payment_method)
  commit('processAuth', post)
end
capture(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 45
def capture(money, identification, options = {})
  post = {}
  add_amount(post, money, options)
  add_transaction(post, identification)
  post['captureAmount'] = amount(money)
  commit('processCapture', post)
end
purchase(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 36
def purchase(money, payment_method, options = {})
  post = {}
  add_amount(post, money, options)
  add_order_id(post, options)
  add_address(post, options)
  add_payment_method(post, payment_method)
  commit('processCard', post)
end
refund(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 53
def refund(money, identification, options = {})
  post = {}
  add_amount(post, money, options)
  add_transaction(post, identification)
  post['refundAmount'] = amount(money)
  commit('refundCard', post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 75
def scrub(transcript)
  transcript.
    gsub(%r((&?paymentCardNumber=)[^&]*)i, '\1[FILTERED]').
    gsub(%r((CardNumber=)[^&]*)i, '\1[FILTERED]').
    gsub(%r((&?paymentCardCSC=)[^&]*)i, '\1[FILTERED]').
    gsub(%r((&?apiKey=)[^&]*)i, '\1[FILTERED]')
end
store(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 61
def store(creditcard, options = {})
  post = {
    'cardName' => scrub_name(creditcard.name),
    'cardNumber' => creditcard.number,
    'cardExpiryMonth' => format(creditcard.month, :two_digits),
    'cardExpiryYear'  => format(creditcard.year, :two_digits)
  }
  commit('addCard', post)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 71
def supports_scrubbing?
  true
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 89
def add_address(post, options)
  return unless(address = (options[:billing_address] || options[:address]))

  post['customerName'] = scrub_name(address[:name])
  post['customerCountry'] = address[:country]
  post['customerState'] = address[:state] || 'N/A'
  post['customerCity'] = address[:city]
  post['customerAddress'] = address[:address1]
  post['customerPostCode'] = address[:zip]
  post['customerIP'] = address[:ip]
  post['customerPhone'] = address[:phone]
  post['customerEmail'] = address[:email]
end
add_amount(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 130
def add_amount(post, money, options)
  currency = (options[:currency] || currency(money))

  post['transactionAmount'] = amount(money)
  post['transactionCurrency'] = currency
  post['hash'] = verification_hash(amount(money), currency)
end
add_auth(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 181
def add_auth(action, post)
  post['merchantUUID'] = @options[:merchant_uuid]
  post['apiKey'] = @options[:api_key]
  unless token?(post)
    post['method'] = action
  end
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 119
def add_creditcard(post, creditcard)
  post['paymentCardNumber'] = creditcard.number
  post['paymentCardName'] = scrub_name(creditcard.name)
  post['paymentCardExpiry'] = creditcard.expiry_date.expiration.strftime('%m%y')
  post['paymentCardCSC'] = creditcard.verification_value if creditcard.verification_value?
end
add_order_id(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 103
def add_order_id(post, options)
  post['transactionProduct'] = truncate(options[:order_id], 34) || SecureRandom.hex(15)
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 107
def add_payment_method(post, payment_method)
  if payment_method.respond_to?(:number)
    add_creditcard(post, payment_method)
  else
    add_token(post, payment_method)
  end
end
add_token(post, token) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 115
def add_token(post, token)
  post['cardID'] = token
end
add_transaction(post, identification) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 85
def add_transaction(post, identification)
  post['transactionID'] = identification
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 167
def commit(action, post)
  add_auth(action, post)

  response = parse(ssl_post(url_for(action, post), post_data(post)))

  Response.new(
    success?(response),
    response[:response_message],
    response,
    :test => test?,
    :authorization => (response[:card_id] || response[:transaction_id])
  )
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 149
def parse(body)
  xml = REXML::Document.new(body)

  response = {}
  xml.root.elements.to_a.each do |node|
    parse_element(response, node)
  end
  response
end
parse_element(response, node) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 159
def parse_element(response, node)
  if node.has_elements?
    node.elements.each { |element| parse_element(response, element) }
  else
    response[node.name.underscore.to_sym] = node.text
  end
end
post_data(post) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 205
def post_data(post)
  post.collect { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
end
scrub_name(name) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 126
def scrub_name(name)
  name.gsub(/[^a-zA-Z\. -]/, '')
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 201
def success?(response)
  (response[:response_code] == '0')
end
token?(post) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 197
def token?(post)
  (post['cardID'] || post['cardName'])
end
url_for(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 189
def url_for(action, post)
  if token?(post)
    [(test? ? TOKEN_TEST_URL : TOKEN_LIVE_URL), action].join('/')
  else
    (test? ? POST_TEST_URL : POST_LIVE_URL)
  end
end
verification_hash(money, currency) click to toggle source
# File lib/active_merchant/billing/gateways/merchant_warrior.rb, line 138
def verification_hash(money, currency)
  Digest::MD5.hexdigest(
    (
      @options[:api_passphrase].to_s +
      @options[:merchant_uuid].to_s +
      money.to_s +
      currency
    ).downcase
  )
end