1 % if ( $cgi->param('batch') ) {
3 <% include( '/elements/header.html', ucfirst($type{$payby}). ' processing successful',
4 include('/elements/menubar.html'),
9 <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(3). "view/cust_main.cgi" ) %>
11 <% include('/elements/footer.html') %>
13 % #2.5/2.7?# } elsif ( $curuser->access_right('View payments') ) {
14 % } elsif ( $curuser->access_right(['View invoices', 'View payments']) ) {
15 <% $cgi->redirect(popurl(3). "view/cust_pay.html?paynum=$paynum" ) %>
17 <% $cgi->redirect(popurl(3). "view/cust_main.html?custnum=$custnum" ) %>
21 my $curuser = $FS::CurrentUser::CurrentUser;
22 die "access denied" unless $curuser->access_right('Process payment');
24 my $conf = new FS::Conf;
26 #some false laziness w/MyAccount::process_payment
28 $cgi->param('custnum') =~ /^(\d+)$/
29 or die "illegal custnum ". $cgi->param('custnum');
32 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
33 die "unknown custnum $custnum" unless $cust_main;
35 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
36 or errorpage("illegal amount ". $cgi->param('amount'));
38 errorpage("amount <= 0") unless $amount > 0;
40 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
42 $amount = sprintf('%.2f', $amount + $fee);
45 $cgi->param('year') =~ /^(\d+)$/
46 or errorpage("illegal year ". $cgi->param('year'));
49 $cgi->param('month') =~ /^(\d+)$/
50 or errorpage("illegal month ". $cgi->param('month'));
53 $cgi->param('payby') =~ /^(CARD|CHEK)$/
54 or errorpage("illegal payby ". $cgi->param('payby'));
57 'CARD' => [ qw( address1 address2 city county state zip country ) ],
58 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
60 my %type = ( 'CARD' => 'credit card',
61 'CHEK' => 'electronic check (ACH)',
64 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
65 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
68 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
69 or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
72 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
73 or errorpage("illegal balance");
78 if ( $payby eq 'CHEK' ) {
80 if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
81 $payinfo = $cust_main->payinfo;
83 $cgi->param('payinfo1') =~ /^(\d+)$/
84 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
86 $cgi->param('payinfo2') =~ /^(\d+)$/
87 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
89 if ( $conf->config('echeck-country') eq 'CA' ) {
90 $cgi->param('payinfo3') =~ /^(\d{5})$/
91 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
92 $payinfo2 = "$1.$payinfo2";
94 $payinfo = $payinfo1. '@'. $payinfo2;
97 } elsif ( $payby eq 'CARD' ) {
99 $payinfo = $cgi->param('payinfo');
100 if ($payinfo eq $cust_main->paymask) {
101 $payinfo = $cust_main->payinfo;
104 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
105 or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
108 or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
110 errorpage(gettext('unknown_card_type'))
111 if $payinfo !~ /^99\d{14}$/ #token
112 && cardtype($payinfo) eq "Unknown";
114 if ( defined $cust_main->dbdef_table->column('paycvv') ) {
115 if ( length($cgi->param('paycvv') ) ) {
116 if ( cardtype($payinfo) eq 'American Express card' ) {
117 $cgi->param('paycvv') =~ /^(\d{4})$/
118 or errorpage("CVV2 (CID) for American Express cards is four digits.");
121 $cgi->param('paycvv') =~ /^(\d{3})$/
122 or errorpage("CVV2 (CVC2/CID) is three digits.");
129 die "unknown payby $payby";
132 $cgi->param('discount_term') =~ /^(\d*)$/
133 or errorpage("illegal discount_term");
134 my $discount_term = $1;
138 if ( $cgi->param('batch') ) {
140 $error = 'Prepayment discounts not supported with batched payments'
143 $error ||= $cust_main->batch_card(
146 'payinfo' => $payinfo,
147 'paydate' => "$year-$month-01",
148 'payname' => $payname,
149 map { $_ => $cgi->param($_) }
150 @{$payby2fields{$payby}}
152 errorpage($error) if $error;
156 $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
159 'balance' => $balance,
160 'payinfo' => $payinfo,
161 'paydate' => "$year-$month-01",
162 'payname' => $payname,
163 'payunique' => $payunique,
165 'paynum_ref' => \$paynum,
166 'discount_term' => $discount_term,
167 map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
169 errorpage($error) if $error;
171 #no error, so order the fee package if applicable...
172 if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
174 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
176 my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
177 errorpage("payment processed successfully, but error ordering fee: $error")
180 #and generate an invoice for it now too
181 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
182 errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
187 $cust_main->apply_payments;
191 if ( $cgi->param('save') ) {
192 my $new = new FS::cust_main { $cust_main->hash };
193 if ( $payby eq 'CARD' ) {
194 $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) );
195 } elsif ( $payby eq 'CHEK' ) {
196 $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) );
198 die "unknown payby $payby";
200 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
201 $new->set( 'paydate' => "$year-$month-01" );
202 $new->set( 'payname' => $payname );
204 #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
206 if ( $payby eq 'CARD' &&
207 grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) {
208 $new->set( 'paycvv' => $paycvv );
210 $new->set( 'paycvv' => '');
213 $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}};
215 my $error = $new->replace($cust_main);
216 errorpage("payment processed successfully, but error saving info: $error")