Refactor submit_api_request for additional actions
authorMitch Jackson <mitch@freeside.biz>
Thu, 11 Apr 2019 21:52:49 +0000 (17:52 -0400)
committerMitch Jackson <mitch@freeside.biz>
Thu, 11 Apr 2019 21:52:49 +0000 (17:52 -0400)
lib/Business/OnlinePayment/Bambora.pm

index 9d07c5a..a61cda0 100755 (executable)
@@ -122,6 +122,30 @@ sub submit_normal_authorization {
   }
 
   $self->path('/v1/payments');
+  my $response = $self->submit_api_request( $post_body );
+
+  # Error messages already populated upon failure
+  return unless $self->is_success;
+
+  # Populate transaction result values
+  $self->message_id( $response->{message_id} );
+  $self->authorization( $response->{auth_code} );
+  $self->order_number( $response->{id} );
+  $self->txn_date( $response->{created} );
+  $self->avs_code( $response->{card}{avs_result} );
+  $self->is_success( 1 );
+}
+
+=head2 submit_api_request json_string
+
+Make the appropriate API request with the given JSON string
+
+=cut
+
+sub submit_api_request {
+  my $self = shift;
+  my $post_body = shift
+    or die 'submit_api_request() requires a json_string parameter';
 
   my ( $response_body, $response_code, %response_headers ) = $self->https_post(
     {
@@ -146,16 +170,20 @@ sub submit_normal_authorization {
       });
     }
 
-    # API should always return a JSON response,
-    die $response_body || 'connection error'
-      if $@ || !$response;
+    # API should always return a JSON response, likely network problem
+    if ( $@ || !$response ) {
+      $self->error_message( $response_body || 'connection error' );
+      $self->is_success( 0 );
+      return;
+    }
   }
   $self->response_decoded( $response );
 
+  # Response returned an error
   if ( $response->{code} && $response->{code} != 1 ) {
-
     $self->is_success( 0 );
     $self->result_code( $response->{code} );
+
     return $self->error_message(
       sprintf '%s %s',
         $response->{code},
@@ -163,28 +191,11 @@ sub submit_normal_authorization {
     );
   }
 
-  # success
-  # Populate transaction result values
-  $self->message_id( $response->{message_id} );
-  $self->authorization( $response->{auth_code} );
-  $self->order_number( $response->{id} );
-  $self->txn_date( $response->{created} );
-  $self->avs_code( $response->{card}{avs_result} );
+  # Success
+  # Return the decoded json of the response back to handler
   $self->is_success( 1 );
-}
-
-=head2 submit_api_request json_string
-
-Make the appropriate API request with the given JSON string
-
-=cut
-
-sub submit_api_request {
-  my $self = shift;
-  my $json_string = shift
-    or die 'submit_api_request() requires a json_string parameter';
+  return $response;
 
-  
 }
 
 =head2 submit_action_unsupported