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");
77 my $paymask; # override only used by loaded cust payinfo, only implemented for realtime processing
79 if ( $payby eq 'CHEK' ) {
81 if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
82 $payinfo = $cust_main->payinfo;
83 $paymask = $cust_main->paymask;
85 $cgi->param('payinfo1') =~ /^(\d+)$/
86 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
88 $cgi->param('payinfo2') =~ /^(\d+)$/
89 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
91 if ( $conf->config('echeck-country') eq 'CA' ) {
92 $cgi->param('payinfo3') =~ /^(\d{5})$/
93 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
94 $payinfo2 = "$1.$payinfo2";
96 $payinfo = $payinfo1. '@'. $payinfo2;
99 } elsif ( $payby eq 'CARD' ) {
101 $payinfo = $cgi->param('payinfo');
102 if ($payinfo eq $cust_main->paymask) {
103 $payinfo = $cust_main->payinfo;
104 $paymask = $cust_main->paymask;
107 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
108 or errorpage(gettext('invalid_card'));
111 or errorpage(gettext('invalid_card'));
113 unless ( $payinfo =~ /^99\d{14}$/ ) { #token
115 my $cardtype = cardtype($payinfo);
117 errorpage(gettext('unknown_card_type'))
118 if $cardtype eq "Unknown";
120 my %bop_card_types = map { $_=>1 } values %{ card_types() };
121 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
125 if ( defined $cust_main->dbdef_table->column('paycvv') ) {
126 if ( length($cgi->param('paycvv') ) ) {
127 if ( cardtype($payinfo) eq 'American Express card' ) {
128 $cgi->param('paycvv') =~ /^(\d{4})$/
129 or errorpage("CVV2 (CID) for American Express cards is four digits.");
132 $cgi->param('paycvv') =~ /^(\d{3})$/
133 or errorpage("CVV2 (CVC2/CID) is three digits.");
136 }elsif( $conf->exists('backoffice-require_cvv') ){
137 errorpage("CVV2 is required");
142 die "unknown payby $payby";
145 $cgi->param('discount_term') =~ /^(\d*)$/
146 or errorpage("illegal discount_term");
147 my $discount_term = $1;
149 # save first, for proper tokenization later
150 if ( $cgi->param('save') ) {
151 my $new = new FS::cust_main { $cust_main->hash };
152 if ( $payby eq 'CARD' ) {
153 $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) );
154 } elsif ( $payby eq 'CHEK' ) {
155 $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) );
157 die "unknown payby $payby";
159 $new->payinfo($payinfo); # sets default paymask, but not if it's already tokenized
160 $new->paymask($paymask) if $paymask; # in case it's been tokenized, override with loaded paymask
161 $new->set( 'paydate' => "$year-$month-01" );
162 $new->set( 'payname' => $payname );
164 #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
166 if ( $payby eq 'CARD' &&
167 ( (grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save'))
168 || $conf->exists('business-onlinepayment-verification')
171 $new->set( 'paycvv' => $paycvv );
173 $new->set( 'paycvv' => '');
176 if ( $payby eq 'CARD' ) {
177 my $bill_location = FS::cust_location->new;
178 $bill_location->set( $_ => $cgi->param($_) )
179 foreach @{$payby2fields{$payby}};
180 $new->set('bill_location' => $bill_location);
181 # will do nothing if the fields are all unchanged
183 $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}};
186 my $error = $new->replace($cust_main);
187 errorpage("error saving info, payment not processed: $error")
194 if ( $cgi->param('batch') ) {
196 $error = 'Prepayment discounts not supported with batched payments'
199 $error ||= $cust_main->batch_card(
202 'payinfo' => $payinfo,
203 'paydate' => "$year-$month-01",
204 'payname' => $payname,
205 map { $_ => scalar($cgi->param($_)) }
206 @{$payby2fields{$payby}}
208 errorpage($error) if $error;
212 $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
215 'balance' => $balance,
216 'payinfo' => $payinfo,
217 'paymask' => $paymask,
218 'paydate' => "$year-$month-01",
219 'payname' => $payname,
220 'payunique' => $payunique,
222 'paynum_ref' => \$paynum,
223 'discount_term' => $discount_term,
224 'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
226 map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
228 errorpage($error) if $error;
230 #no error, so order the fee package if applicable...
231 if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
233 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
235 my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
236 errorpage("payment processed successfully, but error ordering fee: $error")
239 #and generate an invoice for it now too
240 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
241 errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
246 $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');