class OffsitePayments::Integrations::RealexOffsite::Helper

Public Class Methods

new(order, account, options = {}) click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/realex_offsite.rb, line 558
def initialize(order, account, options = {})
  @timestamp   = Time.now.strftime('%Y%m%d%H%M%S')
  @currency    = options[:currency]
  @merchant_id = account
  @sub_account = options[:credential2]
  @secret      = options[:credential3]
  super
  # Credentials
  add_field 'MERCHANT_ID', @merchant_id
  add_field 'ACCOUNT', @sub_account
  # Defaults
  add_field 'AUTO_SETTLE_FLAG', '1'
  add_field 'RETURN_TSS', '1'
  add_field 'TIMESTAMP', @timestamp
  add_field 'HPP_VERSION', '2'
  # Realex does not send back CURRENCY param in response
  # however it does echo any other param so we send it twice.
  add_field 'X-CURRENCY', @currency
  add_field 'X-TEST', @test.to_s
  add_field 'ORDER_ID', "#{order}#{@timestamp.to_i}"
  add_field 'COMMENT1', application_id
end

Public Instance Methods

addresses_match(address_match = nil) click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 651
def addresses_match(address_match = nil)
  return if address_match.nil?

  add_field(
    mappings[:addresses_match],
    extract_address_match_indicator(address_match)
  )

  copy_billing_address if address_match
end
amount=(amount) click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 585
def amount=(amount)
  add_field 'AMOUNT', format_amount(amount, @currency)
end
billing_address(params={}) click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/realex_offsite.rb, line 589
def billing_address(params={})
  country = params[:country]
  country_code = lookup_country_code(country, :alpha2)
  avs_code = extract_avs_code(params)
  params[:state] = lookup_state_code(country_code, params[:state])

  super

  add_field(mappings[:billing_address][:country], lookup_country_code(country))
  add_field(mappings[:billing_address][:code], avs_code)

  unless ['US', 'CA'].include?(country_code)
    # HPP_BILLING_STATE is required only for US and CA, otherwise is deleted
    @fields.delete_if do |k, _|
      k == 'HPP_BILLING_STATE'
    end
  end

  unless ['US', 'CA', 'GB'].include?(country_code)
    # BILLING_CODE is required only for US, CA and GB, otherwise is nil,
    # therefore the field is deleted for the other countries
    @fields.delete_if do |k, _|
      k == 'BILLING_CODE'
    end
  end

  if @fields[mappings[:customer][:phone]]
    add_field(mappings[:customer][:phone], format_phone_number(@phone_number, country_code))
  end
end
comment(comment = nil) click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 662
def comment(comment = nil)
  add_field(mappings[:comment], comment)
end
customer(params={}) click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/realex_offsite.rb, line 643
def customer(params={})
  country = @fields[mappings[:billing_address][:country]]
  @phone_number = params[:phone]
  params[:phone] = format_phone_number(@phone_number, lookup_country_code(country, :alpha2))

  super
end
form_fields() click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 581
def form_fields
  sign_fields
end
generate_signature() click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 680
def generate_signature
  fields_to_sign = []
  ['TIMESTAMP', 'MERCHANT_ID', 'ORDER_ID', 'AMOUNT', 'CURRENCY'].each do |field|
    fields_to_sign << @fields[field]
  end

  create_signature(fields_to_sign, @secret)
end
require_shipping(require_shipping = nil) click to toggle source

HPP does not want shipping address and HPP_ADDRESS_MATCH_INDICATOR to be sent if the product does not require shipping

# File lib/offsite_payments/integrations/realex_offsite.rb, line 668
def require_shipping(require_shipping = nil)
  return unless require_shipping == false

  @fields.delete_if do |k, _|
    k.start_with?('HPP_SHIPPING_') || k == 'HPP_ADDRESS_MATCH_INDICATOR'
  end
end
shipping_address(params={}) click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/realex_offsite.rb, line 620
def shipping_address(params={})
  country = params[:country]
  country_code = lookup_country_code(country, :alpha2)
  params[:state] = lookup_state_code(country_code, params[:state])

  super

  add_field(mappings[:shipping_address][:country], lookup_country_code(country))
  # the mapping for 'SHIPPING_CODE' field, which has the same value as the 'HPP_SHIPPING_POSTALCODE'
  add_field(mappings[:shipping_address][:code], params[:zip])

  unless ['US', 'CA'].include?(country_code)
    # HPP_SHIPPING_STATE is required only for US and CA, otherwise is deleted
    @fields.delete_if do |k, _|
      k == 'HPP_SHIPPING_STATE'
    end
  end

  if @fields[mappings[:customer][:phone]]&.[](0..1) == '0|'
    add_field(mappings[:customer][:phone], format_phone_number(@phone_number, country_code))
  end
end
sign_fields() click to toggle source
# File lib/offsite_payments/integrations/realex_offsite.rb, line 676
def sign_fields
  @fields.merge!('SHA1HASH' => generate_signature)
end