--- signup_pay.module	2008-04-11 16:25:59.000000000 -0400
+++ signup_payUPDATE239597.module	2008-04-11 16:29:23.000000000 -0400
@@ -30,27 +30,24 @@ define('SIGNUP_PAY_PAGER',              
 define('SIGNUP_PAY_DEFAULT_PAID_STATUS_CODE', 0);
 define('SIGNUP_PAY_DEFAULT_DENY_ANON', TRUE);
 
-// Payment methods
-define('SIGNUP_PAY_METHOD_PAYPAL',        1);
-define('SIGNUP_PAY_METHOD_CHEQUE',        2);
-define('SIGNUP_PAY_METHOD_CASH',          3);
-define('SIGNUP_PAY_METHOD_BANK_TRANSFER', 4);
-define('SIGNUP_PAY_METHOD_FREE',          5);
-
-
 function signup_pay_get_methods($include_disabled = FALSE) {
-  $methods = array(
-    SIGNUP_PAY_METHOD_PAYPAL        => t('Paypal'),
-    SIGNUP_PAY_METHOD_CHEQUE        => t('Cheque'),
-    SIGNUP_PAY_METHOD_CASH          => t('Cash'),
-    SIGNUP_PAY_METHOD_BANK_TRANSFER => t('Bank transfer'),
-    );
+  $methods = array();
+  
+  foreach (module_implements('payapi') as $module) {
+    if ($result = module_invoke($module, 'payapi', $edit, 'display name')) {
+      //create payment option and place in static cache
+      //find all instances of SIGNUP_PAY_METHOD_
+      //find all instances of signup_pay_get_methods
+      $methods['SIGNUP_PAY_METHOD_'.strtoupper($result)] = $result;
+    }
+  }
   
   if ($include_disabled) {
     return $methods;
   }
   else {
-    $enabled_methods = variable_get(SIGNUP_PAY_ENABLED_METHODS, array(SIGNUP_PAY_METHOD_PAYPAL => 1));
+    //TODO set the default enabled methods in the contributed module itself
+    $enabled_methods = variable_get(SIGNUP_PAY_ENABLED_METHODS, array('SIGNUP_PAY_METHOD_PAYPAL' => 'SIGNUP_PAY_METHOD_PAYPAL'));
     foreach($methods as $key => $value) {
       if (!$enabled_methods[$key]) {
         unset($methods[$key]);
@@ -83,7 +80,7 @@ function signup_pay_settings() {
     '#type'            => 'checkboxes',
     '#title'          => t('Accept payment using the following methods'),
     '#options'        => signup_pay_get_methods(TRUE),
-    '#default_value'   => variable_get(SIGNUP_PAY_ENABLED_METHODS, array(SIGNUP_PAY_METHOD_PAYPAL => 1)),
+    '#default_value'   => variable_get(SIGNUP_PAY_ENABLED_METHODS, array('SIGNUP_PAY_METHOD_PAYPAL')),
     );
   $form['signup_pay_general'][SIGNUP_PAY_EMAIL] = array(
     '#type' => 'textfield',
@@ -192,14 +189,7 @@ function signup_pay_menu($may_cache) {
 
   if ($may_cache) {
     $access = user_access('administer site configuration');
-
-    $items[] = array(
-      'path'     => 'ipn/signup_pay',
-      'title'    => t('IPN post'),
-      'callback' => 'signup_pay_paypal_ipn',
-      'access'   => TRUE,
-      'type'     => MENU_CALLBACK,
-    );
+    
     $items[] = array(
       'path'               => 'admin/settings/signup_pay',
       'title'              => t('Signup pay'),
@@ -209,18 +199,18 @@ function signup_pay_menu($may_cache) {
       'access'             => $access,
     );
     $items[] = array(
-      'path'        => 'admin/content/signup_pay',
-      'title'       => t('Signup pay'),
-      'access'      => $access,
-      'callback'    => 'signup_pay_admin',
-      'description' => t('Manages signup payments to your site via Paypal'),
+      'path'               => 'admin/content/signup_pay',
+      'title'              => t('Signup pay'),
+      'access'             => $access,
+      'callback'           => 'signup_pay_admin',
+      'description'        => t('Manages signup payments to your site'),
     );
     $items[] = array(
-      'path'   => 'admin/content/signup_pay/list',
-      'title'  => t('List'),
-      'access' => $access,
-      'type'   => MENU_DEFAULT_LOCAL_TASK,
-      'weight' => -10,
+      'path'               => 'admin/content/signup_pay/list',
+      'title'              => t('List'),
+      'access'             => $access,
+      'type'               => MENU_DEFAULT_LOCAL_TASK,
+      'weight'             => -10,
     );
     $items[] = array(
       'path'               => 'admin/content/signup_pay/add',
@@ -351,7 +341,9 @@ function signup_pay_edit($mode) {
     '#description'   => t('Amount after any fees are deducted.'),
   );
   $methods = signup_pay_get_methods();
-  $methods[SIGNUP_PAY_METHOD_FREE] = t('Free attendance');
+  //TODO specifiy options to display custom methods in custom places
+  //i.e. Free Attendance on Edit of a pay record
+  $methods['SIGNUP_PAY_METHOD_FREE'] = t('Free attendance');
   $form['method'] = array(
     '#type'          => 'select',
     '#title'         => t('Method'),
@@ -414,8 +406,8 @@ function signup_pay_edit_submit($form_id
 
   switch($mode) {
     case 'add':
-      db_query("INSERT INTO {signup_pay} (id, uid, nid, time_paid, method_id, currency, amount, net_amount, mail, name)
-        VALUES (0, %d, %d, %d, %d, '%s', %f, '%s', '%s')",
+      db_query("INSERT INTO {signup_pay} (uid, nid, time_paid, method_id, currency, amount, net_amount, mail, name)
+        VALUES (%d, %d, %d, '%s', '%s', %f, '%s', '%s', '%s')",
         $form['uid'],
         $form['nid'],
         strtotime($form['time_paid']),
@@ -463,12 +455,13 @@ function signup_pay_edit_submit($form_id
       break;
   }
 
-  drupal_goto('admin/build/signup_pay');
+  drupal_goto('admin/content/signup_pay');
 }
 
 function signup_pay_admin() {
   $methods = signup_pay_get_methods();
-  $methods[SIGNUP_PAY_METHOD_FREE] = t('Free attendance');
+  //TODO see previous todo on This specific entry
+  $methods['SIGNUP_PAY_METHOD_FREE'] = t('Free attendance');
   $rows = array();
   $header = array(
     array('data' => t('Date'),   'field' => 'time_paid', 'sort' => 'desc'),
@@ -487,15 +480,16 @@ function signup_pay_admin() {
   $result = pager_query($sql, SIGNUP_PAY_PAGER, 0, NULL);
   while ($row = db_fetch_object($result)) {
     $node = node_load($row->nid);
+    $timestamp = $row->time_paid ? format_date($row->time_paid, 'custom', 'Y-m-d H:i') : NULL;
     $rows[] = array(
-      format_date($row->time_paid, 'custom', 'Y-m-d H:i'),
+      $timestamp,
       theme('username', $row),
       $node->title,
       $methods[$row->method_id],
       _signup_pay_format_amount($row->amount, $row->currency),
       $row->invoice_name,
-      l(t('Edit'), "admin/build/signup_pay/edit/$row->id"),
-      l(t('Delete'), "admin/build/signup_pay/delete/$row->id"),
+      l(t('Edit'), "admin/content/signup_pay/edit/$row->id"),
+      l(t('Delete'), "admin/content/signup_pay/delete/$row->id"),
       );
   }
 
@@ -528,7 +522,8 @@ function theme_signup_pay_receipt($uid, 
   }
 
   $methods = signup_pay_get_methods();
-  $methods[SIGNUP_PAY_METHOD_FREE] = t('Free attendance');
+  //TODO same as before
+  $methods['SIGNUP_PAY_METHOD_FREE'] = t('Free attendance');
 
   $result = db_query('SELECT u.mail, p.name as invoice_name, p.nid, p.currency, p.amount, p.time_paid, p.method_id
     FROM {users} u
@@ -553,69 +548,6 @@ function signup_pay_thanks() {
   print theme('page', t('Thank you for registering.'));
 }
 
-function signup_pay_paypal_ipn() {
-  // Verify that the request came from Paypal, and not from some intrusion
-  if (!simple_paypal_ipn_verify($_POST)) {
-    // Verification failed
-    return;
-  }
-
-  if ($_POST['receiver_email'] != variable_get(SIGNUP_PAY_EMAIL, SIGNUP_PAY_DEFAULT_EMAIL)) {
-    // Payment is not for the email address configured
-    watchdog('signup_pay', t('Payment email (@payment) is not for the email address configured (@configured)',array('@payment'=>variable_get(SIGNUP_PAY_EMAIL, SIGNUP_PAY_DEFAULT_EMAIL),'@configured'=>$_POST['receiver_email'])));
-    return;
-  }
-
-  // Parse the custom field, and break it into its components
-  $data = explode(':', $_POST['custom']);
-  $custom = array();
-  foreach($data as $row) {
-    list($key, $value) = explode('=', $row);
-    $custom[$key] = $value;
-  }
-
-  $time_paid = check_plain(strtotime($_POST['payment_date']));
-  $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']);
-  //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)",
-    (int)$custom['uid'],
-    (int)$custom['nid'],
-    SIGNUP_PAY_METHOD_PAYPAL,
-    $time_paid,
-    $name,
-    $mail,
-    $currency,
-    $net_amount,
-    $gross_amount);
-  $body = theme('signup_pay_receipt', $custom['uid'], $custom['nid'], 'mail');
-  $from = variable_get('site_mail', ini_get('sendmail_from'));
-  $subject = t('[@site] Receipt for payment paid', array('@site' => variable_get('site_name', 'Drupal')));
-  drupal_mail('signup_pay', $mail, $subject, $body, $from);
-  watchdog('signup_pay', t('Payment from @name (@mail) amount of @amount @currency.', array(
-    '@name' => $name,
-    '@mail' => $mail,
-    '@amount' => $amount,
-    '@currency' => $currency,
-    )));
-
-  if (module_exists('signup_status')) {
-    $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);
-  }
-}
-
 function signup_pay_form_alter($form_id, &$form) {
   global $user;
   // Intercept signup form
@@ -632,7 +564,7 @@ function signup_pay_form_alter($form_id,
           '#type'          => 'radios',
           '#title'         => t('Payment method'),
           '#options'       => signup_pay_get_methods(),
-          '#default_value' => SIGNUP_PAY_METHOD_PAYPAL,
+          '#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'),
         );
@@ -765,109 +697,52 @@ 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'];
+  $_SESSION['signup_pay_anon_mail'] = $form_element[$form_element['#value']]['#post']['signup_anon_mail'];
+  $_SESSION['signup_pay_name'] = $form_element[$form_element['#value']]['#post']['signup_form_data']['Name'];
+  $_SESSION['signup_pay_phone'] = $form_element[$form_element['#value']]['#post']['signup_form_data']['Phone'];
   return $form_element;
 }
 
 function signup_pay_do_payment() {
   global $user;
 
+  $record = array();
   // Get method and node, from session
-  $nid    = $_SESSION['signup_pay_nid'];
-  $method = $_SESSION['signup_pay_method'];
-  $anon   = $_SESSION['signup_pay_anon_mail'];
+  $record['nid']    = $_SESSION['signup_pay_nid'];
+  $record['uid']    = $user->uid;
+  $record['method'] = $_SESSION['signup_pay_method'];
+  $record['mail']   = $_SESSION['signup_pay_anon_mail'] ? $_SESSION['signup_pay_anon_mail'] : $user->mail;
+  $record['phone']   = $_SESSION['signup_pay_phone'];
+  $record['name']   = $_SESSION['signup_pay_name'];
 
-  // Check if user has already paid
-  if (signup_pay_check_payment($nid,$anon)) {
+  // TODO decide if we should integrate into payapi
+  if (signup_pay_check_payment($nid,$record['mail'])) {
     drupal_set_message(t('You have already paid for this event.'));
     return;
   }
 
-  // Check if it is Paypal
-  if ($method == SIGNUP_PAY_METHOD_PAYPAL) {
-    $data = array(
-      'uid' => $user->uid,
-      'nid' => $nid,
-      'mail' => $anon,
-    );
-    return signup_pay_do_paypal($data);
+  foreach (module_implements('payapi') as $module) {
+    if ('SIGNUP_PAY_METHOD_'.strtoupper(module_invoke($module, 'payapi', $record, 'display name')) == $record['method']) {
+      return module_invoke($module, 'payapi', $record, 'process');
+    }
   }
-}
-
-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);
-
-  $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;
-
-  $form['body'] = array(
-    '#value' => t('<div>You will now be redirected to Paypal for payment.</div>'),
-  );
-
-  $form['business'] = array(
-    '#type' => 'hidden',
-    '#name' => 'business',
-    '#value' => variable_get(SIGNUP_PAY_EMAIL, SIGNUP_PAY_DEFAULT_EMAIL),
-  );
-  $form['currency_code'] = array(
-    '#type' => 'hidden',
-    '#value' => $currency,
-    '#name' => 'currency_code',
-  );
-  $form['amount'] = array(
-    '#type' => 'hidden',
-    '#value' => $amount,
-    '#name' => 'amount',
-    );
-  $form['cmd'] = array(
-    '#type' => 'hidden',
-    '#value' => '_xclick',
-    '#name' => 'cmd',
-  );
-  $form['item_name'] = array(
-    '#type' => 'hidden',
-    '#value' => t('Signup payment for @title', array('@title' => $signup_node->title)),
-    '#name' => 'item_name',
-  );
-  $form['no_shipping'] = array(
-    '#type' => 'hidden',
-    '#value' => 1,
-    '#name' => 'no_shipping',
-  );
-  $form['return'] = array(
-    '#type' => 'hidden',
-    '#value' => url('signup_pay/thanks', NULL, NULL, TRUE),
-    '#name' => 'return',
-  );
-  $form['custom'] = array(
-    '#type' => 'hidden',
-    '#value' => $custom,
-    '#name' => 'custom',
-  );
-  $form['notify_url'] = array(
-    '#type' => 'hidden',
-    '#value' => url('ipn/signup_pay', NULL, NULL, TRUE),
-    '#name' => 'notify_url',
-  );
-  $form['submit'] = array(
-    '#type' => 'submit', 
-    '#value' => t('Proceed to Paypal'),
-    '#name' => 'submit',
-  );
+  drupal_set_message(t('The payment has been declined.'));
+  watchdog('payment', t('@naem, User @user added a transaction (@mail)',
+        array('@name' => $record['name'], '@user' => $user->name, '@mail' => $record['mail'], 'nid' => $record['nid'])));
+  return;
+}
 
-  return $form;
+/**
+  * Signup Pay Payment API
+  * allows others to submit payment processing options to signup pay
+  *
+  * @param object $items
+  * @param string $op The action to execute (e.g., validate, insert, update, delete)
+  * @param string $arg Optional argument to pass along. Currently being used to
+  *                    pass an array of errors during form display
+  * @return array Collection of output from the all *_payapi calls.
+  */
+function signup_pay_invoke_payapi(&$items, $op, $arg = 0) {
+  return module_invoke_all('payapi', $items, $op, $arg);
 }
