--- uc_sage_payments.module.orig	2012-02-13 15:36:37.000000000 -0600
+++ uc_sage_payments.module	2016-03-18 16:47:35.423837636 -0500
@@ -19,7 +19,7 @@
     'description' => t('Process credit card payments using Sage Payment Solutions (HTTPS POST).'),
     'settings' => 'uc_sage_payments_settings_form',
     'credit' => 'uc_sage_payments_charge',
-    'credit_txn_types' => array(UC_CREDIT_AUTH_CAPTURE),
+    'credit_txn_types' => array(UC_CREDIT_AUTH_ONLY, UC_CREDIT_PRIOR_AUTH_CAPTURE, UC_CREDIT_AUTH_CAPTURE),
   );
 
   return $gateways;
@@ -99,6 +99,21 @@
   return $form;
 }
 
+function _uc_sage_payments_txn_map($type) {
+  switch ($type) {
+    case UC_CREDIT_AUTH_ONLY:
+      return '02'; // AuthOnly
+    case UC_CREDIT_PRIOR_AUTH_CAPTURE:
+      return '11'; // PriorAuthSale
+    case UC_CREDIT_AUTH_CAPTURE:
+      return '01'; // Sale
+    case UC_CREDIT_CREDIT:
+      return '06'; // Credit
+    case UC_CREDIT_VOID:
+      return '04'; // Void
+  }
+}
+
 /**
  * Module and helper functions.
  */
@@ -137,8 +152,14 @@
       'C_email' => $order->primary_email,
       'C_telephone' => $order->billing_phone,
       'T_amt' => round($amount, 2),
-      'T_code' => '01',
+      'T_code' => _uc_sage_payments_txn_map($data['txn_type']), //'T_code' => '02',
     );
+    
+    if ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) {
+      $transaction['T_reference'] = $data['auth_id'];
+    }
+    
+    $transaction['T_ordernum'] = $order_id;
 
     // Put all this data into an HTTPS POST request.
     $post = '';
@@ -170,15 +191,36 @@
 
     $result = uc_sage_payments_response($raw_response, $response);
 
-    if ($result['success'] == TRUE) {
+		// Fixed conditional
+    if (isset($result['success']) && $result['success'] == TRUE) {
+    
+    	// If this was an authorization only transaction...
+    	if ($data['txn_type'] == UC_CREDIT_AUTH_ONLY) {
+    	  // Log the authorization to the order.
+    	  uc_credit_log_authorization($order_id, $result['reference'], $amount);
+    	}
+    	elseif ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) {
+    	  uc_credit_log_prior_auth_capture($order_id, $data['auth_id']);
+    	}
+    
       uc_order_comment_save($order_id, $user->uid, $result['comment']);
     }
+    else {
+    	$result['success'] = FALSE;
+    }
+    // Issue: https://www.drupal.org/node/2461897
   }
   else {
     $result['message'] = t('Free order processed.');
     $result['success'] = TRUE;
   }
-
+  
+  // Don't log payment if this is auth_only. This makes the order status 'Pending' until admin actually
+  // charges the order.
+  if (in_array($data['txn_type'], array(UC_CREDIT_AUTH_ONLY))) {
+    $result['log_payment'] = FALSE;
+  }
+  
   return $result;
 }
 
@@ -224,6 +266,8 @@
     'reference' => substr($raw_response, 46, 10),
     'order_number' => substr($raw_response, strpos($raw_response, chr(28)) + 1, strrpos($raw_response, chr(28) - 1)),
   );
+  
+  $result['reference'] = $response['reference'];
 
   switch ($response['approval_indicator']) {
 
@@ -310,6 +354,9 @@
     uc_store_encryption_errors($crypt, 'uc_recurring');
 
     // Set gateway specific fee information for renewals.
+    // Added object declaration line for newer version of PHP
+    $fee = new stdClass();
+    // http://cgit.drupalcode.org/uc_sage_payments/commit/?id=703ddbb
     $fee->data['billing'] = $data['billing'];
     $fee->data['payment_details'] = $data['payment_details'];
   }
