Index: uc_paypal.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/payment/uc_paypal/uc_paypal.module,v
retrieving revision 1.5.2.13
diff -u -p -r1.5.2.13 uc_paypal.module
--- uc_paypal.module	28 Apr 2009 21:03:16 -0000	1.5.2.13
+++ uc_paypal.module	14 Jun 2009 06:41:44 -0000
@@ -895,7 +895,40 @@ function uc_paypal_wps_form($form_state,
     $data['paymentaction'] = 'authorization';
   }
 
-  if (variable_get('uc_paypal_wps_submit_method', 'single') == 'itemized') {
+  // if recurring fees exist in the order
+  $recurring_fees = array();
+  if (function_exists('uc_recurring_find_fees')) {
+     $recurring_fees = uc_recurring_find_fees($order);
+  }
+
+  if (count($recurring_fees) > 0) {
+    // TODO: what do we do if someone tries to order more then one subscription??
+    // we will just process the first for now
+    $recurring_fee = $recurring_fees[0]; 
+    if (count($recurring_fees) > 1) {
+      drupal_set_message(t('Sorry recurring payments can only be setup for one product at a time when paying via paypal, only the first recurring payment will be setup when you click on Submit order.'), 'warning');
+    }
+
+    $data['cmd'] = '_xclick-subscriptions';  
+    $data['item_name'] = t('Order @order_id at !store', array('@order_id' => $order->order_id, '!store' => variable_get('uc_store_name', url('<front>', array('absolute' => TRUE)))));
+    // first payment
+    list($p, $t) = explode(' ', $recurring_fee->initial_charge);
+    $data['a1'] = uc_price($order->order_total, $context, $options);
+    $data['p1'] = $p;  
+    $data['t1'] = strtoupper($t[0]); 
+    // recurring payments
+    list($p, $t) = explode(' ', $recurring_fee->regular_interval);
+    $data['a3'] = uc_price($recurring_fee->fee_amount, $context, $options);
+    $data['p3'] = $p;  
+    $data['t3'] = strtoupper($t[0]);
+    
+    $data['src'] = 1;
+    if ($recurring_fee->number_intervals > 0) {
+      $data['srt'] = $recurring_fee->number_intervals;
+    }
+    $data['sra'] = 1; // reattempt failed payments
+  } 
+  else if (variable_get('uc_paypal_wps_submit_method', 'single') == 'itemized') {
     // List individual items
     $i = 0;
     foreach ($order->products as $item) {
@@ -940,6 +973,24 @@ function uc_paypal_wps_form($form_state,
   return $form;
 }
 
+/**
+ * Callback for setting up a recurring payment for WPS
+ */
+function uc_recurring_paypal_wps_fee($order, &$fee) {
+  // the recurring payment is setup at the time of product purchase
+  $subscr_id = check_plain($_POST['subscr_id']);
+  $fee->fee_handler = 'paypal_wps';
+  $fee->data = array('subscr_id' => $subscr_id);
+  return TRUE;
+}
+
+/**
+ * Implements callback uc_recurring_[fee_handler]_renew()
+ */
+function uc_recurring_paypal_wps_renew() {
+  return TRUE;
+}
+
 // Sends a request to PayPal and returns a response array.
 function uc_paypal_api_request($request, $server) {
   $request['USER'] = variable_get('uc_paypal_api_username', '');
Index: uc_paypal.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/payment/uc_paypal/Attic/uc_paypal.pages.inc,v
retrieving revision 1.1.2.8
diff -u -p -r1.1.2.8 uc_paypal.pages.inc
--- uc_paypal.pages.inc	18 Apr 2009 07:29:16 -0000	1.1.2.8
+++ uc_paypal.pages.inc	14 Jun 2009 06:41:44 -0000
@@ -16,6 +16,41 @@ function uc_paypal_ipn($order_id = 0) {
     return;
   }
 
+  // Assign posted variables to local variables
+  $payment_status = check_plain($_POST['payment_status']);
+  $payment_amount = check_plain($_POST['mc_gross']);
+  $payment_currency = check_plain($_POST['mc_currency']);
+  $payment_fee = check_plain($_POST['mc_fee']);
+  $receiver_email = check_plain($_POST['receiver_email']);
+  $txn_id = check_plain($_POST['txn_id']);
+  $txn_type = check_plain($_POST['txn_type']);
+  $payer_email = check_plain($_POST['payer_email']);
+
+  // subscription
+  $subscr_id = check_plain($_POST['subscr_id']);
+
+  switch ($txn_type) {
+    case 'subscr_signup':
+      if (module_exists('uc_recurring')) { 
+        // first we need to setup the recurring fee, since paypal_wps overrides the submit order function we need to call uc_recurring_order submit function here
+        uc_recurring_order('submit', $order, NULL);
+        $txn_id = $subscr_id; // subscriptions do not have a txn_id, so we will record the subscr_id instead
+      }
+      break;
+    case 'subscr_payment':
+      if (module_exists('uc_recurring')) {
+        // fetch fee from database
+        $fee = db_fetch_array(db_query("SELECT ru.* from {uc_recurring_users} ru 
+                                          LEFT JOIN {uc_payment_paypal_ipn} ppi ON ru.order_id=ppi.order_id 
+                                        WHERE ppi.txn_id = '%s' AND ppi.txn_type = 'subscr_signup'", $subscr_id));
+        if ($fee && $order_id != $fee['order_id']) { // if this is not the first payment we need to create a new order in ubercart
+          $order_id = uc_recurring_renew($fee);
+          db_query("UPDATE {uc_payment_paypal_ipn} SET order_id = %d WHERE txn_id = '%s' AND txn_type = 'subscr_signup'", $order_id, $subscr_id);
+        }
+      }
+      break;
+  }
+
   if (($len = strpos($_POST['invoice'], '-')) > 0) {
     $order_id = intval(substr($_POST['invoice'], 0, $len));
   }
@@ -30,23 +65,11 @@ function uc_paypal_ipn($order_id = 0) {
     return;
   }
 
-  // Assign posted variables to local variables
-  $payment_status = check_plain($_POST['payment_status']);
-  $payment_amount = check_plain($_POST['mc_gross']);
-  $payment_currency = check_plain($_POST['mc_currency']);
-  $payment_fee = check_plain($_POST['mc_fee']);
-  $receiver_email = check_plain($_POST['receiver_email']);
-  $txn_id = check_plain($_POST['txn_id']);
-  $txn_type = check_plain($_POST['txn_type']);
-  $payer_email = check_plain($_POST['payer_email']);
-
   $req = '';
-
   foreach ($_POST as $key => $value) {
     $value = urlencode(stripslashes($value));
     $req .= $key .'='. $value .'&';
   }
-
   $req .= 'cmd=_notify-validate';
 
   if (variable_get('uc_paypal_wpp_server', '') == 'https://api-3t.paypal.com/nvp') {
@@ -67,7 +90,7 @@ function uc_paypal_ipn($order_id = 0) {
   if (strcmp($response->data, 'VERIFIED') == 0) {
     watchdog('uc_paypal', 'IPN transaction verified.');
 
-    $duplicate = db_result(db_query("SELECT COUNT(*) FROM {uc_payment_paypal_ipn} WHERE txn_id = '%s' AND status <> 'Pending'", $txn_id));
+    $duplicate = db_result(db_query("SELECT COUNT(*) FROM {uc_payment_paypal_ipn} WHERE txn_id = '%s' AND txn_type = '%s' AND status <> 'Pending'", $txn_id, $txn_type));
     if ($duplicate > 0) {
       if ($order->payment_method != 'credit') {
         watchdog('uc_paypal', 'IPN transaction ID has been processed before.', array(), WATCHDOG_NOTICE);
