From 3b7d99c6a569b9b7056238771b1d254cd7e283e1 Mon Sep 17 00:00:00 2001
From: Peter <aegir@create.simplistechsolutions.com>
Date: Mon, 11 Oct 2010 10:42:19 -0500
Subject: [PATCH] chaos patch and some mods for anon login

chaos patch, some needed to do by hand

chaos patch and some simple changes
---
 payment/uc_paypal/uc_paypal.pages.inc |    6 +++
 uc_cart/uc_cart.module                |   57 ++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/payment/uc_paypal/uc_paypal.pages.inc b/payment/uc_paypal/uc_paypal.pages.inc
index 07b1a66..b6fd48e 100644
--- a/payment/uc_paypal/uc_paypal.pages.inc
+++ b/payment/uc_paypal/uc_paypal.pages.inc
@@ -104,6 +104,12 @@ 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 is a deferred user creation.
+        if ($order->uid == 0) {
+          $order->data['user_create_defer'] = 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)));
diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module
index 691aca8..12c1bde 100644
--- a/uc_cart/uc_cart.module
+++ b/uc_cart/uc_cart.module
@@ -1192,6 +1192,7 @@ function uc_cart_continue_shopping_url($unset = TRUE) {
  */
 function uc_cart_complete_sale($order, $login = FALSE) {
   global $user;
+  $user_created = FALSE;
 
   // Logic to create new user if necessary:
   if ($order->uid == 0) {
@@ -1231,26 +1232,33 @@ function uc_cart_complete_sale($order, $login = FALSE) {
         $account->password = $fields['pass'];
 
         // Send the e-mail through the user module.
-        drupal_mail('user', 'register_no_approval_required', $order->primary_email, NULL, array('account' => $account), uc_store_email_from());
+        drupal_mail('user', 'register_admin_created', $order->primary_email, NULL, array('account' => $account), uc_store_email_from());
       }
 
-      // Store the login details in the session for use on the page display.
-      $_SESSION['new_user'] = array('name' => $fields['name'], 'pass' => $fields['pass']);
-
-      // Update the order's uid in this request and in the database.
+      // Update the order's uid in this request...
       $order->uid = $account->uid;
-      unset($order->data['new_user']['pass']);
+      $order->data['new_user'] = array(
+        'name' => $fields['name'],
+        'pass' => $fields['pass'],
+      );
+      // ... and in the database.
       db_query("UPDATE {uc_orders} SET uid = %d, data = '%s' WHERE order_id = %d", $order->uid, serialize($order->data), $order->order_id);
 
       // Login the user if specified.
       if ($login) {
-        $form_state = array('values' => $fields);
-        drupal_execute('user_login', $form_state);
+	$form_state = array('values' => $fields);
+	drupal_execute('user_login', $form_state);
       }
 
       $message_type = 'new_user';
+      $user_created = TRUE;
     }
   }
+
+  // Deferred user creation.
+  elseif (!empty($order->data['user_create_defer'])) {
+    $message_type = 'new_user';
+  }
   else {
     if ($order->uid == $user->uid) {
       $message_type = 'logged_in';
@@ -1265,8 +1273,35 @@ 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']);
+    $variables['!new_username'] = check_plain($order->data['new_user']['name']);
+    $variables['!new_password'] = check_plain($order->data['new_user']['pass']);
+
+    // Delete the (now) junk data out of the order if possible.
+    if (!(!empty($order->data['user_create_defer']) && $user_created)) {
+
+      //did paypal IPN already create the user
+      if (!empty($order->data['user_create_defer'])) {
+	// Login the user if specified.
+	if ($login) {
+	  //set login info
+	  $fields = array(
+	      'name' => $order->data['new_user']['name'],
+	      'mail' => $order->primary_email,
+	      'init' => $order->primary_email,
+	      'pass' => $order->data['new_user']['pass'],
+	    );
+	  //login the user
+	  $form_state = array('values' => $fields);
+	  drupal_execute('user_login', $form_state);
+	}
+      }
+
+      //delet the "junk" data
+      unset($order->data['new_user']);
+      unset($order->data['user_create_defer']);
+      db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($order->data), $order->order_id);
+    }
+
     $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'));
@@ -1289,7 +1324,7 @@ function uc_cart_complete_sale($order, $login = FALSE) {
   uc_cart_empty(uc_cart_get_id());
 
   // Clear our the session variables used to force the cart workflow.
-  unset($_SESSION['cart_order'], $_SESSION['do_complete'], $_SESSION['new_user']);
+  unset($_SESSION['cart_order'], $_SESSION['do_complete']);
 
   module_invoke_all('uc_checkout_complete', $order, $account);
   ca_pull_trigger('uc_checkout_complete', $order, $account);
-- 
1.5.4.3

