ssafronova (ssafronova) wrote in changelog,
ssafronova
ssafronova
changelog

[ljcom] r10250: LJSUP-8158: Show all errors (for Credit ...

Committer: ssafronova
LJSUP-8158: Show all errors (for Credit card payment process refactoring)
U   trunk/cgi-bin/LJ/Widget/CreditCard.pm
U   trunk/ssldocs/manage/account/cc.bml
U   trunk/ssldocs/pay/cc.bml
Modified: trunk/cgi-bin/LJ/Widget/CreditCard.pm
===================================================================
--- trunk/cgi-bin/LJ/Widget/CreditCard.pm	2011-03-16 08:01:52 UTC (rev 10249)
+++ trunk/cgi-bin/LJ/Widget/CreditCard.pm	2011-03-16 10:08:23 UTC (rev 10250)
@@ -306,15 +306,13 @@
 
 # Will also set state to value of state_other if non-US
 sub validate {
-    my ($POST, $err, $field) = @_;
+    my ($POST) = @_;
 
-    my $a_field;
-    $field ||= \$a_field; # id of HTML element
+    my @errs = ();
 
     unless ($POST->{type} =~ /^Visa|MasterCard|AmEx|Discover$/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardtype', {'cardtype' => $POST->{type}});
-        $$field = 'type';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardtype', {'cardtype' => $POST->{type}}),
+                      field => 'type' };
     }
 
     my $cc_len = 16;
@@ -326,39 +324,33 @@
     }
 
     unless ($POST->{cc_num} =~ /^\d{$cc_len}$/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardnumber', {'cardtype' => $POST->{type}, 'num' => $cc_len});
-        $$field = 'cc_num';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardnumber', {'cardtype' => $POST->{type}, 'num' => $cc_len}),
+                      field => 'cc_num' };
     }
 
     if ($POST->{type} ne 'Visa' and $POST->{cc_num} =~ /^4/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'Visa' });
-        $$field = 'type';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'Visa' }),
+                      field => 'type' };
     }
 
     if ($POST->{type} ne 'MasterCard' and $POST->{cc_num} =~ /^5/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'MasterCard' });
-        $$field = 'type';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'MasterCard' }),
+                      field => 'type' };
     }
 
     if ($POST->{type} ne 'Discover' and $POST->{cc_num} =~ /^6/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'Discover' });
-        $$field = 'type';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'Discover' }),
+                      field => 'type' };
     }
 
     if ($POST->{type} ne 'AmEx' and $POST->{cc_num} =~ /^3/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'American Express' });
-        $$field = 'type';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardtype2', { 'cardtype' => $POST->{type}, 'actual' => 'American Express' }),
+                      field => 'type' };
     }
 
     unless ($POST->{cc_cvv2} =~ /^\d{$cv_len}$/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cvv2', {'cardtype' => $POST->{type}, 'num' => $cv_len});
-        $$field = 'cc_cvv2';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cvv2', {'cardtype' => $POST->{type}, 'num' => $cv_len}),
+                      field => 'cc_cvv2' };
     }
 
     my ($sec,$min,$hour,$mday,$mon,$year,
@@ -367,49 +359,42 @@
     $year += 1900;
 
     if ($POST->{exp_y} < $year || ($POST->{exp_y} == $year && $POST->{exp_m} < $mon)) {
-        $$err = LJ::Lang::ml('pay.cc.validate.cardexpires');
-        $$field = 'exp_y';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.cardexpires'),
+                      field => 'exp_y' };
     }
 
     unless ($POST->{name_full} =~ /\S+\s+\S+/) {
-        $$err = LJ::Lang::ml('pay.cc.validate.name');
-        $$field = 'name_full';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.name'),
+                      field => 'name_full' };
     }
 
     unless (length($POST->{addr})) {
-        $$err = LJ::Lang::ml('pay.cc.validate.address');
-        $$field = 'addr';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.address'),
+                      field => 'addr' };
     }
 
     if ($POST->{country} eq 'US') {
         unless ($POST->{postal_code} =~ /^(\d{5})(-\d{4})?$/) {
-            $$err = LJ::Lang::ml('pay.cc.validate.postalcode.us.invalid');
-            $$field = 'postal_code';
-            return undef;
+            push @errs, { err => LJ::Lang::ml('pay.cc.validate.postalcode.us.invalid'),
+                          field => 'postal_code' };
         }
 
         if (int($1) == 0) {
-            $$err = LJ::Lang::ml('pay.cc.validate.postalcode.us.zero');
-            $$field = 'postal_code';
-            return undef;
+            push @errs, { err => LJ::Lang::ml('pay.cc.validate.postalcode.us.zero'),
+                          field => 'postal_code' };
         }
 
         my (%states);
         LJ::load_codes({ "state" => \%states });
         if (! $states{$POST->{state}}) {
-            $$err = LJ::Lang::ml('pay.cc.validate.state.us');
-            $$field = 'state';
-            return undef
+            push @errs, { err => LJ::Lang::ml('pay.cc.validate.state.us'),
+                          field => 'state' };
         }
     } else {
         if ( $LJ::POSTALCODE_COUNTRIES{$POST->{country}} &&
             !(length($POST->{postal_code}) > 0 && length($POST->{postal_code}) < 15) ) {
-            $$err = LJ::Lang::ml('pay.cc.validate.postalcode.other');
-            $$field = 'postal_code';
-            return undef;
+            push @errs, { err => LJ::Lang::ml('pay.cc.validate.postalcode.other'),
+                          field => 'postal_code' };
         }
 
         # Since we are not-US, they should be using the state
@@ -421,26 +406,23 @@
 
     my $countries = LJ::Pay::RecBill->valid_countries;
     unless ($countries->{$POST->{country}}) {
-        $$err = LJ::Lang::ml('pay.cc.validate.country');
-        $$field = 'country';
-        return undef;
+        push @errs, { err => LJ::Lang::ml('pay.cc.validate.country'),
+                      field => 'country' };
     }
 
     unless ($POST->{email}) {
-        $$err =  LJ::Lang::ml('pay.cc.validate.email.need');
-        $$field = 'email';
-        return undef;
+        push @errs, { err =>  LJ::Lang::ml('pay.cc.validate.email.need'),
+                      field => 'email' };
     }
 
     my @email_errors;
     LJ::check_email($POST->{email}, \@email_errors);
     if ($POST->{email} && @email_errors) {
-        $$err = join(", ", @email_errors);
-        $$field = 'email';
-        return undef;
+        push @errs, { err => join(", ", @email_errors),
+                      field => 'email' };
     }
 
-    return 1;
+    return \@errs;
 }
 
 1;

Modified: trunk/ssldocs/manage/account/cc.bml
===================================================================
--- trunk/ssldocs/manage/account/cc.bml	2011-03-16 08:01:52 UTC (rev 10249)
+++ trunk/ssldocs/manage/account/cc.bml	2011-03-16 10:08:23 UTC (rev 10250)
@@ -77,7 +77,7 @@
 
     my $now = time();
 
-    my ($ret, $err, $err_field, $print_cc_form, $old_err_text);
+    my ($ret, $err, $err_field, $print_cc_form, $old_err_text, $errors);
 
     # Sysban checks
     my $uniq = LJ::Request->notes('uniq');
@@ -140,6 +140,7 @@
             }
             else {
                 ($old_err_text, $$err, $err_field) = LJ::Pay::Payment->authcap_err_str($rv, BML::get_language());
+                $errors = [{ err => $$err, field => $err_field }];
                 $print_cc_form = 1;
                 return 0;
             }
@@ -196,10 +197,10 @@
         ## 
         FOO: while (1) {  # so we can 'last' out of it
 
-            my $err;
-            unless (LJ::Widget::CreditCard::validate(\%POST, \$err, \$err_field)) {
+            $errors = LJ::Widget::CreditCard::validate(\%POST);
+            if (scalar @$errors) {
                 $print_cc_form = 1;
-                $die->($err);
+                $die->($errors->[0]->{err});
             }
 
             # attempt to gather and store user's country/state
@@ -498,6 +499,7 @@
                 }
                 else {
                     ($old_err_text, $err, $err_field) = LJ::Pay::Payment->authcap_err_str($rv, BML::get_language());
+                    $errors = [{ err => $err, field => $err_field }];
                     $print_cc_form = 1;
                     $die->( $err, 1 );
                 }
@@ -531,10 +533,14 @@
     }
 
     if ($print_cc_form) {
-        $ret .= "<span class='b-bubble b-bubble-warning' id='move_to_$err_field'><i class='i-bubble-arrow-border'></i><i class='i-bubble-arrow'></i>$err</span>";
-        $ret .= "<script type=\"text/javascript\">jQuery(function(){";
-        $ret .= "jQuery(\"#move_to_$err_field\").appendTo(jQuery(\"#ccform [name=$err_field]\").closest(\"li\")).show();";
-        $ret .= "});</script>";
+        foreach my $error (@$errors) {
+            my $err = $error->{err};
+            my $err_field = $error->{field};
+            $ret .= "<span class='b-bubble b-bubble-warning' id='move_to_$err_field'><i class='i-bubble-arrow-border'></i><i class='i-bubble-arrow'></i>$err</span>";
+            $ret .= "<script type=\"text/javascript\">jQuery(function(){";
+            $ret .= "jQuery(\"#move_to_$err_field\").appendTo(jQuery(\"#ccform [name=$err_field]\").closest(\"li\")).show();";
+            $ret .= "});</script>";
+        }
     }
 
     $ret .= '<form method="post" id="ccform" name="ccform">';

Modified: trunk/ssldocs/pay/cc.bml
===================================================================
--- trunk/ssldocs/pay/cc.bml	2011-03-16 08:01:52 UTC (rev 10249)
+++ trunk/ssldocs/pay/cc.bml	2011-03-16 10:08:23 UTC (rev 10250)
@@ -118,14 +118,19 @@
 
 
     my $cc_form_return = sub {
-        my ($err, $err_field) = @_;
+        my ($errs) = @_;
 
-        $ccform .= "<span class='b-bubble b-bubble-warning' id='move_to_$err_field'><i class='i-bubble-arrow-border'></i><i class='i-bubble-arrow'></i>$err</span>";
-        $ccform .= "<script type=\"text/javascript\">jQuery(function(){";
-        $ccform .= "jQuery(\"#move_to_$err_field\").appendTo(jQuery(\"#ccform [name=$err_field]\").closest(\"li\")).show();";
-        $ccform .= "});</script>";
+        foreach my $error (@$errs) {
+            my $err = $error->{err};
+            my $err_field = $error->{field};
 
-        $ccform .= LJ::Widget::CreditCard::render( LJ::Pay::RecBill->valid_countries, $err, \%POST, undef,
+            $ccform .= "<span class='b-bubble b-bubble-warning' id='move_to_$err_field'><i class='i-bubble-arrow-border'></i><i class='i-bubble-arrow'></i>$err</span>";
+            $ccform .= "<script type=\"text/javascript\">jQuery(function(){";
+            $ccform .= "jQuery(\"#move_to_$err_field\").appendTo(jQuery(\"#ccform [name=$err_field]\").closest(\"li\")).show();";
+            $ccform .= "});</script>";
+        }
+
+        $ccform .= LJ::Widget::CreditCard::render( LJ::Pay::RecBill->valid_countries, '', \%POST, undef,
                           {
                               'cvv2' => 1,
                               'has_coppa' => $has_coppa,
@@ -143,7 +148,7 @@
 
     ### POST: authorize/capture
 
-    my ($err, $err_text, $err_field);
+    my ($err, $errors);
     if (LJ::did_post()) {
         my $dbh = LJ::get_db_writer();
         my $no_charge = $ML{'.error.nocharge'};
@@ -529,9 +534,9 @@
         }
 
         # process credit card form fields
-        my ($formerr, $err_field);
-        unless (LJ::Widget::CreditCard::validate(\%POST, \$formerr, \$err_field) || $LJ::DEBUG{'no_cc_needed'}) {
-            return $cc_form_return->($formerr, $err_field);
+        $errors = LJ::Widget::CreditCard::validate(\%POST);
+        if (scalar @$errors && !$LJ::DEBUG{'no_cc_needed'}) {
+            return $cc_form_return->($errors);
         }
  
         # send fraud watch emails if necessary
@@ -605,7 +610,7 @@
             $rv = $cartobj->authorize ( %capture );
             unless (LJ::Pay::Payment->authcap_is_success($rv) || $LJ::DEBUG{'no_cc_needed'}) {
                 my ($old_text, $err_text, $err_field) = LJ::Pay::Payment->authcap_err_str($rv, BML::get_language());
-                return $cc_form_return->($err_text, $err_field);
+                return $cc_form_return->( [{ err => $err_text, field => $err_field }] );
             }
         } elsif ($cartobj->get_amount() > 0) {
             $rv = eval { $cartobj->capture ( %capture ) };
@@ -616,8 +621,10 @@
                 $complete_order->();
             } else {
                 my %done; my $emailtxt;
+                my ($err_text, $err_field);
                 ($err, $err_text, $err_field) = LJ::Pay::Payment->authcap_err_str($rv, BML::get_language());
                 # $err is old message and will be 'en' in any case
+                $errors = [{ err => $err_text, field => $err_field }];
 
                 my @rcpts = map { [ $_->{'rcptid'}, $_->{'rcptemail'} ] } @{$cartobj->{'items'}}; 
                 push @rcpts, [ $remote->{userid}, $remote->email_raw ] if $cartobj->{userid};
@@ -685,7 +692,7 @@
 
         if ($err) {
             $unlock->();
-            return $cc_form_return->($err_text, $err_field);
+            return $cc_form_return->($errors);
         }
 
         # if the order amount is 0, mark this cart as free.  this catches

Tags: bml, ljcom, pm, ssafronova
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments