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
capture(amount, authorization, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 72 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 79 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 108 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 97 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 104 def supports_scrubbing? true end
verify(credit_card, options = {})
click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 90 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 86 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 123 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 117 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 128 def add_payment(post, payment) card = {} card[:pan] = payment.number card[:expire_month] = '%02d' % payment.month card[:expire_year] = payment.year card[:csc] = payment.verification_value if payment.verification_value? post[:card] = card if card.any? end
commit(action, parameters)
click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 150 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 214 def error_code_from(response) response['status']['code'] unless success_from(response) end
generate_signature(body)
click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 207 def generate_signature(body) key = OpenSSL::PKey::RSA.new(@options[:private_key]) hex = key.sign(OpenSSL::Digest.new('sha256'), body).unpack1('H*') "#{@options[:signing_key]} RS256-hex #{hex}" end
headers(api_key)
click to toggle source
# File lib/active_merchant/billing/gateways/clearhaus.rb, line 139 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 200 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 186 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 146 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 182 def success_from(response) (response && (response['status']['code'] == 20000)) end