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','');
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);
25 $freeside = new FreesideSelfService();
26 $info = $freeside->signup_info($packet);
27 cache_set($cid, $info, 'cache', CACHE_TEMPORARY);
32 $freeside = new FreesideSelfService();
33 return $freeside->signup_info($packet);
37 function subextract($array, $key) {
38 // map { $_->{$key} } (...)
40 foreach ($array as $i) {
46 function freeside_signup_form($form_state) {
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');
54 $freeside = new FreesideSelfService();
56 'agentnum' => $agentnum,
60 $signup_info = signup_info($packet);
65 $refs = $signup_info['part_referral'];
66 $form['refnum'] = count($refs) > 1 ?
69 '#title' => t('How did you hear about us?'),
70 '#options'=> array_combine(
71 subextract($refs, 'refnum'),
72 subextract($refs, 'referral')
74 '#default_value'=>$signup_info['refnum'],
77 '#value' => $refs[0]['refnum'],
80 $form['contact'] = array(
81 '#type' => 'fieldset',
82 '#title' => t('Contact Information'),
84 '#prefix' => '<div class="container-inline">',
85 '#type' => 'textfield',
86 '#title' => t('Contact name (last, first)'),
91 '#type' => 'textfield',
94 '#suffix' => '</div>',
97 '#type' => 'textfield',
98 '#title' => t('Company'),
102 '#type' => 'textfield',
103 '#title' => t('Address'),
108 '#type' => 'textfield',
112 '#prefix' => '<div class="container-inline">',
113 '#type' => 'textfield',
114 '#title' => t('City'),
119 '#type' => 'textfield',
120 '#title' => t('State'),
123 '#default_value'=>$info['statedefault'],
126 '#type' => 'textfield',
127 '#title' => t('Zip'),
130 '#suffix' => '</div>',
133 '#type' => 'textfield',
134 '#title' => t('Daytime Phone'),
138 '#type' => 'textfield',
139 '#title' => t('Night Phone'),
144 $emailinvoiceonly = $signup_info['emailinvoiceonly'];
146 $form['billing'] = array(
147 'invoicing_list' => array(
148 '#type' => 'textfield',
149 '#title' => t('Email invoice to'),
151 '#required'=>$emailinvoiceonly,
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,
162 if( count($signup_info['payby']) > 1 ) {
163 $form['billing']['payby'] = array(
165 '#title' => t('Payment method'),
166 '#options'=> array_combine(
167 $signup_info['payby'],
168 $signup_info['payby_longname']
173 $form['billing']['payby'] = array(
175 '#value' => $signup_info['payby'][0],
178 $form['billing']['payby_CARD'] = array(
179 '#type' => 'fieldset',
181 '#prefix' => '<div class="container-inline">',
182 '#type' => 'textfield',
183 '#title' => t('Credit card number'),
187 '#suffix' => '</div>',
190 '#prefix' => '<div class="container-inline">',
191 '#type' => 'textfield',
192 '#title' => t('Expiration date'),
198 '#field_prefix' => '/',
199 '#type' => 'textfield',
203 '#suffix' => '</div>',
206 '#prefix' => '<div class="container-inline">',
207 '#type' => 'textfield',
212 '#suffix' => '</div>',
215 '#prefix' => '<div class="container-inline">',
216 '#type' => 'textfield',
217 '#title' => t('Exact name on card'),
220 '#suffix' => '</div>',
224 $pkgs = $signup_info['part_pkg'];
225 $form['package'] = array(
226 '#type' => 'fieldset',
227 '#title' => t('First Package'),
228 'pkgpart' => (count($pkgs) > 1 ?
232 '#options'=> array_combine(
233 subextract($pkgs, 'pkgpart'),
234 subextract($pkgs, 'pkg')
236 '#default_value'=>$signup_info['default_pkgpart'],
239 '#value' => $pkgs[0]['pkgpart'],
243 '#type' => 'textfield',
244 '#title' => t('Username'),
249 '#type' => 'password_confirm',
254 $form['package']['pkgpart']['#default_value'] = $signup_info['default_pkgpart'];
256 $form['submit'] = array(
258 '#value' => 'Sign me up!',
263 function freeside_signup_form_submit($form, &$form_state) {
264 $freeside = new FreesideSelfService();
265 $values = $form_state['values'];
269 $customer['agentnum'] = variable_get('freeside_agentnum','');
270 foreach( array( 'first',
286 $customer[$field] = $values[$field];
288 if($values['invoicing_list_POST']) {
289 $customer['invoicing_list'] =
290 implode(',', array($customer['invoicing_list'], 'POST'));
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'];
302 /* other paybys not implemented */
305 $response = $freeside->new_customer($customer);
307 error_log("[new_customer] received response from Freeside: $response");
308 $error = $response['error'];
310 drupal_set_message(t("Signup error: $error"), 'error');
311 $form_state['redirect'] = FALSE;
314 drupal_set_message(t("Signup successful!"),'status');