Class: OffsitePayments::Integrations::RealexOffsite::Helper

Inherits:
Helper
  • Object
show all
Includes:
Common
Defined in:
lib/offsite_payments/integrations/realex_offsite.rb

Constant Summary

Constants included from Common

Common::CANADIAN_STATES, Common::COUNTRY_PHONE_NUMBERS, Common::CURRENCY_SPECIAL_MINOR_UNITS, Common::US_STATES

Instance Attribute Summary

Attributes inherited from Helper

#fields

Instance Method Summary collapse

Methods included from Common

#add_field, #adjust_phone_number_length, #copy_billing_address, #create_signature, #extract_address_match_indicator, #extract_avs_code, #extract_digits, #format_amount, #format_amount_as_float, #format_phone_number, #get_message, #get_pattern, #lookup_state_code, #validate

Methods inherited from Helper

#add_field, #add_fields, #add_raw_html_field, #form_method, inherited, mapping, #raw_html_fields, #test?

Constructor Details

#initialize(order, account, options = {}) ⇒ Helper

Returns a new instance of Helper.



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 553

def initialize(order, , options = {})
  @timestamp   = Time.now.strftime('%Y%m%d%H%M%S')
  @currency    = options[:currency]
  @merchant_id = 
  @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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OffsitePayments::Helper

Instance Method Details

#addresses_match(address_match = nil) ⇒ Object



638
639
640
641
642
643
644
645
646
647
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 638

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) ⇒ Object



580
581
582
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 580

def amount=(amount)
  add_field 'AMOUNT', format_amount(amount, @currency)
end

#billing_address(params = {}) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 584

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

  if @fields[mappings[:customer][:phone]]
    add_field(mappings[:customer][:phone], format_phone_number(@phone_number, country_code))
  end
end

#comment(comment = nil) ⇒ Object



649
650
651
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 649

def comment(comment = nil)
  add_field(mappings[:comment], comment)
end

#customer(params = {}) ⇒ Object



630
631
632
633
634
635
636
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 630

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_fieldsObject



576
577
578
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 576

def form_fields
  sign_fields
end

#generate_signatureObject



667
668
669
670
671
672
673
674
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 667

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) ⇒ Object

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



655
656
657
658
659
660
661
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 655

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 = {}) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 607

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_fieldsObject



663
664
665
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 663

def sign_fields
  @fields.merge!('SHA1HASH' => generate_signature)
end