class ActiveMerchant::Billing::PayHubGateway

Constants

AVS_CODE_TRANSLATOR
CVV_CODE_TRANSLATOR
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/pay_hub.rb, line 69
def initialize(options={})
  requires!(options, :orgid, :username, :password, :tid)

  super
end

Public Instance Methods

authorize(amount, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 75
def authorize(amount, creditcard, options = {})
  post = setup_post('auth')
  add_creditcard(post, creditcard)
  add_amount(post, amount)
  add_address(post, (options[:address] || options[:billing_address]))
  add_customer_data(post, options)

  commit(post)
end
capture(amount, trans_id, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 107
def capture(amount, trans_id, options = {})
  post = setup_post('capture')

  add_reference(post, trans_id)
  add_amount(post, amount)

  commit(post)
end
purchase(amount, creditcard, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 85
def purchase(amount, creditcard, options={})
  post = setup_post('sale')
  add_creditcard(post, creditcard)
  add_amount(post, amount)
  add_address(post, (options[:address] || options[:billing_address]))
  add_customer_data(post, options)

  commit(post)
end
refund(amount, trans_id, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 95
def refund(amount, trans_id, options={})
  # Attempt a void in case the transaction is unsettled
  post = setup_post('void')
  add_reference(post, trans_id)
  response = commit(post)
  return response if response.success?

  post = setup_post('refund')
  add_reference(post, trans_id)
  commit(post)
end
verify(creditcard, options={}) click to toggle source

No void, as PayHub's void does not work on authorizations

# File lib/active_merchant/billing/gateways/pay_hub.rb, line 118
def verify(creditcard, options={})
  authorize(100, creditcard, options)
end

Private Instance Methods

add_address(post, address) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 146
def add_address(post, address)
  return unless address
  post[:address1] = address[:address1]
  post[:address2] = address[:address2]
  post[:zip] = address[:zip]
  post[:state] = address[:state]
  post[:city] = address[:city]
end
add_amount(post, amount) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 155
def add_amount(post, amount)
  post[:amount] =  amount(amount)
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 159
def add_creditcard(post, creditcard)
  post[:cc] = creditcard.number
  post[:month] = creditcard.month.to_s
  post[:year] = creditcard.year.to_s
  post[:cvv] = creditcard.verification_value
end
add_customer_data(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 139
def add_customer_data(post, options = {})
  post[:first_name] = options[:first_name]
  post[:last_name] = options[:last_name]
  post[:phone] = options[:phone]
  post[:email] = options[:email]
end
add_reference(post, trans_id) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 135
def add_reference(post, trans_id)
  post[:trans_id] = trans_id
end
commit(post) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 170
def commit(post)
  success = false

  begin
    raw_response = ssl_post(live_url, post.to_json, {'Content-Type' => 'application/json'})
    response = parse(raw_response)
    success = (response['RESPONSE_CODE'] == '00')
  rescue ResponseError => e
    raw_response = e.response.body
    response = response_error(raw_response)
  rescue JSON::ParserError
    response = json_error(raw_response)
  end

  Response.new(success,
    response_message(response),
    response,
    test: test?,
    avs_result: {code: response['AVS_RESULT_CODE']},
    cvv_result: response['VERIFICATION_RESULT_CODE'],
    error_code: (success ? nil : STANDARD_ERROR_CODE_MAPPING[response['RESPONSE_CODE']]),
    authorization: response['TRANSACTION_ID']
  )
end
json_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 201
def json_error(raw_response)
  {
    error_message: 'Invalid response received from the Payhub API.  Please contact wecare@payhub.com if you continue to receive this message.' \
      "  (The raw response returned by the API was #{raw_response.inspect})"
  }
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 166
def parse(body)
  JSON.parse(body)
end
response_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 195
def response_error(raw_response)
  parse(raw_response)
rescue JSON::ParserError
  json_error(raw_response)
end
response_message(response) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 208
def response_message(response)
  (response['RESPONSE_TEXT'] || response['RESPONSE_CODE'] || response[:error_message])
end
setup_post(action) click to toggle source
# File lib/active_merchant/billing/gateways/pay_hub.rb, line 124
def setup_post(action)
  post = {}
  post[:orgid] = @options[:orgid]
  post[:tid] = @options[:tid]
  post[:username] = @options[:username]
  post[:password] = @options[:password]
  post[:mode] = (test? ? 'demo' : 'live')
  post[:trans_type] = action
  post
end