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 my $loaded_cust_payby;
80 if ( $payby eq 'CHEK' ) {
82 if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
84 my $search_paymask = $cgi->param('payinfo1') . '@' . $cgi->param('payinfo2');
85 $search_paymask .= '.' . $cgi->param('payinfo3')
86 if $conf->config('echeck-country') eq 'CA';
88 #paymask might not be saved in database, need to run paymask method for any potential match
89 foreach my $search_cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
90 if ($search_paymask eq $search_cust_payby->paymask) {
91 # if there are multiple matches, assume for now that it's the first one returned,
92 # since that's what auto-fills; it's unlikely a masked number would be entered by hand,
93 # but it's very likely users will just click-through what's been auto-filled
94 $loaded_cust_payby = $search_cust_payby;
98 errorpage("Masked payinfo not found") unless $loaded_cust_payby;
99 $payinfo = $loaded_cust_payby->payinfo;
100 $paymask = $loaded_cust_payby->paymask;
103 $cgi->param('payinfo1') =~ /^(\d+)$/
104 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
106 $cgi->param('payinfo2') =~ /^(\d+)$/
107 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
109 if ( $conf->config('echeck-country') eq 'CA' ) {
110 $cgi->param('payinfo3') =~ /^(\d{5})$/
111 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
112 $payinfo2 = "$1.$payinfo2";
114 $payinfo = $payinfo1. '@'. $payinfo2;
117 } elsif ( $payby eq 'CARD' ) {
119 $payinfo = $cgi->param('payinfo');
120 if ($payinfo =~ /xx/i) {
122 #paymask might not be saved in database, need to run paymask method for any potential match
123 foreach my $search_cust_payby ($cust_main->cust_payby('CARD','DCRD')) {
124 if ($payinfo eq $search_cust_payby->paymask) {
125 $loaded_cust_payby = $search_cust_payby;
130 errorpage("Masked payinfo not found") unless $loaded_cust_payby;
131 $payinfo = $loaded_cust_payby->payinfo;
132 $paymask = $loaded_cust_payby->paymask;
137 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
138 or errorpage(gettext('invalid_card'));
141 or errorpage(gettext('invalid_card'));
143 unless ( $payinfo =~ /^99\d{14}$/ ) { #token
145 my $cardtype = cardtype($payinfo);
147 errorpage(gettext('unknown_card_type'))
148 if $cardtype eq "Unknown";
150 my %bop_card_types = map { $_=>1 } values %{ card_types() };
151 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
155 if ( defined $cust_main->dbdef_table->column('paycvv') ) { #is this test necessary anymore?
156 if ( length($cgi->param('paycvv') ) ) {
157 if ( cardtype($payinfo) eq 'American Express card' ) {
158 $cgi->param('paycvv') =~ /^(\d{4})$/
159 or errorpage("CVV2 (CID) for American Express cards is four digits.");
162 $cgi->param('paycvv') =~ /^(\d{3})$/
163 or errorpage("CVV2 (CVC2/CID) is three digits.");
166 }elsif( $conf->exists('backoffice-require_cvv') ){
167 errorpage("CVV2 is required");
172 die "unknown payby $payby";
175 $cgi->param('discount_term') =~ /^(\d*)$/
176 or errorpage("illegal discount_term");
177 my $discount_term = $1;
179 # save first, for proper tokenization later
180 if ( $cgi->param('save') ) {
183 if ( $payby eq 'CARD' ) {
184 my $bill_location = FS::cust_location->new;
185 $bill_location->set( $_ => $cgi->param($_) )
186 foreach @{$payby2fields{$payby}};
187 $saveopt{'bill_location'} = $bill_location;
188 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
189 $saveopt{'paydate'} = "$year-$month-01";
191 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
192 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
195 my $error = $cust_main->save_cust_payby(
196 'payment_payby' => $payby,
197 'auto' => scalar($cgi->param('auto')),
198 'payinfo' => $payinfo,
199 'paymask' => $paymask,
200 'payname' => $payname,
201 'paytype => $paytype,
205 errorpage("error saving info, payment not processed: $error")
211 if ( $cgi->param('batch') ) {
213 $error = 'Prepayment discounts not supported with batched payments'
216 $error ||= $cust_main->batch_card(
219 'payinfo' => $payinfo,
220 'paydate' => "$year-$month-01",
221 'payname' => $payname,
222 map { $_ => $cgi->param($_) }
223 @{$payby2fields{$payby}}
225 errorpage($error) if $error;
229 $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
232 'balance' => $balance,
233 'payinfo' => $payinfo,
234 'paymask' => $paymask,
235 'paydate' => "$year-$month-01",
236 'payname' => $payname,
237 'payunique' => $payunique,
239 'paynum_ref' => \$paynum,
240 'discount_term' => $discount_term,
241 map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
243 errorpage($error) if $error;
245 #no error, so order the fee package if applicable...
246 if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
248 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
250 my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
251 errorpage("payment processed successfully, but error ordering fee: $error")
254 #and generate an invoice for it now too
255 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
256 errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
261 $cust_main->apply_payments;