Stable working code.
authorAlex Brelsfoard <alex@Alexs-MacBook-Pro.local>
Fri, 6 Feb 2015 23:09:53 +0000 (18:09 -0500)
committerAlex Brelsfoard <alex@Alexs-MacBook-Pro.local>
Fri, 6 Feb 2015 23:09:53 +0000 (18:09 -0500)
vSecureProcessing.pm

index 518368a..3c73067 100644 (file)
@@ -1,12 +1,10 @@
 package Business::OnlinePayment::vSecureProcessing;
 
 use strict;
-use URI::Escape;
 use Carp;
 use Template;
 use XML::Simple;
 use Data::Dumper;
-use MIME::Entity;
 
 use Business::OnlinePayment;
 use Business::OnlinePayment::HTTPS;
@@ -14,7 +12,7 @@ use Business::OnlinePayment::HTTPS;
 use vars qw($VERSION $DEBUG @ISA $me);
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
-$DEBUG = 3;
+$DEBUG = 0;
 $VERSION = '0.01';
 $me = 'Business::OnlinePayment::vSecureProcessing';
 
@@ -61,7 +59,7 @@ my %payment_actions = (
 );
 
 my %action_mapping = (
-    'normal authorization'    => 'charge',
+    'normal authorization'  => 'charge',
     'credit'                => 'refund',
     'authorization only'    => 'authorize',
     'post authorization'    => 'capture',
@@ -69,63 +67,6 @@ my %action_mapping = (
     # void => void
 );
 
-#BEGIN{
-#eval 'use bytes; sub blength ($) { length $_[0] }';
-#$@ and eval '    sub blength ($) { length $_[0] }' ;
-#}
-#sub Net::SSLeay::do_httpx3 {
-#  $DB::single = 1;
-#
-#    my ($method, $usessl, $site, $port, $path, $headers,
-#        $content, $mime_type, $crt_path, $key_path) = @_;
-#    my ($response, $page, $h,$v);
-#    my $CRLF = "\x0d\x0a";  # because \r\n is not fully portable
-#    if ($content) {
-#        $mime_type = "";#application/x-www-form-urlencoded" unless $mime_type;
-#        my $len = length($content);
-#        #$content = "$mime_type${CRLF}Content-Length: $len$CRLF$CRLF$content";
-#        $content = "Cache-Control: no-cache$CRLF"
-#            . "Content-Type: multipart/form-data; boundary=--FormBoundaryE19zNvXGzXaLvS5C$CRLF"
-#            . "Accept: */*$CRLF"
-#            . "Content-Length: $len$CRLF$CRLF$content";
-#    } else {
-#        $content = "$CRLF$CRLF";
-#    }
-#    my $req = "$method $path HTTP/1.1$CRLF";
-#    unless (defined $headers && $headers =~ /^Host:/m) {
-#        $req .= "Host: $site";
-#        unless (($port == 80 && !$usessl) || ($port == 443 && $usessl)) {
-#            $req .= ":$port";
-#        }
-#        $req .= $CRLF;
-#    }
-#    $req .= (defined $headers ? $headers : '') . "$content";
-#
-#    warn "do_httpx3($method,$usessl,$site:$port)" if $DEBUG;
-#    my ($http, $errs, $server_cert)
-#        = Net::SSLeay::httpx_cat($usessl, $site, $port, $req, $crt_path, $key_path);
-#    return (undef, "HTTP/1.0 900 NET OR SSL ERROR$CRLF$CRLF$errs") if $errs;
-#
-#    $http = '' if !defined $http;
-#    ($headers, $page) = split /\s?\n\s?\n/, $http, 2;
-#    warn "headers >$headers< page >>$page<< http >>>$http<<<" if $DEBUG>1;
-#    ($response, $headers) = split /\s?\n/, $headers, 2;
-#    return ($page, $response, $headers, $server_cert);
-#};
-
-#sub Net::SSLeay::make_form {
-#    my (@fields) = @_;
-#    my $form;
-#    while (@fields) {
-#        my ($name, $data) = (shift(@fields), shift(@fields));
-##       $data =~ s/([^\w\-.\@\$ ])/sprintf("%%%2.2x",ord($1))/gse;
-##       $data =~ tr[ ][+];
-#        $form .= "$name=$data&";
-#    }
-#    chop $form;
-#    return $form;
-#}
-
 sub set_defaults {
     my $self = shift;
     my %options = @_;
@@ -146,8 +87,8 @@ sub set_defaults {
     #     result_code, error_message,
     
     $self->build_subs(qw/
-            env platform userid gid tid appid action
-            cvv_response avs_response risk_score
+            env platform userid gid tid appid action cvv_response
+            avs_response risk_score txn_amount txn_date
     /);
     
     $DEBUG = exists($options{debug}) ? $options{debug} : $DEBUG;
@@ -166,7 +107,6 @@ sub set_defaults {
     
     $self->env((defined($options{'env'})) ? $options{'env'} : 'live'); # 'live'/'test'
     
-#     $self->port(($options{'env'} eq 'test') ? 80 : 443);
     $self->port(443);
 }
 
@@ -240,31 +180,31 @@ sub submit {
         
         auth => {
             platform    => $self->platform,
-            userid        => $self->userid,
-            gid            => $self->gid,
-            tid            => $self->tid
+            userid      => $self->userid,
+            gid         => $self->gid,
+            tid         => $self->tid
         },
         
         payment => {
-            amount            => $content{'amount'},
-            track1            => ($content{'track1'}) ? $content{'track1'} : '',
-            track2            => ($content{'track2'}) ? $content{'track2'} : '',
+            amount          => $content{'amount'},
+            track1          => ($content{'track1'}) ? $content{'track1'} : '',
+            track2          => ($content{'track2'}) ? $content{'track2'} : '',
             type            => ($content{'description'}) ? $content{'description'} : '',
-            cf1                => ($content{'UDField1'}) ? $content{'UDField1'} : '',
-            cf2                => ($content{'UDField2'}) ? $content{'UDField2'} : '',
-            cf3                => '',
-            account_number    => ($content{'card_number'}) ? $content{'card_number'} : '',
-            exp_month        => $content{'exp_month'},
+            cf1             => ($content{'UDField1'}) ? $content{'UDField1'} : '',
+            cf2             => ($content{'UDField2'}) ? $content{'UDField2'} : '',
+            cf3             => '',
+            account_number  => ($content{'card_number'}) ? $content{'card_number'} : '',
+            exp_month       => $content{'exp_month'},
             exp_year        => $content{'exp_year'},
-            cvv                => ($content{'cvv'}) ? $content{'cvv'} : ($content{'cvv2'}) ? $content{'cvv2'} : '',
-            first_name        => ($content{'first_name'}) ? $content{'first_name'} : '',
-            last_name        => ($content{'last_name'}) ? $content{'last_name'} : '',
-            postal_code        => ($content{'zip'}) ? $content{'zip'} : '',
-            street_address    => ($content{'street_number'}) ? $content{'street_number'} : '',
-            industry_type    => ($content{'IndustryInfo'} && lc($content{'IndustryInfo'}) eq 'ecommerce') ? 'ecom_3' : '',
-            invoice_num        => ($content{'invoice_number'}) ? $content{'invoice_number'} : '',
-            appid            => $self->appid(),
-            recurring        => ($content{'recurring_billing'} && $content{'recurring_billing'} eq 'YES' ) ? 1 : 0,
+            cvv             => ($content{'cvv'}) ? $content{'cvv'} : ($content{'cvv2'}) ? $content{'cvv2'} : '',
+            first_name      => ($content{'first_name'}) ? $content{'first_name'} : '',
+            last_name       => ($content{'last_name'}) ? $content{'last_name'} : '',
+            postal_code     => ($content{'zip'}) ? $content{'zip'} : '',
+            street_address  => ($content{'street_number'}) ? $content{'street_number'} : '',
+            industry_type   => ($content{'IndustryInfo'} && lc($content{'IndustryInfo'}) eq 'ecommerce') ? 'ecom_3' : '',
+            invoice_num     => ($content{'invoice_number'}) ? $content{'invoice_number'} : '',
+            appid           => $self->appid(),
+            recurring       => ($content{'recurring_billing'} && $content{'recurring_billing'} eq 'YES' ) ? 1 : 0,
             response_code   => ($content{'response_code'}) ? $content{'response_code'} : '',
             reference_number=> ($content{'ref_num'}) ? $content{'ref_num'} : '',
             token           => ($content{'token'}) ? $content{'token'} : '',
@@ -275,27 +215,27 @@ sub submit {
         
         # we won't be using level2 nor level3.  So I'm leaving them blank for now.
         level2 => {
-            card_type                => '',
-            purchase_code            => '',
-            country_code            => '',
-            ship_tp_postal_code        => '',
-            ship_from_postal_code    => '',
-            sales_tax                => '',
-            product_description1    => '',
-            product_description2    => '',
-            product_description3    => '',
-            product_description4    => ''
+            card_type             => '',
+            purchase_code         => '',
+            country_code          => '',
+            ship_tp_postal_code   => '',
+            ship_from_postal_code => '',
+            sales_tax             => '',
+            product_description1  => '',
+            product_description2  => '',
+            product_description3  => '',
+            product_description4  => ''
         },
         
         level3 => {
             purchase_order_num    => '',
             order_date            => '',
-            duty_amount            => '',
+            duty_amount           => '',
             alt_tax_amount        => '',
-            discount_amount        => '',
+            discount_amount       => '',
             freight_amount        => '',
             tax_exempt            => '',
-            line_item_count        => '',
+            line_item_count       => '',
             purchase_items        => $self->_parse_line_items()
         }
     };
@@ -306,9 +246,9 @@ sub submit {
     if ($action eq 'charge') {
         push(@required_fields, $_) foreach (qw/ account_number cvv exp_month exp_year /);
     }elsif ($action eq 'void') {
-        push(@required_fields, $_) foreach (qw/ response_code reference_number receipt token transaction_date exp_month exp_year /);
+        push(@required_fields, $_) foreach (qw/ reference_number transaction_date /);
     }elsif ($action eq 'refund') {
-        push(@required_fields, $_) foreach (qw/ merchant_data token account_number exp_month exp_year /);
+        push(@required_fields, $_) foreach (qw/ amount account_number exp_month exp_year /);
     }
     
     # check the requirements are met.
@@ -332,7 +272,7 @@ sub submit {
     
     my $boundary = sprintf('FormBoundary%06d', int(rand(1000000)));
     # opts for B:OP:HTTPS::https_post
-    my $opts = { headers => {}, debug => $DEBUG };
+    my $opts = { headers => {}};
     $opts->{'Content-Type'} =
     $opts->{headers}->{'Content-Type'} =
         "multipart/form-data, boundary=$boundary";
@@ -365,7 +305,12 @@ sub submit {
               "(Raw HTTPS content: ".$page.")"
             );
         } else {
-            $self->error_message('No error information was returned by vSecureProcessing (enable debugging for raw HTTPS response)');
+            my $response_code = $self->response_code() || '';
+            if ($response_code) {
+                $self->error_message(qq|Error code ${response_code} was returned by vSecureProcessing. (enable debugging for raw HTTPS response)|);
+            }else {
+                $self->error_message('No error information was returned by vSecureProcessing (enable debugging for raw HTTPS response)');
+            }
         }
     }
     
@@ -378,9 +323,13 @@ sub parse_response {
 
     if ($self->server_response =~ /^200/) {
         my $response = XMLin($page);
+        warn "RESPONSE: \n".Dumper($response)."\n";
         $self->result_code($response->{Status});
         $self->avs_response($response->{AvsResponse});
         $self->cvv_response($response->{CvvResponse});
+        $self->txn_date($response->{TransactionDate});
+        $self->txn_amount($response->{TransactionAmount} / 100);
+        $self->cvv_response($response->{CvvResponse});
         $self->is_success($self->result_code() eq '0' ? 1 : 0);
         if ($self->is_success()) {
             $self->authorization($response->{AuthIdentificationResponse});