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?

Methods included from MoneyCompatibility

#to_cents

Constructor Details

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

Returns a new instance of Helper.



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 558

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



651
652
653
654
655
656
657
658
659
660
# 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) ⇒ Object



585
586
587
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 585

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

#billing_address(params = {}) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# 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) ⇒ Object



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

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

#customer(params = {}) ⇒ Object



643
644
645
646
647
648
649
# 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_fieldsObject



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

def form_fields
  sign_fields
end

#generate_signatureObject



680
681
682
683
684
685
686
687
# 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) ⇒ Object

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



668
669
670
671
672
673
674
# 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 = {}) ⇒ Object



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# 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_fieldsObject



676
677
678
# File 'lib/offsite_payments/integrations/realex_offsite.rb', line 676

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