--- uc_paypal.pages.inc.original	2010-10-10 21:27:16.000000000 -0400
+++ uc_paypal.pages.inc	2011-01-12 17:36:14.000000000 -0500
@@ -114,6 +114,7 @@ function uc_paypal_ipn() {
         }
         $comment = t('PayPal transaction ID: @txn_id', array('@txn_id' => $txn_id));
         uc_payment_enter($order_id, 'paypal_wps', $payment_amount, $order->uid, NULL, $comment);
+        $order = uc_order_load($order_id); // order must be loaded again to pick up changes possibly made by uc_payment_enter
         uc_cart_complete_sale($order);
         uc_order_comment_save($order_id, 0, t('Payment of @amount @currency submitted through PayPal.', array('@amount' => uc_price($payment_amount, $context, $options), '@currency' => $payment_currency)), 'order', 'payment_received');
         uc_order_comment_save($order_id, 0, t('PayPal IPN reported a payment of @amount @currency.', array('@amount' => uc_price($payment_amount, $context, $options), '@currency' => $payment_currency)));
@@ -425,6 +426,14 @@ function uc_paypal_complete($order) {
     drupal_goto('cart');
   }
 
+  // for new users, if the IPN connection already completed, we need to login with the
+  // saved new_user data (the IPN call to uc_cart_complete_sale() created the user and
+  // attempted to login)
+  if (variable_get('uc_new_customer_login', FALSE) && !empty($order->uid) && !user_is_logged_in()) {
+    $user = user_load($order->uid);
+    user_external_login($user);
+  }
+
   // This lets us know it's a legitimate access of the complete page.
   $_SESSION['do_complete'] = TRUE;
 
