RT# 81183 Fix crash processing payment with new payment method
[freeside.git] / httemplate / misc / payment.cgi
1 <& /elements/header-cust_main.html, view=>'payment_history', cust_main=>$cust_main &>
2
3 <h2><% emt("Process [_1] payment",$type{$payby}) %></h2>
4
5 <FORM NAME="OneTrueForm" ACTION="process/payment.cgi" METHOD="POST" onSubmit="document.OneTrueForm.process.disabled=true">
6 <INPUT TYPE="hidden" NAME="custnum"   VALUE="<% $custnum %>">
7 <INPUT TYPE="hidden" NAME="payby"     VALUE="<% $payby %>">
8 <INPUT TYPE="hidden" NAME="payunique" VALUE="<% $payunique %>">
9 <INPUT TYPE="hidden" NAME="balance"   VALUE="<% $balance %>">
10
11 <& /elements/init_overlib.html &>
12
13 <TABLE class="fsinnerbox">
14
15   <& /elements/tr-select-payment_options.html,
16        'cust_main'          => $cust_main,
17        'process-pkgpart'    => 
18           scalar($conf->config('manual_process-pkgpart', $cust_main->agentnum)),
19        'process-display'    => scalar($conf->config('manual_process-display')),
20        'process-skip_first' => $conf->exists('manual_process-skip_first'),
21        'num_payments'       => scalar($cust_main->cust_pay), 
22        'surcharge_percentage' =>
23          ( $payby eq 'CARD'
24              ? scalar($conf->config('credit-card-surcharge-percentage', $cust_main->agentnum))
25              : 0
26          ),
27        'surcharge_flatfee' =>
28          ( $payby eq 'CARD'
29              ? scalar($conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum))
30              : 0
31          ),
32   &>
33
34 % if ( $conf->exists('part_pkg-term_discounts') ) {
35     <& /elements/tr-select-discount_term.html,
36          'custnum'   => $custnum,
37          'amount_id' => 'amount',
38     &>
39 % }
40
41 % my $disallow_no_auto_apply = 0;
42 % if ( $conf->exists("batch-enable")
43 %      || grep $payby eq $_, $conf->config('batch-enable_payby')
44 %    ) {
45 %
46 %     if ( grep $payby eq $_, $conf->config('realtime-disable_payby') ) {
47 %       $disallow_no_auto_apply = 1;
48
49           <INPUT TYPE="hidden" NAME="batch" VALUE="1">
50
51 %     } else {
52
53           <TR>
54             <TH ALIGN="right">&nbsp;&nbsp;&nbsp;<% mt('Add to current batch') |h %></TH>
55             <TD>
56               <INPUT TYPE="checkbox" NAME="batch" VALUE="1" ID="batch_checkbox" ONCHANGE="change_batch_checkbox()">
57             </TD>
58           </TR>
59
60 %     }
61 % }
62
63 % unless ($disallow_no_auto_apply) {
64 %   # false laziness with edit/cust_pay.cgi
65
66 <TR ID="apply_box_row">
67   <TH ALIGN="right"><% mt('Auto-apply to invoices') |h %></TH>
68   <TD>
69     <SELECT NAME="apply" ID="apply_box">
70       <OPTION VALUE="yes" SELECTED><% mt('yes') |h %></OPTION> 
71       <OPTION VALUE=""><% mt('not now') |h %></OPTION>
72       <OPTION VALUE="never"><% mt('never') |h %></OPTION>
73     </SELECT>
74   </TD>
75 </TR>
76
77 % # this can go away if no_auto_apply handling gets added to batch payment processing
78 <SCRIPT>
79 function change_batch_checkbox () {
80   if (document.getElementById('batch_checkbox').checked) {
81     document.getElementById('apply_box').disabled = true;
82     document.getElementById('apply_box_row').style.display = 'none';
83   } else {
84     document.getElementById('apply_box').disabled = false;
85     document.getElementById('apply_box_row').style.display = '';
86   }
87 }
88 </SCRIPT>
89
90 % }
91
92 <SCRIPT TYPE="text/javascript">
93   function cust_payby_changed (what) {
94     var custpaybynum = what.options[what.selectedIndex].value
95     if ( custpaybynum == '' || custpaybynum == '0' ) {
96        //what.form.payinfo.disabled = false;
97        $('#cust_payby').slideDown();
98     } else {
99        //what.form.payinfo.value = '';
100        //what.form.payinfo.disabled = true;
101        $('#cust_payby').slideUp();
102     }
103   }
104
105   function enableAmountField() {
106     document.getElementById('amount').disabled = false;
107   }
108
109 </SCRIPT>
110
111 % #can't quite handle CARD/CHEK on the same page yet, but very close
112 % #does it make sense from a UI/usability perspective?
113 %
114 % my @cust_payby = ();
115 % if ( $payby eq 'CARD' ) {
116 %   @cust_payby = $cust_main->cust_payby('CARD','DCRD');
117 % } elsif ( $payby eq 'CHEK' ) {
118 %   @cust_payby = $cust_main->cust_payby('CHEK','DCHK');
119 % } else {
120 %   die "unknown payby $payby";
121 % }
122 %
123 % my $custpaybynum = length(scalar($cgi->param('custpaybynum')))
124 %                      ? scalar($cgi->param('custpaybynum'))
125 %                      : scalar(@cust_payby) && $cust_payby[0]->custpaybynum;
126
127 <& /elements/tr-select-cust_payby.html,
128      'cust_payby' => \@cust_payby,
129      'curr_value' => $custpaybynum,
130      'onchange'   => 'cust_payby_changed(this)',
131 &>
132
133 </TABLE>
134 <BR>
135 <DIV ID="cust_payby"
136   <% $custpaybynum ? 'STYLE="display:none"'
137                    : ''
138   %>
139 >
140 <TABLE class="fsinnerbox">
141
142 <& /elements/cust_payby_new.html,
143      'cust_payby' => \@cust_payby,
144      'curr_value' => $custpaybynum,
145 &>
146
147 </TABLE>
148 </DIV>
149
150 <BR>
151 <INPUT TYPE="submit" NAME="process" ID="process" VALUE="<% mt('Process payment') |h %>" disabled="disabled" onclick="enableAmountField()">
152 </FORM>
153
154 <SCRIPT TYPE="text/javascript">
155
156 $(document).ready(function (){
157     validate();
158     $('<% $validate_select_fields %>').change(validate);
159     $('<% $validate_input_fields %>').keyup(validate);
160 });
161
162 function validate(){
163     if (
164       $('#amount').val() > 0 && (
165         ( $('#custpaybynum').val() > 0 ) ||
166 % if ($payby eq "CHEK") {
167         ( $('input[name=payinfo1]').val().length > 0 &&
168           $('input[name=payinfo2]').val().length > 0 &&
169           $('input[name=payname]').val().length > 0  &&
170           $('select[name=paytype]').val().length > 0
171         )
172 % }
173 % elsif ($payby eq "CARD") {
174         ( $('input[name=payinfo]').val().length > 0 &&
175           $('input[name=paycvv]').val().length > 0 &&
176           $('input[name=payname]').val().length > 0 &&
177           $('#city').val().length > 0 &&
178           $('#city').val().length > 0 &&
179           $('#state').val().length > 0 &&
180           $('#country').val().length > 0
181         )
182 % }
183       )
184      ) {
185         $("#process").prop("disabled", false);
186     }
187     else {
188         $("#process").prop("disabled", true);
189     }
190 }
191
192 </SCRIPT>
193
194 <& /elements/footer-cust_main.html &>
195 <%once>
196
197 my %weight = (
198   1 => 'Primary',
199   2 => 'Secondary',
200   3 => 'Tertiary',
201   4 => 'Fourth',
202   5 => 'Fifth',
203   6 => 'Sixth',
204   7 => 'Seventh',
205 );
206
207 </%once>
208 <%init>
209
210 die "access denied"
211   unless $FS::CurrentUser::CurrentUser->access_right('Process payment');
212
213 my %type = ( 'CARD' => 'credit card',
214              'CHEK' => 'electronic check (ACH)',
215            );
216
217 $cgi->param('payby') =~ /^(CARD|CHEK)$/
218   or die "unknown payby ". $cgi->param('payby');
219 my $payby = $1;
220
221 my $validate_select_fields = "#payment_option, #invoice, #custpaybynum, ";
222 my $validate_input_fields  = "#amount, input[name=payname], ";
223 if ($payby eq "CHEK") {
224   $validate_input_fields  .= "input[name=payinfo1], input[name=payinfo2]";
225   $validate_select_fields .= "select[name=paytype] ";
226 }
227 elsif ($payby eq "CARD") {
228   $validate_input_fields  .= "input[name=payinfo], input[name=paycvv], input[name=address1], #city, #zip";
229   $validate_select_fields .= "#state, #country ";
230 }
231
232 $cgi->param('custnum') =~ /^(\d+)$/
233   or die "illegal custnum ". $cgi->param('custnum');
234 my $custnum = $1;
235
236 my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } );
237 die "unknown custnum $custnum" unless $cust_main;
238
239 my $balance = $cust_main->balance;
240
241 my $payinfo = '';
242
243 my $conf = new FS::Conf;
244
245 my $payunique = "webui-payment-". time. "-$$-". rand() * 2**32;
246
247 </%init>