Committer: ssafronova
LJSUP-8014: Credit card payment process refactoring - first partU trunk/bin/upgrading/en_LJ.dat U trunk/cgi-bin/LJ/Pay/Method/CreditCard.pm U trunk/cgi-bin/LJ/Widget/CreditCard.pm U trunk/ssldocs/manage/account/cc.bml
Modified: trunk/bin/upgrading/en_LJ.dat =================================================================== --- trunk/bin/upgrading/en_LJ.dat 2011-02-25 11:25:21 UTC (rev 10132) +++ trunk/bin/upgrading/en_LJ.dat 2011-02-25 12:59:26 UTC (rev 10133) @@ -4061,6 +4061,8 @@ pay.cc.validate.cardtype=Invalid credit card type, [[cardtype]], selected. +pay.cc.validate.cardtype2=Wrong card type selected. You have selected [[cardtype]], but entered a [[actual]] number. + pay.cc.validate.country=Please select a valid country. pay.cc.validate.cvv2=Invalid CVV2 number entered. [[cardtype]] has a [[num]] digit CVV2 number. Modified: trunk/cgi-bin/LJ/Pay/Method/CreditCard.pm =================================================================== --- trunk/cgi-bin/LJ/Pay/Method/CreditCard.pm 2011-02-25 11:25:21 UTC (rev 10132) +++ trunk/cgi-bin/LJ/Pay/Method/CreditCard.pm 2011-02-25 12:59:26 UTC (rev 10133) @@ -24,7 +24,7 @@ . LJ::Widget::CreditCard::render( LJ::Pay::RecBill->valid_countries, $err, - {}, # only get method + { LJ::Request->post_params }, # only get method undef, { 'cvv2' => 1, Modified: trunk/cgi-bin/LJ/Widget/CreditCard.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/CreditCard.pm 2011-02-25 11:25:21 UTC (rev 10132) +++ trunk/cgi-bin/LJ/Widget/CreditCard.pm 2011-02-25 12:59:26 UTC (rev 10133) @@ -12,6 +12,8 @@ sub render { my ( $countries, $err, $POST, $card, $opts ) = @_; + my $remote = LJ::get_remote(); + LJ::need_res(need_res()); my $ret; $ret = '<ul class="b-ccform">'; @@ -114,7 +116,8 @@ ); $ret .= '</dd></dl></li>'; - my $countryval = $card ? $card->get_country : $POST->{country}; + my $profile_country = $remote && $remote->prop('country'); + my $countryval = ($card ? $card->get_country : $POST->{country}) || $profile_country; $ret .= "<li><dl><dt><label for='country'>" . LJ::Lang::ml('pay.cc.form.country.label') . "</label></dt><dd>"; $ret .= LJ::html_select( @@ -216,7 +219,7 @@ name => 'email', id => 'email', value => ( $card ? $card->get_email() : $POST->{email} ) - || ($LJ::IS_DEV_SERVER && LJ::get_remote() && LJ::get_remote()->email_raw), + || ($remote && $remote->email_raw), class => 'input_field', tabindex => 12, style => 'width:225px', @@ -260,10 +263,14 @@ # Will also set state to value of state_other if non-US sub validate { - my ($POST, $err) = @_; + my ($POST, $err, $field) = @_; + my $a_field; + $field ||= \$a_field; # id of HTML element + unless ($POST->{type} =~ /^Visa|MasterCard|AmEx|Discover$/) { $$err = LJ::Lang::ml('pay.cc.validate.cardtype', {'cardtype' => $POST->{type}}); + $$field = 'type'; return undef; } @@ -277,11 +284,37 @@ 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; } + 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; + } + + 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; + } + + 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; + } + + 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; + } + 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; } @@ -292,27 +325,32 @@ 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; } unless ($POST->{name_full} =~ /\S+\s+\S+/) { $$err = LJ::Lang::ml('pay.cc.validate.name'); + $$field = 'name_full'; return undef; } unless (length($POST->{addr})) { $$err = LJ::Lang::ml('pay.cc.validate.address'); + $$field = 'addr'; return undef; } 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; } if (int($1) == 0) { $$err = LJ::Lang::ml('pay.cc.validate.postalcode.us.zero'); + $$field = 'postal_code'; return undef; } @@ -320,12 +358,14 @@ LJ::load_codes({ "state" => \%states }); if (! $states{$POST->{state}}) { $$err = LJ::Lang::ml('pay.cc.validate.state.us'); + $$field = 'state'; return undef } } 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; } @@ -339,6 +379,7 @@ my $countries = LJ::Pay::RecBill->valid_countries; unless ($countries->{$POST->{country}}) { $$err = LJ::Lang::ml('pay.cc.validate.country'); + $$field = 'country'; return undef; } @@ -346,6 +387,7 @@ LJ::check_email($POST->{email}, \@email_errors); if ($POST->{email} && @email_errors) { $$err = join(", ", @email_errors); + $$field = 'email'; return undef; } Modified: trunk/ssldocs/manage/account/cc.bml =================================================================== --- trunk/ssldocs/manage/account/cc.bml 2011-02-25 11:25:21 UTC (rev 10132) +++ trunk/ssldocs/manage/account/cc.bml 2011-02-25 12:59:26 UTC (rev 10133) @@ -77,7 +77,7 @@ my $now = time(); - my ($ret, $err); + my ($ret, $err, $err_field, $print_cc_form); # Sysban checks my $uniq = LJ::Request->notes('uniq'); @@ -197,7 +197,8 @@ FOO: while (1) { # so we can 'last' out of it my $err; - unless (LJ::Widget::CreditCard::validate(\%POST, \$err)) { + unless (LJ::Widget::CreditCard::validate(\%POST, \$err, \$err_field)) { + $print_cc_form = 1; $die->($err); } @@ -510,7 +511,7 @@ } } - if ($err) { + if ($err and not $print_cc_form) { return "<?errorbar $err errorbar?>"; } @@ -528,6 +529,8 @@ $ret .= " p?>"; } + $ret .= "<?errorbar $err (field: $err_field) errorbar?>" if $print_cc_form; + $ret .= '<form method="POST" id="ccform" onsubmit="return validate_cc_input()">'; $ret .= LJ::html_hidden('pmtid', $pmtid) if $pmtid; $ret .= LJ::Widget::CreditCard::render( $rec->valid_countries(), $err, \%POST, $card, { cvv2 => 1 } );