--- signup_pay.module	2008-04-02 13:25:22.000000000 -0400
+++ signup_payUPDATE238741.module	2008-04-02 13:27:30.000000000 -0400
@@ -11,6 +11,7 @@ define('SIGNUP_PAY_RECEIPT_EMAIL',      
 define('SIGNUP_PAY_ROLE_WEIGHT',          'signup_pay_role_weight');
 define('SIGNUP_PAY_ENABLED',              'signup_pay_enabled_default');
 define('SIGNUP_PAY_ENABLED_METHODS',      'signup_pay_enebaled_methods');
+define('SIGNUP_PAY_DENY_ANON',            'signup_pay_deny_anon');
 
 // Defaults
 define('SIGNUP_PAY_DEFAULT_ENABLED',      FALSE);
@@ -27,6 +28,7 @@ define('SIGNUP_PAY_DEFAULT_RECEIPT',    
 ');
 define('SIGNUP_PAY_PAGER',                25);
 define('SIGNUP_PAY_DEFAULT_PAID_STATUS_CODE', 0);
+define('SIGNUP_PAY_DEFAULT_DENY_ANON', TRUE);
 
 // Payment methods
 define('SIGNUP_PAY_METHOD_PAYPAL',        1);
@@ -57,6 +59,7 @@ function signup_pay_get_methods($include
     return $methods;
   }
 }
+
 function signup_pay_get_currency() {
   return array(
     'EUR'  => t('Euro'),
@@ -100,9 +103,14 @@ function signup_pay_settings() {
     '#default_value'  => variable_get(SIGNUP_PAY_CURRENCY, SIGNUP_PAY_DEFAULT_CURRENCY),
     '#options'        => signup_pay_get_currency(),
     );
+  $form['signup_pay_general'][SIGNUP_PAY_DENY_ANON] = array(
+    '#type'           => 'checkbox',
+    '#title'          => t('Deny Anonymous access to Paying for an event.'),
+    '#default_value'  => variable_get(SIGNUP_PAY_DENY_ANON,SIGNUP_PAY_DEFAULT_DENY_ANON),
+    );
   //TODO stronger admin features per role for default price and weighting for selecting correct amount
 
-  $roles = user_roles(TRUE, 'sign up for content');
+  $roles = user_roles(variable_get(SIGNUP_PAY_DENY_ANON,SIGNUP_PAY_DEFAULT_DENY_ANON), 'sign up for content');
   $role_description = array();
   $default_role_description = array();
   foreach ($roles as $key => $value) {
@@ -166,11 +174,14 @@ function signup_pay_settings() {
   return system_settings_form($form);
 }
 
-function signup_pay_check_payment($nid = 0) {
+function signup_pay_check_payment($nid = 0, $anon = FALSE) {
   global $user;
-
-  $num_rows = db_result(db_query("SELECT COUNT(*) FROM {signup_pay} WHERE uid = %d and nid = %d", $user->uid, $nid));
-  if ($user->uid && !$num_rows) {
+  $email = $user->mail;
+  if ($anon) {
+    $email = $anon;
+  }
+  $num_rows = db_result(db_query("SELECT COUNT(*) FROM {signup_pay} WHERE uid = %d and nid = %d AND mail = '%s'", $user->uid, $nid, $email));
+  if (!$num_rows) {
     return FALSE;
   }
   return TRUE;
@@ -567,7 +578,12 @@ function signup_pay_paypal_ipn() {
   $name = check_plain($_POST['first_name'] .' '. $_POST['last_name'] . ($_POST['payer_business_name'] ? ' ('. $_POST['payer_business_name'] .')' : ''));
   $gross_amount = check_plain((float)$_POST['mc_gross']);
   $net_amount   = check_plain((float)$_POST['mc_gross'] - (float)$_POST['mc_fee']);
-  $mail         = check_plain($_POST['payer_email']);
+  //TODO test emails and display a message indicating these emails are not the same
+  if (!$custom['mail']) {
+    $mail       = check_plain($_POST['payer_email']);
+  } else {
+    $mail       = $custom['mail'];
+  }
   $currency     = check_plain($_POST['mc_currency']);
   db_query("INSERT INTO {signup_pay} (id, uid, nid, method_id, time_paid, name, mail, currency, net_amount, amount)
     VALUES (0, %d, %d, %d, %d, '%s', '%s', '%s', %f, %f)",
@@ -592,7 +608,8 @@ function signup_pay_paypal_ipn() {
     )));
 
   if (module_exists('signup_status')) {
-    $users = array($custom['uid']);
+    $user = (int)$custom['uid'] ? (int)$custom['uid'] : (int)$custom['uid'].":".$mail;
+    $users = array($user);
     $new_code = variable_get(SIGNUP_PAY_PAID_STATUS_CODE, SIGNUP_PAY_DEFAULT_PAID_STATUS_CODE);
     signup_status_operations($users, $custom['nid'], 'status_code', $new_code);
   }
@@ -635,7 +652,7 @@ function signup_pay_form_alter($form_id,
  * find user role for determining amount and currency
  */
 function signup_pay_find_user_role(&$user) {
-  $role_weight_list = variable_get(SIGNUP_PAY_ROLE_WEIGHT, 0);
+  $role_weight_list = str_replace(" ", "", variable_get(SIGNUP_PAY_ROLE_WEIGHT, 0));
   foreach(explode(",",$role_weight_list) as $rid) {
     if ($user->roles[$rid]) {
       return $rid;
@@ -650,8 +667,8 @@ function signup_pay_find_user_role(&$use
  * alter node edit form to admin role, amount, currency per node
  */
 function signup_pay_alter_node_form($form_id, &$form) {
-  $node = $form['#node'];
-  $roles = user_roles(TRUE, 'sign up for content');
+  $node = $form['#node']; 
+  $roles = user_roles(variable_get(SIGNUP_PAY_DENY_ANON,SIGNUP_PAY_DEFAULT_DENY_ANON), 'sign up for content');
   $form['signup']['node_settings']['settings']['signup_pay_per_role'] = array(
     '#type' => 'fieldset',
     '#title' => t('Signup Amount Per Role'),
@@ -708,7 +725,7 @@ function signup_pay_nodeapi(&$node, $op,
     case 'insert':
     case 'update':
       if ($node->signup_pay_per_role['signup_pay_enabled']) {
-        $roles = user_roles(TRUE, 'sign up for content');
+        $roles = user_roles(variable_get(SIGNUP_PAY_DENY_ANON,SIGNUP_PAY_DEFAULT_DENY_ANON), 'sign up for content');
         $pay_node = db_result(db_query("SELECT amount, currency FROM {signup_pay_node} WHERE nid = %d", $node->nid));
         foreach ($roles as $rid => $role) {
           $pay_role = $node->signup_pay_per_role[$rid];
@@ -747,6 +764,7 @@ function signup_pay_get_node_pay_roles($
 function signup_pay_form_check($form_element) {
   $_SESSION['signup_pay_nid'] = arg(1);
   $_SESSION['signup_pay_method'] = $form_element['#value'];
+  $_SESSION['signup_pay_anon_mail'] = $form_element[1]['#post']['signup_anon_mail'];
   return $form_element;
 }
 
@@ -756,9 +774,10 @@ function signup_pay_do_payment() {
   // Get method and node, from session
   $nid    = $_SESSION['signup_pay_nid'];
   $method = $_SESSION['signup_pay_method'];
+  $anon   = $_SESSION['signup_pay_anon_mail'];
 
   // Check if user has already paid
-  if (signup_pay_check_payment($nid)) {
+  if (signup_pay_check_payment($nid,$anon)) {
     drupal_set_message(t('You have already paid for this event.'));
     return;
   }
@@ -768,6 +787,7 @@ function signup_pay_do_payment() {
     $data = array(
       'uid' => $user->uid,
       'nid' => $nid,
+      'mail' => $anon,
     );
     return signup_pay_do_paypal($data);
   }
