Skip to content

Instantly share code, notes, and snippets.

@pavelnikolov
Forked from rbjordan3/netaxept.rb
Created June 10, 2013 11:55
Show Gist options
  • Save pavelnikolov/5748217 to your computer and use it in GitHub Desktop.
Save pavelnikolov/5748217 to your computer and use it in GitHub Desktop.
require 'test_helper'
class RemoteNetaxeptTest < Test::Unit::TestCase
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
def setup
@gateway = NetaxeptGateway.new(fixtures(:netaxept))
@amount = 100
@credit_card = credit_card('4925000000000004')
@declined_card = credit_card('4925000000000087')
@options = {
:order_id => generate_unique_id
}
end
def test_successful_purchase
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
assert_equal 'OK', response.message
end
#def test_failed_purchase
# assert response = @gateway.purchase(@amount, @declined_card, @options)
# assert_failure response
# assert_equal 'Unable to sale', response.message
#end
#
#def test_authorize_and_capture
# assert auth = @gateway.authorize(@amount, @credit_card, @options)
# assert_success auth
# assert_equal 'OK', auth.message
# assert auth.authorization
# assert capture = @gateway.capture(@amount, auth.authorization)
# assert_success capture
#end
#
#def test_failed_capture
# assert response = @gateway.capture(@amount, '')
# assert_failure response
# assert_equal "Unable to find transaction", response.message
#end
#
def test_successful_credit
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
response = @gateway.credit(@amount, response.authorization)
assert_success response
end
#
#def test_failed_credit
# assert response = @gateway.purchase(@amount, @credit_card, @options)
# assert_success response
#
# response = @gateway.credit(@amount+100, response.authorization)
# assert_failure response
# assert_equal "Unable to credit more than captured amount", response.message
#end
#
def test_successful_void
assert response = @gateway.authorize(@amount, @credit_card, @options)
assert_success response
response = @gateway.void(response.authorization)
assert_success response
end
#def test_failed_void
# assert response = @gateway.purchase(@amount, @credit_card, @options)
# assert_success response
#
# response = @gateway.void(response.authorization)
# assert_failure response
# assert_equal "Unable to annul, wrong state", response.message
#end
def test_successful_amex_purchase
credit_card = credit_card('378282246310005', :brand => 'american_express')
assert credit_card.valid?, credit_card.errors.inspect
assert response = @gateway.purchase(@amount, credit_card, @options)
assert_success response
assert_equal 'OK', response.message
end
def test_successful_master_purchase
credit_card = credit_card('5413000000000000', :brand => 'master')
assert credit_card.valid?, credit_card.errors.inspect
assert response = @gateway.purchase(@amount, credit_card, @options)
assert_success response
assert_equal 'OK', response.message
end
#
#def test_error_in_transaction_setup
# assert response = @gateway.purchase(@amount, @credit_card, @options.merge(:currency => 'BOGG'))
# assert_failure response
# assert_match(/currency code/, response.message)
#end
# def test_error_in_payment_details #
#def test_successful_amex_purchase
# credit_card = credit_card('378282246310005', :brand => 'american_express')
# assert credit_card.valid?, credit_card.errors.inspect
# assert response = @gateway.purchase(@amount, credit_card, @options)
# assert_success response
# assert_equal 'OK', response.message
#end
#
#def test_successful_master_purchase
# credit_card = credit_card('5413000000000000', :brand => 'master')
# assert credit_card.valid?, credit_card.errors.inspect
# assert response = @gateway.purchase(@amount, credit_card, @options)
# assert_success response
# assert_equal 'OK', response.message
#end
# assert response = @gateway.purchase(@amount, credit_card(''), @options)
# assert_failure response
# assert_equal "Unable to process setup", response.message
# assert_equal "137:7", response.error_detail
#end
#def test_amount_is_not_required_again_when_capturing_authorization
# assert response = @gateway.authorize(@amount, @credit_card, @options)
# assert_success response
#
# assert response = @gateway.capture(nil, response.authorization)
# assert_equal "OK", response.message
#end
#def test_invalid_login
# gateway = NetaxeptGateway.new(
# :login => '',
# :password => ''
# )
# assert response = gateway.purchase(@amount, @credit_card, @options)
# assert_failure response
# assert_match(/Unable to authenticate merchant/, response.message)
#end
end
require 'digest/md5'
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class NetaxeptGateway < Gateway
self.test_url = 'https://epayment-test.bbs.no/'
self.live_url = 'https://epayment.bbs.no/'
# The countries the gateway supports merchants from as 2 digit ISO country codes
self.supported_countries = ['NO', 'DK', 'SE', 'FI']
# The card types supported by the payment gateway
self.supported_cardtypes = [:visa, :master, :american_express]
# The homepage URL of the gateway
self.homepage_url = 'http://www.betalingsterminal.no/Netthandel-forside/'
# The name of the gateway
self.display_name = 'BBS Netaxept'
self.money_format = :cents
self.default_currency = 'NOK'
def initialize(options = {})
requires!(options, :login, :password)
@options = options
super
end
def purchase(money, creditcard, options = {})
requires!(options, :order_id)
post = {}
add_credentials(post, options)
add_transaction(post, options)
add_creditcard(post, creditcard)
add_order(post, money, options)
commit('CAPTURE', post)
end
def authorize(money, creditcard, options = {})
requires!(options, :order_id)
post = {}
add_credentials(post, options)
add_transaction(post, options)
add_order(post, money, options)
add_creditcard(post, creditcard)
commit('Auth', post)
end
def capture(money, authorization, options = {})
post = {}
add_credentials(post, options)
add_authorization(post, authorization, money)
commit('Capture', post, false)
end
def refund(money, authorization, options = {})
post = {}
add_credentials(post, options)
add_authorization(post, authorization, money)
commit('Credit', post, false)
end
def credit(money, authorization, options = {})
deprecated CREDIT_DEPRECATION_MESSAGE
refund(money, authorization, options)
end
def void(authorization, options = {})
post = {}
add_credentials(post, options)
add_authorization(post, authorization)
commit('Annul', post, false)
end
def test?
@options[:test] || Base.gateway_mode == :test
end
private
def add_credentials(post, options)
post[:merchantId] = @options[:login]
post[:token] = @options[:password]
end
def add_authorization(post, authorization, money=nil)
post[:transactionId] = authorization
post[:transactionAmount] = amount(money) if money
end
def add_transaction(post, options)
post[:serviceType] = 'M'
post[:redirectUrl] = 'http://localhost/netaxept/Process.php'
end
def add_order(post, money, options)
post[:orderNumber] = options[:order_id]
post[:amount] = amount(money)
post[:currencyCode] = (options[:currency] || currency(money))
end
def add_creditcard(post, options)
post[:pan] = options.number
post[:expiryDate] = format(options.month, :two_digits) + format(options.year, :two_digits)
post[:securityCode] = options.verification_value
end
def commit(action, parameters, setup=true)
response = {:success => false}
catch(:exception) do
if setup
commit_transaction_setup(response, parameters)
commit_payment_details(response, parameters)
# Use query to check that auth was successful
query_transaction(response, parameters)
unless action == "Auth"
commit_transaction(action, response, parameters)
end
end
response[:success] = true
end
Response.new(response[:success], response[:message], response, :test => test?, :authorization => response[:authorization])
end
def commit_transaction_setup(response, parameters)
parameters[:autoAuth] = "true"
response[:setup] = parse(ssl_get(build_url("Netaxept/Register.aspx", pick(parameters, :merchantId, :token,
:amount, :currencyCode, :orderNumber,
:serviceType, :autoAuth ))))
process(response, :setup)
unless response[:authorization].blank?
parameters[:transactionId] = response[:authorization]
end
end
def commit_payment_details(response, parameters)
response[:paymentDetails] = parse(ssl_get(build_url("terminal/default.aspx",
pick(parameters, :merchantId, :transactionId, :pan,
:expiryDate, :securityCode ))), false)
process(response, :paymentDetails)
end
def query_transaction(response, parameters)
result = ssl_get(build_url("Netaxept/query.aspx", pick(parameters, :merchantId, :token, :transactionId)))
response[:processSetup] = parse(result)
process(response, :processSetup)
# since auto auth is turned - Cannot look at the auto response this query is as close as it gets.
#process(response, :processSetup)
end
def commit_transaction(action, response, parameters)
parameters[:operation] = action
result = ssl_get(build_url("Netaxept/process.aspx", pick(parameters, :merchantId, :token, :transactionId, :description, :operation)))
response[:action] = parse(result)
process(response, :action)
end
def process(response, step)
if response[step][:container] =~ /Exception|Error/
response[:message] = response[step]['Message']
throw :exception
else
if response[step]['Error'] =~ /.*/
response[:message] = response[step]['Error']['ResponseText']
throw :exception
else
message = (response[step]['ResponseText'] || response[step]['ResponseCode'])
response[:message] = (message || response[:message])
response[:authorization] = response[step]['TransactionId']
end
end
end
def parse(result, expects_xml=true)
if expects_xml
doc = REXML::Document.new(result)
extract_xml(doc.root).merge(:container => doc.root.name)
else
{:result => result}
end
end
def extract_xml(element)
if element.has_elements?
hash = {}
element.elements.each do |e|
hash[e.name] = extract_xml(e)
end
hash
else
element.text
end
end
def url
(test? ? self.test_url : self.live_url)
end
def generate_transaction_id(options)
Digest::MD5.hexdigest("#{options.inspect}+#{Time.now}+#{rand}")
end
def pick(hash, *keys)
keys.inject({}){|h,key| h[key] = hash[key] if hash[key]; h}
end
def build_url(base, parameters=nil)
url = "#{test? ? self.test_url : self.live_url}"
url << base
if parameters
url << '?'
url << encode(parameters)
end
url
end
def encode(hash)
hash.collect{|(k,v)| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
end
class Response < Billing::Response
attr_reader :error_detail
def initialize(success, message, raw, options)
super
unless success
@error_detail = raw[:processSetup]['Result']['ResponseText'] if raw[:processSetup] && raw[:processSetup]['Result']
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment