From 73f34cbe08a9be968a2557cd6906dde5bb9a97a3 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 23 Mar 2004 00:06:55 +0000 Subject: [PATCH] one-time credit card and ACH payments (like self-service) closes: Bug#648 --- FS/FS/ClientAPI/MyAccount.pm | 82 ++++++++++--- FS/FS/cust_main.pm | 28 ++++- htetc/global.asa | 1 + htetc/handler.pl | 1 + httemplate/elements/small_custview.html | 2 + httemplate/misc/payment.cgi | 209 ++++++++++++++++++++++++++++++++ httemplate/misc/process/payment.cgi | 148 ++++++++++++++++++++++ httemplate/view/cust_main.cgi | 9 +- 8 files changed, 460 insertions(+), 20 deletions(-) create mode 100644 httemplate/elements/small_custview.html create mode 100644 httemplate/misc/payment.cgi create mode 100644 httemplate/misc/process/payment.cgi diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm index a865a22d5..44d81c999 100644 --- a/FS/FS/ClientAPI/MyAccount.pm +++ b/FS/FS/ClientAPI/MyAccount.pm @@ -9,6 +9,7 @@ use Cache::SharedMemoryCache; #store in db? use FS::CGI qw(small_custview); #doh use FS::Conf; use FS::Record qw(qsearch qsearchs); +use FS::Msgcat qw(gettext); use FS::svc_acct; use FS::svc_domain; use FS::cust_main; @@ -176,11 +177,7 @@ sub payment_info { warn $return{card_type} = cardtype($cust_main->payinfo); $return{payinfo} = $cust_main->payinfo; - if ( $cust_main->paydate =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #Pg date format - @return{'month', 'year'} = ( $2, $1 ); - } elsif ( $cust_main->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) { - @return{'month', 'year'} = ( $1, $3 ); - } + @return{'month', 'year'} = $cust_main->paydate_monthyear; } @@ -212,7 +209,10 @@ sub payment_info { }; +#some false laziness with httemplate/process/payment.cgi - look there for +#ACH and CVV support stuff sub process_payment { + my $p = shift; my $session = $cache->get($p->{'session_id'}) @@ -225,6 +225,69 @@ sub process_payment { my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) or return { 'error' => "unknown custnum $custnum" }; + $p->{'payname'} =~ /^([\w \,\.\-\']+)$/ + or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} }; + my $payname = $1; + + $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/ + or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} }; + my $paybatch = $1; + + my $payinfo; + my $paycvv = ''; + #if ( $payby eq 'CHEK' ) { + # + # $p->{'payinfo1'} =~ /^(\d+)$/ + # or return { 'error' => "illegal account number ". $p->{'payinfo1'} }; + # my $payinfo1 = $1; + # $p->{'payinfo2'} =~ /^(\d+)$/ + # or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} }; + # my $payinfo2 = $1; + # $payinfo = $payinfo1. '@'. $payinfo2; + # + #} elsif ( $payby eq 'CARD' ) { + + $payinfo = $p->{'payinfo'}; + $payinfo =~ s/\D//g; + $payinfo =~ /^(\d{13,16})$/ + or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo + $payinfo = $1; + validate($payinfo) + or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo + return { 'error' => gettext('unknown_card_type') } + if cardtype($payinfo) eq "Unknown"; + + if ( defined $cust_main->dbdef_table->column('paycvv') ) { + if ( length($p->{'paycvv'} ) ) { + if ( cardtype($payinfo) eq 'American Express card' ) { + $p->{'paycvv'} =~ /^(\d{4})$/ + or return { 'error' => "CVV2 (CID) for American Express cards is four digits." }; + $paycvv = $1; + } else { + $p->{'paycvv'} =~ /^(\d{3})$/ + or return { 'error' => "CVV2 (CVC2/CID) is three digits." }; + $paycvv = $1; + } + } + } + + #} else { + # die "unknown payby $payby"; + #} + + my $error = $cust_main->realtime_bop( 'CC', $p->{'amount'}, + 'quiet' => 1, + 'payinfo' => $payinfo, + 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01', + 'payname' => $payname, + 'paybatch' => $paybatch, + 'paycvv' => $paycvv, + map { $_ => $p->{$_} } qw( address1 address2 city state zip ) + ); + return { 'error' => $error } if $error; + + $cust_main->apply_payments; + if ( $p->{'save'} ) { my $new = new FS::cust_main { $cust_main->hash }; $new->set( $_ => $p->{$_} ) @@ -236,15 +299,6 @@ sub process_payment { $cust_main = $new; } - my $error = $cust_main->realtime_bop( 'CC', $p->{'amount'}, quiet=>1, - 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01', - map { $_ => $p->{$_} } - qw( payname address1 address2 city state zip payinfo paybatch ) - ); - return { 'error' => $error } if $error; - - $cust_main->apply_payments; - return { 'error' => '' }; } diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index fc744dc5f..59e357055 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -1701,7 +1701,7 @@ sub realtime_bop { #overrides $self->set( $_ => $options{$_} ) foreach grep { exists($options{$_}) } - qw( payname address1 address2 city state zip payinfo paydate ); + qw( payname address1 address2 city state zip payinfo paydate paycvv); #load up config my $bop_config = 'business-onlinepayment'; @@ -1711,6 +1711,9 @@ sub realtime_bop { $conf->config($bop_config); $action ||= 'normal authorization'; pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/; + die "No real-time processor is enabled - ". + "did you set the business-onlinepayment configuration value?\n" + unless $processor; #massage data @@ -1837,11 +1840,12 @@ sub realtime_bop { } #remove paycvv after initial transaction - #make this disable-able via a config option if anyone insists? - # (though that probably violates cardholder agreements) + #false laziness w/misc/process/payment.cgi - check both to make sure working + # correctly if ( defined $self->dbdef_table->column('paycvv') && length($self->paycvv) && ! grep { $_ eq cardtype($self->payinfo) } $conf->config('cvv-save') + && ! length($options{'paycvv'}) ) { my $new = new FS::cust_main { $self->hash }; $new->paycvv(''); @@ -2121,6 +2125,24 @@ sub balance_date { ); } +=item paydate_monthyear + +Returns a two-element list consisting of the month and year of this customer's +paydate (credit card expiration date for CARD customers) + +=cut + +sub paydate_monthyear { + my $self = shift; + if ( $self->paydate =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #Pg date format + ( $2, $1 ); + } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) { + ( $1, $3 ); + } else { + ('', ''); + } +} + =item invoicing_list [ ARRAYREF ] If an arguement is given, sets these email addresses as invoice recipients diff --git a/htetc/global.asa b/htetc/global.asa index 5652a6f4f..bb94b9268 100644 --- a/htetc/global.asa +++ b/htetc/global.asa @@ -14,6 +14,7 @@ use Tie::IxHash; use HTML::Entities; use IO::Handle; use IO::File; +use Business::CreditCard; use String::Approx qw(amatch); use Chart::LinesPoints; use HTML::Widgets::SelectLayers 0.03; diff --git a/htetc/handler.pl b/htetc/handler.pl index 8541b4ce5..8d4f1e71f 100644 --- a/htetc/handler.pl +++ b/htetc/handler.pl @@ -71,6 +71,7 @@ sub handler use HTML::Entities; use IO::Handle; use IO::File; + use Business::CreditCard; use String::Approx qw(amatch); use Chart::LinesPoints; use HTML::Widgets::SelectLayers 0.03; diff --git a/httemplate/elements/small_custview.html b/httemplate/elements/small_custview.html new file mode 100644 index 000000000..1e8ae739a --- /dev/null +++ b/httemplate/elements/small_custview.html @@ -0,0 +1,2 @@ +<% my $conf = new FS::Conf; %> +<%= small_custview( shift, shift || $conf->config('countrydefault') ) %> diff --git a/httemplate/misc/payment.cgi b/httemplate/misc/payment.cgi new file mode 100644 index 000000000..bf89a3da7 --- /dev/null +++ b/httemplate/misc/payment.cgi @@ -0,0 +1,209 @@ +<% + 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('defaultcountry') || '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 ) %> +
+ + + + +<% #include( '/elements/table.html', '#cccccc' ) %> +<%= ntable('#cccccc') %> + + Payment amount + +
+ $"> +
+ + +<% if ( $payby eq 'CARD' ) { + my( $payinfo, $paycvv, $month, $year ) = ( '', '', '', '' ); + my $payname = $cust_main->first. ' '. $cust_main->getfield('last'); + my $address1 = $cust_main->address1; + my $address2 = $cust_main->address2; + my $city = $cust_main->city; + my $state = $cust_main->state; + my $zip = $cust_main->zip; + if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) { + $payinfo = $cust_main->payinfo; + $paycvv = $cust_main->paycvv; + ( $month, $year ) = $cust_main->paydate_monthyear; + $payname = $cust_main->payname if $cust_main->payname; + } +%> + + Card number + + + + + + + + + +
+ Exp. + + / + +
+ + + + CVV2 + + (help) + + + + Exact name on card + + + Card billing address + + + + + Address line 2 + + + + + City + + + + + + + + + +
+ + State + + Zip + +
+ + + +<% } elsif ( $payby eq 'CHEK' ) { + my( $payinfo1, $payinfo2, $payname, $ss ) = ( '', '', '', '' ); + if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) { + $cust_main->payinfo =~ /^(\d+)\@(\d+)$/ + or die "unparsable payinfo ". $cust_main->payinfo; + ($payinfo1, $payinfo2) = ($1, $2); + $payname = $cust_main->payname; + $ss = $cust_main->ss; + } +%> + + + + Account number + + + + ABA/Routing number + + + (help) + + + + Bank name + + + + + Account holder
+ Social security or tax ID # + + + + +<% } %> + + + + + Remember this information + + + + NAME="auto" VALUE="1" onClick="if (this.checked) { document.OneTrueForm.save.checked=true; }"> + Charge future payments to this <%= $type{$payby} %> automatically + + + +
+ +
+ + diff --git a/httemplate/misc/process/payment.cgi b/httemplate/misc/process/payment.cgi new file mode 100644 index 000000000..fa0ede89c --- /dev/null +++ b/httemplate/misc/process/payment.cgi @@ -0,0 +1,148 @@ +<% + +#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 %payby2bop = ( + 'CARD' => 'CC', + 'CHEK' => 'ECHECK', +); +my %payby2fields = ( + 'CARD' => [ qw( address1 address2 city state zip ) ], + 'CHEK' => [ qw( ss ) ], +); +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' ) { + + $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'); + $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 = $cust_main->realtime_bop( $payby2bop{$payby}, $amount, + 'quiet' => 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 sucessfully, but error saving info: $error" + if $error; + $cust_main = $new; +} + +#success! + +%> +<%= include( '/elements/header.html', ucfirst($type{$payby}). ' processing sucessful', + include('/elements/menubar.html', + 'Main menu' => popurl(3), + "View this customer (#$custnum)" => + popurl(3). "view/cust_main.cgi?$custnum", + ), + + ) +%> +<%= include( '/elements/small_custview.html', $cust_main ) %> + + diff --git a/httemplate/view/cust_main.cgi b/httemplate/view/cust_main.cgi index cc3600641..c846b655d 100755 --- a/httemplate/view/cust_main.cgi +++ b/httemplate/view/cust_main.cgi @@ -570,9 +570,12 @@ function cust_credit_areyousure(href) { <% if ( $conf->config('payby-default') ne 'HIDE' ) { %> -

Payment History - (Post payment - | Post credit) +

Payment History
+ Post cash/check payment + | Process credit card payment + | Process electronic check (ACH) payment +
Post credit +
<% #get payment history -- 2.11.0