assorted drupal self-service fixes, RT#9380
[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   );
70   $signup_info = signup_info($keys);
71   dkpr($signup_info);
72
73   $form = array();
74
75   $refs = $signup_info['part_referral'];
76   $form['refnum'] = count($refs) > 1 ?
77     array(
78       '#type'   => 'select',
79       '#title'  => t('How did you hear about us?'),
80       '#options'=> array_combine(
81         subextract($refs, 'refnum'), 
82         subextract($refs, 'referral')
83       ),
84       '#default_value'=>$signup_info['refnum'],
85     ) : array (
86       '#type'   => 'hidden',
87       '#value'  => $refs[0]['refnum'],
88     );
89
90   $form['contact'] = array(
91     '#type'   => 'fieldset',
92     '#title'  => t('Contact Information'),
93     'last'    => array( 
94       '#prefix' => '<div class="container-inline">',
95       '#type'   => 'textfield',
96       '#title'  => t('Contact name (last, first)'),
97       '#size'   => 20,
98       '#required' => 1,
99     ),
100     'first'   => array( 
101       '#type'   => 'textfield',
102       '#size'   => 20,
103       '#required' => 1,
104       '#suffix' => '</div>',
105     ),
106     'company' => array(
107       '#type'   => 'textfield',
108       '#title'  => t('Company'),
109       '#size'   => 20,
110     ),
111     'address1'=> array(
112       '#type'   => 'textfield',
113       '#title'  => t('Address'),
114       '#size'   => 30,
115       '#required'=>1,
116     ),
117     'address2'=> array(
118       '#type'   => 'textfield',
119       '#size'   => 30,
120     ),
121     'city'    => array(
122       '#prefix' => '<div class="container-inline">',
123       '#type'   => 'textfield',
124       '#title'  => t('City'),
125       '#size'   => 15,
126       '#required'=>1,
127     ),
128     'state'   => array(
129       '#type'   => 'textfield',
130       '#title'  => t('State'),
131       '#size'   => 2,
132       '#required'=>1,
133       '#default_value'=>$info['statedefault'],
134     ),
135     'zip'     => array(
136       '#type'   => 'textfield',
137       '#title'  => t('Zip'),
138       '#size'   => 10,
139       '#required'=>1,
140       '#suffix' => '</div>',
141     ),
142     'daytime' => array(
143       '#type'   => 'textfield',
144       '#title'  => t('Daytime Phone'),
145       '#size'   => 18,
146     ),
147     'night'   => array(
148       '#type'   => 'textfield',
149       '#title'  => t('Night Phone'),
150       '#size'   => 18,
151     ),
152   );
153
154   $emailinvoiceonly = $signup_info['emailinvoiceonly'];
155
156   $form['billing'] = array(
157     'invoicing_list'  => array(
158       '#type'   => 'textfield',
159       '#title'  => t('Email invoice to'),
160       '#size'   => '40',
161       '#required'=>$emailinvoiceonly,
162     ),
163     '#type'   => 'fieldset',
164     '#title'  => t('Billing Information'),
165     'invoicing_list_POST' => array(
166       '#type'   => $emailinvoiceonly ? 'hidden' : 'checkbox',
167       '#title'  => t('Send a paper invoice'),
168       '#default_value' => 0,
169     ),
170   );
171
172   if( count($signup_info['payby']) > 1 ) {
173     $form['billing']['payby'] = array(
174       '#type'   => 'select',
175       '#title'  => t('Payment method'),
176       '#options'=> array_combine(
177         $signup_info['payby'],
178         $signup_info['payby_longname']
179       ),
180     );
181   }
182   else {
183     $form['billing']['payby'] = array(
184       '#type'   => 'hidden',
185       '#value'  => $signup_info['payby'][0],
186     );
187   }
188   $form['billing']['payby_CARD'] = array(
189     '#type'   => 'fieldset',
190     'cardnum' => array(
191       '#prefix' => '<div class="container-inline">',
192       '#type'   => 'textfield',
193       '#title'  => t('Credit card number'),
194       '#size'   => 20,
195       '#maxlength'=>20,
196       '#required'=>1,
197       '#suffix' => '</div>',
198     ),
199     'expmonth' => array(
200       '#prefix' => '<div class="container-inline">',
201       '#type'   => 'textfield',
202       '#title'  => t('Expiration date'),
203       '#size'   => 2,
204       '#required'=>1,
205       '#maxlength' => 2,
206     ),
207     'expyear' => array(
208       '#field_prefix' => '/',
209       '#type'   => 'textfield',
210       '#size'   => 2,
211       '#maxlength' => 2,
212       '#required'=>1,
213       '#suffix' => '</div>',
214     ),
215     'paycvv'  => array(
216       '#prefix' => '<div class="container-inline">',
217       '#type'   => 'textfield',
218       '#title'  => 'CVV',
219       '#size'   => 3,
220       '#maxlength'  => 3,
221       '#required'=>1,
222       '#suffix' => '</div>',
223     ),
224     'cardname'=> array(
225       '#prefix' => '<div class="container-inline">',
226       '#type'   => 'textfield',
227       '#title'  => t('Exact name on card'),
228       '#size'   => 40,
229       '#maxlength'=>60,
230       '#suffix' => '</div>',
231     ),
232   );
233
234   $pkgs = $signup_info['part_pkg'];
235   $form['package'] = array(
236     '#type'   => 'fieldset',
237     '#title'  => t('First Package'),
238     'pkgpart' => (count($pkgs) > 1 ?
239       array(
240         '#type'   => 'select',
241         '#title'  => '',
242         '#options'=> array_combine(
243           subextract($pkgs, 'pkgpart'),
244           subextract($pkgs, 'pkg')
245         ),
246         '#default_value'=>$signup_info['default_pkgpart'],
247       ) : array (
248         '#type'   => 'hidden',
249         '#value'  => $pkgs[0]['pkgpart'],
250       )
251     ),
252     'username'=> array(
253       '#type'   => 'textfield',
254       '#title'  => t('Username'),
255       '#size'   => 20,
256       '#required'=>1,
257     ),
258     'password'=> array(
259       '#type'   => 'password_confirm',
260       '#size'   => 20,
261       '#required'=>1,
262       '#process'=> array('freeside_expand_password_confirm'),
263     ),
264   );
265   $form['package']['pkgpart']['#default_value'] = $signup_info['default_pkgpart'];
266
267   $form['submit'] = array(
268     '#type'   => 'submit',
269     '#value'  => 'Sign me up!',
270   );
271   return $form;
272 }
273
274 /* workaround for silly Drupal behavior */
275 function freeside_expand_password_confirm($element) {
276   $element = expand_password_confirm($element);
277   $element['pass1']['#attributes']['value'] = $element['#value']['pass1'];
278   $element['pass2']['#attributes']['value'] = $element['#value']['pass2'];
279   return $element;
280 }
281
282 function freeside_signup_form_submit($form, &$form_state) {
283   $freeside = new FreesideSelfService();
284   $values = $form_state['values'];
285   dkpr($values);
286
287   $customer = array();
288   $customer['agentnum'] = variable_get('freeside_agentnum','');
289   foreach( array( 'first',
290                  'last',
291                  'address1',
292                  'address2',
293                  'city',
294                  'state',
295                  'zip',
296                  'daytime',
297                  'night',
298                  'fax',
299                  'payby',
300                  'refnum',
301                  'invoicing_list',
302                  'pkgpart',
303                  'username'
304                ) 
305     as $field ) {
306     $customer[$field] = $values[$field];
307     }
308   if($values['invoicing_list_POST']) {
309     $customer['invoicing_list'] = 
310       implode(',', array($customer['invoicing_list'], 'POST'));
311   }
312   $customer['_password']  = $values['password'];
313   $customer['country']    = 'US';
314   if($customer['payby'] == 'CARD') {
315     $customer['payinfo'] = preg_replace('/\D/','',$values['cardnum']);
316     $customer['paydate'] = $values['expmonth'] . '/' . $values['expyear'];
317     $customer['payname'] = isset($values['cardname']) ? 
318       $values['cardname'] :
319       ($values['first'] . ' ' . $values['last']);
320     $customer['paycvv'] = $values['paycvv'];
321   }
322   /* other paybys not implemented */
323
324   dkpr($customer);
325   $response = $freeside->new_customer($customer);
326   dkpr($response);
327   error_log("[new_customer] received response from Freeside: $response");
328   $error = $response['error'];
329
330   if ( $error ) {
331     drupal_set_message(t("Signup error: $error"), 'error');
332     $form_state['redirect'] = FALSE;
333   }
334   else {
335     drupal_set_message(t("Signup successful!"),'status');
336     $form_state['redirect'] = variable_get('freeside_redirect_after_signup','/');
337   }
338 }
339
340 ?>