diff -rupN sagepayserver.orig/README.txt sagepayserver.new/README.txt
--- sagepayserver.orig/README.txt	2011-09-22 10:01:43.000000000 +0100
+++ sagepayserver.new/README.txt	2011-11-25 19:32:12.000000000 +0000
@@ -1,5 +1,9 @@
 === About Ubercart Sagepay Server module ===
-
++++++++++++++++++++++++++++++++++++++++++++++++++++
+This is a initial conversion of Ubercart SagepayServer for Ubercart 3.x on Drupal 7
+Converted by Colin Thomson - aireworth on drupal.org
+Please review and report any issues. 25/11/2011
+++++++++++++++++++++++++++++++++++++++++++++++++++++
 This module is intended to be used with Ubercart 2.x, to provide a simple method of integrating with Sagepay's (formerly Protx) Server payment transaction protocol (http://www.sagepay.com/integrating_sagepay.asp#server).
 
 To use this module you will need a Sagepay vendorname.
diff -rupN sagepayserver.orig/uc_sagepayserver.info sagepayserver.new/uc_sagepayserver.info
--- sagepayserver.orig/uc_sagepayserver.info	2011-09-22 13:29:32.000000000 +0100
+++ sagepayserver.new/uc_sagepayserver.info	2011-11-25 19:30:50.000000000 +0000
@@ -2,11 +2,11 @@ name = "Sagepay Server"
 description = Process payments using Sagepay Server
 dependencies[] = uc_payment
 package = Ubercart - payment
-core = 6.x
+core = 7.x
 php = 5.0
-; Information added by drupal.org packaging script on 2011-09-22
-version = "6.x-1.x-dev"
-core = "6.x"
+version = "7.x-1.x-dev"
+core = "7.x"
 project = "uc_sagepayserver"
-datestamp = "1316694572"
-
+files[] = uc_sagepayserver.module
+files[] = uc_sagepayserver.install
+files[] = uc_sagepayserver.pages.inc
diff -rupN sagepayserver.orig/uc_sagepayserver.install sagepayserver.new/uc_sagepayserver.install
--- sagepayserver.orig/uc_sagepayserver.install	2011-09-22 10:01:43.000000000 +0100
+++ sagepayserver.new/uc_sagepayserver.install	2011-11-25 20:25:41.000000000 +0000
@@ -123,11 +123,8 @@ function uc_sagepayserver_install() {
     variable_set('uc_sagepayserver_vendor_name', variable_get('uc_sagepaygoserver_vendor_name', NULL));
     variable_set('uc_sagepayserver_debug_notifications', variable_get('uc_sagepaygoserver_debug_notifications', FALSE));
   }
-  // Otherwise install schema as usual
-  else {
-    drupal_install_schema('uc_sagepayserver');
-  }
-  // Insert order status values in both cases
+  
+  // Insert order status values in any case
   $t = get_t();
   db_query("INSERT INTO {uc_order_statuses} (order_status_id, title, state, weight, locked) VALUES ('uc_sagepayserver_pending_reg', '" . $t('Pending Sagepay Registration Notification') . "', 'in_checkout', 0, 1);");
   db_query("INSERT INTO {uc_order_statuses} (order_status_id, title, state, weight, locked) VALUES ('uc_sagepayserver_pending_result', '" . $t('Pending Sagepay Transaction Results') . "', 'in_checkout', 10, 1);");
@@ -138,7 +135,6 @@ function uc_sagepayserver_install() {
  * Implementation of hook_uninstall().
  */
 function uc_sagepayserver_uninstall() {
-  drupal_uninstall_schema('uc_sagepayserver');
   db_query("DELETE FROM {variable} WHERE name IN ('uc_sagepayserver_mode', 'uc_sagepayserver_vendor_name', 'uc_sagepayserver_debug_notifications')");
   db_query("DELETE FROM {uc_order_statuses} WHERE order_status_id IN ('uc_sagepayserver_pending_reg', 'uc_sagepayserver_pending_result')");
 }
@@ -150,5 +146,5 @@ function uc_sagepayserver_update_6101() 
   $ret = array();
   // Remove transaction_auth_no from uc_sagepayserver_transactions
   db_drop_field($ret, 'uc_sagepayserver_transactions', 'transaction_auth_no');
-  return $ret;
+  return t($ret);
 }
diff -rupN sagepayserver.orig/uc_sagepayserver.module sagepayserver.new/uc_sagepayserver.module
--- sagepayserver.orig/uc_sagepayserver.module	2011-09-22 10:01:43.000000000 +0100
+++ sagepayserver.new/uc_sagepayserver.module	2011-11-25 20:25:40.000000000 +0000
@@ -59,7 +59,7 @@ function uc_sagepayserver_menu() {
  *
  * Add config form to Ubercart Payment Methods administration page
  */
-function uc_sagepayserver_payment_method() {
+function uc_sagepayserver_uc_payment_method() {
   $methods[] = array(
     'id' => 'uc_sagepayserver',
     'name' => t('Sagepay Server'),
@@ -75,7 +75,7 @@ function uc_sagepayserver_payment_method
 /**
  * Callback for hook_payment_method (see uc_sagepayserver_payment_method)
  */
-function uc_payment_method_sagepayserver($op) {
+function uc_payment_method_sagepayserver($op, &$order, $form = NULL, &$form_state = NULL) {
   switch ($op) {
     case 'settings':
       $form['uc_payment_method_sagepayserver']['uc_sagepayserver_mode'] = array(
@@ -113,7 +113,7 @@ function uc_sagepayserver_form_alter(&$f
     if ($order->payment_method == 'uc_sagepayserver') {
       // ... alter the checkout form to integrate with uc_sagepayserver
       // Point the form at the processing URL
-      $form['#submit'] = array('uc_sagepayserver_checkout_form_submit');
+       $form['#submit'] = array('uc_sagepayserver_checkout_form_submit');
     }
   }
 }
@@ -122,7 +122,7 @@ function uc_sagepayserver_form_alter(&$f
  * Implementation of hook_form_alter on the uc_cart_checkout_form to
  * make elements conform with sagepays maxlengths etc.
  */
-function uc_sagepayserver_form_uc_cart_checkout_form_alter(&$form, &$form_state){
+function uc_sagepayserver_form_uc_cart_checkout_form_alter(&$form, &$form_state) {
   //Billing Address
   $form['panes']['billing']['billing_first_name']['#maxlength'] = 20;
   $form['panes']['billing']['billing_last_name']['#maxlength'] = 20;
@@ -145,19 +145,16 @@ function uc_sagepayserver_checkout_form_
 
   global $user;
   global $base_url;
-  
   // Get order details
   $order = uc_order_load($_SESSION['cart_order']);
-  
   // Generate a unique vendor_tx_code
   $vendor_tx_code = "AUTH-" . (rand(0, 32000)*rand(0, 32000)) . "-" . $order->order_id;
   
   // Get billing country code
   $country_data = uc_get_country_data(array('country_id' => $order->billing_country));
   $billing_country_iso_code = $country_data[0]["country_iso_code_2"];
-  
-  // If delivery country was specified, get the code
-  if ($order->delivery_country) {
+  // If delivery country was specified, get the code - make sure delivery_street1 is present as well as another country can be specified by default.
+  if (($order->delivery_country) && ($order->delivery_street1)) {
     $country_data = uc_get_country_data(array('country_id' => $order->delivery_country));
     $delivery_country_iso_code = $country_data[0]["country_iso_code_2"];
   }
@@ -200,7 +197,6 @@ function uc_sagepayserver_checkout_form_
     'Basket' => check_plain($basket),
     'NotificationURL' => url('uc_sagepayserver/' . $order->order_id . '/notification/' . uc_cart_get_id(), array('absolute' => TRUE))
   );
-  
   // Only provide BillingState and DeliveryState fields if country is US
   if ($billing_country_iso_code == 'US') {
     // Get billing state code
@@ -218,7 +214,7 @@ function uc_sagepayserver_checkout_form_
   }
 
   // Should the data be sent to the simulator, test or the live transaction notification url?
-  switch (variable_get('uc_sagepayserver_mode', 0)) {
+  switch (variable_get('uc_sagepayserver_mode', 0)) {    
     case 0:
         $registration_url = UC_SAGEPAYSERVER_SIMULATOR_URL;
       break;
@@ -233,7 +229,7 @@ function uc_sagepayserver_checkout_form_
       break;
   }
   // Send the data to the processor and await response
-  $nvp_response = uc_sagepayserver_request($nvp_request, $registration_url, NULL);
+  $nvp_response = uc_sagepayserver_request($nvp_request, $registration_url);
   
   // Check for drupal_http_request errors
   if (isset($nvp_response->error)) {
@@ -245,7 +241,7 @@ function uc_sagepayserver_checkout_form_
   if (!$nvp_response->data || empty($nvp_response->data)) {
     form_set_error('', t('There was an error processing the purchase. Please contact us.'));
     watchdog('uc_sagepayserver', 'No data returned from Sagepay Server notification URL', array(), WATCHDOG_NOTICE);
-    uc_order_update_status($order, 'in_checkout');
+    uc_order_update_status($order->order_id, 'in_checkout');
     return;
   }
   
@@ -257,7 +253,7 @@ function uc_sagepayserver_checkout_form_
   if ($nvp_response_data['VPSProtocol'] != '2.23') {
     form_set_error('', t('There was an error processing the purchase. Please contact us.'));
     watchdog('uc_sagepayserver', 'The VPSProtocol returned from Sagepay Server does not match the VPSProtocol of the notification data', array(), WATCHDOG_NOTICE);
-    uc_order_update_status($order, 'in_checkout');
+    uc_order_update_status($order->order_id, 'in_checkout');
     return;
   }
   // Status
@@ -267,7 +263,7 @@ function uc_sagepayserver_checkout_form_
      * and store the Security Key, NextURL and VPSTxId with the orderId
      */
     // Set the order status
-    uc_order_update_status($order, 'uc_sagepayserver_pending_results');    
+     uc_order_update_status($order->order_id, 'uc_sagepayserver_pending_result');    
     // Record data in uc_sagepayserver_transactions table
     $data = new stdClass();
     $data->order_id = $order->order_id;
@@ -276,7 +272,7 @@ function uc_sagepayserver_checkout_form_
     $data->status = $nvp_response_data['Status'];
     $data->status_detail = $nvp_response_data['StatusDetail'];
     $data->security_key = $nvp_response_data['SecurityKey'];
-    $data->received = time();
+    $data->received = REQUEST_TIME;
     drupal_write_record('uc_sagepayserver_transactions', $data);    
     header('Location: ' . $nvp_response_data['NextURL']);
     module_invoke_all('exit');
@@ -287,7 +283,7 @@ function uc_sagepayserver_checkout_form_
     watchdog('uc_sagepayserver', $nvp_response_data['StatusDetail'], NULL, WATCHDOG_ERROR);
     form_set_error('', t('There was an error processing the purchase. Please contact us.'));
     watchdog('uc_sagepayserver', 'Sagepay Server sent a not OK response.@debug', array('@debug' => variable_get('uc_sagepayserver_debug_notifications', FALSE) ? print_r($nvp_response_data, TRUE) : ''), WATCHDOG_NOTICE);
-    uc_order_update_status($order, 'in_checkout');
+    uc_order_update_status($order->order_id, 'in_checkout');
     return;
   }
   return;
@@ -295,15 +291,21 @@ function uc_sagepayserver_checkout_form_
 
 // Sends a request to remote server and return a response array.
 function uc_sagepayserver_request($request, $server) {
-  $data = http_build_query($request, NULL, '&');
-  $headers = array('Content-Type' => 'application/x-www-form-urlencoded');  
-  $response = drupal_http_request($server, $headers, 'POST', $data);
-  return $response;
-}
+    $data = http_build_query($request, NULL, '&'); 
+    $options = array(
+                  'method' => 'POST',
+                  'data' => $data,
+                  'headers' => array('Content-Type' => 'application/x-www-form-urlencoded')
+                   );
+    $response = drupal_http_request($server, $options);
+ 
+  return $response;       
+  }
+
 
 // Turns Sagepay's initial reponse to an array
 function _uc_sagepayserver_response_to_array($nvpstr) {
-  $response = split(chr(10), $nvpstr);
+  $response = preg_split("/[\n]/", $nvpstr);
   $output = array();
   for ($i=0; $i<count($response); $i++) {
     $splitAt = strpos($response[$i], "=");
@@ -318,7 +320,7 @@ function _uc_sagepayserver_response_to_a
 function _uc_sagepayserver_make_order_description($order) {
   
   $site_name = variable_get('site_name', 'Drupal');
-  $date_time = format_date(time(), 'medium');
+  $date_time = format_date(REQUEST_TIME, 'medium');
   $description = t('Purchase from @site_name on @date_time.', array('@site_name' => $site_name, '@date_time' => $date_time));
   $description = drupal_substr($description, 0, 100);
   
@@ -330,7 +332,6 @@ function _uc_sagepayserver_make_order_de
  * format for submission to Sagepay
  */
 function _uc_sagepayserver_make_order_basket($order) {
-
   $basket = '';
   $break = ":";
   $no_lines = 0;
@@ -345,23 +346,15 @@ function _uc_sagepayserver_make_order_ba
         'node' => node_load($product->nid),
       ),
     );
-    
-    $price_info = array(
-      'price' => $product->price,
-      'qty' => $product->qty,
-    );
-    
-    $price = uc_price($price_info, $context);
-    
+    // Removed all refernce to uc_price as not present in Ubercart 3 
     $basket .= $break . str_replace(':', ' ', $product->title);
     $basket .= $break . $product->qty;
-    $basket .= $break . number_format($price, 2, '.', '');
+    $basket .= $break . number_format($product->price, 2, '.', '');
     $basket .= $break . '---';
-    $basket .= $break . number_format($price, 2, '.', '');
-    $basket .= $break . number_format(($product->qty * $price), 2, '.', '');
+    $basket .= $break . number_format($product->price, 2, '.', '');
+    $basket .= $break . number_format(($product->qty * $product->price), 2, '.', '');
     $no_lines++;
   }
-  
   return $no_lines . $basket;
   
 }
\ No newline at end of file
diff -rupN sagepayserver.orig/uc_sagepayserver.pages.inc sagepayserver.new/uc_sagepayserver.pages.inc
--- sagepayserver.orig/uc_sagepayserver.pages.inc	2011-09-22 10:01:43.000000000 +0100
+++ sagepayserver.new/uc_sagepayserver.pages.inc	2011-11-25 20:04:28.000000000 +0000
@@ -181,8 +181,8 @@ function uc_sagepayserver_notification($
    */
   $transaction_id = $_POST['VendorTxCode'];
   $result = db_query("SELECT COUNT(*) AS no_rows FROM {uc_sagepayserver_transactions} WHERE vendor_transaction_code = '%s'", array($transaction_id));
-  $row = db_fetch_object($result);
-  if ($row->no_rows < 1) {
+  $number_of_rows = $result->rowCount();
+  if ($number_of_rows < 1) {
     $data['Status'] = 'INVALID';
     $data['StatusDetail'] = t('No matching order');
     $data['RedirectURL'] = url('cart/checkout', array('absolute' => TRUE));
@@ -196,14 +196,20 @@ function uc_sagepayserver_notification($
   }
   
   // Get details about the transaction from uc_sagepayserver_transactions table
-  $result = db_query("SELECT * FROM {uc_sagepayserver_transactions} WHERE vendor_transaction_code = '%s'", array($transaction_id));
-  $row = db_fetch_object($result);
+   $result = db_select('uc_sagepayserver_transactions')
+      ->fields('uc_sagepayserver_transactions')
+      ->condition('vendor_transaction_code', array($transaction_id))     
+      ->execute();
+  
+  foreach ($result as $row) {
+  $row_order_id = $row->order_id;
+  $row_security_key = $row->security_key;
+  }
   
-
   /**
    * Check that the transaction_id matches the same order as the path
    */
-  if ($order_id != $row->order_id) {
+  if ($order_id != $row_order_id) {
     $data['Status'] = 'INVALID';
     $data['StatusDetail'] = t('Transaction id does not match with the expected order');
     $data['RedirectURL'] = url('cart/checkout', array('absolute' => TRUE));
@@ -219,7 +225,7 @@ function uc_sagepayserver_notification($
   /**
    * The message is legit, take appropriate action depending upon the status
    */  
-  $order = uc_order_load($row->order_id);
+  $order = uc_order_load($row_order_id);
 
   switch ($_POST['Status']) {
     case 'OK':
@@ -229,7 +235,7 @@ function uc_sagepayserver_notification($
          */
         $signature_string = $_POST['VPSTxId'] . $_POST['VendorTxCode'] . $_POST['Status']
                           . $_POST['TxAuthNo'] . variable_get('uc_sagepayserver_vendor_name', NULL) . $_POST['AVSCV2']
-                          . $row->security_key . $_POST['AddressResult'] . $_POST['PostCodeResult']
+                          . $row_security_key . $_POST['AddressResult'] . $_POST['PostCodeResult']
                           . $_POST['CV2Result'] . $_POST['GiftAid'] . $_POST['3DSecureStatus']
                           . $_POST['CAVV'] . ((!empty($_POST['AddressStatus'])) ? $_POST['AddressStatus'] : '') . ((!empty($_POST['PayerStatus'])) ? $_POST['PayerStatus'] : '')
                           . $_POST['CardType'] . $_POST['Last4Digits'];
@@ -268,6 +274,7 @@ function uc_sagepayserver_notification($
          * The order is completed on the Drupal side once the user is returned to the thankyou page
          */
         $data['Status'] = 'OK';
+        $data['StatusDetail'] = t('Order OK');
         $data['RedirectURL'] = url('order/' . $order_id . '/thankyou', array('absolute' => TRUE));
         echo _uc_sagepayserver_encode_response($data);
         watchdog('uc_sagepayserver', 'Payment confirmed by Sagepay. @status: @status_detail.@debug',
@@ -283,7 +290,7 @@ function uc_sagepayserver_notification($
           <br />VendorTxCode: @VendorTxCode
           <br />TxAuthNo: @TxAuthNo
           <br />SecurityKey: @SecurityKey',
-          array('@Status' => $_POST['Status'], '@VPSTxId' => $_POST['VPSTxId'], '@VendorTxCode' => $_POST['VendorTxCode'], '@TxAuthNo' => $_POST['TxAuthNo'], '@SecurityKey' => $row->security_key)
+          array('@Status' => $_POST['Status'], '@VPSTxId' => $_POST['VPSTxId'], '@VendorTxCode' => $_POST['VendorTxCode'], '@TxAuthNo' => $_POST['TxAuthNo'], '@SecurityKey' => $row_security_key)
         );
         uc_order_comment_save($order_id, 0, $comment, 'admin', uc_order_state_default('post_checkout'));
       break;
@@ -291,8 +298,13 @@ function uc_sagepayserver_notification($
     case 'NOTAUTHED':
     
         $data['Status'] = 'OK';
+        $data['StatusDetail'] = t('NOTAUTHED');
         // Append an error message key to the redirectURL so that we can show the user an appropriate error message
-        $data['RedirectURL'] = url('cart/checkout/review', array('absolute' => TRUE, 'query' => 'uc_sagepayserver_error=notauthed'));
+        $myoptions = array(
+                    'absolute' => TRUE,
+                    'query' => array('uc_sagepayserver_error' => 'notauthed')
+                   );
+        $data['RedirectURL'] = url('cart/checkout/review', $myoptions );
         echo _uc_sagepayserver_encode_response($data);
         watchdog('uc_sagepayserver', 'Payment not authorised by Sagepay. @status: @status_detail.@debug',
         array('@status' => $data['Status'], '@status_detail' => $data['StatusDetail'], '@debug' => variable_get('uc_sagepayserver_debug_notifications', FALSE) ? ' <pre>' . print_r($_POST, TRUE) . '</p>' : ''), WATCHDOG_ERROR);
@@ -305,6 +317,7 @@ function uc_sagepayserver_notification($
     case 'ABORT':
     
         $data['Status'] = 'OK';
+        $data['StatusDetail'] = t('ABORTED');
         $data['RedirectURL'] = url('cart/checkout/review', array('absolute' => TRUE));
         echo _uc_sagepayserver_encode_response($data);
         watchdog('uc_sagepayserver', 'Payment cacelled by user. @status: @status_detail.@debug',
@@ -318,7 +331,14 @@ function uc_sagepayserver_notification($
     case 'REJECTED':
     
         $data['Status'] = 'OK';
-        $data['RedirectURL'] = url('cart/checkout/review', array('absolute' => TRUE, 'query' => 'uc_sagepayserver_error=rejected'));
+        $data['StatusDetail'] = t('REJECTED');
+        // Append an error message key to the redirectURL so that we can show the user an appropriate error message
+        $myoptions = array(
+                    'absolute' => TRUE,
+                    'query' => array('uc_sagepayserver_error' => 'rejected')
+                    );
+        $data['RedirectURL'] = url('cart/checkout/review', $myoptions );
+       // $data['RedirectURL'] = url('cart/checkout/review', array('absolute' => TRUE, 'query' => 'uc_sagepayserver_error=rejected'));
         echo _uc_sagepayserver_encode_response($data);
         watchdog('uc_sagepayserver', 'Payment rejected by Sagepay because of the rules set on account. @status: @status_detail.@debug',
         array('@status' => $data['Status'], '@status_detail' => $data['StatusDetail'], '@debug' => variable_get('uc_sagepayserver_debug_notifications', FALSE) ? ' <pre>' . print_r($_POST, TRUE) . '</p>' : ''), WATCHDOG_ERROR);
@@ -330,7 +350,13 @@ function uc_sagepayserver_notification($
     case 'ERROR':
     
         $data['Status'] = 'OK';
-        $data['RedirectURL'] = url('cart/checkout/review', array('absolute' => TRUE, 'query' => 'uc_sagepayserver_error=error'));
+        $data['StatusDetail'] = t('ERROR');
+        // Append an error message key to the redirectURL so that we can show the user an appropriate error message
+        $myoptions = array(
+                    'absolute' => TRUE,
+                    'query' => array('uc_sagepayserver_error' => 'error')
+                   );
+        $data['RedirectURL'] = url('cart/checkout/review', $myoptions );
         echo _uc_sagepayserver_encode_response($data);
         watchdog('uc_sagepayserver', 'Payment could not be completed because of an error at Sagepay. @status: @status_detail.@debug',
         array('@status' => $data['Status'], '@status_detail' => $data['StatusDetail'], '@debug' => variable_get('uc_sagepayserver_debug_notifications', FALSE) ? ' <pre>' . print_r($_POST, TRUE) . '</p>' : ''), WATCHDOG_ERROR);
