class ActiveMerchant::Billing::CenposGateway

Constants

STANDARD_ERROR_CODE_MAPPING

Public Class Methods

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

Public Instance Methods

authorize(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 30
def authorize(amount, payment_method, options={})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method)
  add_customer_data(post, options)

  commit("Auth", post)
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 39
def capture(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit("SpecialForce", post)
end
credit(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 67
def credit(amount, payment_method, options={})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method)

  commit("Credit", post)
end
purchase(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 21
def purchase(amount, payment_method, options={})
  post = {}
  add_invoice(post, amount, options)
  add_payment_method(post, payment_method)
  add_customer_data(post, options)

  commit("Sale", post)
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 58
def refund(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_reference(post, authorization)
  add_customer_data(post, options)

  commit("SpecialReturn", post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 86
def scrub(transcript)
  transcript.
    gsub(%r((<acr1:CardNumber>)[^<]+(<))i, '\1[FILTERED]\2').
    gsub(%r((<acr1:CardVerificationNumber>)[^<]+(<))i, '\1[FILTERED]\2').
    gsub(%r((<acr:Password>)[^<]+(<))i, '\1[FILTERED]\2')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 82
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 75
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/cenpos.rb, line 48
def void(authorization, options={})
  post = {}
  add_void_required_elements(post)
  add_reference(post, authorization)
  add_remembered_amount(post, authorization)
  add_tax(post, options)

  commit("Void", post)
end

Private Instance Methods

add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 113
def add_customer_data(post, options)
  if(billing_address = (options[:billing_address] || options[:address]))
    post[:CustomerEmailAddress] = billing_address[:email]
    post[:CustomerPhone] = billing_address[:phone]
    post[:CustomerBillingAddress] = billing_address[:address1]
    post[:CustomerCity] = billing_address[:city]
    post[:CustomerState] = billing_address[:state]
    post[:CustomerZipCode] = billing_address[:zip]
  end
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 95
def add_invoice(post, money, options)
  post[:Amount] = amount(money)
  post[:CurrencyCode] = options[:currency] || currency(money)
  post[:InvoiceNumber] = options[:order_id]
  post[:InvoiceDetail] = options[:invoice_detail] if options[:invoice_detail]
  post[:CustomerCode] = options[:customer_code] if options[:customer_code]
  add_tax(post, options)
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 104
def add_payment_method(post, payment_method)
  post[:NameOnCard] = payment_method.name
  post[:CardNumber] = payment_method.number
  post[:CardVerificationNumber] = payment_method.verification_value
  post[:CardExpirationDate] = format(payment_method.month, :two_digits) + format(payment_method.year, :two_digits)
  post[:CardLastFourDigits] = payment_method.last_digits
  post[:MagneticData] = payment_method.track_data if payment_method.track_data
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 133
def add_reference(post, authorization)
  reference_number, last_four_digits = split_authorization(authorization)
  post[:ReferenceNumber] = reference_number
  post[:CardLastFourDigits] = last_four_digits
end
add_remembered_amount(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 139
def add_remembered_amount(post, authorization)
  post[:Amount] = split_authorization(authorization).last
end
add_tax(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 129
def add_tax(post, options)
  post[:TaxAmount] = amount(options[:tax] || 0)
end
add_void_required_elements(post) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 124
def add_void_required_elements(post)
  post[:GeoLocationInformation] = nil
  post[:IMEI] = nil
end
authorization_from(request, response) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 248
def authorization_from(request, response)
  [ response[:reference_number], request[:CardLastFourDigits], request[:Amount] ].join("|")
end
build_request(post) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 181
def build_request(post)
  xml = Builder::XmlMarkup.new :indent => 8
  xml.tag!("acr:MerchantId", post.delete(:MerchantId))
  xml.tag!("acr:Password", post.delete(:Password))
  xml.tag!("acr:UserId", post.delete(:UserId))
  post.sort.each do |field, value|
    xml.tag!("acr1:#{field}", value)
  end
  envelope(xml.target!)
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 143
def commit(action, post)
  post[:MerchantId] = @options[:merchant_id]
  post[:Password] = @options[:password]
  post[:UserId] = @options[:user_id]
  post[:TransactionType] = action

  data = build_request(post)
  begin
    raw = parse(ssl_post(self.live_url, data, headers))
  rescue ActiveMerchant::ResponseError => e
    if(e.response.code == "500" && e.response.body.start_with?("<s:Envelope"))
      raw = {
        message: "See transcript for detailed error description."
      }
    else
      raise
    end
  end

  succeeded = success_from(raw[:result])
  Response.new(
    succeeded,
    message_from(succeeded, raw),
    raw,
    authorization: authorization_from(post, raw),
    error_code: error_code_from(succeeded, raw),
    test: test?
  )
end
envelope(body) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 192
      def envelope(body)
        <<-EOS
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:acr="http://schemas.datacontract.org/2004/07/Acriter.ABI.CenPOS.EPayment.VirtualTerminal.Common" xmlns:acr1="http://schemas.datacontract.org/2004/07/Acriter.ABI.CenPOS.EPayment.VirtualTerminal.v6.Common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
   <soapenv:Body>
      <tem:ProcessCreditCard>
         <tem:request>
           #{body}
         </tem:request>
      </tem:ProcessCreditCard>
   </soapenv:Body>
</soapenv:Envelope>
        EOS
      end
error_code_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 257
def error_code_from(succeeded, response)
  succeeded ? nil : STANDARD_ERROR_CODE_MAPPING[response[:result]]
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 173
def headers
  {
    "Accept-Encoding" => "gzip,deflate",
    "Content-Type"  => "text/xml;charset=UTF-8",
    "SOAPAction"  => "http://tempuri.org/Transactional/ProcessCreditCard"
  }
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 231
def message_from(succeeded, response)
  if succeeded
    "Succeeded"
  else
    response[:message] || "Unable to read error message"
  end
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 207
def parse(xml)
  response = {}

  doc = Nokogiri::XML(xml)
  doc.remove_namespaces!
  body = doc.xpath("//ProcessCreditCardResult")
  body.children.each do |node|
    if (node.elements.size == 0)
      response[node.name.underscore.to_sym] = node.text
    else
      node.elements.each do |childnode|
        name = "#{node.name.underscore}_#{childnode.name.underscore}"
        response[name.to_sym] = childnode.text
      end
    end
  end unless doc.root.nil?

  response
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 252
def split_authorization(authorization)
  reference_number, last_four_digits, original_amount = authorization.split("|")
  [reference_number, last_four_digits, original_amount]
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/cenpos.rb, line 227
def success_from(response)
  response == "0"
end