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