class ActiveMerchant::Billing::ClearhausGateway

Constants

ACTION_CODE_MESSAGES

Public Class Methods

new(options={}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 39
def initialize(options={})
  requires!(options, :api_key)
  options[:private_key] = options[:private_key].strip if options[:private_key]
  super
end

Public Instance Methods

authorize(amount, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 52
def authorize(amount, payment, options={})
  post = {}
  add_invoice(post, amount, options)

  action = if payment.respond_to?(:number)
             add_payment(post, payment)
             '/authorizations'
           elsif payment.kind_of?(String)
             "/cards/#{payment}/authorizations"
           else
             raise ArgumentError.new("Unknown payment type #{payment.inspect}")
  end

  post[:recurring] = options[:recurring] if options[:recurring]
  post[:card][:pares] = options[:pares] if options[:pares]

  commit(action, post)
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 71
def capture(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)

  commit("/authorizations/#{authorization}/captures", post)
end
purchase(amount, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 45
def purchase(amount, payment, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(amount, payment, options) }
    r.process { capture(amount, r.authorization, options) }
  end
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 78
def refund(amount, authorization, options={})
  post = {}
  add_amount(post, amount, options)

  commit("/authorizations/#{authorization}/refunds", post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 107
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )[\w=]+), '\1[FILTERED]').
    gsub(%r((&?card(?:\[|%5B)csc(?:\]|%5D)=)[^&]*)i, '\1[FILTERED]').
    gsub(%r((&?card(?:\[|%5B)pan(?:\]|%5D)=)[^&]*)i, '\1[FILTERED]')
end
store(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 96
def store(credit_card, options={})
  post = {}
  add_payment(post, credit_card)

  commit('/cards', post)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 103
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 89
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(0, 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/clearhaus.rb, line 85
def void(authorization, options = {})
  commit("/authorizations/#{authorization}/voids", options)
end

Private Instance Methods

add_amount(post, amount, options) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 122
def add_amount(post, amount, options)
  post[:amount]   = localized_amount(amount, options[:currency] || default_currency)
  post[:currency] = (options[:currency] || default_currency)
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 116
def add_invoice(post, money, options)
  add_amount(post, money, options)
  post[:reference] = options[:order_id] if options[:order_id]
  post[:text_on_statement] = options[:text_on_statement] if options[:text_on_statement]
end
add_payment(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 127
def add_payment(post, payment)
  card = {}
  card[:pan]          = payment.number
  card[:expire_month] = '%02d'% payment.month
  card[:expire_year]  = payment.year

  if payment.verification_value?
    card[:csc] = payment.verification_value
  end

  post[:card] = card if card.any?
end
authorization_from(action, response) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 195
def authorization_from(action, response)
  id_of_auth_for_capture(action) || response['id']
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 151
def commit(action, parameters)
  url = (test? ? test_url : live_url) + action
  headers = headers(@options[:api_key])
  body = parameters.to_query

  if @options[:signing_key] && @options[:private_key]
    begin
      headers['Signature'] = generate_signature(body)
    rescue OpenSSL::PKey::RSAError => e
      return Response.new(false, e.message)
    end
  end

  response = begin
    parse(ssl_post(url, body, headers))
  rescue ResponseError => e
    raise unless(e.response.code.to_s =~ /400/)
    parse(e.response.body)
  end

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(action, response),
    test: test?,
    error_code: error_code_from(response)
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 213
def error_code_from(response)
  unless success_from(response)
    response['status']['code']
  end
end
generate_signature(body) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 206
def generate_signature(body)
  key = OpenSSL::PKey::RSA.new(@options[:private_key])
  hex = key.sign(OpenSSL::Digest.new('sha256'), body).unpack('H*').first

  "#{@options[:signing_key]} RS256-hex #{hex}"
end
headers(api_key) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 140
def headers(api_key)
  {
    'Authorization' => 'Basic ' + Base64.strict_encode64("#{api_key}:"),
    'User-Agent'    => "Clearhaus ActiveMerchantBindings/#{ActiveMerchant::VERSION}"
  }
end
id_of_auth_for_capture(action) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 199
def id_of_auth_for_capture(action)
  match = action.match(/authorizations\/(.+)\/captures/)
  return nil unless match

  match.captures.first
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 185
def message_from(response)
  default_message = ACTION_CODE_MESSAGES[response['status']['code']]

  if success_from(response)
    default_message
  else
    (response['status']['message'] || default_message)
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 147
def parse(body)
  JSON.parse(body) rescue body
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 181
def success_from(response)
  (response && (response['status']['code'] == 20000))
end