class ActiveMerchant::Billing::EpayGateway

Constants

API_HOST
CURRENCY_CODES

Public Class Methods

new(options = {}) click to toggle source

login: merchant number password: referrer url (for authorize authentication)

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

Public Instance Methods

authorize(money, credit_card_or_reference, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 60
def authorize(money, credit_card_or_reference, options = {})
  post = {}

  add_amount(post, money, options)
  add_invoice(post, options)
  add_creditcard_or_reference(post, credit_card_or_reference)
  add_instant_capture(post, false)

  commit(:authorize, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 82
def capture(money, authorization, options = {})
  post = {}

  add_reference(post, authorization)
  add_amount_without_currency(post, money)

  commit(:capture, post)
end
credit(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 108
def credit(money, identification, options = {})
  ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
  refund(money, identification, options)
end
purchase(money, credit_card_or_reference, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 71
def purchase(money, credit_card_or_reference, options = {})
  post = {}

  add_amount(post, money, options)
  add_creditcard_or_reference(post, credit_card_or_reference)
  add_invoice(post, options)
  add_instant_capture(post, true)

  commit(:authorize, post)
end
refund(money, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 99
def refund(money, identification, options = {})
  post = {}

  add_amount_without_currency(post, money)
  add_reference(post, identification)

  commit(:credit, post)
end
void(identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 91
def void(identification, options = {})
  post = {}

  add_reference(post, identification)

  commit(:void, post)
end

Private Instance Methods

add_amount(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 115
def add_amount(post, money, options)
  post[:amount]   = amount(money)
  post[:currency] = CURRENCY_CODES[(options[:currency] || currency(money)).to_sym]
end
add_amount_without_currency(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 120
def add_amount_without_currency(post, money)
  post[:amount] = amount(money)
end
add_creditcard(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 132
def add_creditcard(post, credit_card)
  post[:cardno]   = credit_card.number
  post[:cvc]      = credit_card.verification_value
  post[:expmonth] = credit_card.month
  post[:expyear]  = credit_card.year
end
add_creditcard_or_reference(post, credit_card_or_reference) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 139
def add_creditcard_or_reference(post, credit_card_or_reference)
  if credit_card_or_reference.respond_to?(:number)
    add_creditcard(post, credit_card_or_reference)
  else
    add_reference(post, credit_card_or_reference.to_s)
  end
end
add_instant_capture(post, option) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 147
def add_instant_capture(post, option)
  post[:instantcapture] = option ? 1 : 0
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 128
def add_invoice(post, options)
  post[:orderid] = format_order_number(options[:order_id])
end
add_reference(post, identification) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 124
def add_reference(post, identification)
  post[:transaction] = identification
end
authorize_post_data(params = {}) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 259
def authorize_post_data(params = {})
  params[:language] = '2'
  params[:cms] = 'activemerchant'
  params[:accepturl] = 'https://ssl.ditonlinebetalingssystem.dk/auth/default.aspx?accept=1'
  params[:declineurl] = 'https://ssl.ditonlinebetalingssystem.dk/auth/default.aspx?decline=1'
  params[:merchantnumber] = @options[:login]

  params.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end
commit(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 151
def commit(action, params)
  response = send("do_#{action}", params)

  if action == :authorize
    Response.new response['accept'].to_i == 1,
                 response['errortext'],
                 response,
                 :test => test?,
                 :authorization => response['tid']
  else
    Response.new response['result'] == 'true',
                 messages(response['epay'], response['pbs']),
                 response,
                 :test => test?,
                 :authorization => params[:transaction]
  end
end
do_authorize(params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 181
def do_authorize(params)
  headers = {}
  headers['Referer'] = (options[:password] || "activemerchant.org")

  response = raw_ssl_request(:post, 'https://' + API_HOST + '/auth/default.aspx', authorize_post_data(params), headers)

  # Authorize gives the response back by redirecting with the values in
  # the URL query
  if location = response['Location']
    query = CGI::parse(URI.parse(location.gsub(' ', '%20')).query)
  else
    return {
      'accept' => '0',
      'errortext' => 'ePay did not respond as expected. Please try again.',
      'response_code' => response.code,
      'response_message' => response.message
    }
  end

  result = {}
  query.each_pair do |k,v|
    result[k] = v.is_a?(Array) && v.size == 1 ? v[0] : v # make values like ['v'] into 'v'
  end
  result
end
do_capture(params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 207
def do_capture(params)
  response = soap_post('capture', params)
  {
    'result' => response.elements['//captureResponse/captureResult'].text,
    'pbs' => response.elements['//captureResponse/pbsResponse'].text,
    'epay' => response.elements['//captureResponse/epayresponse'].text
  }
end
do_credit(params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 216
def do_credit(params)
  response = soap_post('credit', params)
  {
    'result' => response.elements['//creditResponse/creditResult'].text,
    'pbs' => response.elements['//creditResponse/pbsresponse'].text,
    'epay' => response.elements['//creditResponse/epayresponse'].text
  }
end
do_void(params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 225
def do_void(params)
  response = soap_post('delete', params)
  {
    'result' => response.elements['//deleteResponse/deleteResult'].text,
    'epay' => response.elements['//deleteResponse/epayresponse'].text
  }
end
format_order_number(number) click to toggle source

Limited to 20 digits max

# File lib/active_merchant/billing/gateways/epay.rb, line 270
def format_order_number(number)
  number.to_s.gsub(/[^\w]/, '').rjust(4, "0")[0...20]
end
make_headers(data, soap_call) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 233
def make_headers(data, soap_call)
  {
    'Content-Type' => 'text/xml; charset=utf-8',
    'Host' => API_HOST,
    'Content-Length' => data.size.to_s,
    'SOAPAction' => self.live_url + '/' + soap_call
  }
end
messages(epay, pbs = nil) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 169
def messages(epay, pbs = nil)
  response = "ePay: #{epay}"
  response << " PBS: #{pbs}" if pbs
  return response
end
soap_post(method, params) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 175
def soap_post(method, params)
  data = xml_builder(params, method)
  headers = make_headers(data, method)
  REXML::Document.new(ssl_post('https://' + API_HOST + '/remote/payment.asmx', data, headers))
end
xml_builder(params, soap_call) click to toggle source
# File lib/active_merchant/billing/gateways/epay.rb, line 242
def xml_builder(params, soap_call)
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct!
    xml.tag! 'soap:Envelope', { 'xmlns:xsi' => 'http://schemas.xmlsoap.org/soap/envelope/',
                                'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
                                'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/' } do
      xml.tag! 'soap:Body' do
        xml.tag! soap_call, { 'xmlns' => self.live_url } do
          xml.tag! 'merchantnumber', @options[:login]
          xml.tag! 'transactionid', params[:transaction]
          xml.tag! 'amount', params[:amount].to_s if soap_call != 'delete'
        end
      end
    end
  xml.target!
end