add manual_process-display config to subtract fee from amount instead of adding,...
[freeside.git] / httemplate / misc / payment.cgi
1 <% include( '/elements/header.html', "Process $type{$payby} payment" ) %>
2 <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(2) . "view/cust_main.cgi" ) %>
3 <FORM NAME="OneTrueForm" ACTION="process/payment.cgi" METHOD="POST" onSubmit="document.OneTrueForm.process.disabled=true">
4 <INPUT TYPE="hidden" NAME="custnum"   VALUE="<% $custnum %>">
5 <INPUT TYPE="hidden" NAME="payby"     VALUE="<% $payby %>">
6 <INPUT TYPE="hidden" NAME="payunique" VALUE="<% $payunique %>">
7 <INPUT TYPE="hidden" NAME="balance"   VALUE="<% $balance %>">
8
9 <% include('/elements/init_overlib.html') %>
10
11 % #include( '/elements/table.html', '#cccccc' ) 
12
13 <% ntable('#cccccc') %>
14   <TR>
15     <TH ALIGN="right">Payment amount</TH>
16     <TD COLSPAN=7>
17       <TABLE><TR><TD BGCOLOR="#ffffff">
18         <% $money_char %><INPUT NAME     = "amount"
19                                 TYPE     = "text"
20                                 VALUE    = "<% $amount %>"
21                                 SIZE     = 8
22                                 STYLE    = "text-align:right;"
23 %                               if ( $fee ) {
24                                   onChange   = "amount_changed(this)"
25                                   onKeyDown  = "amount_changed(this)"
26                                   onKeyUp    = "amount_changed(this)"
27                                   onKeyPress = "amount_changed(this)"
28 %                               }
29                          >
30       </TD><TD BGCOLOR="#cccccc">
31 %        if ( $fee ) {
32            <INPUT TYPE="hidden" NAME="fee_pkgpart" VALUE="<% $fee_pkg->pkgpart %>">
33            <INPUT TYPE="hidden" NAME="fee" VALUE="<% $fee_display eq 'add' ? $fee : '' %>">
34            <B><FONT SIZE='+1'><% $fee_op %></FONT>
35               <% $money_char . $fee %>
36            </B>
37            <% $fee_pkg->pkg |h %>
38            <B><FONT SIZE='+1'>=</FONT></B>
39       </TD><TD ID="ajax_total_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
40            <FONT SIZE="+1"><% length($amount) ? $money_char. sprintf('%.2f', ($fee_display eq 'add') ? $amount + $fee : $amount - $fee ) : '' %> <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>
41   
42 %        }
43       </TD></TR></TABLE>
44     </TD>
45   </TR>
46
47 % if ( $fee ) {
48
49     <SCRIPT TYPE="text/javascript">
50
51       function amount_changed(what) {
52
53
54         var total = '';
55         if ( what.value.length ) {
56           total = parseFloat(what.value) <% $fee_op %> <% $fee %>;
57           /* total = Math.round(total*100)/100; */
58           total = '<% $money_char %>' + total.toFixed(2);
59         }
60
61         var total_cell = document.getElementById('ajax_total_cell');
62         total_cell.innerHTML = '<FONT SIZE="+1">' + total + ' <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>';
63
64       }
65
66     </SCRIPT>
67
68 % }
69
70
71 % if ( $payby eq 'CARD' ) {
72 %
73 %   my( $payinfo, $paycvv, $month, $year ) = ( '', '', '', '' );
74 %   my $payname = $cust_main->first. ' '. $cust_main->getfield('last');
75 %   if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
76 %     $payinfo = $cust_main->paymask;
77 %     $paycvv = $cust_main->paycvv;
78 %     ( $month, $year ) = $cust_main->paydate_monthyear;
79 %     $payname = $cust_main->payname if $cust_main->payname;
80 %   }
81
82     <TR>
83       <TH ALIGN="right">Card&nbsp;number</TH>
84       <TD COLSPAN=7>
85         <TABLE>
86           <TR>
87             <TD>
88               <INPUT TYPE="text" NAME="payinfo" SIZE=20 MAXLENGTH=19 VALUE="<%$payinfo%>"> </TD>
89             <TH>Exp.</TH>
90             <TD>
91               <SELECT NAME="month">
92 % for ( ( map "0$_", 1 .. 9 ), 10 .. 12 ) { 
93
94                   <OPTION<% $_ == $month ? ' SELECTED' : '' %>><% $_ %>
95 % } 
96
97               </SELECT>
98             </TD>
99             <TD> / </TD>
100             <TD>
101               <SELECT NAME="year">
102 % my @a = localtime; for ( $a[5]+1900 .. $a[5]+1915 ) { 
103
104                   <OPTION<% $_ == $year ? ' SELECTED' : '' %>><% $_ %>
105 % } 
106
107               </SELECT>
108             </TD>
109           </TR>
110         </TABLE>
111       </TD>
112     </TR>
113     <TR>
114       <TH ALIGN="right">CVV2</TH>
115       <TD><INPUT TYPE="text" NAME="paycvv" VALUE="<% $paycvv %>" SIZE=4 MAXLENGTH=4>
116           (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('../docs/cvv2.html', 480, 352, 'cvv2_popup' ), CAPTION, 'CVV2 Help', STICKY, AUTOSTATUSCAP, CLOSECLICK, DRAGGABLE ); return false;">help</A>)
117       </TD>
118     </TR>
119     <TR>
120       <TH ALIGN="right">Exact&nbsp;name&nbsp;on&nbsp;card</TH>
121       <TD><INPUT TYPE="text" SIZE=32 MAXLENGTH=80 NAME="payname" VALUE="<%$payname%>"></TD>
122     </TR>
123
124     <% include( '/elements/location.html',
125                   'object'         => $cust_main, #XXX errors???
126                   'no_asterisks'   => 1,
127                   'address1_label' => 'Card billing address',
128               )
129     %>
130
131 % } elsif ( $payby eq 'CHEK' ) {
132 %
133 %   my( $payinfo1, $payinfo2, $payname, $ss, $paytype, $paystate,
134 %       $stateid, $stateid_state )
135 %     = ( '', '', '', '', '', '', '', '' );
136 %   if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
137 %     $cust_main->paymask =~ /^([\dx]+)\@([\dx]+)$/i
138 %       or die "unparsable payinfo ". $cust_main->payinfo;
139 %     ($payinfo1, $payinfo2) = ($1, $2);
140 %     $payname = $cust_main->payname;
141 %     $ss = $cust_main->ss;
142 %     $paytype = $cust_main->getfield('paytype');
143 %     $paystate = $cust_main->getfield('paystate');
144 %     $stateid = $cust_main->getfield('stateid');
145 %     $stateid_state = $cust_main->getfield('stateid_state');
146 %   }
147
148     <INPUT TYPE="hidden" NAME="month" VALUE="12">
149     <INPUT TYPE="hidden" NAME="year" VALUE="2037">
150     <TR>
151       <TD ALIGN="right">Account&nbsp;number</TD>
152       <TD><INPUT TYPE="text" SIZE=10 NAME="payinfo1" VALUE="<%$payinfo1%>"></TD>
153       <TD ALIGN="right">Type</TD>
154       <TD><SELECT NAME="paytype"><% join('', map { qq!<OPTION VALUE="$_" !.($paytype eq $_ ? 'SELECTED' : '').">$_</OPTION>" } @FS::cust_main::paytypes) %></SELECT></TD>
155     </TR>
156     <TR>
157       <TD ALIGN="right">ABA/Routing&nbsp;number</TD>
158       <TD>
159         <INPUT TYPE="text" SIZE=10 MAXLENGTH=9 NAME="payinfo2" VALUE="<%$payinfo2%>">
160         (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('../docs/ach.html', 380, 240, 'ach_popup' ), CAPTION, 'ACH Help', STICKY, AUTOSTATUSCAP, CLOSECLICK, DRAGGABLE ); return false;">help</A>)
161       </TD>
162     </TR>
163     <TR>
164       <TD ALIGN="right">Bank&nbsp;name</TD>
165       <TD><INPUT TYPE="text" NAME="payname" VALUE="<%$payname%>"></TD>
166     </TR>
167
168 %   if ( $conf->exists('show_bankstate') ) {
169       <TR>
170         <TD ALIGN="right">Bank&nbsp;state</TD>
171         <TD><% include('/elements/select-state.html',
172                          'disable_empty' => 0,
173                          'empty_label'   => '(choose)',
174                          'state'         => $paystate,
175                          'country'       => $cust_main->country,
176                          'prefix'        => 'pay',
177                       )
178             %>
179         </TD>
180       </TR>
181 %   } else {
182       <INPUT TYPE="hidden" NAME="paystate" VALUE="<% $paystate %>">
183 %   }
184
185 %   if ( $conf->exists('show_ss') ) {
186       <TR>
187         <TD ALIGN="right">
188           Account&nbsp;holder<BR>
189           Social&nbsp;security&nbsp;or&nbsp;tax&nbsp;ID&nbsp;#
190         </TD>
191         <TD><INPUT TYPE="text" NAME="ss" VALUE="<% $ss %>"></TD>
192       </TR>
193 %   } else {
194       <INPUT TYPE="hidden" NAME="ss" VALUE="<% $ss %>"></TD>
195 %   }
196
197 %   if ( $conf->exists('show_stateid') ) {
198       <TR>
199         <TD ALIGN="right">
200           Account&nbsp;holder<BR>
201           Driver&rsquo;s&nbsp;license&nbsp;or&nbsp;state&nbsp;ID&nbsp;#
202         </TD>
203         <TD><INPUT TYPE="text" NAME="stateid" VALUE="<% $stateid %>"></TD>
204         <TD ALIGN="right">State</TD>
205         <TD><% include('/elements/select-state.html',
206                          'disable_empty' => 0,
207                          'empty_label'   => '(choose)',
208                          'state'         => $stateid_state,
209                          'country'       => $cust_main->country,
210                          'prefix'        => 'stateid_',
211                       )
212             %>
213         </TD>
214       </TR>
215 %   } else {
216       <INPUT TYPE="hidden" NAME="stateid" VALUE="<% $stateid %>">
217       <INPUT TYPE="hidden" NAME="stateid_state" VALUE="<% $stateid_state %>">
218 %   }
219
220 % } #end CARD/CHEK-specific section
221
222
223 <TR>
224   <TD COLSPAN=2>
225     <INPUT TYPE="checkbox" CHECKED NAME="save" VALUE="1">
226     Remember this information
227   </TD>
228 </TR>
229
230 % if ( $conf->exists("batch-enable")
231 %      || grep $payby eq $_, $conf->config('batch-enable_payby')
232 %    ) {
233 %
234 %     if ( grep $payby eq $_, $conf->config('realtime-disable_payby') ) {
235
236           <INPUT TYPE="hidden" NAME="batch" VALUE="1">
237
238 %     } else {
239
240           <TR>
241             <TD COLSPAN=2>
242               <INPUT TYPE="checkbox" NAME="batch" VALUE="1">
243               Add to current batch
244             </TD>
245           </TR>
246
247 %     }
248 % }
249
250 <TR>
251   <TD COLSPAN=2>
252     <INPUT TYPE="checkbox"<% ( ( $payby eq 'CARD' && $cust_main->payby ne 'DCRD' ) || ( $payby eq 'CHEK' && $cust_main->payby eq 'CHEK' ) ) ? ' CHECKED' : '' %> NAME="auto" VALUE="1" onClick="if (this.checked) { document.OneTrueForm.save.checked=true; }">
253     Charge future payments to this <% $type{$payby} %> automatically
254   </TD>
255 </TR>
256
257 </TABLE>
258
259 <BR>
260 <INPUT TYPE="submit" NAME="process" VALUE="Process payment">
261 </FORM>
262
263 <% include('/elements/footer.html') %>
264 <%init>
265
266 die "access denied"
267   unless $FS::CurrentUser::CurrentUser->access_right('Process payment');
268
269 my %type = ( 'CARD' => 'credit card',
270              'CHEK' => 'electronic check (ACH)',
271            );
272
273 $cgi->param('payby') =~ /^(CARD|CHEK)$/
274   or die "unknown payby ". $cgi->param('payby');
275 my $payby = $1;
276
277 $cgi->param('custnum') =~ /^(\d+)$/
278   or die "illegal custnum ". $cgi->param('custnum');
279 my $custnum = $1;
280
281 my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } );
282 die "unknown custnum $custnum" unless $cust_main;
283
284 my $balance = $cust_main->balance;
285
286 my $payinfo = '';
287
288 my $conf = new FS::Conf;
289
290 my $money_char = $conf->config('money_char') || '$';
291
292 #false laziness w/selfservice make_payment.html shortcut for one-country
293 my %states = map { $_->state => 1 }
294                qsearch('cust_main_county', {
295                  'country' => $conf->config('countrydefault') || 'US'
296                } );
297 my @states = sort { $a cmp $b } keys %states;
298
299 my $fee = '';
300 my $fee_pkg = '';
301 my $fee_display = '';
302 my $fee_op = '';
303 if ( $conf->config('manual_process-pkgpart') ) {
304
305   $fee_display = $conf->config('manual_process-display') || 'add';
306   $fee_op = $fee_display eq 'add' ? '+' : '-';
307
308   $fee_pkg =
309     qsearchs('part_pkg', { pkgpart=>$conf->config('manual_process-pkgpart') } );
310
311   #well ->unit_setup or ->calc_setup both call for a $cust_pkg
312   # (though ->unit_setup doesn't use it...)
313   $fee = $fee_pkg->option('setup_fee')
314     if $fee_pkg; #in case.. better than dying with a perl traceback
315
316 }
317
318 my $amount = '';
319 if ( $balance > 0 ) {
320   $amount = $balance;
321   $amount += $fee
322     if $fee && $fee_display eq 'subtract';
323   $amount = sprintf("%.2f", $amount);
324 }
325
326 my $payunique = "webui-payment-". time. "-$$-". rand() * 2**32;
327
328 </%init>