Class: CnpOnline::CnpAUBatch

Inherits:
Object
  • Object
show all
Includes:
XML::Mapping
Defined in:
lib/CnpBatchRequest.rb

Overview

IF YOU ARE A MERCHANT, DON’T LOOK HERE. IT’S SCARY!

Instance Method Summary collapse

Constructor Details

#initializeCnpAUBatch

Returns a new instance of CnpAUBatch.



642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/CnpBatchRequest.rb', line 642

def initialize
  #load configuration data
  @config_hash = Configuration.new.config

  @txn_counts = { :id=>nil,
    :merchantId=>nil,
    :numAccountUpdates=>0,
    :total=>0
  }
  @cnp_txn = CnpTransaction.new
  @path_to_batch = nil
  @txn_file = nil
  @MAX_TXNS_IN_BATCH = 100000
end

Instance Method Details

#account_update(options) ⇒ Object



727
728
729
730
731
732
# File 'lib/CnpBatchRequest.rb', line 727

def (options)
  transaction = @cnp_txn.(options)
  @txn_counts[:numAccountUpdates] += 1

  add_txn_to_batch(transaction, :authorization, options)
end

#close_batch(txn_location = @txn_file) ⇒ Object



712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/CnpBatchRequest.rb', line 712

def close_batch(txn_location = @txn_file)
  header = build_batch_header(@txn_counts)

  File.rename(@path_to_batch, @path_to_batch + '.closed-' + @txn_counts[:total].to_s)
  @path_to_batch = @path_to_batch + '.closed-' + @txn_counts[:total].to_s
  File.open(@path_to_batch, 'w') do |fo|
    fo.puts header
    File.foreach(txn_location) do |li|
      fo.puts li
    end
    fo.puts('</batchRequest>')
  end
  File.delete(txn_location)
end

#create_new_batch(path) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/CnpBatchRequest.rb', line 657

def create_new_batch(path)
  ts = Time::now.to_i.to_s
  begin
    ts += Time::now.nsec.to_s
  rescue NoMethodError # ruby 1.8.7 fix
    ts += Time::now.usec.to_s
  end

  if(File.file?(path)) then
    raise ArgumentError, "Entered a file not a path."
  end

  if(path[-1,1] != '/' && path[-1,1] != '\\') then
    path = path + File::SEPARATOR
  end
  if(!File.directory?(path)) then
    Dir.mkdir(path)
  end

  @path_to_batch = path + 'batch_' + ts
  @txn_file = @path_to_batch + '_txns'
  if(File.file?(@path_to_batch)) then
    create_new_batch(path)
    return
  end
  File.open(@path_to_batch, 'a+') do |file|
    file.write("")
  end
  File.open(@txn_file, 'a+') do |file|
    file.write("")
  end
end

#get_batch_nameObject



742
743
744
# File 'lib/CnpBatchRequest.rb', line 742

def get_batch_name
  return @path_to_batch
end

#get_counts_and_amountsObject



734
735
736
# File 'lib/CnpBatchRequest.rb', line 734

def get_counts_and_amounts
  return @txn_counts
end

#get_num_transactionsObject



738
739
740
# File 'lib/CnpBatchRequest.rb', line 738

def get_num_transactions
  return @txn_counts[:total]
end

#open_existing_batch(pathToBatchFile) ⇒ Object



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/CnpBatchRequest.rb', line 690

def open_existing_batch(pathToBatchFile)
  if(!File.file?(pathToBatchFile)) then
    raise ArgumentError, "No batch file exists at the passed location!"
  end

  if((pathToBatchFile =~ /batch_\d+.closed-\d+\z/) != nil) then
    raise ArgumentError, "The passed batch file is closed!"
  end

  @txn_file = pathToBatchFile + '_txns'
  @path_to_batch = pathToBatchFile
  temp_counts = File.open(@path_to_batch, "rb") { |f| Marshal.load(f) }
  if(temp_counts.keys.size > 4) then
    raise RuntimeException, "Tried to open an AU batch with a non-AU batch file"
  end

  @txn_counts[:id] = temp_counts[:id]
  @txn_counts[:merchantId] = temp_counts[:merchantId]
  @txn_counts[:numAccountUpdates] = temp_counts[:numAccountUpdates]
  @txn_counts[:total] = temp_counts[:total]
end