--- signup_pay.module	2008-03-20 14:09:22.000000000 -0400
+++ signup_pay.moduleUPDATE2	2008-03-20 14:14:36.000000000 -0400
@@ -84,26 +84,38 @@ function signup_pay_settings() {
     '#default_value'  => variable_get(SIGNUP_PAY_RECEIPT_EMAIL, SIGNUP_PAY_DEFAULT_RECEIPT),
     '#description' => t('Template for receipt when emailed to the user. You can use any HTML, including the following tokens Date: @date, Payment method: @method, Amount: @amount, Name: @name, Email: @mail'),
     );
-
   if (module_exists('signup_status')) {
     $access = user_access('administer site configuration');
     if ($access) {
       $create_link = l("Insert New status code", "admin/settings/signup_status/add", array(), "destination=admin/settings/signup_pay");
     }
-
     $options = array();
     foreach (signup_status_codes() as $cid => $code) {
       $options[$cid] = $code['name'];
     }
-
     $form[SIGNUP_PAY_PAID_STATUS_CODE] = array(
       '#type'           => 'select',
       '#title'          => t('Paid Status Code'),
       '#options'        => $options,
       '#default_value'  => variable_get(SIGNUP_PAY_PAID_STATUS_CODE, SIGNUP_PAY_DEFAULT_PAID_STATUS_CODE),
-      '#description'    => t('Select the status code which will be used when a payment transaction is completed successfully. '.$create_link),
+      '#description'    => t('Select the status code which will be used when a payment transaction is completed successfully. @link', array('@link' => $create_link)),
     );
   }
+  //TODO stronger admin features per role for default price and weighting for selecting correct amount
+  
+  $roles = user_roles(TRUE, 'sign up for content');
+  $role_description = array();
+  $default_role_description = array();
+  foreach ($roles as $key => $value) {
+    $role_description[] = $key." = ".$value;
+    $default_role_description[] = $key;
+  }
+  $form[SIGNUP_PAY_ROLE_WEIGHT] = array(
+    '#type'           => 'textfield',
+    '#title'          => t('Role Weight'),
+    '#default_value'  => variable_get(SIGNUP_PAY_ROLE_WEIGHT, implode(", ", $default_role_description)),
+    '#description'    => t('KEY [@legend]<br>Order the roles to prioritize thier use when determining the price to pay for signups. For now, enter the ID of the role in the order you would like them used.', array('@legend' => implode(", ", $role_description))),
+  );
 
   return system_settings_form($form);
 }
@@ -210,7 +222,7 @@ function _signup_pay_format_amount($amou
   $amount = number_format($amount, 2);
   switch($currency) {
     case 'EUR':
-      return "â‚¬ $amount";
+      return "€ $amount";
     case 'USD':
       return "$ $amount";
     case 'CAD':
@@ -530,23 +542,125 @@ function signup_pay_paypal_ipn() {
 }
 
 function signup_pay_form_alter($form_id, &$form) {
+  global $user;
   // Intercept signup form
-  if ($form_id == 'signup_form') {
-    $form['collapse']['signup_user_form']['payment'] = array(
-      // TODO Add price information for roles here
-      '#value' => t('<div>The price will go here.</div>'),
-    );
-    $form['collapse']['signup_user_form']['method'] = array(
-      '#type'          => 'radios',
-      '#title'         => t('Payment method'),
-      '#options'       => signup_pay_get_methods(),
-      '#default_value' => SIGNUP_PAY_METHOD_PAYPAL,
-      '#description'   => t('Select a method of payment. If you select Paypal, you will be redirected to the Paypal site where you can pay using your credit card or balance from Paypal.'),
-      '#after_build'   => array('signup_pay_form_check'),
+  switch ($form_id) {
+    case 'signup_form':
+      $default_rid = signup_pay_find_user_role($user);
+      $pay_roles = signup_pay_get_node_pay_roles($form['nid']['#value']);
+      $form['collapse']['signup_user_form']['payment'] = array(
+        '#value' => t('<div>The price you pay: %amount</div>', array('%amount' => _signup_pay_format_amount($pay_roles[$default_rid]['amount'],$pay_roles[$default_rid]['currency']))),
+      );
+      $form['collapse']['signup_user_form']['method'] = array(
+        '#type'          => 'radios',
+        '#title'         => t('Payment method'),
+        '#options'       => signup_pay_get_methods(),
+        '#default_value' => SIGNUP_PAY_METHOD_PAYPAL,
+        '#description'   => t('Select a method of payment. If you select Paypal, you will be redirected to the Paypal site where you can pay using your credit card or balance from Paypal.'),
+        '#after_build'   => array('signup_pay_form_check'),
+      );
+      // Add a redirect to our Paypal form
+      $form['#redirect'] = 'signup_pay_do_payment';
+      break;
+      
+    case $form['type']['#value'] .'_node_form':
+      if (isset($form['signup'])) {
+        signup_pay_alter_node_form($form_id, $form);
+      }
+      break;
+  }
+}
+
+/**
+ * find user role for determining amount and currencty
+ */
+function signup_pay_find_user_role(&$user) {
+  $role_weight_list = variable_get(SIGNUP_PAY_ROLE_WEIGHT, 0);
+  foreach(explode(",",$role_weight_list) as $rid) {
+    if ($user->roles[$rid]) {
+      return $rid;
+    }
+  }
+  foreach($user->roles as $rid) {
+    return $rid;
+  }
+}
+
+/**
+ * alter node edit form to admin role, amount, currency per nod
+ */
+function signup_pay_alter_node_form($form_id, &$form) {
+  $node = $form['#node'];
+  $roles = user_roles(TRUE, 'sign up for content');
+  $form['signup']['node_settings']['settings']['signup_pay_per_role'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Signup Amount Per Role'),
+    '#tree' => TRUE
+  );
+  $weight = 0;
+  foreach ($roles as $rid => $role) {
+    $form['signup']['node_settings']['settings']['signup_pay_per_role'][$rid]['amount'] = array(
+      '#type' => 'textfield',
+      '#title' => t('%name pay amount', array('%name' => $role)),
+      '#default_value' => isset($node->pay_roles[$rid]['amount']) ? $node->pay_roles[$rid]['amount'] : variable_get(SIGNUP_PAY_AMOUNT, SIGNUP_PAY_DEFAULT_AMOUNT),
+      '#size' => 4, 
+      '#maxlength' => 8,
+      '#description' => t('Price per signup for the %name role.', array('%name' => $role)),
+      '#weight' => $weight++,
     );
-    // Add a redirect to our Paypal form
-    $form['#redirect'] = 'signup_pay_do_payment';
+    $form['signup']['node_settings']['settings']['signup_pay_per_role'][$rid]['currency'] = array(
+      '#type'           => 'select',
+      '#title'          => t('%name Currency', array('%name' => $role)),
+      '#default_value'  => isset($node->pay_roles[$rid]['currency']) ? $node->pay_roles[$rid]['currency'] : variable_get(SIGNUP_PAY_CURRENCY, SIGNUP_PAY_DEFAULT_CURRENCY),
+      '#options'        => signup_pay_get_currency(),
+      '#description' => t('Currency for the amount to collect for the %name role.', array('%name' => $role)),
+      '#weight' => $weight++,
+    );
+  }
+}
+
+/**
+ * update node data
+ */
+function signup_pay_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+  if ($node->signup) {
+    switch($op) {
+      case 'load':
+        $pay_roles = signup_pay_get_node_pay_roles($node->nid);
+        $node->pay_roles = $pay_roles;
+        break;
+
+    case 'insert':
+    case 'update':
+      $roles = user_roles(TRUE, '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];
+        signup_pay_set_node_role_pay($node->nid, $rid, $pay_role);
+      }
+      break;
+    }
+  }
+}
+
+/**
+ * databbase interaction for insert and delete of role amount and currencty per nod
+ */
+function signup_pay_set_node_role_pay($nid, $rid, $pay_role) {
+  db_query("DELETE FROM {signup_pay_node} WHERE nid = %d AND rid = %d", $nid, $rid);
+  db_query("INSERT INTO {signup_pay_node} (nid, rid, amount, currency) VALUES (%d, %d, %d, '%s')", $nid, $rid, $pay_role['amount'], $pay_role['currency']);
+}
+
+/**
+ * grab array of amount and currency per role for a given nod
+ */
+function signup_pay_get_node_pay_roles($nid) {
+  $amounts = array();
+  $result = db_query("SELECT rid, amount, currency FROM {signup_pay_node} WHERE nid = %d", $nid);
+  while ($row = db_fetch_object($result)) {
+    $amounts[$row->rid] = array("amount" => $row->amount, "currency" => $row->currency);
   }
+  return $amounts;
 }
 
 function signup_pay_form_check($form_element) {
@@ -574,22 +688,26 @@ function signup_pay_do_payment() {
       'uid' => $user->uid,
       'nid' => $nid,
     );
-
     return signup_pay_do_paypal($data);
   }
 }
 
 function signup_pay_do_paypal($data = array()) {
   $new_data = array();
-
+  
   foreach($data as $key => $value) {
     $new_data[] = "$key=$value";
+    $$key = $value;
   }
   $custom = implode(':', $new_data);
 
-  // Get node title, currency and amount for this node, for this role
-  $amount = variable_get(SIGNUP_PAY_AMOUNT, SIGNUP_PAY_DEFAULT_AMOUNT);
-  $currency = variable_get(SIGNUP_PAY_CURRENCY, SIGNUP_PAY_DEFAULT_CURRENCY);
+  $signup_node = node_load($nid);
+  $signup_user = user_load(array("uid"=>$uid));
+  $signup_rid = signup_pay_find_user_role($signup_user);
+  $pay_roles = signup_pay_get_node_pay_roles($nid);
+  
+  $amount = $pay_roles[$signup_rid]['amount'] ? $pay_roles[$signup_rid]['amount'] : variable_get(SIGNUP_PAY_AMOUNT, SIGNUP_PAY_DEFAULT_AMOUNT);
+  $currency = $pay_roles[$signup_rid]['currency'] ? $pay_roles[$signup_rid]['currency'] : variable_get(SIGNUP_PAY_CURRENCY, SIGNUP_PAY_DEFAULT_CURRENCY);
 
   $url = simple_paypal_get_url();
   $form['#action'] = $url;
@@ -620,7 +738,7 @@ function signup_pay_do_paypal($data = ar
   );
   $form['item_name'] = array(
     '#type' => 'hidden',
-    '#value' => 'Signup payment',
+    '#value' => t('Signup payment for @title', array('@title' => $signup_node->title)),
     '#name' => 'item_name',
   );
   $form['no_shipping'] = array(
