class ActiveMerchant::Billing::SecurionPayGateway

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/securion_pay.rb, line 34
def initialize(options={})
  requires!(options, :secret_key)
  super
end

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 44
def authorize(money, payment, options={})
  post = create_post_for_auth_or_purchase(money, payment, options)
  post[:captured] = 'false'
  commit('charges', post, options)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 50
def capture(money, authorization, options = {})
  post = {}
  add_amount(post, money, options)
  commit("charges/#{CGI.escape(authorization)}/capture", post, options)
end
customer(options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 88
def customer(options = {})
  if options[:customer_id].blank?
    return nil
  else
    commit("customers/#{CGI.escape(options[:customer_id])}", nil, options, :get)
  end
end
purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 39
def purchase(money, payment, options={})
  post = create_post_for_auth_or_purchase(money, payment, options)
  commit('charges', post, options)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 56
def refund(money, authorization, options = {})
  post = {}
  add_amount(post, money, options)
  commit("charges/#{CGI.escape(authorization)}/refund", post, options)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 100
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r((card\[number\]=)\d+), '\1[FILTERED]').
    gsub(%r((card\[cvc\]=)\d+), '\1[FILTERED]')
end
store(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 73
def store(credit_card, options = {})
  if options[:customer_id].blank?
    MultiResponse.run() do |r|
      # create charge object
      r.process { authorize(100, credit_card, options) }
      # create customer and save card
      r.process { create_customer_add_card(r.authorization, options) }
      # void the charge
      r.process(:ignore_result) { void(r.params['metadata']['chargeId'], options) }
    end
  else
    verify(credit_card, options)
  end
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 96
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 66
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/securion_pay.rb, line 62
def void(authorization, options = {})
  commit("charges/#{CGI.escape(authorization)}/refund", {}, options)
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 167
def add_address(post, options)
  return unless post[:card]&.kind_of?(Hash)
  if address = options[:billing_address]
    post[:card][:addressLine1] = address[:address1] if address[:address1]
    post[:card][:addressLine2] = address[:address2] if address[:address2]
    post[:card][:addressCountry] = address[:country] if address[:country]
    post[:card][:addressZip] = address[:zip] if address[:zip]
    post[:card][:addressState] = address[:state] if address[:state]
    post[:card][:addressCity] = address[:city] if address[:city]
  end
end
add_amount(post, money, options, include_currency = false) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 143
def add_amount(post, money, options, include_currency = false)
  currency = (options[:currency] || default_currency)
  post[:amount] = localized_amount(money, currency)
  post[:currency] = currency.downcase if include_currency
end
add_creditcard(post, creditcard, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 149
def add_creditcard(post, creditcard, options)
  card = {}
  if creditcard.respond_to?(:number)
    card[:number] = creditcard.number
    card[:expMonth] = creditcard.month
    card[:expYear] = creditcard.year
    card[:cvc] = creditcard.verification_value if creditcard.verification_value?
    card[:cardholderName] = creditcard.name if creditcard.name

    post[:card] = card
    add_address(post, options)
  elsif creditcard.kind_of?(String)
    post[:card] = creditcard
  else
    raise ArgumentError.new("Unhandled payment method #{creditcard.class}.")
  end
end
add_customer(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 119
def add_customer(post, payment, options)
  post[:customerId] = options[:customer_id] if options[:customer_id]
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 123
def add_customer_data(post, options)
  post[:description] = options[:description]
  post[:ip] =          options[:ip]
  post[:user_agent] =  options[:user_agent]
  post[:referrer] =    options[:referrer]
end
api_request(endpoint, parameters = nil, options = {}, method = nil) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 231
def api_request(endpoint, parameters = nil, options = {}, method = nil)
  raw_response = response = nil
  begin
    if method.blank?
      raw_response = ssl_post(self.live_url + endpoint, post_data(parameters), headers(options))
    else
      raw_response = ssl_request(method, self.live_url + endpoint, post_data(parameters), headers(options))
    end
    response = parse(raw_response)
  rescue ResponseError => e
    raw_response = e.response.body
    response = response_error(raw_response)
  rescue JSON::ParserError
    response = json_error(raw_response)
  end
  response
end
commit(url, parameters = nil, options = {}, method = nil) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 183
def commit(url, parameters = nil, options = {}, method = nil)
  response = api_request(url, parameters, options, method)
  success = !response.key?('error')

  Response.new(success,
    (success ? 'Transaction approved' : response['error']['message']),
    response,
    test: test?,
    authorization: (success ? response['id'] : response['error']['charge']),
    error_code: (success ? nil : STANDARD_ERROR_CODE_MAPPING[response['error']['code']])
  )
end
create_customer_add_card(authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 109
def create_customer_add_card(authorization, options)
  post = {}
  post[:email] = options[:email]
  post[:description] = options[:description]
  post[:card] = authorization
  post[:metadata] = {}
  post[:metadata][:chargeId] = authorization
  commit('customers', post, options)
end
create_post_for_auth_or_purchase(money, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 130
def create_post_for_auth_or_purchase(money, payment, options)
  post = {}
  add_amount(post, money, options, true)
  add_creditcard(post, payment, options)
  add_customer(post, payment, options)
  add_customer_data(post, options)
  if options[:email]
    post[:metadata] = {}
    post[:metadata][:email] = options[:email]
  end
  post
end
headers(options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 196
def headers(options = {})
  secret_key = options[:secret_key] || @options[:secret_key]

  headers = {
    'Authorization' => 'Basic ' + Base64.encode64(secret_key.to_s + ':').strip,
    'User-Agent' => "SecurionPay/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}"
  }
  headers
end
json_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 249
def json_error(raw_response)
  msg = 'Invalid response received from the SecurionPay API.'
  msg += "  (The raw response returned by the API was #{raw_response.inspect})"
  {
    'error' => {
      'message' => msg
    }
  }
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 179
def parse(body)
  JSON.parse(body)
end
post_data(params) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 212
def post_data(params)
  return nil unless params

  params.map do |key, value|
    next if value.blank?
    if value.is_a?(Hash)
      h = {}
      value.each do |k, v|
        h["#{key}[#{k}]"] = v unless v.blank?
      end
      post_data(h)
    elsif value.is_a?(Array)
      value.map { |v| "#{key}[]=#{CGI.escape(v.to_s)}" }.join('&')
    else
      "#{key}=#{CGI.escape(value.to_s)}"
    end
  end.compact.join('&')
end
response_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 206
def response_error(raw_response)
  parse(raw_response)
rescue JSON::ParserError
  json_error(raw_response)
end
test?() click to toggle source
# File lib/active_merchant/billing/gateways/securion_pay.rb, line 259
def test?
  (@options[:secret_key]&.include?('_test_'))
end