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;
27 # info for all payments, stored or unstored
30 #some false laziness w/MyAccount::process_payment
32 $cgi->param('custnum') =~ /^(\d+)$/
33 or die "illegal custnum ". $cgi->param('custnum');
36 my $cust_main = qsearchs({
37 'table' => 'cust_main',
38 'hashref' => { 'custnum' => $custnum },
39 'extra_sql' => ' AND '. $curuser->agentnums_sql,
40 }) or die "unknown custnum $custnum";
42 my $invoice = ($cgi->param('invoice') =~ /^(\d+)$/) ? $cgi->param('invoice') : '';
44 my $processing_fee = $cgi->param('processing_fee') ? $cgi->param('processing_fee') : '';
46 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
47 or errorpage("illegal amount ". $cgi->param('amount'));
49 errorpage("amount <= 0") unless $amount > 0;
51 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
53 $amount = sprintf('%.2f', $amount + $fee);
56 $cgi->param('payby') =~ /^(CARD|CHEK)$/
57 or errorpage("illegal payby ". $cgi->param('payby'));
60 'CARD' => [ qw( address1 address2 city county state zip country ) ],
61 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
63 my %type = ( 'CARD' => 'credit card',
64 'CHEK' => 'electronic check (ACH)',
67 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
68 or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
71 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
72 or errorpage("illegal balance");
75 $cgi->param('discount_term') =~ /^(\d*)$/
76 or errorpage("illegal discount_term");
77 my $discount_term = $1;
79 my( $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
81 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
84 # use stored cust_payby info
87 $cust_payby = qsearchs('cust_payby', { custnum => $custnum,
88 custpaybynum => $custpaybynum, } )
89 or die "unknown custpaybynum $custpaybynum";
91 # not needed for realtime_bop, but still needed for batch_card
92 $payinfo = $cust_payby->payinfo;
93 $paymask = $cust_payby->paymask;
94 $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
95 ( $month, $year ) = $cust_payby->paydate_mon_year;
96 $payname = $cust_payby->payname;
97 $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
105 $cgi->param('year') =~ /^(\d{4})/
106 or errorpage("illegal year ". $cgi->param('year'));
109 $cgi->param('month') =~ /^(\d{2})/
110 or errorpage("illegal month ". $cgi->param('month'));
113 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
114 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
117 if ( $payby eq 'CHEK' ) {
119 $cgi->param('payinfo1') =~ /^(\d+)$/
120 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
122 $cgi->param('payinfo2') =~ /^(\d+)$/
123 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
125 if ( $conf->config('echeck-country') eq 'CA' ) {
126 $cgi->param('payinfo3') =~ /^(\d{5})$/
127 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
128 $payinfo2 = "$1.$payinfo2";
130 $payinfo = $payinfo1. '@'. $payinfo2;
132 } elsif ( $payby eq 'CARD' ) {
134 $payinfo = $cgi->param('payinfo');
137 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
138 or errorpage(gettext('invalid_card'));
141 or errorpage(gettext('invalid_card'));
143 unless ( $cust_main->tokenized($payinfo) ) { #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 ( length($cgi->param('paycvv') ) ) {
156 if ( cardtype($payinfo) eq 'American Express card' ) {
157 $cgi->param('paycvv') =~ /^(\d{4})$/
158 or errorpage("CVV2 (CID) for American Express cards is four digits.");
161 $cgi->param('paycvv') =~ /^(\d{3})$/
162 or errorpage("CVV2 (CVC2/CID) is three digits.");
165 } elsif ( $conf->exists('backoffice-require_cvv') ){
166 errorpage("CVV2 is required");
170 die "unknown payby $payby";
173 # save first, for proper tokenization
174 if ( $cgi->param('save') ) {
177 if ( $payby eq 'CARD' ) {
178 my $bill_location = FS::cust_location->new;
179 $bill_location->set( $_ => scalar($cgi->param($_)) )
180 foreach @{$payby2fields{$payby}};
181 $saveopt{'bill_location'} = $bill_location;
182 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
183 $saveopt{'paydate'} = "$year-$month-01";
185 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
186 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
189 my $error = $cust_main->save_cust_payby(
190 'saved_cust_payby' => \$cust_payby,
191 'payment_payby' => $payby,
192 'auto' => scalar($cgi->param('auto')),
193 'weight' => scalar($cgi->param('weight')),
194 'payinfo' => $payinfo,
195 'payname' => $payname,
199 errorpage("error saving info, payment not processed: $error")
202 } elsif ( $payby eq 'CARD' ) { # not saving
204 $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
211 # now run the payment
217 if ( $cgi->param('batch') ) {
219 $error = 'Prepayment discounts not supported with batched payments'
222 # Invalid payment expire dates are replaced with 2037-12-01 (why?)
223 my $paydate = "${year}-${month}-01";
227 eval { DateTime->new({ year => $year, month => $month, day => 1 }) };
228 $paydate = '2037-12-01' if $@;
231 $error ||= $cust_main->batch_card(
234 'payinfo' => $payinfo,
235 'paydate' => $paydate,
236 'payname' => $payname,
237 'invnum' => $invoice,
238 'processing-fee' => $processing_fee,
239 map { $_ => scalar($cgi->param($_)) }
240 @{$payby2fields{$payby}}
242 errorpage($error) if $error;
246 $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
247 'cust_payby' => $cust_payby, # if defined, will override passed payinfo, etc
250 'balance' => $balance,
251 'payinfo' => $payinfo,
252 'paymask' => $paymask,
253 'paydate' => "$year-$month-01",
254 'payname' => $payname,
255 'payunique' => $payunique,
257 'paynum_ref' => \$paynum,
258 'discount_term' => $discount_term,
259 'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
261 'invnum' => $invoice,
262 'processing-fee' => $processing_fee,
263 map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
265 errorpage($error) if $error;
267 #no error, so order the fee package if applicable...
268 if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
270 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
272 my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
273 errorpage("payment processed successfully, but error ordering fee: $error")
276 #and generate an invoice for it now too
277 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
278 errorpage("payment processed and fee ordered successfully, but error billing fee: $error")
283 $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');
288 # success! step 3: profit!