diff -cr lm_paypal.orig/lm_paypal.admin.inc lm_paypal/lm_paypal.admin.inc
*** lm_paypal.orig/lm_paypal.admin.inc	2009-04-04 16:29:56.000000000 +0000
--- lm_paypal/lm_paypal.admin.inc	2010-02-04 18:00:42.000000000 +0000
***************
*** 44,49 ****
--- 44,85 ----
      '#description' => t('Maximum age of an old IPN record in hours before it is deleted. Type 0 if you want to keep them forever.'),
    );
  
+   $form['lm_paypal_cron_publish'] = array(
+     '#type' => 'checkbox',
+     '#title' => t('Cron Publish nodes'),
+     '#default_value' => variable_get('lm_paypal_cron_publish', FALSE),
+     '#description' => t('Normally you must set the workflow for your node types to "Published".  Using this option you can set them to "unPublished".  The cron process will publish them after payment is made and unpublish them when they expire.'),
+   );
+ 
+   $form['lm_paypal_expire_fudge'] = array(
+     '#type' => 'checkbox',
+     '#title' => t('Expire fudge'),
+     '#default_value' => variable_get('lm_paypal_expire_fudge', FALSE),
+     '#description' => t('Add a 24 hour fudge to the expire time.  Use if the cron runs only once per day or if you are using varnish or another cache server that only expires cache once per day.  This feature is only available when the "Cron Publish Node" is enabled.'),
+   );
+ 
+  $form['lm_paypal_prefix_nid'] = array(
+     '#type' => 'checkbox',
+     '#title' => t('Prefix node id'),
+     '#default_value' => variable_get('lm_paypal_prefix_nid', FALSE),
+     '#description' => t('Prefix paypal item description with node id. ie: 312/item description'),
+   );
+ 
+  $form['lm_paypal_prefix_hostname'] = array(
+     '#type' => 'checkbox',
+     '#title' => t('Prefix hostname'),
+     '#default_value' => variable_get('lm_paypal_prefix_hostname', FALSE),
+     '#description' => t('Prefix paypal item description with hostname. ie: hostname/item description'),
+   );
+ 
+  $form['lm_paypal_disable_myaccount'] = array(
+     '#type' => 'checkbox',
+     '#title' => t('Disable My Account display'),
+     '#default_value' => variable_get('lm_paypal_disable_myaccount', FALSE),
+     '#description' => t('Disable display in My Account of Paypal Payments and Subscription when no payments, no subscriptions and no nodes that need paid exist.'),
+   );
+ 
+ 
    $form['lm_paypal_obey_test_ipns'] = array(
      '#type' => 'checkbox',
      '#title' => t('LM PayPal Obey Test IPNS'),
diff -cr lm_paypal.orig/lm_paypal.info lm_paypal/lm_paypal.info
*** lm_paypal.orig/lm_paypal.info	2009-05-31 00:14:04.000000000 +0000
--- lm_paypal/lm_paypal.info	2010-01-23 20:23:41.000000000 +0000
***************
*** 6,12 ****
  
  
  ; Information added by drupal.org packaging script on 2009-05-31
! version = "6.x-2.x-dev"
  core = "6.x"
  project = "lm_paypal"
  datestamp = "1243728844"
--- 6,12 ----
  
  
  ; Information added by drupal.org packaging script on 2009-05-31
! version = "6.x-2.x-dev-k"
  core = "6.x"
  project = "lm_paypal"
  datestamp = "1243728844"
diff -cr lm_paypal.orig/lm_paypal.install lm_paypal/lm_paypal.install
*** lm_paypal.orig/lm_paypal.install	2009-01-05 11:53:16.000000000 +0000
--- lm_paypal/lm_paypal.install	2010-01-25 18:23:11.000000000 +0000
***************
*** 25,30 ****
--- 25,33 ----
    drupal_uninstall_schema('lm_paypal');
    variable_del('lm_paypal_debug');
    variable_del('lm_paypal_host');
+   variable_del('lm_paypal_cron_publish');
+   variable_del('lm_paypal_prefix_nid');
+   variable_del('lm_paypal_prefix_hostname');
    variable_del('lm_paypal_business');
    variable_del('lm_paypal_obey_test_ipns');
    variable_del('lm_paypal_ipns_max_age');
diff -cr lm_paypal.orig/lm_paypal.ipn.inc lm_paypal/lm_paypal.ipn.inc
*** lm_paypal.orig/lm_paypal.ipn.inc	2009-04-04 15:03:11.000000000 +0000
--- lm_paypal/lm_paypal.ipn.inc	2010-01-25 23:00:35.000000000 +0000
***************
*** 8,13 ****
--- 8,14 ----
   * IPN is present as a form submission which this routine unravels and saves for
   * processing.
   */
+ 
  function lm_paypal_ipn_in() {
    // file_put_contents('/tmp/ipn-post.txt', serialize($_POST));
    watchdog(LM_PAYPAL, 'lm_paypal_ipn_in called', NULL);
***************
*** 32,38 ****
      }
    }
  
- 
    $validated = _lm_paypal_validate_ipn($_POST);
  
    // Sanitize and remove risky fields from $_POST to make it suitable for
--- 33,38 ----
***************
*** 46,52 ****
    $wr = drupal_write_record('lm_paypal_ipns', $ipn);
    if ($wr == SAVED_NEW) {
      $last = $ipn['id'];
!     watchdog(LM_PAYPAL, 'lm_paypal_ipn_in $last = '. $ipn['id'], NULL);
      $link = l(t('view'), "admin/settings/lm_paypal/id/$ipn[id]");
  
      if ($validated) {
--- 46,52 ----
    $wr = drupal_write_record('lm_paypal_ipns', $ipn);
    if ($wr == SAVED_NEW) {
      $last = $ipn['id'];
!     watchdog(LM_PAYPAL, 'lm_paypal_ipn_in $last = '. $ipn['id'], array(),NULL);
      $link = l(t('view'), "admin/settings/lm_paypal/id/$ipn[id]");
  
      if ($validated) {
***************
*** 78,83 ****
--- 78,86 ----
   *  The id of the saved IPN to be processed.
   */
  function lm_paypal_process_in($ipn) {
+ 
+    watchdog(LM_PAYPAL, 'lm_paypal_ipn_proccess_in called', NULL);
+ 
    $business = lm_paypal_api_get_business();
    $link = l(t('view'), "admin/settings/lm_paypal/id/$ipn->id");
  
***************
*** 104,111 ****
--- 107,119 ----
  
    // Additionally, IPNs are displatched to a txn_type-specific hook.
    $txn_type = preg_replace('/[^a-z0-9_]*/', '', $ipn->txn_type);
+ 
+   watchdog(LM_PAYPAL, 'type --- %type',array('%type' => $ipn->txn_type));
+ 
    $results2 = module_invoke_all('lm_paypal_ipn_' . $txn_type, $ipn);
  
+   lm_paypal_mark_processed($ipn);
+ 
    // Determine whether _some_ module handled the IPN, and warn if not.
    $processed = FALSE;
    foreach ($results1 as $res) {
***************
*** 184,200 ****
  function lm_paypal_lm_paypal_ipn_web_accept($ipn) {
    // error_log("Called lm_paypal_lm_paypal_ipn_web_accept($ipn->txn_id)");
    _lm_paypal_process_in_web_accept($ipn);
    return TRUE;
  }
  
- 
- 
  /**
   * Process a newly arrived web_accept IPN message
   *
   * @param $ipn
   */
  function _lm_paypal_process_in_web_accept($ipn) {
    $debug = variable_get('lm_paypal_debug', LM_PAYPAL_DEBUG_DEFAULT);
    if ($debug) {
      watchdog(LM_PAYPAL, 'in_web_accept');
--- 192,208 ----
  function lm_paypal_lm_paypal_ipn_web_accept($ipn) {
    // error_log("Called lm_paypal_lm_paypal_ipn_web_accept($ipn->txn_id)");
    _lm_paypal_process_in_web_accept($ipn);
+ 
    return TRUE;
  }
  
  /**
   * Process a newly arrived web_accept IPN message
   *
   * @param $ipn
   */
  function _lm_paypal_process_in_web_accept($ipn) {
+ 
    $debug = variable_get('lm_paypal_debug', LM_PAYPAL_DEBUG_DEFAULT);
    if ($debug) {
      watchdog(LM_PAYPAL, 'in_web_accept');
***************
*** 203,209 ****
    $link = l(t('view'), "admin/settings/lm_paypal/id/$ipn->id");
  
    if (lm_paypal_already_processed($ipn->txn_id)) {
!     watchdog(
        LM_PAYPAL,
        'This transaction has already been processed, ignored: %id',
        array('%id' => check_plain($ipn->txn_id)),
--- 211,217 ----
    $link = l(t('view'), "admin/settings/lm_paypal/id/$ipn->id");
  
    if (lm_paypal_already_processed($ipn->txn_id)) {
!      watchdog(
        LM_PAYPAL,
        'This transaction has already been processed, ignored: %id',
        array('%id' => check_plain($ipn->txn_id)),
***************
*** 212,218 ****
      return;
    }
  
!   lm_paypal_mark_processed($ipn);
  
    if ($ipn->payment_status == 'Pending') {
      watchdog(
--- 220,226 ----
      return;
    }
  
!   //lm_paypal_mark_processed($ipn);
  
    if ($ipn->payment_status == 'Pending') {
      watchdog(
***************
*** 224,229 ****
--- 232,238 ----
      return;
    }
  
+ 
    $custom = lm_paypal_unpack_ipn_custom($ipn);
    $uid = $custom['uid'];
    $other = $custom['other'];
***************
*** 327,332 ****
--- 336,342 ----
      }
    }
  
+ return;
  /*
    // Find the correct web_accept processor
    $ranges = lm_paypal_web_accept_register();
diff -cr lm_paypal.orig/lm_paypal.module lm_paypal/lm_paypal.module
*** lm_paypal.orig/lm_paypal.module	2009-05-30 17:05:54.000000000 +0000
--- lm_paypal/lm_paypal.module	2010-02-08 12:38:33.000000000 +0000
***************
*** 125,130 ****
--- 125,133 ----
   * Implementation of hook_cron().
   */
  function lm_paypal_cron() {
+ 
+   if (lm_paypal_cron_publish()) process_subscribers();
+ 
    _lm_paypal_clean_stored_ipns();
  }
  
***************
*** 309,316 ****
--- 312,321 ----
  }
  
  function _lm_paypal_clean_stored_ipns() {
+ 
    $ipns_max_age = (int) variable_get('lm_paypal_ipns_max_age', LM_PAYPAL_IPNS_MAX_AGE_DEFAULT);
    // Do not launch this action if no max age is specified (keep for ever).
+ 
    if ($ipns_max_age > 0) {
      if (lm_paypal_debug()) {
        watchdog(LM_PAYPAL, 'cron');
***************
*** 320,325 ****
--- 325,414 ----
    }
  }
  
+ function lm_paypal_cron_publish() {
+   return (bool) variable_get('lm_paypal_cron_publish', FALSE);
+ }
+ 
+ function lm_paypal_expire_fudge() {
+   return (bool) variable_get('lm_paypal_expire_fudge', FALSE);
+ }
+ 
+ function lm_paypal_prefix_nid() {
+   return (bool) variable_get('lm_paypal_prefix_nid', FALSE);
+ }
+ 
+ function lm_paypal_prefix_hostname() {
+   return (bool) variable_get('lm_paypal_prefix_hostname', FALSE);
+ }
+ 
+ function lm_paypal_disable_myaccount() {
+   return (bool) variable_get('lm_paypal_disable_myaccount', FALSE);
+ }
+ 
+ function process_subscribers() {
+ 
+     watchdog(LM_PAYPAL,"cron process_subscribers node");
+ 
+     $sql = "SELECT * from {lm_paypal_subscribers} where status = '1'";
+     $result = db_query($sql);
+     if (! $result) {
+         watchdog(LM_PAYPAL,"no subscribers to process");
+     } 
+     while ($ss = db_fetch_object($result)) {
+ 
+         $so = lm_paypal_subscriptions_load_subscription($ss->subid);
+ 
+         $expires = lm_paypal_expire_date($ss->subid,$ss->started);
+ 
+         $dt = date('m/d/y',$expires);
+ 
+         watchdog(LM_PAYPAL,"node %nid - expires %expires",array('%nid' => $ss->nid, '%expires' => $dt));
+ 
+         $status=1; 
+         if (time() > $expires) {
+             $status=0; //unpublish this node if expired.
+         }
+ 
+         if ($ss->nid != 0) {
+            $node=node_load($ss->nid);
+            if (!$node) {
+              $status=0; // since the node does not exist, expire the subscriber
+            } else {
+              if ($status != $node->status) {
+                $node->status=$status;
+                $node = node_submit($node);
+ 
+                if ($node->validated) {
+                  node_save($node);
+                } else {
+                  watchdog(LM_PAYPAL,"cron FAILED SAVE node: %nid - %title",array('%nid' => $node->nid, '%title' =>$node->title));
+                }
+              }
+            }
+         }
+ 
+         if ($status != 1) {
+            $sql = "UPDATE {lm_paypal_subscribers} SET status = 5 WHERE subscr_id = '%s'";
+            $result = db_query($sql, $ss->subscr_id);
+ 
+            if (! $result) {
+               watchdog(LM_PAYPAL,"failed subscriber update");
+            }
+ 
+            if ($so->kind == 0) {
+                lm_paypal_subscriptions_user_loose_role($ss->uid, $so->rid);
+                $variables = array('%Subscription' => $so->item_name, '%Days' => $so->nearend_days, '%Node' => '');
+             } else {
+                if ($so->kind == 2) lm_paypal_subscriptions_user_loose_group($ss->uid, $so->rid);
+                $variables = array('%Subscription' => $so->item_name, '%Days' => $so->nearend_days, '%Node' => $node);
+             }
+ 
+             lm_paypal_mail_user($ss->uid, $ss->uid, $so->send_user_onend_subject, $so->send_user_onend_body, $variables);
+         }
+ 
+     }
+ }
+ 
  /**
   * Default return_path for paypal.
   *
***************
*** 525,530 ****
--- 614,620 ----
      '#value' => isset($options['item_name']) ? check_plain($options['item_name']) : t('Donation'),
      '#name' => 'item_name',
    );
+ 
    $form['item_number'] = array(
      '#type' => 'hidden',
      '#value' => isset($options['item_number']) ? ((int) $options['item_number']) : 0,
***************
*** 593,599 ****
--- 683,696 ----
    if (empty($custom) || is_array($custom)) {
      $custom = array();
    }
+   $custom['module'] = LM_PAYPAL_SUBSCRIPTIONS;
    $custom['uid'] = $user->uid;
+   $nid=request_uri();
+   $nid=str_replace("/lm_paypal","",$nid);
+   if (strpos($nid,"/node/") !== false) {
+      $custom['nid'] = str_replace("/node/","",$nid);
+   }
+ 
    $form['custom'] = array(
      '#type' => 'hidden',
      '#value' => serialize($custom),
***************
*** 894,925 ****
   *  a better way around this but there was nothing mentioned in the
   *  documentation.)
   */
! function lm_paypal_mail_user($to_uid, $about_uid, $subject, $message, $vars) {
    // TODO: use Token module
    global $base_url;
  
    if (lm_paypal_debug()) {
!     watchdog(LM_PAYPAL, "lm_paypal_mail_user($to_uid, $about_uid, $subject, $message, $vars)", NULL);
    }
  
    $to_account = user_load(array('uid' => $to_uid, 'status' => 1));
    $to = $to_account->mail;
  
    $about_user = user_load(array('uid' => $about_uid, 'status' => 1));
! 
    //TODO: Maybe use the subscription adminstrators email instead?
    $from = variable_get('site_mail', ini_get('sendmail_from'));
  
    $variables = array(
      '%Username' => $about_user->name,
      '%Login' => $about_user->login,
!     '%Site' => variable_get('site_name', 'drupal'),
      '%Uri' => $base_url,
      '%Uri_brief' => substr($base_url, strlen('http://')),
      '%Mailto' => $to,
!     '%Date' => format_date(time()),
    );
!   $variables = $variables + $vars;
  
    $body = strtr(t($message), $variables);
    $subject = strtr(t($subject), $variables);
--- 991,1027 ----
   *  a better way around this but there was nothing mentioned in the
   *  documentation.)
   */
! function lm_paypal_mail_user($to_uid, $about_uid, $subject, $message, &$vars = array()) {
    // TODO: use Token module
    global $base_url;
  
    if (lm_paypal_debug()) {
!     watchdog(LM_PAYPAL, "lm_paypal_mail_user(%tuid, %auid, %sub, %msg, %vars)", array('%tuid' => $to_uid,'%auid' => $about_uid, '%sub' => $subject, '%msg' => $message, '%vars' => $vars));
    }
  
    $to_account = user_load(array('uid' => $to_uid, 'status' => 1));
    $to = $to_account->mail;
  
    $about_user = user_load(array('uid' => $about_uid, 'status' => 1));
!  
    //TODO: Maybe use the subscription adminstrators email instead?
    $from = variable_get('site_mail', ini_get('sendmail_from'));
  
+   $site_name = ( variable_get('site_name', false ) ? ' ('. variable_get('site_name', '') .')' : '' );
+ 
    $variables = array(
      '%Username' => $about_user->name,
      '%Login' => $about_user->login,
!     '%Site' => $site_name,
      '%Uri' => $base_url,
      '%Uri_brief' => substr($base_url, strlen('http://')),
      '%Mailto' => $to,
!     '%Date' => format_date(time())
    );
!   $variables = array_merge($variables, $vars);
! 
!   $body = str_replace("%site","%Site",$body);
!   $subject = str_replace("%site","%Site",$subject);
  
    $body = strtr(t($message), $variables);
    $subject = strtr(t($subject), $variables);
***************
*** 976,978 ****
--- 1078,1093 ----
      return array();
    }
  }
+ 
+ function lm_paypal_expire_date($subid,$started) {
+ 
+    $so = lm_paypal_subscriptions_load_subscription($subid);
+    $fudge = 0;
+    if (lm_paypal_cron_publish()) {
+       if (lm_paypal_expire_fudge()) $fudge = 1; // 24 hour fudge to allow cron to run or boost and varnish to clear.
+    }
+    $expires = $started + $fudge + lm_paypal_subscription_days($so) * 3600*24;
+ 
+ return $expires;
+ 
+ }
diff -cr lm_paypal.orig/lm_paypal_paid_adverts/lm_paypal_paid_adverts.module lm_paypal/lm_paypal_paid_adverts/lm_paypal_paid_adverts.module
*** lm_paypal.orig/lm_paypal_paid_adverts/lm_paypal_paid_adverts.module	2009-04-17 00:33:46.000000000 +0000
--- lm_paypal/lm_paypal_paid_adverts/lm_paypal_paid_adverts.module	2010-02-14 16:51:29.000000000 +0000
***************
*** 36,41 ****
--- 36,42 ----
        $cache[$object->node_type][$object->subid] = $object->subid;
      }
    }
+ 
    if ($node_type) {
      return isset($cache[$node_type]) ? $cache[$node_type] : array();
    }
***************
*** 76,82 ****
   */
  function lm_paypal_paid_adverts_type_is_handled($node_type) {
    $live = lm_paypal_paid_adverts_get_live_subscriptions($node_type);
!   return ! empty($live);
  }
  
  /**
--- 77,83 ----
   */
  function lm_paypal_paid_adverts_type_is_handled($node_type) {
    $live = lm_paypal_paid_adverts_get_live_subscriptions($node_type);
!   return (!empty($live));
  }
  
  /**
***************
*** 104,110 ****
      // Does this node type have a PayPal subscription associated with it
      if (is_array($ts) && count($ts) > 0) {
        // It does so print some help
!       $output = '<center><b>'. t('Nodes of this type will not be published until paid for.<br/>After Submitting this new node use the <em>PayPal Publish</em> tab to publish.<br/>The status of paid items can be found under <em>my account</em>.') .'</b></center>';
        return $output;
      }
    }
--- 105,117 ----
      // Does this node type have a PayPal subscription associated with it
      if (is_array($ts) && count($ts) > 0) {
        // It does so print some help
!         if ($type == "software") {
!         $output = t("It can take up to 4 weeks before this is published.  You can escalate this process and have it published in 24 hours by using the <Em>PayPal Publish</em> tab at the top that you will see after saving.");
!         } else {
!         $output = t("After creating, use the <em>PayPal Publish</em> tab at top to make it live!");
!         $output .= t("<br><br>Note: It can take up 24 hours for your listing to appear after your payment has cleared.");
!         }
! //      $output = '<center><b>'. t('Nodes of this type will not be published until paid for.<br/>After Submitting this new node use the <em>PayPal Publish</em> tab to publish.<br/>The status of paid items can be found under <em>my account</em>.') .'</b></center>';
        return $output;
      }
    }
***************
*** 160,167 ****
  function lm_paypal_paid_adverts_menu() {
    $items = array();
    // Main tab
    $items['admin/settings/lm_paypal/paid_adverts'] = array(
!     'title' => 'Paid Adverts',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('lm_paypal_paid_adverts_settings_form'),
      'access arguments' => array('administer lm_paypal'),
--- 167,175 ----
  function lm_paypal_paid_adverts_menu() {
    $items = array();
    // Main tab
+ 
    $items['admin/settings/lm_paypal/paid_adverts'] = array(
!     'title' => 'PayPal Payments',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('lm_paypal_paid_adverts_settings_form'),
      'access arguments' => array('administer lm_paypal'),
***************
*** 177,184 ****
    );
    // Tab 4.2: Show Paid Advertisements
    $items['admin/settings/lm_paypal/paid_adverts/show'] = array(
!     'title' => 'Show Paid Adverts',
!     'description' => 'Show all PayPal Paid Adverts.',
      'page callback' => 'lm_paypal_paid_adverts_list_nodes',
      'access arguments' => array('administer lm_paypal'),
      'type' => MENU_LOCAL_TASK,
--- 185,192 ----
    );
    // Tab 4.2: Show Paid Advertisements
    $items['admin/settings/lm_paypal/paid_adverts/show'] = array(
!     'title' => 'Show Payments',
!     'description' => 'Show all PayPal Payments.',
      'page callback' => 'lm_paypal_paid_adverts_list_nodes',
      'access arguments' => array('administer lm_paypal'),
      'type' => MENU_LOCAL_TASK,
***************
*** 231,245 ****
    if (! $account) {
      global $user;
      $account = $user;
    }
!   return lm_paypal_paid_adverts_node_is_handled($node)
!     and (
!       node_access('update', $node, $account)
!       or user_access('access unpaid adverts', $account)
!       or user_access('administer lm_paypal', $account)
        // In some cases, node creator may not have rights to edit its own content
        or $node->uid == $account->uid
!     );
  }
  
  /**
--- 239,253 ----
    if (! $account) {
      global $user;
      $account = $user;
+ 
    }
!   return (lm_paypal_paid_adverts_node_is_handled($node) and (
!  //     node_access('update', $node, $account)
!  //     or user_access('access unpaid adverts', $account)
!       user_access('administer lm_paypal', $account)
        // In some cases, node creator may not have rights to edit its own content
        or $node->uid == $account->uid
!     ));
  }
  
  /**
***************
*** 254,260 ****
        $links[] = array(
          'title' => t('Mark as paid'),
          'href' => 'admin/lm_paypal/subscriber/pay/'.$subid.'/'.$object->nid.'/'.$object->uid,
!         'attributes'  => array('title' => t('Click on this link to mark this node as paid without having to go to PayPal.')),
          'query' => array('destination' => $_GET['q']),
        );
      }
--- 262,268 ----
        $links[] = array(
          'title' => t('Mark as paid'),
          'href' => 'admin/lm_paypal/subscriber/pay/'.$subid.'/'.$object->nid.'/'.$object->uid,
!         'attributes'  => array('title' => t('Click on this link to mark this as paid without having to go to PayPal.')),
          'query' => array('destination' => $_GET['q']),
        );
      }
***************
*** 274,291 ****
    if (lm_paypal_paid_adverts_node_is_paid($node)) {
      require_once(drupal_get_path('module', 'lm_paypal_subscriptions').'/lm_paypal_subscriptions.pages.inc');
      $sub .= lm_paypal_subscribe_subscription($node->lm_paypal_paid_adverts->payement_subid, 23);
!     return t("This article has been paid for by the subscription: !sub. Thank you.", array("!sub" => $sub));
    }
  
!   $output .= '<p>'. t('Not yet paid for. To make this article viewable by others you need to pay with PayPal. Please take out <em>ONE</em> of the subscriptions below.') .'</p>';
  
    $type_subs = lm_paypal_paid_adverts_get_live_subscriptions();
    if (! empty($type_subs[$node->type])) foreach ($type_subs[$node->type] as $subid => $enabled) {
      if ($enabled) {
        require_once(drupal_get_path('module', 'lm_paypal_subscriptions').'/lm_paypal_subscriptions.pages.inc');
        $output .= lm_paypal_subscribe_subscription($subid, 15, NULL, $node->nid);
      }
    }
    return $output;
  }
  
--- 282,315 ----
    if (lm_paypal_paid_adverts_node_is_paid($node)) {
      require_once(drupal_get_path('module', 'lm_paypal_subscriptions').'/lm_paypal_subscriptions.pages.inc');
      $sub .= lm_paypal_subscribe_subscription($node->lm_paypal_paid_adverts->payement_subid, 23);
!     return t("This has been paid for by the subscription: !sub. Thank you.", array("!sub" => $sub));
    }
  
!   $term_subid = $_SESSION['lm_paypal_I_agree'];
! 
!   if ($term_subid) {
!      $output .= '<p>'. t('To make your payment, click "Proceed to Paypal" button below.') . '</p>';
!   } else {
!      $output .= '<p>'. t('Not yet paid for.  To Publish, Please agree to <em>1</em> of the options below') .'</p>';
!   }
  
    $type_subs = lm_paypal_paid_adverts_get_live_subscriptions();
    if (! empty($type_subs[$node->type])) foreach ($type_subs[$node->type] as $subid => $enabled) {
      if ($enabled) {
+       if ($term_subid == $subid || !$term_subid) {
        require_once(drupal_get_path('module', 'lm_paypal_subscriptions').'/lm_paypal_subscriptions.pages.inc');
        $output .= lm_paypal_subscribe_subscription($subid, 15, NULL, $node->nid);
+       }
      }
    }
+ 
+   if ($term_subid) {
+  // This reset the subid agreement
+ //     lm_paypal_get_form_index(TRUE); // reset form index
+      lm_paypal_subscriptions_get_agreement(NULL, FALSE);
+   }
+ 
+   lm_paypal_get_form_index(TRUE); // reset form index
    return $output;
  }
  
***************
*** 297,318 ****
  function lm_paypal_paid_adverts_user($op, &$edit, &$account, $category = NULL) {
    global $user;
  
    // In the "my account" view area show all paid_adverts
    if ($op == 'view' && (user_access('administer lm_paypal') || $user->uid == $account->uid)) {
      $account->content['paid_adverts'] = array(
        '#type' => 'user_profile_category',
!       '#title' => t('PayPal Paid Adverts'),
        '#weight' => 6, // under History which is 5.
      );
      $account->content['paid_adverts']['subs'] = array(
        '#type' => 'user_profile_item',
!       '#title' => t('Adverts you paid with us:'),
        '#value' => lm_paypal_paid_adverts_list_nodes($account->uid, 10),
      );
    }
  }
  
  function lm_paypal_paid_adverts_list_nodes($uid = 0, $nodes_per_page = 50) {
    $type_subs = lm_paypal_paid_adverts_get_live_subscriptions();
  
    if (!is_numeric($uid) || ((int) $uid) != $uid) {
--- 321,374 ----
  function lm_paypal_paid_adverts_user($op, &$edit, &$account, $category = NULL) {
    global $user;
  
+   if (lm_paypal_disable_myaccount()) {
+       $tstr = lm_paypal_get_subscription_types();
+       $sql = db_rewrite_sql("SELECT n.nid, n.uid, u.name, n.title, n.type, s.started, s.subid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {lm_paypal_subscribers} s ON n.nid = s.nid WHERE n.uid = %d AND n.type IN $tstr");
+       $result = db_query($sql,$account->uid);
+       if ($result->num_rows == 0) return;
+   }
+ 
    // In the "my account" view area show all paid_adverts
    if ($op == 'view' && (user_access('administer lm_paypal') || $user->uid == $account->uid)) {
      $account->content['paid_adverts'] = array(
        '#type' => 'user_profile_category',
!       '#title' => t('PayPal Payments'),
        '#weight' => 6, // under History which is 5.
      );
      $account->content['paid_adverts']['subs'] = array(
        '#type' => 'user_profile_item',
!       '#title' => t(''),
        '#value' => lm_paypal_paid_adverts_list_nodes($account->uid, 10),
      );
    }
  }
  
+ function lm_paypal_get_subscription_types() {
+ 
+   $type_subs = lm_paypal_paid_adverts_get_live_subscriptions();
+ // Create a list of known paid node types
+ 
+   $ts = array();
+   foreach ($type_subs as $k => $v) {
+     $ts[$k] = 1; // This will loose duplicates
+   }
+   // Turn it into a string to be used in an "IN" clause
+   $tstr = '(';
+   $nts = 0;
+   foreach ($ts as $t => $v) {
+     if ($nts > 0) {
+       $tstr .= ',';
+     }
+     $tstr .= "'". $t ."'";
+     ++$nts;
+   }
+   $tstr .= ')';
+ 
+   return $tstr;
+ }
+ 
  function lm_paypal_paid_adverts_list_nodes($uid = 0, $nodes_per_page = 50) {
+ 
    $type_subs = lm_paypal_paid_adverts_get_live_subscriptions();
  
    if (!is_numeric($uid) || ((int) $uid) != $uid) {
***************
*** 321,330 ****
  
    $header = array(
      array('data' => t('#'), 'field' => 'n.nid', 'sort' => 'desc'),
!     array('data' => t('User'), 'field' => 'u.name'),
      array('data' => t('Title'), 'field' => 'n.title'),
      array('data' => t('Type'), 'field' => 'n.type'),
!     array('data' => t('Published')),
    );
  
    // Create a list of known paid node types
--- 377,386 ----
  
    $header = array(
      array('data' => t('#'), 'field' => 'n.nid', 'sort' => 'desc'),
!     array('data' => t('Name'), 'field' => 'u.name'),
      array('data' => t('Title'), 'field' => 'n.title'),
      array('data' => t('Type'), 'field' => 'n.type'),
!     array('data' => t('Status')),
    );
  
    // Create a list of known paid node types
***************
*** 346,356 ****
  
    $rows = array();
    if ($nts > 0) {
      if ($uid == 0) {
        $sql = db_rewrite_sql("SELECT n.nid, n.uid, u.name, n.title, n.type FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.type IN $tstr");
      }
      else {
!       $sql = db_rewrite_sql("SELECT n.nid, n.uid, u.name, n.title, n.type FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.uid = %d AND n.status = 1 AND n.type IN $tstr");
      }
  
      $tablesort = tablesort_sql($header);
--- 402,413 ----
  
    $rows = array();
    if ($nts > 0) {
+ 
      if ($uid == 0) {
        $sql = db_rewrite_sql("SELECT n.nid, n.uid, u.name, n.title, n.type FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.type IN $tstr");
      }
      else {
!    $sql = db_rewrite_sql("SELECT n.nid, n.uid, u.name, n.title, n.type, s.started, s.subid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {lm_paypal_subscribers} s ON n.nid = s.nid WHERE n.uid = %d AND n.type IN $tstr");
      }
  
      $tablesort = tablesort_sql($header);
***************
*** 365,382 ****
        $paidfor = lm_paypal_paid_adverts_node_is_paid($node);
        $subid = lm_paypal_paid_adverts_node_is_handled($node);
  
!       if ($paidfor) {
!         $pub = t('published');
        }
        else {
!         $pub = l(t('not published'), "node/$n->nid/lm_paypal",
                  array(
                    'attributes' => array(
                      'alt' => t('Link to the page allowing you to publish this ad.'),
                      'title' => t('Click to pay for this ad to be published immediately.'),
                    ),
                  ));
-       }
  
        $link_to_node = l($n->title, "node/$n->nid",
          array(
--- 422,459 ----
        $paidfor = lm_paypal_paid_adverts_node_is_paid($node);
        $subid = lm_paypal_paid_adverts_node_is_handled($node);
  
!       $published = $node->status;
!       if (!lm_paypal_cron_publish()) $published = 1;
! 
!       $expires = "";
!       $started = "";
!       $msg_dt = "processing";
! 
!       if ($n->subid) {
!          $started = $n->started;
!          $expires = lm_paypal_expire_date($n->subid,$n->started);
!          $msg_dt = "paid " . date("m/d/Y", $started);
!          if (time() > $expires) $msg_dt = "ended " . date("m/d/Y", $expires);         
!       }
! 
!      if ($paidfor || $started) {
!         if ($published == 1) {
!             $msg = "running";
!         } else {
!             $msg = $msg_dt;
!         }
! //        $pub = t($msg);
        }
        else {
!         $msg = 'unpaid';
!       }
!         $pub = l($msg, "node/$n->nid/lm_paypal",
                  array(
                    'attributes' => array(
                      'alt' => t('Link to the page allowing you to publish this ad.'),
                      'title' => t('Click to pay for this ad to be published immediately.'),
                    ),
                  ));
  
        $link_to_node = l($n->title, "node/$n->nid",
          array(
***************
*** 386,406 ****
            ),
          )
        );
- 
        $rows[] = array('data' =>
!         array(
!           $n->nid,
!           l($n->name, "user/$n->uid"),
!           $link_to_node,
!           check_plain($node_types[$n->type]),
!           $pub,
!         ),
        );
      }
    }
  
    if (!$rows) {
!     $rows[] = array(array('data' => t('No paid adverts.'), 'colspan' => 5));
    }
  
    $output = theme('table', $header, $rows);
--- 463,482 ----
            ),
          )
        );
        $rows[] = array('data' =>
!             array(
!               $n->nid,
!               l($n->name, "user/$n->uid"),
!               $link_to_node,
!               check_plain($node_types[$n->type]),
!               $pub,
!             ),
        );
      }
    }
  
    if (!$rows) {
!     $rows[] = array(array('data' => t('No PayPal payments have been made.'), 'colspan' => 5));
    }
  
    $output = theme('table', $header, $rows);
***************
*** 412,432 ****
  /**
   * Implementation of hook_nodeapi().
   */
! function lm_paypal_paid_adverts_nodeapi(&$node, $op, $a3, $a4) {
    if ($op == 'load') {
      if ($subscription = lm_paypal_paid_adverts_type_is_handled($node->type)) {
        $data = new stdClass();
        $data->is_handled = TRUE;
        $data->subscriptions = lm_paypal_paid_adverts_get_live_subscriptions($node->type);
!       $data->payement_subid = lm_paypal_subscriptions_node_subscribed($node);
        return array('lm_paypal_paid_adverts' => $data);
      }
    }
  
!   if ($op == 'view'
!     && lm_paypal_paid_adverts_node_is_handled($node)
!     && ! $node->lm_paypal_paid_adverts->payement_subid)
!   {
      /*
       * If our user own the node, we have to avert him the node can be paid using
       * paypal.
--- 488,512 ----
  /**
   * Implementation of hook_nodeapi().
   */
! function lm_paypal_paid_adverts_nodeapi(&$node, $op, $a3 = NULL , $a4 = NULL) {
! 
    if ($op == 'load') {
      if ($subscription = lm_paypal_paid_adverts_type_is_handled($node->type)) {
        $data = new stdClass();
        $data->is_handled = TRUE;
        $data->subscriptions = lm_paypal_paid_adverts_get_live_subscriptions($node->type);
!       $data->payement_subid = lm_paypal_subscriptions_node_subscribed($node->nid);
        return array('lm_paypal_paid_adverts' => $data);
      }
    }
  
!   if (lm_paypal_cron_publish()) {
!      if ($node->status == 1) return;
!   }
! 
!   if ($op == 'view' && lm_paypal_paid_adverts_node_is_handled($node)) {
!   if (! $node->lm_paypal_paid_adverts->payement_subid)  {
! 
      /*
       * If our user own the node, we have to avert him the node can be paid using
       * paypal.
***************
*** 434,441 ****
      if (lm_paypal_paid_adverts_paypal_access($node)) {
        $subid = key($node->lm_paypal_paid_adverts->subscriptions);
  
        $node->content['paid_adverts_message'] = array(
!         '#value' => '<p>'.t('This classified ad for subscription #@subid is still unpaid. Other users will not be able to access it.', array(
            '@subid' => $subid
          )).'</p>',
          '#weight' => -10,
--- 514,522 ----
      if (lm_paypal_paid_adverts_paypal_access($node)) {
        $subid = key($node->lm_paypal_paid_adverts->subscriptions);
  
+       #'#value' => '<p>'.t('This item #@subid is still unpaid. Other users will not be able to access it.', array(
        $node->content['paid_adverts_message'] = array(
!         '#value' => '<p>'.t('This item is still unpaid. Other users will not be able to access it.<br> Use the "Paypal Publish" above to make it live.', array(
            '@subid' => $subid
          )).'</p>',
          '#weight' => -10,
***************
*** 466,469 ****
--- 547,552 ----
        $node->content['body']['#value'] = t('<em>Not yet published.</em>');
      }
    }
+  }
  }
+ 
diff -cr lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.admin.inc lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.admin.inc
*** lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.admin.inc	2009-05-30 17:05:54.000000000 +0000
--- lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.admin.inc	2010-01-25 21:35:20.000000000 +0000
***************
*** 1084,1090 ****
      else if ($sub->kind == 2) {
        $kind = t('Group');
      }
- 
      $rows[] = array('data' =>
        array(
          $kind,
--- 1084,1089 ----
diff -cr lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.module lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.module
*** lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.module	2009-05-30 17:05:54.000000000 +0000
--- lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.module	2010-02-05 06:37:26.000000000 +0000
***************
*** 249,254 ****
--- 249,255 ----
      'file' => 'lm_paypal_subscriptions.admin.inc',
      'type' => MENU_CALLBACK,
    );
+ 
    $items['admin/settings/lm_paypal/subscriptions/%/delete'] = array(
      'title' => 'Delete subscription',
      'type' => MENU_CALLBACK,
***************
*** 258,264 ****
      'access arguments' => array('subscription', 4),
      'file' => 'lm_paypal_subscriptions.admin.inc',
    );
- 
    // View and edit tabs
    $items['admin/settings/lm_paypal/subscriptions/%'] = array(
      'title' => 'View subscription details',
--- 259,264 ----
***************
*** 284,289 ****
--- 284,290 ----
      'file' => 'lm_paypal_subscriptions.admin.inc',
      'weight' => 2,
    );
+ 
    // View subscribers for one subscription
    $items['admin/settings/lm_paypal/subscriptions/%/subscribers'] = array(
      'title' => 'Subscribers',
***************
*** 295,301 ****
      'file' => 'lm_paypal_subscriptions.admin.inc',
      'weight' => 3,
    );
- 
    // View all suscribers
    $items['admin/settings/lm_paypal/subscribers'] = array(
      'title' => 'Subscribers',
--- 296,301 ----
***************
*** 305,311 ****
      'file' => 'lm_paypal_subscriptions.admin.inc',
      'weight' => 32,
    );
!   // View and edit a subscriber
    $items['admin/settings/lm_paypal/subscribers/subscriber/%'] = array(
      'title' => 'Subscriber',
      'page callback' => 'drupal_get_form',
--- 305,311 ----
      'file' => 'lm_paypal_subscriptions.admin.inc',
      'weight' => 32,
    );
!   // View and edit a subscriber 
    $items['admin/settings/lm_paypal/subscribers/subscriber/%'] = array(
      'title' => 'Subscriber',
      'page callback' => 'drupal_get_form',
***************
*** 360,366 ****
   * that checks our subscription/subscriber exists before we let the user access
   * to the page.
   */
! function lm_paypal_subscription_access($op, $subid) {
    // Superuser access bypass
    global $user;
    if ($user->uid == 1) {
--- 360,366 ----
   * that checks our subscription/subscriber exists before we let the user access
   * to the page.
   */
! function lm_paypal_subscription_access($op, $subid = NULL) {
    // Superuser access bypass
    global $user;
    if ($user->uid == 1) {
***************
*** 368,379 ****
    }
  
    if (user_access('administer lm_paypal')) {
      switch ($op) {
        case 'subscription':
          return lm_paypal_subscriptions_load_subscription($subid) != NULL;
  
        case 'subscriber':
!         return lm_paypal_subscriptions_load_subscriber($subid) != NULL;
      }
    }
    return FALSE;
--- 368,382 ----
    }
  
    if (user_access('administer lm_paypal')) {
+ 
+  //   return TRUE; //TODO: see why we check below if they have administer access.
+ 
      switch ($op) {
        case 'subscription':
          return lm_paypal_subscriptions_load_subscription($subid) != NULL;
  
        case 'subscriber':
!         return lm_paypal_subscriptions_load_subscriber(array('usid' => $user)) != NULL;
      }
    }
    return FALSE;
***************
*** 437,442 ****
--- 440,446 ----
      'subscr_id' => "'%s'",
      'uid' => "%d",
      'nid' => "%d",
+     'subid' => "%d",
    );
  
    // Static cache for later use
***************
*** 524,530 ****
   *   A subscriber object fully loaded with its data updated
   */
  function lm_paypal_subscriptions_save_subscriber($subscriber) {
!   if ($subscriber->status == LM_PAYPAL_SUBSCRIPTIONS_STATUS_LIVE) {
      $subscriber->started = time();
    }
    $update = $subscriber->usid ? array('usid') : array();
--- 528,534 ----
   *   A subscriber object fully loaded with its data updated
   */
  function lm_paypal_subscriptions_save_subscriber($subscriber) {
!   if ($subscriber->status == LM_PAYPAL_SUBSCRIPTIONS_STATUS_LIVE || $subscriber->$status == LM_PAYPAL_SUBSCRIPTIONS_STATUS_PAID) {
      $subscriber->started = time();
    }
    $update = $subscriber->usid ? array('usid') : array();
***************
*** 578,583 ****
--- 582,593 ----
  function lm_paypal_subscriptions_user($op, &$edit, &$account, $category = NULL) {
    global $user;
  
+ // don't show this if there are no subsriptions
+   if (lm_paypal_disable_myaccount()) {
+   $also  = db_query("SELECT * FROM {lm_paypal_subscriptions} d INNER JOIN {lm_paypal_subscribers} s ON d.subid = s.subid WHERE uid = %d AND s.status = 1 AND d.src > 0", array($user)); 
+    if (!db_result($also)) return;
+    }
+ 
    // TODO: On deleting a user issue a cancel on any subscriptions
    // obviously not on PayPal (can't!) just on here.
  
***************
*** 636,646 ****
--- 646,658 ----
    $nid = is_object($node) ? $node->nid : $nid = $node;
  
    if (! isset($cache[$nid])) {
+ 
      $result = db_query("SELECT subid FROM {lm_paypal_subscribers} WHERE status = 1 AND nid = %d", $nid);
  
      if ($data = db_fetch_object($result)) {
        $cache[$nid] = $data->subid;
      }
+ 
    }
  
    return isset($cache[$nid]) ? $cache[$nid] : FALSE;
***************
*** 914,923 ****
--- 926,939 ----
    if ($so->src && $so->srt == '') {
      return 0;
    }
+ 
    $multiply = 1;
    if ($so->src && $so->srt > 1) {
      $multiply = $so->srt;
    }
+ // - If using cron_publish then Use 1 since we only care about the current days for the current subscription cycle not alls days for alls cycles.  Also a subscriber may cancel subscription the subscription in paypal and the current paid cycle needs to complete since no refunds are offered.
+   if (lm_paypal_cron_publish()) $multiply = 1;
+ 
    return
      (lm_paypal_period_unit2days($so->p1, $so->t1)
        + lm_paypal_period_unit2days($so->p2, $so->t2)
***************
*** 925,930 ****
--- 941,948 ----
  }
  
  function _lm_paypal_subscriptions_ipn_is_valid($ipn) {
+   if ($ipn->subscr_id == $ipn->txn_id) return TRUE; // if they match it's a web accept payment
+ 
    $subscription = lm_paypal_subscriptions_load_subscription($ipn->item_number);
    $custom = lm_paypal_unpack_ipn_custom($ipn);
    return $subscription != NULL && $custom['module'] == LM_PAYPAL_SUBSCRIPTIONS;
***************
*** 1049,1054 ****
--- 1067,1073 ----
  }
  
  function lm_paypal_subscriptions_lm_paypal_ipn_subscr_signup($ipn) {
+ 
    if (! _lm_paypal_subscriptions_ipn_is_valid($ipn)) {
      return FALSE;
    }
***************
*** 1070,1075 ****
--- 1089,1096 ----
    // signups so this is possible).
    $subscriber = lm_paypal_subscriptions_load_subscriber(array('subscr_id' => $ipn->subscr_id));
  
+  // watchdog(LM_PAYPAL_SUBSCRIPTIONS, 'load signup subscr_id %sid', array('%sid' => $ipn->subscr_id));
+ 
    if (! $subscriber) {
      // Try and track problem maybe caused by multiple subscr_signups
      watchdog(LM_PAYPAL_SUBSCRIPTIONS, 'new subscr_signup subscr_id %sid', array('%sid' => $ipn->subscr_id));
***************
*** 1085,1091 ****
        case LM_PAYPAL_SUBSCRIPTIONS_STATUS_BLOCKED:
          watchdog(LM_PAYPAL_SUBSCRIPTIONS, 'Attempt to signup to blocked subscription, ignored', NULL, WATCHDOG_ERROR, $link);
          return;
- 
        case LM_PAYPAL_SUBSCRIPTIONS_STATUS_DEAD:
        case LM_PAYPAL_SUBSCRIPTIONS_STATUS_CANCELLED:
        case LM_PAYPAL_SUBSCRIPTIONS_STATUS_EOT:
--- 1106,1111 ----
***************
*** 1106,1111 ****
--- 1126,1133 ----
      }
    }
  
+   watchdog(LM_PAYPAL_SUBSCRIPTIONS, 'save signup subscr_id %sid', array('%sid' => $ipn->subscr_id));
+ 
    // A signup alone is enough to go live. A payment IPN may not arrive at all
    // if the subscription amount is zero (usually a trial period)
    $subscriber->status = LM_PAYPAL_SUBSCRIPTIONS_STATUS_LIVE;
***************
*** 1154,1168 ****
    return TRUE;
  }
  
  /**
   * Implementation of hook_lm_paypal_process_in_<txn_type>().
   */
  function lm_paypal_subscriptions_lm_paypal_ipn_subscr_payment($ipn) {
!   if (! _lm_paypal_subscriptions_ipn_is_valid($ipn)) {
      return FALSE;
    }
  
    if ($ipn->payment_status != 'Completed') {
      return FALSE;
    }
  
--- 1176,1214 ----
    return TRUE;
  }
  
+ function lm_paypal_subscriptions_lm_paypal_ipn_web_accept($ipn) {
+ 
+ if (empty($ipn->subscr_id)) {
+     $ipn->subscr_id = $ipn->txn_id;  // no subscr id for web accept use transaction id
+ }
+ 
+ return lm_paypal_subscriptions_lm_paypal_ipn_subscr_payment($ipn);
+ 
+ }
+ 
  /**
   * Implementation of hook_lm_paypal_process_in_<txn_type>().
   */
  function lm_paypal_subscriptions_lm_paypal_ipn_subscr_payment($ipn) {
! 
!      if (! _lm_paypal_subscriptions_ipn_is_valid($ipn)) {
!  watchdog(
!       LM_PAYPAL_SUBSCRIPTIONS,
!       'Invalid Transaction, ignored: %id',
!       array('%id' => check_plain($ipn->txn_id)),
!       WATCHDOG_WARNING,
!       $link);
! 
      return FALSE;
    }
  
    if ($ipn->payment_status != 'Completed') {
+  watchdog(
+       LM_PAYPAL_SUBSCRIPTIONS,
+       'Transaction status %st, ignored: %id',
+       array('%id' => check_plain($ipn->txn_id), '%st' => check_plain($ipn->payment_status)),
+       WATCHDOG_WARNING,
+       $link);
      return FALSE;
    }
  
***************
*** 1170,1175 ****
--- 1216,1228 ----
  
    // User validity check
    if (! $account = user_load($uid)) {
+  watchdog(
+       LM_PAYPAL_SUBSCRIPTIONS,
+       'User Invalid %user, ignored: %id',
+       array('%id' => check_plain($ipn->txn_id), '%user' => $account),
+       WATCHDOG_WARNING,
+       $link);
+ 
      return FALSE;
    }
  
***************
*** 1185,1196 ****
  
    // This should be an existing subscription (but sometimes payments come
    // before signups).
!   $ss = lm_paypal_subscriptions_load_subscriber($ipn->subscr_id);
  
    if (! $ss) {
      // Not already present.
      // Create an entry in the subscribers table.
!     lm_paypal_add_subscriber($so->kind, $uid, $nid, $so->subid, $ipn->subscr_id, LM_PAYPAL_SUBSCRIPTIONS_STATUS_PAID);
    }
    else {
      // It is already present
--- 1238,1249 ----
  
    // This should be an existing subscription (but sometimes payments come
    // before signups).
!   $ss = lm_paypal_subscriptions_load_subscriber(array('subscr_id' => $ipn->subscr_id));
  
    if (! $ss) {
      // Not already present.
      // Create an entry in the subscribers table.
!     $ss = lm_paypal_add_subscriber($so->kind, $uid, $nid, $so->subid, $ipn->subscr_id, LM_PAYPAL_SUBSCRIPTIONS_STATUS_PAID);
    }
    else {
      // It is already present
***************
*** 1207,1212 ****
--- 1260,1269 ----
      }
    }
  
+ // Completed payment received.  Let's update the status back to live and reset the start date.
+   $ss->status = LM_PAYPAL_SUBSCRIPTIONS_STATUS_LIVE;
+   lm_paypal_subscriptions_save_subscriber($ss);
+ 
    // Notify lm_paypal module we processed it.
    return TRUE;
  }
***************
*** 1252,1258 ****
        return;
    }
  
!   // Mark the subscription ended
    $subscriber->status = ($end == 'eot' ? LM_PAYPAL_SUBSCRIPTIONS_STATUS_EOT : LM_PAYPAL_SUBSCRIPTIONS_STATUS_CANCEL);
    lm_paypal_subscriptions_save_subscriber($subscriber);
  
--- 1309,1318 ----
        return;
    }
  
! // We can recieve a cancel anytime, even if the current subscription is paid for.  Let the cron cancel it normally when it expires when using the lm_paypal_cron_publish option.  
! 
! if (!lm_paypal_cron_publish()) { 
! // Mark the subscription ended
    $subscriber->status = ($end == 'eot' ? LM_PAYPAL_SUBSCRIPTIONS_STATUS_EOT : LM_PAYPAL_SUBSCRIPTIONS_STATUS_CANCEL);
    lm_paypal_subscriptions_save_subscriber($subscriber);
  
***************
*** 1294,1300 ****
      }
      lm_paypal_mail_user($uid, $uid, $so->send_user_onend_subject, $so->send_user_onend_body, $variables);
    }
! 
    // Notify lm_paypal module we processed it.
    return TRUE;
  }
--- 1354,1360 ----
      }
      lm_paypal_mail_user($uid, $uid, $so->send_user_onend_subject, $so->send_user_onend_body, $variables);
    }
! }
    // Notify lm_paypal module we processed it.
    return TRUE;
  }
diff -cr lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.pages.inc lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.pages.inc
*** lm_paypal.orig/lm_paypal_subscriptions/lm_paypal_subscriptions.pages.inc	2009-05-30 17:05:54.000000000 +0000
--- lm_paypal/lm_paypal_subscriptions/lm_paypal_subscriptions.pages.inc	2010-01-26 07:58:04.000000000 +0000
***************
*** 6,11 ****
--- 6,16 ----
   * Paypal subscriptions interface, user pages.
   */
  
+ // Paypal form: handle 'cmd' setting for paypal
+ define('LM_PAYPAL_FORM_CMD_BUY_NOW',   1);
+ define('LM_PAYPAL_FORM_CMD_DONATE',    2);
+ define('LM_PAYPAL_FORM_CMD_SUBSCRIBE', 3);
+ 
  /////////////////////////// BELOW THIS LINE CODE IS WORKING BUT UGLY
  
  
***************
*** 115,122 ****
        }
        $amount = theme('lm_paypal_amount', $so->a3, $so->currency_code, TRUE);
        $duration = lm_paypal_unit2str($so->p3, $so->t3);
!       $output .= t('Rate !amount for %duration. ', array('!amount' => $amount, '%duration' => $duration));
        if ($so->src) {
          if ($so->srt != '') {
            $times = $so->srt;
            $output .= t('Recurs %times times.', array('%times' => $times)) ;
--- 120,130 ----
        }
        $amount = theme('lm_paypal_amount', $so->a3, $so->currency_code, TRUE);
        $duration = lm_paypal_unit2str($so->p3, $so->t3);
! 
! 
        if ($so->src) {
+         $output .= t('Rate !amount for %duration. ', array('!amount' => $amount, '%duration' => $duration));
+ 
          if ($so->srt != '') {
            $times = $so->srt;
            $output .= t('Recurs %times times.', array('%times' => $times)) ;
***************
*** 124,136 ****
          else {
            $output .= t('Recurs till cancelled') .'. ';
          }
        }
        if ($so->kind == 0) {
          $role = $roles[$so->rid];
          $output .= t('User becomes member of role %role. ', array('%role' => $role));
        }
        else if ($so->kind == 1) {
!         $output .= t('Node becomes viewable by others after the payment was confirmed. ');
        }
        else if ($so->kind == 2) {
          $group = $groups[$so->rid];
--- 132,147 ----
          else {
            $output .= t('Recurs till cancelled') .'. ';
          }
+ //add message to not include duration for 1 time charges
+       } else {
+         $output .= t('Rate !amount', array('!amount' => $amount)) . ".  " ; 
        }
        if ($so->kind == 0) {
          $role = $roles[$so->rid];
          $output .= t('User becomes member of role %role. ', array('%role' => $role));
        }
        else if ($so->kind == 1) {
!         $output .= t('Item will be viewable by others in 24 hours after the payment is confirmed. ');
        }
        else if ($so->kind == 2) {
          $group = $groups[$so->rid];
***************
*** 143,149 ****
          $output .= t('(Duration - till cancelled.)');
        }
        else {
!         $output .= t('(Duration %duration days.)', array('%duration' => $duration));
        }
  
        if (($display & 16) == 0) {
--- 154,160 ----
          $output .= t('(Duration - till cancelled.)');
        }
        else {
!           if ($so->src) $output .= t('(Duration %duration days.)', array('%duration' => $duration));
        }
  
        if (($display & 16) == 0) {
***************
*** 222,227 ****
--- 233,242 ----
   *   TODO: ??
   */
  function lm_paypal_subscribe($subid = NULL, $display = 15, $button_url = NULL, $nid = NULL, $account = NULL) {
+ 
+  //reset form index 
+   lm_paypal_get_form_index(TRUE);
+ 
    $subid = (int) $subid;
    if ($subid) {
      return lm_paypal_subscribe_subscription($subid, $display, $button_url, $nid);
***************
*** 231,236 ****
--- 246,262 ----
    }
  }
  
+ //index for term form when displaying multiple term forms on same page. 
+ function lm_paypal_get_form_index($breset = FALSE) {
+  if ($breset) {
+     unset($_SESSION['lm_paypal_form_index']);
+  }
+   else {
+     $_SESSION['lm_paypal_form_index'] += 1;
+     return $_SESSION['lm_paypal_form_index'];
+   }
+ }
+ 
  function lm_paypal_subscriptions_get_agreement($subid = NULL, $set = FALSE) {
    if ($set) {
      $_SESSION['lm_paypal_I_agree'] = $subid;
***************
*** 247,273 ****
    }
  }
  
  /**
   * The user has to agree to the terms and conditions
   */
  function lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format) {
    $form['#submit'] = array('lm_paypal_subscription_terms_submit');
  
    // This reset the subid agreement
!   lm_paypal_subscriptions_get_agreement(NULL, FALSE);
  
    $form['terms'] = array(
      '#type' => 'markup',
      '#value' => t('<h2>Terms and Conditions</h2><p>') . check_markup($terms, $terms_format) .
!     t('</p><p><em>Before you can subscribe you must agree to these terms and conditions</em></p>'),
    );
    // Some countries made legally mandatory the checkbox to agress before submit
    // TODO: maybe we should put this checkbox in a variable_get from module
    $form['terms_accepted'] = array(
      '#type' => 'checkbox',
      // TODO: and the text is bad too, we should really put it in a variable too
!     '#title' => t('Checking this option, I confirm that I read and understood all terms.'),
      '#default_value' => 0,
    );
    $form['terms_subid'] = array(
      '#type' => 'hidden',
--- 273,330 ----
    }
  }
  
+ function lm_paypal_subscriptions_terms_form_1($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_2($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_3($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_4($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_5($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_6($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_7($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_8($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ function lm_paypal_subscriptions_terms_form_9($form_state, $subid, $terms, $terms_format) {
+    return lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format);
+ }
+ 
  /**
   * The user has to agree to the terms and conditions
   */
  function lm_paypal_subscriptions_terms_form($form_state, $subid, $terms, $terms_format) {
+ 
    $form['#submit'] = array('lm_paypal_subscription_terms_submit');
  
    // This reset the subid agreement
! // commented out below and moved logic to somewhere else .
! //  lm_paypal_subscriptions_get_agreement(NULL, FALSE);
  
    $form['terms'] = array(
      '#type' => 'markup',
      '#value' => t('<h2>Terms and Conditions</h2><p>') . check_markup($terms, $terms_format) .
!     t('</p><p><em>Before you can subscribe, you must agree to these terms and conditions</em></p>'),
    );
    // Some countries made legally mandatory the checkbox to agress before submit
    // TODO: maybe we should put this checkbox in a variable_get from module
    $form['terms_accepted'] = array(
      '#type' => 'checkbox',
      // TODO: and the text is bad too, we should really put it in a variable too
!     '#title' => t('Checking this option, I confirm that I have read and understood all terms.'),
      '#default_value' => 0,
+     '#return_value' => $subid,
    );
    $form['terms_subid'] = array(
      '#type' => 'hidden',
***************
*** 277,288 ****
      '#type' => 'submit',
      '#value' => t('I agree'),
    );
    return $form;
  }
  
  function lm_paypal_subscription_terms_submit($form, &$form_state) {
    if ($form_state['values']['terms_accepted']) {
!     $subid = ($form_state['values']['terms_subid']);
      lm_paypal_subscriptions_get_agreement($subid, TRUE);
    }
    else {
--- 334,347 ----
      '#type' => 'submit',
      '#value' => t('I agree'),
    );
+ 
    return $form;
  }
  
  function lm_paypal_subscription_terms_submit($form, &$form_state) {
+ 
    if ($form_state['values']['terms_accepted']) {
!     $subid = ($form_state['values']['terms_accepted']);
      lm_paypal_subscriptions_get_agreement($subid, TRUE);
    }
    else {
***************
*** 290,296 ****
    }
  }
  
! /**
   * Display single subscription with a link to PayPal.
   *
   * @param int $subid
--- 349,355 ----
    }
  }
  
! /*
   * Display single subscription with a link to PayPal.
   *
   * @param int $subid
***************
*** 323,332 ****
      return $output;
    }
  
    $terms = trim($so->terms);
    if (! empty($terms)) {
      if (! lm_paypal_subscriptions_get_agreement($subid)) {
!       $output .= drupal_get_form('lm_paypal_subscriptions_terms_form', $subid, $so->terms, $so->terms_format);
        return $output;
      }
    }
--- 382,397 ----
      return $output;
    }
  
+   if (isset($_SESSION['lm_paypal_I_agree'])) {
+      if ($_SESSION['lm_paypal_I_agree'] != $subid) return;  // only display term that was agreed to.
+   }
+ 
    $terms = trim($so->terms);
    if (! empty($terms)) {
      if (! lm_paypal_subscriptions_get_agreement($subid)) {
!       $next_form_index = lm_paypal_get_form_index();   
!       $id = 'lm_paypal_subscriptions_terms_form_' . $next_form_index; 
!       $output .= drupal_get_form($id, $subid, $so->terms, $so->terms_format);
        return $output;
      }
    }
***************
*** 347,354 ****
--- 412,431 ----
      $return_path = lm_paypal_subscriptions_get_inprogress();
    }
  
+ // add host and node id to item desription
+     $nid=request_uri() . "/";
+     if (strpos($nid,"node") !== 0) {
+        $nid=str_replace("/lm_paypal/","",$nid);
+        $nid=str_replace("/node/","",$nid);
+     } else {
+       $nid="";
+     }
+ 
    $item_name = check_plain($so->item_name);
  
+   if (lm_paypal_prefix_nid()) $item_name = $nid . "/" . $item_name;
+   if (lm_paypal_prefix_hostname()) $item_name = $_SERVER['SERVER_NAME'] . "/" . $item_name;
+ 
    // Output a form that will redirect the user to PayPal - note all the fields
    // are hidden so only the submit appears
    $hidden = array();
***************
*** 367,373 ****
--- 444,453 ----
      $hidden['p3'] = check_plain($so->p3);
      $hidden['t3'] = check_plain($so->t3);
    }
+ 
+   $cmd = LM_PAYPAL_FORM_CMD_BUY_NOW;
    if (! empty($so->srt)) {
+     $cmd = LM_PAYPAL_FORM_CMD_SUBSCRIBE;
      $hidden['srt'] = check_plain($so->srt);
      // If recurring times is set but recurring isn't then set it otherwise
      // PayPal will reject this request
***************
*** 375,386 ****
--- 455,468 ----
        $hidden['src'] = "1";
      }
    }
+ 
    if (! empty($so->src)) {
      $hidden['src'] = check_plain($so->src);
    }
    $hidden['sra'] = "1";
    $hidden['no_note'] = "1";
    $hidden['usr_manage'] = "0";
+ 
    $output .= drupal_get_form(
      'lm_paypal_api_payment_form',
      $so->a3,
***************
*** 394,400 ****
          'module' => LM_PAYPAL_SUBSCRIPTIONS,
        ),
        'hidden' => $hidden,
!       'cmd' => LM_PAYPAL_FORM_CMD_SUBSCRIBE,
      )
    );
  
--- 476,482 ----
          'module' => LM_PAYPAL_SUBSCRIPTIONS,
        ),
        'hidden' => $hidden,
!       'cmd' => $cmd,
      )
    );
  
