Handle and pass back error responses in ResultCode as well
[Business-OnlinePayment-vSecureProcessing.git] / lib / Business / OnlinePayment / vSecureProcessing.pm
index 947aaf6..85d5f1b 100644 (file)
@@ -11,7 +11,7 @@ use Business::OnlinePayment::HTTPS;
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
 $DEBUG = 0;
-$VERSION = '0.02';
+$VERSION = '0.06';
 
 # mapping out all possible endpoints
 # but this version will only be building out "charge", "void", & "credit"
@@ -97,7 +97,7 @@ sub set_defaults {
     
     $self->build_subs(qw/
             platform tid appid
-            action reference_number cvv_response avs_response response_code
+            action reference_number cvv2_response avs_code response_code
             risk_score txn_amount txn_date
     /);
     
@@ -238,7 +238,7 @@ sub submit {
     my @required_fields = qw/ Amount /;
     if ($action eq 'charge') {
         push @required_fields, $_
-          foreach (qw/ AccountNumber Cvv ExpirationMonth ExpirationYear /);
+          foreach (qw/ AccountNumber ExpirationMonth ExpirationYear /);
     }elsif ($action eq 'void') {
         push @required_fields, $_
           foreach (qw/ ReferenceNumber /);
@@ -346,8 +346,14 @@ sub parse_response {
         warn "Response:\n".Dumper($response)."\n" if $DEBUG > 2;
         $self->result_code($response->{Status}); # 0 /1
         $self->response_code($response->{ResponseCode}); # see documentation for translation
-        $self->avs_response($response->{AvsResponse}); # Y / N
-        $self->cvv_response($response->{CvvResponse}); # P / F
+        $self->avs_code($response->{AvsResponse}); # Y / N
+
+        #weird (missing?) gateway responses turn into a hashref screwing up Card Fortress
+        $self->cvv2_response( $response->{CvvResponse} =~ /^\w$/
+                                ? $response->{CvvResponse}
+                                : ''
+                            );
+
         $self->txn_date($response->{TransactionDate}); # MMDDhhmmss
         $self->txn_amount($response->{TransactionAmount} / 100); # 00000003500 / 100
         $self->reference_number($response->{ReferenceNumber});
@@ -355,17 +361,17 @@ sub parse_response {
         $self->is_success($self->result_code() eq '0' ? 1 : 0);
         if ($self->is_success()) {
             $self->authorization($response->{ReferenceNumber});
-        }
-        # fill in error_message if there is is an error
-        if ( !$self->is_success && exists($response->{AdditionalResponseData})) {
-            $self->error_message('Error '.$response->{ResponseCode}.': '.$response->{AdditionalResponseData});
-        }elsif ( !$self->is_success && exists($response->{Receipt}) ) {
-            $self->error_message('Error '.$response->{ResponseCode}.': '.(exists($response->{Receipt})) ? $response->{Receipt} : '');
+        } else { # fill in error_message if there is is an error
+            $self->error_message( 'Error '.$response->{ResponseCode}.': '.
+                                    (    $response->{AdditionalResponseData}
+                                      || $response->{Receipt}
+                                      || $response->{ResultCode}
+                                    )
+                                );
         }
         
     } else {
-        die 'Error communicating with vSecureProcessing server';
-        return;
+        die 'Error communicating with vSecureProcessing server (server sent response: '. $self->server_response. ')';
     }
     
 }