+$cgi->param('discount_term') =~ /^(\d*)$/
+ or errorpage("illegal discount_term");
+my $discount_term = $1;
+
+# save first, for proper tokenization later
+if ( $cgi->param('save') ) {
+
+ my %saveopt;
+ if ( $payby eq 'CARD' ) {
+ my $bill_location = FS::cust_location->new;
+ $bill_location->set( $_ => $cgi->param($_) )
+ foreach @{$payby2fields{$payby}};
+ $saveopt{'bill_location'} = $bill_location;
+ $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
+ $saveopt{'paydate'} = "$year-$month-01";
+ } else {
+ # ss/stateid/stateid_state won't be saved, but should be harmless to pass
+ %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
+ }
+
+ my $error = $cust_main->save_cust_payby(
+ 'payment_payby' => $payby,
+ 'auto' => scalar($cgi->param('auto')),
+ 'payinfo' => $payinfo,
+ 'paymask' => $paymask,
+ 'payname' => $payname,
+ 'paytype => $paytype,
+ %saveopt
+ );
+
+ errorpage("error saving info, payment not processed: $error")
+ if $error;
+}
+