--- a/html/sites/all/modules/contrib/ubercart/payment/uc_cim/uc_cim.module
+++ b/html/sites/all/modules/contrib/ubercart/payment/uc_cim/uc_cim.module
@@ -242,7 +242,6 @@ function uc_cim_menu() {
         'access callback' => 'user_is_logged_in',
         'type' => MENU_CALLBACK
       );
-
       //Refund or void a CIM stored payment from the order page
       $items['admin/store/orders/%/payments/%/refund'] = array(
         'title' => 'CIM Refund Terminal: Order !order_id',
@@ -253,6 +252,16 @@ function uc_cim_menu() {
         'access arguments' => array('process credit cards'),
         'type' => MENU_CALLBACK
       );
+      //Refund or void a POS stored payment from the order page
+      $items['admin/store/orders/%/payments/%/refundpos'] = array(
+        'title' => 'POS Refund Terminal: Order !order_id',
+        'title arguments' =>  array('!order_id' => 3),
+        'description' => 'Process a credit card refund for an order.',
+        'page callback' => 'drupal_get_form',
+        'page arguments' => array('uc_cim_refund_form', 3, 5, 6),
+        'access arguments' => array('process credit cards'),
+        'type' => MENU_CALLBACK
+      );
       $items['user/%user/creditcards'] = array(
         'title' => 'Manage Credit Cards',
         'page callback' => 'drupal_get_form',
@@ -447,10 +456,14 @@ function uc_cim_form_alter(&$form, &$form_state, $form_id) {
     if ($payments !== FALSE) {
       foreach ($payments as $payment) {
         $data = unserialize($payment->data);
-        if ($data['transaction_id']) {
+        if ($data['transaction_id'] && $data['success']) { //order is a POS transaction
           $refund = l(t('Refund'), 'admin/store/orders/'. $order->order_id .'/payments/'. $payment->receipt_id .'/refundpos');
           $form['payments'][$payment->receipt_id]['action']['#value'] .= '<br/>'. $refund;
         }
+        else { //order is a CIM transaction
+          $refund = l(t('Refund'), 'admin/store/orders/'. $order->order_id .'/payments/'. $payment->receipt_id .'/refund');
+          $form['payments'][$payment->receipt_id]['action']['#value'] .= '<br/>'. $refund;
+        }
       }
     }
   }
@@ -1740,7 +1753,7 @@ function uc_cim_edit_card_form_submit(&$form, &$form_state) {
 }
 
 //Refund payments
-function uc_cim_refund_form(&$form_state, $order_id, $receipt_id) {
+function uc_cim_refund_form(&$form_state, $order_id, $receipt_id, $pos = NULL) {
 
   //How much to refund?
   $order = uc_order_load($order_id);
@@ -1775,6 +1788,13 @@ function uc_cim_refund_form(&$form_state, $order_id, $receipt_id) {
     '#value' => $receipt_id,
   );
 
+  if($pos) {
+    $form['pos'] = array(
+      '#type' => 'hidden',
+      '#value' => 1,
+    );
+  }
+
   return $form;
 
 }
@@ -1804,14 +1824,27 @@ function uc_cim_refund_form_submit($form, &$form_state) {
   $payment = uc_payment_load($formvalues['receipt_id']);
   $data = unserialize($payment->data);
 
-  //If the amount is for the total, try to void the transaction first
-  if ($formvalues['amount'] == $payment->amount) {
-    $result = uc_cim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount'], 'VOID');
+  if ($formvalues['pos']) { //pos refund
+    //If the amount is for the total, try to void the transaction first
+    if ($formvalues['amount'] == $payment->amount) {
+      $result = uc_aim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount'], 'VOID', $data['track1'], $data['exp_date']);
+    }
+
+    //Otherwise (or if that failed), try to process a refund
+    if ($formvalues['amount'] != $payment->amount || $result['success'] == FALSE) {
+      $result = uc_aim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount'], 'CREDIT', $data['track1'], $data['exp_date']);
+    }
   }
+  else { //cim refund
+    //If the amount is for the total, try to void the transaction first
+    if ($formvalues['amount'] == $payment->amount) {
+      $result = uc_cim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount'], 'VOID');
+    }
 
-  //Otherwise (or if that failed), try to process a refund
-  if ($formvalues['amount'] != $payment->amount || $result['success'] == FALSE) {
-    $result = uc_cim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount']);
+    //Otherwise (or if that failed), try to process a refund
+    if ($formvalues['amount'] != $payment->amount || $result['success'] == FALSE) {
+      $result = uc_cim_refund($payment->order_id, $data['transaction_id'], $data['last_four'], $formvalues['amount']);
+    }
   }
 
   //Record the refund in the payments table, display the result to the admin, and save the comment
@@ -2371,6 +2404,82 @@ function validate_customer_payment_profile_request($profile_id, $payment_profile
   return $response;
 
 }
+function uc_aim_refund($order_id, $transaction_id, $last_four, $amount, $type = 'CREDIT', $track1, $exp_date) {
+  global $user; //For order comment saving, NOT processing (use order->uid for that)
+
+  $url = variable_get('up_pos_server', 'https://cardpresent.authorize.net:443/gateway/transact.dll');
+  //$url = 'https://cardpresent.authorize.net:443/gateway/transact.dll';
+  $test_trans = preg_match('/test/', $pos_url) ? 1 : 0;
+
+  //Setup the variables we're sending
+  $submit_data = array(
+    'x_cpversion' => '1.0',
+    'x_login' => variable_get('up_authorizenet_login_id', ''),
+    'x_market_type' => '2', //retail
+    'x_device_type' => '5', //personal computer based terminal
+    'x_amount' => uc_currency_format($amount, FALSE, FALSE, '.'),
+    //'x_split_tender_id' => '',
+    'x_tran_key' => variable_get('up_authorizenet_trans_key', ''),
+    //'x_track1' => $track1,
+    'x_card_num' => $last_four,
+    'x_exp_date' => $exp_date,
+    'x_type' => $type,
+    'x_method' => 'CC',
+    'x_ref_trans_id' => $transaction_id,
+    'x_response_format' => 1,
+    'x_test_request' => $test_trans,
+  );
+
+  while (list($key, $value) = each($submit_data)) {
+    $data .= $key .'='. urlencode(ereg_replace(',', '', $value)) .'&';
+  }
+
+  $data = substr($data, 0, -1);
+
+  $ch = curl_init();
+  curl_setopt($ch, CURLOPT_URL, $url);
+  curl_setopt($ch, CURLOPT_VERBOSE, 0);
+  curl_setopt($ch, CURLOPT_POST, 1);
+  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+  curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
+  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
+  $authorize = curl_exec($ch);
+  curl_close($ch);
+
+  //Process the response
+  $response = explode(variable_get('cim_delimiter', ','), $authorize);
+  $response_code = $response[1];
+  $response_text = $response[3];
+  $approval_code = $response[4];
+
+  if ($response_code != 1) {
+    $message = t('Refund declined: !amount', array('!amount' => uc_currency_format($amount)));
+    $result = array(
+      'success' => FALSE,
+      'comment' => t('Credit card refund declined: !text', array('!text' => $response_text)),
+      'message' => t('Credit card refund declined: !text', array('!text' => $response_text)),
+      'uid' => $user->uid,
+    );
+  }
+  else {
+    $message = t('Credit card refund successful: !amount', array('!amount' => uc_currency_format($amount)));
+    $result = array(
+      'success' => TRUE,
+      'comment' => t('Credit card refund processed successfully. Approval code: !code', array('!code' => $approval_code)),
+      'message' => t('Credit card refund processed successfully. Approval code: !code', array('!code' => $approval_code)),
+      'uid' => $user->uid,
+    );
+  }
+
+  //Save a watchdog warning message
+  watchdog('uc_aim', $message.'<br>Response from Auth.net ('.$url.'): '.print_r($response, true), array(), WATCHDOG_WARNING);
+
+  return $result;
+}
+/* END AIM API */
+
 function uc_cim_refund($order_id, $transaction_id, $last_four, $amount, $type = 'CREDIT') {
   global $user; //For order comment saving, NOT processing (use order->uid for that)
 
