--- uc_paypal.module	2010-08-11 17:52:20.000000000 +0000
+++ uc_paypal.module.new	2010-09-07 08:37:42.000000000 +0000
@@ -196,6 +196,7 @@ function uc_paypal_payment_method() {
     'no_gateway' => TRUE,
   );
 
+
   return $methods;
 }
 
@@ -642,6 +643,23 @@ function uc_paypal_ec_checkout($form, &$
     unset($_SESSION['cart_order']);
     drupal_goto('cart');
   }
+  if(function_exists("uc_recurring_get_recurring_products_in_order")){
+		  $billings=uc_recurring_get_recurring_products_in_order($order);
+		  if (count($billings)>10){
+		    drupal_set_message(
+			t(
+			'You have over 10 recurring items in your shopping cart, while Paypal Express Checkout can only handle ten recurring items per checkout. Please remove some items from your cart and add them to a separate purchase, or use another payment method.'
+			), 
+			'warning'
+		    );
+		    drupal_goto('cart');
+		    return;
+	  }
+
+  }
+
+
+
 
   list($desc, $subtotal) = _uc_paypal_product_details($order->products);
 
@@ -686,13 +704,29 @@ function uc_paypal_ec_checkout($form, &$
     'PHONENUM' => substr($order->delivery_phone, 0, 20),
   );
 
+
+  if(function_exists("uc_recurring_get_recurring_products_in_order")){
+	$nvp_request["VERSION"]="50.0"; // EC Recurring REQUIRES this
+	$ii=0;
+	$billings=uc_recurring_get_recurring_products_in_order($order);
+	$_SESSION["paypal_billing"]=array();
+	foreach ($billings as $recurring_product){
+		if ($ii>9) break;
+		$agreement=check_plain("Renewal of product ".$recurring_product["product"]->title);
+		$nvp_request["L_BILLINGTYPE".$ii]="RecurringPayments";
+		$nvp_request["L_BILLINGAGREEMENTDESCRIPTION".$ii]=$agreement;
+		$ii++;
+	}
+  }
+
   if (!uc_cart_is_shippable()) {
     $nvp_request['NOSHIPPING'] = 1;
     unset($nvp_request['ADDROVERRIDE']);
   }
-
+  watchdog ("uc_paypal", "NVP SetExpressCheckout request ".var_export($nvp_request,true));
   $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
-
+  watchdog ("uc_paypal", "NVP SetExpressCheckout response ".var_export($nvp_response,true));
+  
   if ($nvp_response['ACK'] != 'Success') {
     drupal_set_message(t('Error message from PayPal:<br />@message', array('@message' => $nvp_response['L_LONGMESSAGE0'])), 'error');
     drupal_goto('cart/checkout');
@@ -700,6 +734,7 @@ function uc_paypal_ec_checkout($form, &$
 
   $_SESSION['TOKEN'] = $nvp_response['TOKEN'];
 
+
   if (strpos(variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'), 'sandbox') > 0) {
     $sandbox = 'sandbox.';
   }
@@ -732,14 +767,13 @@ function uc_paypal_ec_form() {
 function uc_paypal_ec_form_submit($form, &$form_state) {
   global $user;
   $items = uc_cart_get_contents();
+  watchdog ("uc_paypal","Cart Items: ".var_export($items, true));
 
   if (!is_array($items) || count($items) == 0) {
     drupal_set_message(t('You do not have any items in your shopping cart.'));
     return;
   }
 
-  list($desc, $subtotal) = _uc_paypal_product_details($items);
-
   $order = uc_order_new($user->uid);
 
   $context = array(
@@ -769,6 +803,21 @@ function uc_paypal_ec_form_submit($form,
   $order->products = $items;
   uc_order_save($order);
 
+  if(function_exists("uc_recurring_get_recurring_products_in_order")){
+	$nvp_request["VERSION"]="50.0"; // EC Recurring REQUIRES this
+	$ii=0;
+	$billings=uc_recurring_get_recurring_products_in_order($order);
+	$_SESSION["paypal_billing"]=array();
+	foreach ($billings as $recurring_product){
+		if ($ii>9) break;
+		$agreement=check_plain("Renewal of product ".$recurring_product["product"]->title);
+		$nvp_request["L_BILLINGTYPE".$ii]="RecurringPayments";
+		$nvp_request["L_BILLINGAGREEMENTDESCRIPTION".$ii]=$agreement;
+		$ii++;
+	}
+  }
+
+
   $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
 
   $_SESSION['cart_order'] = $order->order_id;
@@ -839,8 +888,18 @@ function uc_paypal_ec_submit_form_submit
     'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
   );
 
+  watchdog("uc_paypal", "NVP doExpressCheckout request ".var_export($nvp_request, true));
+
   $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
 
+  watchdog("uc_paypal", "NVP doExpressCheckout response ".var_export($nvp_response, true));
+
+//  if ($nvp_response["PAYMENTSTATUS"]=="Success"){
+//	uc_order_update_status($order->order_id, "Completed");
+//	uc_order_comment_save($order->order_id, 0, t('Payment accepted.'), 'order', "Completed");
+//	watchdog("uc_paypal", "ExpressCheckout payment successful");
+  //}
+
   unset($_SESSION['TOKEN'], $_SESSION['PAYERID']);
   $_SESSION['do_complete'] = TRUE;
 
