From a2c0deaa1ef38f442ac596cd61c6373277aa7258 Mon Sep 17 00:00:00 2001 From: Mitch Jackson Date: Thu, 11 Apr 2019 17:52:49 -0400 Subject: [PATCH] Refactor submit_api_request for additional actions --- lib/Business/OnlinePayment/Bambora.pm | 59 +++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/lib/Business/OnlinePayment/Bambora.pm b/lib/Business/OnlinePayment/Bambora.pm index 9d07c5a..a61cda0 100755 --- a/lib/Business/OnlinePayment/Bambora.pm +++ b/lib/Business/OnlinePayment/Bambora.pm @@ -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 -- 2.11.0