class ActiveMerchant::Billing::SageGateway::SageVault

Constants

ACTION_ELEMENTS
ENVELOPE_NAMESPACES
SOAP_ACTIONS

Public Class Methods

new(options, gateway) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 303
def initialize(options, gateway)
  @live_url = 'https://www.sagepayments.net/web_services/wsVault/wsVault.asmx'
  @options = options
  @gateway = gateway
end

Public Instance Methods

store(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 309
def store(credit_card, options = {})
  request = build_store_request(credit_card, options)
  commit(:store, request)
end
unstore(identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 314
def unstore(identification, options = {})
  request = build_unstore_request(identification, options)
  commit(:unstore, request)
end

Private Instance Methods

add_credit_card(xml, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 353
def add_credit_card(xml, credit_card, options)
  xml.tag! 'ns1:CARDNUMBER', credit_card.number
  xml.tag! 'ns1:EXPIRATION_DATE', exp_date(credit_card)
end
add_customer_data(xml) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 348
def add_customer_data(xml)
  xml.tag! 'ns1:M_ID', @options[:login]
  xml.tag! 'ns1:M_KEY', @options[:password]
end
add_identification(xml, identification, options) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 358
def add_identification(xml, identification, options)
  xml.tag! 'ns1:GUID', identification
end
build_headers(action) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 425
def build_headers(action)
  {
    'SOAPAction' => SOAP_ACTIONS[action],
    'Content-Type' => 'text/xml; charset=utf-8'
  }
end
build_soap_request(action, body) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 405
def build_soap_request(action, body)
  xml = Builder::XmlMarkup.new

  xml.instruct!
  xml.tag! 'SOAP-ENV:Envelope', ENVELOPE_NAMESPACES do
    xml.tag! 'SOAP-ENV:Body' do
      xml.tag! "ns1:#{ACTION_ELEMENTS[action]}" do
        add_customer_data(xml)
        xml << body
      end
    end
  end
  xml.target!
end
build_store_request(credit_card, options) click to toggle source

A valid request example, since the Sage docs have none:

<?xml version=“1.0” encoding=“UTF-8” ?> <SOAP-ENV:Envelope

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://www.sagepayments.net/web_services/wsVault/wsVault">
<SOAP-ENV:Body>
  <ns1:INSERT_CREDIT_CARD_DATA>
    <ns1:M_ID>279277516172</ns1:M_ID>
    <ns1:M_KEY>O3I8G2H8V6A3</ns1:M_KEY>
    <ns1:CARDNUMBER>4111111111111111</ns1:CARDNUMBER>
    <ns1:EXPIRATION_DATE>0915</ns1:EXPIRATION_DATE>
  </ns1:INSERT_CREDIT_CARD_DATA>
</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

# File lib/active_merchant/billing/gateways/sage.rb, line 336
def build_store_request(credit_card, options)
  xml = Builder::XmlMarkup.new
  add_credit_card(xml, credit_card, options)
  xml.target!
end
build_unstore_request(identification, options) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 342
def build_unstore_request(identification, options)
  xml = Builder::XmlMarkup.new
  add_identification(xml, identification, options)
  xml.target!
end
commit(action, request) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 369
def commit(action, request)
  response = parse(
    @gateway.ssl_post(
      @live_url,
      build_soap_request(action, request),
      build_headers(action)
    )
  )

  case action
  when :store
    success = response[:success] == 'true'
    message = response[:message].downcase.capitalize if response[:message]
  when :unstore
    success = response[:delete_data_result] == 'true'
    message = success ? 'Succeeded' : 'Failed'
  end

  Response.new(
    success,
    message,
    response,
    authorization: response[:guid]
  )
end
exp_date(credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 362
def exp_date(credit_card)
  year  = sprintf('%.4i', credit_card.year)
  month = sprintf('%.2i', credit_card.month)

  "#{month}#{year[-2..-1]}"
end
hashify_xml!(xml, response) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 438
def hashify_xml!(xml, response)
  xml = REXML::Document.new(xml)

  # Store
  xml.elements.each('//Table1/*') do |node|
    response[node.name.underscore.to_sym] = node.text
  end

  # Unstore
  xml.elements.each('//DELETE_DATAResponse/*') do |node|
    response[node.name.underscore.to_sym] = node.text
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/sage.rb, line 432
def parse(body)
  response = {}
  hashify_xml!(body, response)
  response
end