2 function freeside_signup() {
3 return drupal_get_form('freeside_signup_form');
7 /* "debug kpr": Krumo-print $var if debugging is on */
9 if(empty($debug)) $debug = variable_get('freeside_debug','');
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);
27 'agentnum' => variable_get('freeside_agentnum',''),
33 $freeside = new FreesideSelfService();
34 $freeside->clear_signup_cache();
35 $info = $freeside->signup_info($packet);
36 cache_set($cid, $info, 'cache', CACHE_TEMPORARY);
41 function subextract($array, $key) {
42 // map { $_->{$key} } (...)
44 foreach ($array as $i) {
50 function freeside_signup_form($form_state) {
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');
59 $freeside = new FreesideSelfService();
61 // all the signup_info that we need
70 $signup_info = signup_info($keys);
75 $refs = $signup_info['part_referral'];
76 $form['refnum'] = count($refs) > 1 ?
79 '#title' => t('How did you hear about us?'),
80 '#options'=> array_combine(
81 subextract($refs, 'refnum'),
82 subextract($refs, 'referral')
84 '#default_value'=>$signup_info['refnum'],
87 '#value' => $refs[0]['refnum'],
90 $form['contact'] = array(
91 '#type' => 'fieldset',
92 '#title' => t('Contact Information'),
94 '#prefix' => '<div class="container-inline">',
95 '#type' => 'textfield',
96 '#title' => t('Contact name (last, first)'),
101 '#type' => 'textfield',
104 '#suffix' => '</div>',
107 '#type' => 'textfield',
108 '#title' => t('Company'),
112 '#type' => 'textfield',
113 '#title' => t('Address'),
118 '#type' => 'textfield',
122 '#prefix' => '<div class="container-inline">',
123 '#type' => 'textfield',
124 '#title' => t('City'),
129 '#type' => 'textfield',
130 '#title' => t('State'),
133 '#default_value'=>$info['statedefault'],
136 '#type' => 'textfield',
137 '#title' => t('Zip'),
140 '#suffix' => '</div>',
143 '#type' => 'textfield',
144 '#title' => t('Daytime Phone'),
148 '#type' => 'textfield',
149 '#title' => t('Night Phone'),
154 $emailinvoiceonly = $signup_info['emailinvoiceonly'];
156 $form['billing'] = array(
157 'invoicing_list' => array(
158 '#type' => 'textfield',
159 '#title' => t('Email invoice to'),
161 '#required'=>$emailinvoiceonly,
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,
172 if( count($signup_info['payby']) > 1 ) {
173 $form['billing']['payby'] = array(
175 '#title' => t('Payment method'),
176 '#options'=> array_combine(
177 $signup_info['payby'],
178 $signup_info['payby_longname']
183 $form['billing']['payby'] = array(
185 '#value' => $signup_info['payby'][0],
188 $form['billing']['payby_CARD'] = array(
189 '#type' => 'fieldset',
191 '#prefix' => '<div class="container-inline">',
192 '#type' => 'textfield',
193 '#title' => t('Credit card number'),
197 '#suffix' => '</div>',
200 '#prefix' => '<div class="container-inline">',
201 '#type' => 'textfield',
202 '#title' => t('Expiration date'),
208 '#field_prefix' => '/',
209 '#type' => 'textfield',
213 '#suffix' => '</div>',
216 '#prefix' => '<div class="container-inline">',
217 '#type' => 'textfield',
222 '#suffix' => '</div>',
225 '#prefix' => '<div class="container-inline">',
226 '#type' => 'textfield',
227 '#title' => t('Exact name on card'),
230 '#suffix' => '</div>',
234 $pkgs = $signup_info['part_pkg'];
235 $form['package'] = array(
236 '#type' => 'fieldset',
237 '#title' => t('First Package'),
238 'pkgpart' => (count($pkgs) > 1 ?
242 '#options'=> array_combine(
243 subextract($pkgs, 'pkgpart'),
244 subextract($pkgs, 'pkg')
246 '#default_value'=>$signup_info['default_pkgpart'],
249 '#value' => $pkgs[0]['pkgpart'],
253 '#type' => 'textfield',
254 '#title' => t('Username'),
259 '#type' => 'password_confirm',
262 '#process'=> array('freeside_expand_password_confirm'),
265 $form['package']['pkgpart']['#default_value'] = $signup_info['default_pkgpart'];
267 $form['submit'] = array(
269 '#value' => 'Sign me up!',
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'];
282 function freeside_signup_form_submit($form, &$form_state) {
283 $freeside = new FreesideSelfService();
284 $values = $form_state['values'];
288 $customer['agentnum'] = variable_get('freeside_agentnum','');
289 foreach( array( 'first',
306 $customer[$field] = $values[$field];
308 if($values['invoicing_list_POST']) {
309 $customer['invoicing_list'] =
310 implode(',', array($customer['invoicing_list'], 'POST'));
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'];
322 /* other paybys not implemented */
325 $response = $freeside->new_customer($customer);
327 error_log("[new_customer] received response from Freeside: $response");
328 $error = $response['error'];
331 drupal_set_message(t("Signup error: $error"), 'error');
332 $form_state['redirect'] = FALSE;
335 drupal_set_message(t("Signup successful!"),'status');
336 $form_state['redirect'] = variable_get('freeside_redirect_after_signup','/');