This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / fs_selfservice / drupal / signup.inc
1 <?php
2 function freeside_signup() {
3   return drupal_get_form('freeside_signup_form');
4 }
5
6 function dkpr($var) {
7   /* "debug kpr": Krumo-print $var if debugging is on */
8   static $debug;
9   if(empty($debug)) $debug = variable_get('freeside_debug','');
10   if($debug) {
11     kpr($var);
12   }
13 }
14
15 function signup_info($keys) {
16   /* local cache, because transporting the entire signup_info
17     through XML-RPC is incredibly slow.  If you change the config,
18     you can flush the local cache with the "Clear cached data" 
19     button on the Drupal "Performance" menu. */
20   $cid = 'FS_signup_info';
21   $info = cache_get($cid);
22   if($info) {
23     return($info->data);
24   }
25   else {
26     $packet = array(
27       'agentnum'    => variable_get('freeside_agentnum',''),
28       'promo_code'  => '',
29       'reg_code'    => '',
30       'keys'        => $keys,
31     );
32
33     $freeside = new FreesideSelfService(); 
34     $freeside->clear_signup_cache();
35     $info = $freeside->signup_info($packet);
36     cache_set($cid, $info, 'cache', CACHE_TEMPORARY);
37     return($info);
38   }
39 }
40
41 function subextract($array, $key) {
42   // map { $_->{$key} } (...)
43   $out = array();
44   foreach ($array as $i) {
45     $out[] = $i[$key];
46   }
47   return $out;
48 }
49
50 function freeside_signup_form($form_state) {
51   dkpr($form_state);
52
53   $agentnum = variable_get('freeside_agentnum','');
54   if( !$agentnum || !(variable_get('freeside_hostname','')) ) {
55     drupal_set_message(t('Freeside self-service is not yet configured.'),'error');
56     return array();
57   }
58
59   $freeside = new FreesideSelfService();
60   $keys = array(
61     // all the signup_info that we need
62     'part_referral',
63     'refnum',
64     'emailinvoiceonly',
65     'payby',
66     'payby_longname',
67     'part_pkg',
68     'default_pkgpart',
69     'signup_service',
70   );
71   $signup_info = signup_info($keys);
72   dkpr($signup_info);
73
74   $form = array();
75
76   $refs = $signup_info['part_referral'];
77   $form['refnum'] = count($refs) > 1 ?
78     array(
79       '#type'   => 'select',
80       '#title'  => t('How did you hear about us?'),
81       '#options'=> array_combine(
82         subextract($refs, 'refnum'), 
83         subextract($refs, 'referral')
84       ),
85       '#default_value'=>$signup_info['refnum'],
86     ) : array (
87       '#type'   => 'hidden',
88       '#value'  => $refs[0]['refnum'],
89     );
90
91   $form['contact'] = array(
92     '#type'   => 'fieldset',
93     '#title'  => t('Contact Information'),
94     'last'    => array( 
95       '#prefix' => '<div class="container-inline">',
96       '#type'   => 'textfield',
97       '#title'  => t('Contact name (last, first)'),
98       '#size'   => 20,
99       '#required' => 1,
100     ),
101     'first'   => array( 
102       '#type'   => 'textfield',
103       '#size'   => 20,
104       '#required' => 1,
105       '#suffix' => '</div>',
106     ),
107     'company' => array(
108       '#type'   => 'textfield',
109       '#title'  => t('Company'),
110       '#size'   => 20,
111     ),
112     'address1'=> array(
113       '#type'   => 'textfield',
114       '#title'  => t('Address'),
115       '#size'   => 30,
116       '#required'=>1,
117     ),
118     'address2'=> array(
119       '#type'   => 'textfield',
120       '#size'   => 30,
121     ),
122     'city'    => array(
123       '#prefix' => '<div class="container-inline">',
124       '#type'   => 'textfield',
125       '#title'  => t('City'),
126       '#size'   => 15,
127       '#required'=>1,
128     ),
129     'state'   => array(
130       '#type'   => 'textfield',
131       '#title'  => t('State'),
132       '#size'   => 2,
133       '#required'=>1,
134       '#default_value'=>$info['statedefault'],
135     ),
136     'zip'     => array(
137       '#type'   => 'textfield',
138       '#title'  => t('Zip'),
139       '#size'   => 10,
140       '#required'=>1,
141       '#suffix' => '</div>',
142     ),
143     'daytime' => array(
144       '#type'   => 'textfield',
145       '#title'  => t('Daytime Phone'),
146       '#size'   => 18,
147     ),
148     'night'   => array(
149       '#type'   => 'textfield',
150       '#title'  => t('Night Phone'),
151       '#size'   => 18,
152     ),
153   );
154
155   $emailinvoiceonly = $signup_info['emailinvoiceonly'];
156
157   $form['billing'] = array(
158     'invoicing_list'  => array(
159       '#type'   => 'textfield',
160       '#title'  => t('Email invoice to'),
161       '#size'   => '40',
162       '#required'=>$emailinvoiceonly,
163     ),
164     '#type'   => 'fieldset',
165     '#title'  => t('Billing Information'),
166     'invoicing_list_POST' => array(
167       '#type'   => $emailinvoiceonly ? 'hidden' : 'checkbox',
168       '#title'  => t('Send a paper invoice'),
169       '#default_value' => 0,
170     ),
171   );
172
173   if( count($signup_info['payby']) > 1 ) {
174     $form['billing']['payby'] = array(
175       '#type'   => 'select',
176       '#title'  => t('Payment method'),
177       '#options'=> array_combine(
178         $signup_info['payby'],
179         $signup_info['payby_longname']
180       ),
181     );
182   }
183   else {
184     $form['billing']['payby'] = array(
185       '#type'   => 'hidden',
186       '#value'  => $signup_info['payby'][0],
187     );
188   }
189   $form['billing']['payby_CARD'] = array(
190     '#type'   => 'fieldset',
191     'cardnum' => array(
192       '#prefix' => '<div class="container-inline">',
193       '#type'   => 'textfield',
194       '#title'  => t('Credit card number'),
195       '#size'   => 20,
196       '#maxlength'=>20,
197       '#required'=>1,
198       '#suffix' => '</div>',
199     ),
200     'expmonth' => array(
201       '#prefix' => '<div class="container-inline">',
202       '#type'   => 'textfield',
203       '#title'  => t('Expiration date'),
204       '#size'   => 2,
205       '#required'=>1,
206       '#maxlength' => 2,
207     ),
208     'expyear' => array(
209       '#field_prefix' => '/',
210       '#type'   => 'textfield',
211       '#size'   => 2,
212       '#maxlength' => 2,
213       '#required'=>1,
214       '#suffix' => '</div>',
215     ),
216     'paycvv'  => array(
217       '#prefix' => '<div class="container-inline">',
218       '#type'   => 'textfield',
219       '#title'  => 'CVV',
220       '#size'   => 3,
221       '#maxlength'  => 3,
222       '#required'=>1,
223       '#suffix' => '</div>',
224     ),
225     'cardname'=> array(
226       '#prefix' => '<div class="container-inline">',
227       '#type'   => 'textfield',
228       '#title'  => t('Exact name on card'),
229       '#size'   => 40,
230       '#maxlength'=>60,
231       '#suffix' => '</div>',
232     ),
233   );
234
235   $pkgs = $signup_info['part_pkg'];
236   $form['package'] = array(
237     '#type'   => 'fieldset',
238     '#title'  => t('First Package'),
239     'pkgpart' => (count($pkgs) > 1 ?
240       array(
241         '#type'   => 'select',
242         '#title'  => '',
243         '#options'=> array_combine(
244           subextract($pkgs, 'pkgpart'),
245           subextract($pkgs, 'pkg')
246         ),
247         '#default_value'=>$signup_info['default_pkgpart'],
248       ) : array (
249         '#type'   => 'hidden',
250         '#value'  => $pkgs[0]['pkgpart'],
251       )
252     ),
253   );
254
255   switch($signup_info['signup_service']) {
256   case 'svc_acct':
257     $form['package'] += array(
258       'username'=> array(
259         '#type'   => 'textfield',
260         '#title'  => t('Username'),
261         '#size'   => 20,
262         '#required'=>1,
263       ),
264       'password'=> array(
265         '#type'   => 'password_confirm',
266         '#size'   => 20,
267         '#required'=>1,
268         '#process'=> array('freeside_expand_password_confirm'),
269       )
270     );
271     break;
272   case 'svc_pbx':
273     break; // nothing yet implemented
274   }
275   $form['package']['pkgpart']['#default_value'] = $signup_info['default_pkgpart'];
276
277   $form['submit'] = array(
278     '#type'   => 'submit',
279     '#value'  => 'Sign me up!',
280   );
281   return $form;
282 }
283
284 /* workaround for silly Drupal behavior */
285 function freeside_expand_password_confirm($element) {
286   $element = expand_password_confirm($element);
287   $element['pass1']['#attributes']['value'] = $element['#value']['pass1'];
288   $element['pass2']['#attributes']['value'] = $element['#value']['pass2'];
289   return $element;
290 }
291
292 function freeside_signup_form_submit($form, &$form_state) {
293   $freeside = new FreesideSelfService();
294   $values = $form_state['values'];
295   dkpr($values);
296
297   $customer = array();
298   $customer['agentnum'] = variable_get('freeside_agentnum','');
299   foreach( array( 'first',
300                  'last',
301                  'address1',
302                  'address2',
303                  'city',
304                  'state',
305                  'zip',
306                  'daytime',
307                  'night',
308                  'fax',
309                  'payby',
310                  'refnum',
311                  'invoicing_list',
312                  'pkgpart',
313                  'username'
314                ) 
315     as $field ) {
316     $customer[$field] = $values[$field];
317     }
318   if($values['invoicing_list_POST']) {
319     $customer['invoicing_list'] = 
320       implode(',', array($customer['invoicing_list'], 'POST'));
321   }
322   $customer['_password']  = $values['password'];
323   $customer['country']    = 'US';
324   if($customer['payby'] == 'CARD') {
325     $customer['payinfo'] = preg_replace('/\D/','',$values['cardnum']);
326     $customer['paydate'] = $values['expmonth'] . '/' . $values['expyear'];
327     $customer['payname'] = isset($values['cardname']) ? 
328       $values['cardname'] :
329       ($values['first'] . ' ' . $values['last']);
330     $customer['paycvv'] = $values['paycvv'];
331   }
332   /* other paybys not implemented */
333
334   dkpr($customer);
335   $response = $freeside->new_customer($customer);
336   dkpr($response);
337   error_log("[new_customer] received response from Freeside: $response");
338   $error = $response['error'];
339
340   if ( $error ) {
341     drupal_set_message(t("Signup error: $error"), 'error');
342     $form_state['redirect'] = FALSE;
343   }
344   else {
345     drupal_set_message(t("Signup successful!"),'status');
346     $form_state['redirect'] = array(
347       variable_get('freeside_redirect_after_signup','/'),
348       //query string would go here
349       //'custnum='.$response['custnum'].'&svcnum='.$response['svcnum'],
350     );
351   }
352 }
353
354 ?>