From 1c8c0310ea8789291d5da85a7f76dd35c99e0d5a Mon Sep 17 00:00:00 2001
From: PeterO <peterolson@simplistechsolutions.com>
Date: Fri, 14 Jan 2011 16:19:08 -0600
Subject: [PATCH] fixed duplicate emails and autologin for wps

---
 payment/uc_paypal/uc_paypal.pages.inc |   16 ++++++++++
 uc_cart/uc_cart.module                |   50 +++++++++++++++++++++++++++++++--
 2 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/payment/uc_paypal/uc_paypal.pages.inc b/payment/uc_paypal/uc_paypal.pages.inc
index 07b1a66..747e8e0 100644
--- a/payment/uc_paypal/uc_paypal.pages.inc
+++ b/payment/uc_paypal/uc_paypal.pages.inc
@@ -104,6 +104,14 @@ function uc_paypal_ipn($order_id = 0) {
         }
         $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);
+
+        // We need to set a flag to let cart complete know that this ipn is running first.
+        if ($order->uid == 0) {
+          $order->data['complete_sale_ipnfirst'] = 1;
+        }
+
+	// set a flag so we know ipn is running uc_cart_complete_sale
+        $order->data['user_wpspaypal_ipn'] = 1;
         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)));
@@ -414,6 +422,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;
 
diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module
index 691aca8..bac8ffb 100644
--- a/uc_cart/uc_cart.module
+++ b/uc_cart/uc_cart.module
@@ -1195,6 +1195,10 @@ function uc_cart_complete_sale($order, $login = FALSE) {
 
   // Logic to create new user if necessary:
   if ($order->uid == 0) {
+	//if ipn isnt running this, then the user is running it first or paypal wps isnt used
+    if (empty($order->data['user_wpspaypal_ipn'])) {
+      $order->data['complete_sale_userfirst'] = 1;
+    }
     // Check for an existing user account with the e-mail address from checkout.
     $result = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $order->primary_email);
 
@@ -1251,6 +1255,10 @@ function uc_cart_complete_sale($order, $login = FALSE) {
       $message_type = 'new_user';
     }
   }
+  // Check if this function has been run already.
+  elseif (!empty($order->data['complete_sale_ipnfirst'])) {
+    $message_type = 'new_user';
+  }
   else {
     if ($order->uid == $user->uid) {
       $message_type = 'logged_in';
@@ -1265,8 +1273,9 @@ function uc_cart_complete_sale($order, $login = FALSE) {
   $messages['uc_msg_order_submit_format'] = variable_get('uc_msg_order_submit', uc_get_message('completion_message'));
   $message = variable_get('uc_msg_order_'.$message_type, uc_get_message('completion_'.$message_type));
   if ($message != '') {
-    $variables['!new_username'] = check_plain($_SESSION['new_user']['name']);
-    $variables['!new_password'] = check_plain($_SESSION['new_user']['pass']);
+    //if ipn created the user, we have to do something different for the username and password
+    $variables['!new_username'] = !empty($order->data['complete_sale_ipnfirst']) ? check_plain(user_load($order->uid)->name) : check_plain($_SESSION['new_user']['name']);
+    $variables['!new_password'] = !empty($order->data['complete_sale_ipnfirst']) ? check_plain('***** (Check Your Email)') : check_plain($_SESSION['new_user']['pass']);
     $messages['uc_msg_order_'.$message_type.'_format'] = strtr($message, $variables);
   }
   $messages['uc_msg_continue_shopping_format'] = variable_get('uc_msg_continue_shopping', uc_get_message('continue_shopping'));
@@ -1292,7 +1301,42 @@ function uc_cart_complete_sale($order, $login = FALSE) {
   unset($_SESSION['cart_order'], $_SESSION['do_complete'], $_SESSION['new_user']);
 
   module_invoke_all('uc_checkout_complete', $order, $account);
-  ca_pull_trigger('uc_checkout_complete', $order, $account);
+
+  //check to see if this function was already run by ipn
+  if (!empty($order->data['complete_sale_ipnfirst'])) {
+    //if ipn is running this function (before user returns) pull both triggers
+    if (!empty($order->data['user_wpspaypal_ipn'])) {
+      ca_pull_trigger('uc_checkout_complete', $order, $account);
+      ca_pull_trigger('uc_checkout_complete_paid', $order, $account);
+      //unset some junk from the database.
+      unset($order->data['user_wpspaypal_ipn']);
+    }
+
+    //user is running the after ipn so pull nothing
+    else {
+      unset($order->data['complete_sale_ipnfirst']);
+    }
+
+    db_query("UPDATE {uc_orders} SET uid = %d, data = '%s' WHERE order_id = %d", $order->uid, serialize($order->data), $order->order_id);
+  }
+  else if (!empty($order->data['complete_sale_userfirst'])) {
+    if (!empty($order->data['user_wpspaypal_ipn'])) {
+      //uc_checkout_complete trigger has already been pulled, so just pull this one.
+      ca_pull_trigger('uc_checkout_complete_paid', $order, $account);
+      unset($order->data['complete_sale_userfirst']);
+      unset($order->data['user_wpspaypal_ipn']);
+      db_query("UPDATE {uc_orders} SET uid = %d, data = '%s' WHERE order_id = %d", $order->uid, serialize($order->data), $order->order_id);
+    }
+    else {
+      //we aren't sure if the order is paid, so just pull one trigger.
+      ca_pull_trigger('uc_checkout_complete', $order, $account);
+    }
+  }
+  //Other payment times usually need to pull both these triggers.
+  else {
+    ca_pull_trigger('uc_checkout_complete', $order, $account);
+    ca_pull_trigger('uc_checkout_complete_paid', $order, $account);
+  }
 
   return $themed_output;
 }
-- 
1.5.4.3

