Added processor_id functionality
[Business-BatchPayment.git] / BatchPayment / Processor.pm
index 2093132..3cb88c6 100644 (file)
@@ -97,14 +97,18 @@ Specifying only 'input' will direct 'output' to /dev/null, and vice versa.
 =item submit BATCH
 
 Send a batch of requests to the gateway.  BATCH must be a 
-L<Business::BatchPayment::Batch>.
+L<Business::BatchPayment::Batch>.  No defined return value,
+but processors may optionally set the 'processor_id' field
+on the input batch, which should then be stored and passed
+to receive.
 
 =item receive
 
 Download/otherwise acquire the available confirmed transactions from the 
 gateway, parse them, and return a list of L<Business::BatchPayment::Batch>
 objects.  The items in these batches will have, at minimum, the 'approved' 
-field and either the 'tid' or 'amount' field set.
+field and either the 'tid' or 'amount' field set.  Accepts an optional
+list of processor_id strings, if required by your processor.
 
 =item format_request BATCH
 
@@ -170,6 +174,30 @@ sub default_transport {
   die blessed($self). " requires a transport or input/output files\n";
 }
 
+has 'on_format_error' => (
+  traits  => ['Code'],
+  is      => 'rw',
+  handles => { format_error => 'execute_method' },
+  default => sub { \&default_on_error },
+);
+
+has 'on_parse_error' => (
+  traits  => ['Code'],
+  is      => 'rw',
+  handles => { parse_error => 'execute_method' },
+  default => sub { \&default_on_error },
+);
+
+sub default_on_error { #re-throw it
+  my ($self, $item, $error) = @_;
+  $DB::single = 1 if defined($DB::single);
+  die $error;
+};
+
+# No error callbacks for other parts of this.  The per-item case 
+# is special in that it might make sense to continue with the 
+# other items.
+
 around BUILDARGS => sub {
   my ($orig, $class, %args) = @_;
   %args = %{ $class->$orig(%args) }; #process as usual
@@ -191,7 +219,6 @@ sub incoming { 0 };
 sub submit {
   my $self = shift;
   my $batch = shift;
-  my @items = @_;
   my $request = $self->format_request($batch);
   warn $request if $self->debug >= 2;
   $self->transport->upload($request);
@@ -200,8 +227,12 @@ sub submit {
 sub receive {
   my $self = shift;
   my @responses = $self->transport->download;
-  warn join("\n\n",@responses) if $self->debug >= 2 and scalar(@responses);
-  map { $self->parse_response($_) } @responses;
+  warn join("\n\n", @responses) if $self->debug >= 2 and scalar(@responses);
+  my @batches;
+  foreach my $response (@responses) {
+    push @batches, $self->parse_response($response);
+  }
+  @batches;
 }
 
 # next level down
@@ -210,12 +241,14 @@ sub format_request {
   my $self = shift;
   my $batch = shift;
   my $output = $self->format_header($batch);
+  $batch->num(0);
   foreach my $item ($batch->elements) {
     try {
       $output .= $self->format_item($item, $batch);
+      $batch->num( $batch->num + 1 );
     } catch {
-      $self->format_error($self, $item, $_);
-    }
+      $self->format_error($item, $_);
+    };
   }
   $output .= $self->format_trailer($batch);
   return $output;
@@ -226,12 +259,14 @@ sub parse_response {
   my $input = shift;
   my $batch = Business::BatchPayment->create(Batch =>
     incoming => $self->incoming,
-    batch_id => $self->parse_batch_id($input)
+    batch_id => $self->parse_batch_id($input),
+    num      => 0,
   );
   while ( $input =~ s/(.*)\n//m ) {
     my $row = $1;
-    try {
+    try { 
       $batch->push( $self->parse_item($row) );
+      $batch->num( $batch->num + 1 );
     } catch {
       $self->parse_error($row, $_);
     };
@@ -248,27 +283,4 @@ sub format_item { die "format_item unimplemented\n" }
 sub parse_batch_id { '' };
 sub parse_item { die "parse_item unimplemented\n" }
 
-sub default_on_error { #re-throw it
-  my ($self, $item, $error) = @_;
-  die $error;
-};
-
-has 'on_format_error' => (
-  traits  => ['Code'],
-  is      => 'rw',
-  handles => { format_error => 'execute_method' },
-  default => sub { \&default_on_error },
-);
-
-has 'on_parse_error' => (
-  traits  => ['Code'],
-  is      => 'rw',
-  handles => { parse_error => 'execute_method' },
-  default => sub { \&default_on_error },
-);
-
-# No error callbacks for other parts of this.  The per-item case 
-# is special in that it might make sense to continue with the 
-# other items.
-
 1;