From fca1ac5d298b3896c1c42c4ffc7bdd3bab9b8516 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 11 Jul 2007 08:08:29 +0000 Subject: [PATCH] finish adding payunique field --- FS/FS/cust_pay.pm | 20 +-- httemplate/misc/payment.cgi | 68 ++++---- httemplate/misc/process/payment.cgi | 316 ++++++++++++++++++------------------ 3 files changed, 202 insertions(+), 202 deletions(-) diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm index 0ead22658..23bcdd93a 100644 --- a/FS/FS/cust_pay.pm +++ b/FS/FS/cust_pay.pm @@ -69,7 +69,9 @@ L and L for conversion functions. =item paymask - Masked payinfo (See L for how this works) -=item paybatch - text field for tracking card processing +=item paybatch - text field for tracking card processing or other batch grouping + +=item payunique - Optional unique identifer to prevent duplicate transactions. =item closed - books closed flag, empty or `Y' @@ -162,17 +164,6 @@ sub insert { } } - if ( $self->paybatch =~ /^webui-/ ) { - my @cust_pay = qsearch('cust_pay', { - 'custnum' => $self->custnum, - 'paybatch' => $self->paybatch, - } ); - if ( scalar(@cust_pay) > 1 ) { - $dbh->rollback if $oldAutoCommit; - return "a payment with webui token ". $self->paybatch. " already exists"; - } - } - $dbh->commit or die $dbh->errstr if $oldAutoCommit; #false laziness w/ cust_credit::insert @@ -421,7 +412,10 @@ sub check { # should give a better error message the other 99.9% of the time... if ( length($self->payunique) && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) { - return "duplicate transaction"; #well, it *could* be a better error message + #well, it *could* be a better error message + return "duplicate transaction". + " - a payment with unique identifer ". $self->payunique. + " already exists"; } $self->SUPER::check; diff --git a/httemplate/misc/payment.cgi b/httemplate/misc/payment.cgi index ac102826a..2c889d73b 100644 --- a/httemplate/misc/payment.cgi +++ b/httemplate/misc/payment.cgi @@ -1,41 +1,9 @@ -% -% my %type = ( 'CARD' => 'credit card', -% 'CHEK' => 'electronic check (ACH)', -% ); -% -% $cgi->param('payby') =~ /^(CARD|CHEK)$/ -% or die "unknown payby ". $cgi->param('payby'); -% my $payby = $1; -% -% $cgi->param('custnum') =~ /^(\d+)$/ -% or die "illegal custnum ". $cgi->param('custnum'); -% my $custnum = $1; -% -% my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } ); -% die "unknown custnum $custnum" unless $cust_main; -% -% my $balance = $cust_main->balance; -% -% my $payinfo = ''; -% -% #false laziness w/selfservice make_payment.html shortcut for one-country -% my $conf = new FS::Conf; -% my %states = map { $_->state => 1 } -% qsearch('cust_main_county', { -% 'country' => $conf->config('countrydefault') || 'US' -% } ); -% my @states = sort { $a cmp $b } keys %states; -% -% my $paybatch = "webui-payment-". time. "-$$-". rand() * 2**32; -% -% - <% include( '/elements/header.html', "Process $type{$payby} payment" ) %> <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(2) . "view/cust_main.cgi" ) %>
- + @@ -246,3 +214,37 @@ function OLiframeContent(src, width, height, name) {
<% include('/elements/footer.html') %> +<%init> + +my %type = ( 'CARD' => 'credit card', + 'CHEK' => 'electronic check (ACH)', + ); + +$cgi->param('payby') =~ /^(CARD|CHEK)$/ + or die "unknown payby ". $cgi->param('payby'); +my $payby = $1; + +$cgi->param('custnum') =~ /^(\d+)$/ + or die "illegal custnum ". $cgi->param('custnum'); +my $custnum = $1; + +my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } ); +die "unknown custnum $custnum" unless $cust_main; + +my $balance = $cust_main->balance; + +my $payinfo = ''; + +#false laziness w/selfservice make_payment.html shortcut for one-country +my $conf = new FS::Conf; +my %states = map { $_->state => 1 } + qsearch('cust_main_county', { + 'country' => $conf->config('countrydefault') || 'US' + } ); +my @states = sort { $a cmp $b } keys %states; + +my $payunique = "webui-payment-". time. "-$$-". rand() * 2**32; + + + + diff --git a/httemplate/misc/process/payment.cgi b/httemplate/misc/process/payment.cgi index 9ac5d5d9e..29157d8f0 100644 --- a/httemplate/misc/process/payment.cgi +++ b/httemplate/misc/process/payment.cgi @@ -1,162 +1,166 @@ -% -%#some false laziness w/MyAccount::process_payment -% -%$cgi->param('custnum') =~ /^(\d+)$/ -% or die "illegal custnum ". $cgi->param('custnum'); -%my $custnum = $1; -% -%my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ); -%die "unknown custnum $custnum" unless $cust_main; -% -%$cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/ -% or eidiot "illegal amount ". $cgi->param('amount'); -%my $amount = $1; -%eidiot "amount <= 0" unless $amount > 0; -% -%$cgi->param('year') =~ /^(\d+)$/ -% or die "illegal year ". $cgi->param('year'); -%my $year = $1; -% -%$cgi->param('month') =~ /^(\d+)$/ -% or die "illegal month ". $cgi->param('month'); -%my $month = $1; -% -%$cgi->param('payby') =~ /^(CARD|CHEK)$/ -% or die "illegal payby ". $cgi->param('payby'); -%my $payby = $1; -%my %payby2fields = ( -% 'CARD' => [ qw( address1 address2 city state zip ) ], -% 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ], -%); -%my %type = ( 'CARD' => 'credit card', -% 'CHEK' => 'electronic check (ACH)', -% ); -% -%$cgi->param('payname') =~ /^([\w \,\.\-\']+)$/ -% or eidiot gettext('illegal_name'). " payname: ". $cgi->param('payname'); -%my $payname = $1; -% -%$cgi->param('paybatch') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/ -% or eidiot gettext('illegal_text'). " paybatch: ". $cgi->param('paybatch'); -%my $paybatch = $1; -% -%my $payinfo; -%my $paycvv = ''; -%if ( $payby eq 'CHEK' ) { -% -% if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) { -% $payinfo = $cust_main->payinfo; -% } else { -% $cgi->param('payinfo1') =~ /^(\d+)$/ -% or eidiot "illegal account number ". $cgi->param('payinfo1'); -% my $payinfo1 = $1; -% $cgi->param('payinfo2') =~ /^(\d+)$/ -% or eidiot "illegal ABA/routing number ". $cgi->param('payinfo2'); -% my $payinfo2 = $1; -% $payinfo = $payinfo1. '@'. $payinfo2; -% } -% -%} elsif ( $payby eq 'CARD' ) { -% -% $payinfo = $cgi->param('payinfo'); -% if ($payinfo eq $cust_main->paymask) { -% $payinfo = $cust_main->payinfo; -% } -% $payinfo =~ s/\D//g; -% $payinfo =~ /^(\d{13,16})$/ -% or eidiot gettext('invalid_card'); # . ": ". $self->payinfo; -% $payinfo = $1; -% validate($payinfo) -% or eidiot gettext('invalid_card'); # . ": ". $self->payinfo; -% eidiot gettext('unknown_card_type') -% if cardtype($payinfo) eq "Unknown"; -% -% if ( defined $cust_main->dbdef_table->column('paycvv') ) { -% if ( length($cgi->param('paycvv') ) ) { -% if ( cardtype($payinfo) eq 'American Express card' ) { -% $cgi->param('paycvv') =~ /^(\d{4})$/ -% or eidiot "CVV2 (CID) for American Express cards is four digits."; -% $paycvv = $1; -% } else { -% $cgi->param('paycvv') =~ /^(\d{3})$/ -% or eidiot "CVV2 (CVC2/CID) is three digits."; -% $paycvv = $1; -% } -% } -% } -% -%} else { -% die "unknown payby $payby"; -%} -% -%my $error = ''; -%if ($cgi->param('batch')) { -% $error = $cust_main->batch_card( -% 'payby' => $payby, -% 'amount' => $amount, -% 'payinfo' => $payinfo, -% 'paydate' => "$year-$month-01", -% 'payname' => $payname, -% map { $_ => $cgi->param($_) } -% @{$payby2fields{$payby}} -% ); -% eidiot($error) if $error; -%}else{ -% $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount, -% 'quiet' => 1, -% 'manual' => 1, -% 'payinfo' => $payinfo, -% 'paydate' => "$year-$month-01", -% 'payname' => $payname, -% 'paybatch' => $paybatch, -% 'paycvv' => $paycvv, -% map { $_ => $cgi->param($_) } @{$payby2fields{$payby}} -% ); -% eidiot($error) if $error; -% -% $cust_main->apply_payments; -%} -% -%if ( $cgi->param('save') ) { -% my $new = new FS::cust_main { $cust_main->hash }; -% if ( $payby eq 'CARD' ) { -% $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) ); -% } elsif ( $payby eq 'CHEK' ) { -% $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) ); -% } else { -% die "unknown payby $payby"; -% } -% $new->set( 'payinfo' => $payinfo ); -% $new->set( 'paydate' => "$year-$month-01" ); -% $new->set( 'payname' => $payname ); -% -% #false laziness w/FS:;cust_main::realtime_bop - check both to make sure -% # working correctly -% my $conf = new FS::Conf; -% if ( $payby eq 'CARD' && -% grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) { -% $new->set( 'paycvv' => $paycvv ); -% } else { -% $new->set( 'paycvv' => ''); -% } -% -% $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}}; -% -% my $error = $new->replace($cust_main); -% eidiot "payment processed successfully, but error saving info: $error" -% if $error; -% $cust_main = $new; -%} -% -%#success! -% -% - <% include( '/elements/header.html', ucfirst($type{$payby}). ' processing successful', include('/elements/menubar.html'), ) %> <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(3). "view/cust_main.cgi" ) %> - - + +<% include('/elements/footer.html') %> +<%init> + +#some false laziness w/MyAccount::process_payment + +$cgi->param('custnum') =~ /^(\d+)$/ + or die "illegal custnum ". $cgi->param('custnum'); +my $custnum = $1; + +my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ); +die "unknown custnum $custnum" unless $cust_main; + +$cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/ + or eidiot "illegal amount ". $cgi->param('amount'); +my $amount = $1; +eidiot "amount <= 0" unless $amount > 0; + +$cgi->param('year') =~ /^(\d+)$/ + or die "illegal year ". $cgi->param('year'); +my $year = $1; + +$cgi->param('month') =~ /^(\d+)$/ + or die "illegal month ". $cgi->param('month'); +my $month = $1; + +$cgi->param('payby') =~ /^(CARD|CHEK)$/ + or die "illegal payby ". $cgi->param('payby'); +my $payby = $1; +my %payby2fields = ( + 'CARD' => [ qw( address1 address2 city state zip ) ], + 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ], +); +my %type = ( 'CARD' => 'credit card', + 'CHEK' => 'electronic check (ACH)', + ); + +$cgi->param('payname') =~ /^([\w \,\.\-\']+)$/ + or eidiot gettext('illegal_name'). " payname: ". $cgi->param('payname'); +my $payname = $1; + +$cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/ + or eidiot gettext('illegal_text'). " payunique: ". $cgi->param('payunique'); +my $payunique = $1; + +my $payinfo; +my $paycvv = ''; +if ( $payby eq 'CHEK' ) { + + if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) { + $payinfo = $cust_main->payinfo; + } else { + $cgi->param('payinfo1') =~ /^(\d+)$/ + or eidiot "illegal account number ". $cgi->param('payinfo1'); + my $payinfo1 = $1; + $cgi->param('payinfo2') =~ /^(\d+)$/ + or eidiot "illegal ABA/routing number ". $cgi->param('payinfo2'); + my $payinfo2 = $1; + $payinfo = $payinfo1. '@'. $payinfo2; + } + +} elsif ( $payby eq 'CARD' ) { + + $payinfo = $cgi->param('payinfo'); + if ($payinfo eq $cust_main->paymask) { + $payinfo = $cust_main->payinfo; + } + $payinfo =~ s/\D//g; + $payinfo =~ /^(\d{13,16})$/ + or eidiot gettext('invalid_card'); # . ": ". $self->payinfo; + $payinfo = $1; + validate($payinfo) + or eidiot gettext('invalid_card'); # . ": ". $self->payinfo; + eidiot gettext('unknown_card_type') + if cardtype($payinfo) eq "Unknown"; + + if ( defined $cust_main->dbdef_table->column('paycvv') ) { + if ( length($cgi->param('paycvv') ) ) { + if ( cardtype($payinfo) eq 'American Express card' ) { + $cgi->param('paycvv') =~ /^(\d{4})$/ + or eidiot "CVV2 (CID) for American Express cards is four digits."; + $paycvv = $1; + } else { + $cgi->param('paycvv') =~ /^(\d{3})$/ + or eidiot "CVV2 (CVC2/CID) is three digits."; + $paycvv = $1; + } + } + } + +} else { + die "unknown payby $payby"; +} + +my $error = ''; +if ( $cgi->param('batch') ) { + + $error = $cust_main->batch_card( + 'payby' => $payby, + 'amount' => $amount, + 'payinfo' => $payinfo, + 'paydate' => "$year-$month-01", + 'payname' => $payname, + map { $_ => $cgi->param($_) } + @{$payby2fields{$payby}} + ); + eidiot($error) if $error; + +} else { + + $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount, + 'quiet' => 1, + 'manual' => 1, + 'payinfo' => $payinfo, + 'paydate' => "$year-$month-01", + 'payname' => $payname, + 'payunique' => $payunique, + 'paycvv' => $paycvv, + map { $_ => $cgi->param($_) } @{$payby2fields{$payby}} + ); + eidiot($error) if $error; + + $cust_main->apply_payments; + +} + +if ( $cgi->param('save') ) { + my $new = new FS::cust_main { $cust_main->hash }; + if ( $payby eq 'CARD' ) { + $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) ); + } elsif ( $payby eq 'CHEK' ) { + $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) ); + } else { + die "unknown payby $payby"; + } + $new->set( 'payinfo' => $payinfo ); + $new->set( 'paydate' => "$year-$month-01" ); + $new->set( 'payname' => $payname ); + + #false laziness w/FS:;cust_main::realtime_bop - check both to make sure + # working correctly + my $conf = new FS::Conf; + if ( $payby eq 'CARD' && + grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) { + $new->set( 'paycvv' => $paycvv ); + } else { + $new->set( 'paycvv' => ''); + } + + $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}}; + + my $error = $new->replace($cust_main); + eidiot "payment processed successfully, but error saving info: $error" + if $error; + $cust_main = $new; +} + +#success! + + -- 2.11.0