--- uc_migs.info	Tue Jan 20 09:26:36 1970
+++ uc_migs.info	Tue Jan 20 09:26:36 1970
@@ -1,11 +1,9 @@
-name = "MIGS"
-description = "Integrates MIGS 3rd party (bank hosted) payment gateway."
-dependencies[] = uc_payment
-package = Ubercart - payment
-core = 6.x
-; Information added by drupal.org packaging script on 2011-03-28
-version = "6.x-1.x-dev"
-core = "6.x"
-project = "uc_migs"
-datestamp = "1301314551"
-
+name = "MIGS"
+description = "Integrates MIGS 3rd party (bank hosted) payment gateway."
+dependencies[] = uc_payment
+; For images:
+dependencies[] = uc_credit
+package = Ubercart - payment
+core = 7.x
+; Modified by PeterMoulding.com for Drupal 7, Ubercart 3.0, and CBA.
+files[] = uc_migs.module
--- uc_migs.install	Tue Jan 20 09:26:36 1970
+++ uc_migs.install	Tue Jan 20 09:26:36 1970
@@ -1,13 +1,11 @@
 <?php
 
 /**
- * Implementation of hook_update_N()
- *
- * [#970228] Improved error messages for users (and detailed logging
- * to watchdog) require a rebuild of the theme registry.
+name = "MIGS"
+description = "Integrates MIGS 3rd party (bank hosted) payment gateway."
+dependencies[] = uc_payment
+package = Ubercart - payment
+core = 7.x
+; Modified by PeterMoulding.com for Drupal 7, Ubercart 3.0, and CBA.
+; Modified for a new install with no attempt at an upgrade.
  */
-function uc_migs_update_6000() {
-  $ret = array();
-  drupal_rebuild_theme_registry();
-  return $ret;
-}
--- uc_migs.module	Tue Jan 20 09:26:36 1970
+++ uc_migs.module	Tue Jan 20 09:26:36 1970
@@ -1,580 +1,620 @@
-<?php
-
-/**
- * @file
- * Integrates MIGS 3rd party payment system.
- *
- * This a payment module intended for the MIGS (MasterCard Internet
- * Gateway Service) for what they call the "3rd party" integration
- * method, or the Virtual Payment Client (VPC) which is the interface
- * that processes the payment on their secure server.
- *
- * See http://drupal.org/project/uc_migs for the most up to date version.
- *
- * Currently maintained by Giant Robot Ltd - www.giantrobot.co.nz 
- * Feedback welcome via issue queue or twitter.com/grobot
- *
- * Thanks also to previous authors & contributors:
- *  T-Rex Art    - http://www.trexart.com.au
- *  Open Web Lab - http://www.openweblab.com
- */
-
-/*******************************************************************************
- * Hook Functions (Drupal)
- ******************************************************************************/
-
-/** 
- * Implementation of hook_theme()
- */
-function uc_migs_theme() {
-  return array(
-    // Because we use uc_cart_complete_sale, we don't have an
-    // overridable theme function for uc completion. But we do have
-    // one for incomplete transactions!
-    'uc_migs_order_incomplete' => array(
-      'arguments' => array(
-        'response_message'   => '', // will be replaced with error description
-        'response_receiptno' => '', // RRN from MIGS
-        'order_info'         => '', // order ID from Ubercart
-        'order'              => NULL, // order, if applicable
-      ),
-      'template'  => 'uc-migs-order-incomplete',
-    ),
-    'uc_migs_cc_logo' => array(
-      'arguments' => array(
-        'filename' => '',
-        'alttext'  => '',
-      ),
-    ),
-  );
-}
-
-/**
- * Implementation of hook_menu().
- */
-function uc_migs_menu() {
-  $items['cart/migs/complete'] = array(
-    'title' => 'Order complete',
-    'page callback' => 'uc_migs_complete',
-    'access arguments' => array('access content'),
-    'type' => MENU_CALLBACK,
-  );
-  return $items;
-}
-
-/**
- * Implementation of hook_form_alter().
- */
-function uc_migs_form_alter(&$form, &$form_state, $form_id) {
-  if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
-    $order = uc_order_load($order_id);
-
-    if ($order->payment_method == 'migs') {
-      unset($form['submit']);
-      $form['#prefix'] = '<table width="100%" style="display: inline; padding-top: 1em;"><tr><td>';
-      $form['#suffix'] = '</td><td>'. drupal_get_form('uc_migs_form', $order) .'</td></tr></table>';
-    }
-  }
-}
-
-
-/*******************************************************************************
- * Hook Functions (Ubercart)
- ******************************************************************************/
-
-/**
- * Implementation of hook_payment_method().
- */
-function uc_migs_payment_method() {
-  $path = base_path() . drupal_get_path('module', 'uc_migs');
-  $title = variable_get('uc_migs_method_title', t('Credit card on a secure server.'));
-  if ( variable_get('uc_migs_method_title_icons', TRUE) ) {
-    $title .= '<br />' ;
-    foreach ( _uc_migs_cc_logos() as $k => $v ) {
-      $title .= theme('uc_migs_cc_logo', $v, $k) ;
-    }    
-  }
-  $methods[] = array(
-    'id' => 'migs',
-    'name' => t('MIGS 3rd party'),
-    'title' => $title,
-    'review' => variable_get('uc_migs_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
-    'desc' => t('Redirect to MIGS to pay by credit card or eCheck.'),
-    'callback' => 'uc_payment_method_migs',
-    'weight' => 3,
-    'checkout' => TRUE,
-    'backend' => TRUE,
-  );
-
-  return $methods;
-}
-
-
-/*******************************************************************************
- * Callback Functions, Forms, and Tables
- ******************************************************************************/
-
-/**
- * Callback for MIGS payment method settings.
- */
-function uc_payment_method_migs($op, &$arg1) {
-  switch ($op) {
-    case 'settings':
-      $form['uc_migs_mid'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Merchant Id'),
-        '#description' => t('Your MIGS merchant id.'),
-        '#default_value' => variable_get('uc_migs_mid', ''),
-        '#size' => 16,
-      );
-      $form['uc_migs_access_code'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Access code'),
-        '#description' => t('The access code is provided to you when you registered your merchant profile.'),
-        '#default_value' => variable_get('uc_migs_access_code', ''),
-        '#size' => 16,
-      );
-      $form['uc_migs_secure_hash_secret'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Secure hash secret'),
-        '#description' => t('The secure hash secret is used to check the integrity of the transaction request.'),
-        '#default_value' => variable_get('uc_migs_secure_hash_secret', ''),
-        '#size' => 32,
-      );
-      $form['uc_migs_version'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Version number'),
-        '#description' => t('The version number of the API being used. Currently developed for version 1. Most likely do not change this.'),
-        '#default_value' => variable_get('uc_migs_version', '1'),
-        '#size' => 5,
-      );
-      $form['uc_migs_locale'] = array(
-        '#type' => 'select',
-        '#title' => t('Language preference'),
-        '#description' => t('Adjust language on MIGS pages.'),
-        '#options' => array(
-          'en' => t('English'),
-        ),
-        '#default_value' => variable_get('uc_migs_locale', 'en'),
-      );
-      $form['uc_migs_method_title'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Payment method title'),
-        '#default_value' => variable_get('uc_migs_method_title', t('Credit card on a secure server:')),
-      );
-      $form['uc_migs_method_title_icons'] = array(
-        '#type' => 'checkbox',
-        '#title' => t('Show credit card icons beside the payment method title.'),
-        '#default_value' => variable_get('uc_migs_method_title_icons', TRUE),
-      );
-      foreach ( _uc_migs_cc_logos() as $cc_logo ) {
-        if ( !file_exists($cc_logo) ) {
-          $missing_cc_logos[] = $cc_logo ;
-        }
-      }
-      if ( $missing_cc_logos ) {
-        $form['uc_migs_method_title_icons']['#prefix'] = "<p class='error'>Please upload appropriate logos to have the correct credit card icons beside the payment method (if the next checkbox is enabled). The missing file(s) are: ". 
-          theme('item_list', $missing_cc_logos, '', 'ul', array('class' => 'error')) . '</p>' ;
-      }
-      $form['uc_migs_method_title_icons']['#description'] = 'The following logos can be displayed:<br />' ;
-      foreach ( _uc_migs_cc_logos() as $k => $v ) {
-        $form['uc_migs_method_title_icons']['#description'] .= theme('uc_migs_cc_logo', $v, $k) ;
-      }
-      $form['uc_migs_checkout_button'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Order review submit button text'),
-        '#description' => t('Provide MIGS specific text for the submit button on the order review page.'),
-        '#default_value' => variable_get('uc_migs_checkout_button', t('Submit Order')),
-      );
-      $form['uc_migs_repeat_checkouts'] = array(
-        '#type' => 'checkbox',
-        '#title' => t('Permit repeated checkouts by reloading order confirmation page. Recommended for debugging only.'),
-        '#default_value' => variable_get('uc_migs_repeat_checkouts', FALSE),
-      );
-      return $form;
-  }
-}
-
-/**
- * Form to build the submission to MIGS server
- */
-function uc_migs_form(&$form_state, $order) {
-  $vpc_Amount = round($order->order_total * 100);
-  $vpc_OrderInfo = $order->order_id;
-  $vpc_MerchTxnRef = $vpc_OrderInfo . '_' . time();
-  $data = array(
-    'vpc_MerchTxnRef' => $vpc_MerchTxnRef,
-    'vpc_OrderInfo' => $vpc_OrderInfo,
-    'vpc_Amount' => $vpc_Amount,
-  );
-  foreach ($data as $name => $value) {
-    $form[$name] = array(
-      '#type' => 'value', 
-      '#value' => $value
-    );
-  }
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => variable_get('uc_migs_checkout_button', t('Submit Order')),
-  );
-  return $form;
-}
-
-/**
- * Submit form and redirect to MIGS
- */
-function uc_migs_form_submit($form, &$form_state) {
-  $vpc_AccessCode  = variable_get('uc_migs_access_code', '');
-  $vpc_Amount      = $form_state['values']['vpc_Amount'];
-  $vpc_Locale      = variable_get('uc_migs_locale', 'en');
-  $vpc_Merchant    = variable_get('uc_migs_mid', '');
-  $vpc_OrderInfo   = $form_state['values']['vpc_OrderInfo'];
-  $vpc_ReturnURL   = url('cart/migs/complete/'. uc_cart_get_id(), array('absolute' => TRUE));
-  $vpc_Version     = variable_get('uc_migs_version', '1');
-  $vpc_MerchTxnRef = $form_state['values']['vpc_MerchTxnRef'] ;
-
-  $secure_secret = variable_get('uc_migs_secure_hash_secret', '');
-  
-  $md5HashData = $secure_secret . $vpc_AccessCode . $vpc_Amount . 'pay' . $vpc_Locale . $vpc_MerchTxnRef . $vpc_Merchant . $vpc_OrderInfo . $vpc_ReturnURL . $vpc_Version;
-  
-  // add the start of the vpcURL querystring parameters
-  $vpcURL = 'https://migs.mastercard.com.au/vpcpay?';
-
-  // urlencode overkill!
-  $vpcURL .= urlencode('vpc_AccessCode') . '=' . urlencode($vpc_AccessCode) .
-    '&' . urlencode('vpc_Amount') . "=" . urlencode($vpc_Amount) .
-    '&' . urlencode('vpc_Command') . "=" . urlencode('pay') .
-    '&' . urlencode('vpc_Locale') . "=" . urlencode($vpc_Locale) .
-    '&' . urlencode('vpc_MerchTxnRef') . "=" . urlencode($vpc_MerchTxnRef) .
-    '&' . urlencode('vpc_Merchant') . "=" . urlencode($vpc_Merchant) .
-    '&' . urlencode('vpc_OrderInfo') . "=" . urlencode($vpc_OrderInfo) .
-    '&' . urlencode('vpc_ReturnURL') . "=" . urlencode($vpc_ReturnURL) .
-    '&' . urlencode('vpc_Version') . '=' . urlencode($vpc_Version) .
-    '&' . urlencode('vpc_SecureHash') . '=' . strtoupper(md5($md5HashData));
-  
-  // redirect to MIGS URL for payment
-  $form_state['redirect'] = $vpcURL;
-}
-
-/**
- * Callback for MIGS order completion.
- */
-function uc_migs_complete($cart_id = 0) {
-  // Standard Receipt Data
-  $amount          = $_GET["vpc_Amount"];
-  $locale          = $_GET["vpc_Locale"];
-  $batchNo         = $_GET["vpc_BatchNo"];
-  $command         = $_GET["vpc_Command"];
-  $message         = $_GET["vpc_Message"]; 
-  $version         = $_GET["vpc_Version"];
-  $cardType        = $_GET["vpc_Card"];
-  $orderInfo       = $_GET["vpc_OrderInfo"];
-  $receiptNo       = $_GET["vpc_ReceiptNo"];
-  $merchantID      = $_GET["vpc_Merchant"];
-  $authorizeID     = $_GET["vpc_AuthorizeId"];
-  $merchTxnRef     = $_GET["vpc_MerchTxnRef"];
-  $transactionNo   = $_GET["vpc_TransactionNo"];
-  $acqResponseCode = $_GET["vpc_AcqResponseCode"];
-  $txnResponseCode = check_plain($_GET["vpc_TxnResponseCode"]);
-  $plain_message   = check_plain($_GET['vpc_Message']);
-
-  // 3-D Secure Data
-  $verType         = array_key_exists("vpc_VerType", $_GET)          ? $_GET["vpc_VerType"]          : "No Value Returned";
-  $verStatus       = array_key_exists("vpc_VerStatus", $_GET)        ? $_GET["vpc_VerStatus"]        : "No Value Returned";
-  $token           = array_key_exists("vpc_VerToken", $_GET)         ? $_GET["vpc_VerToken"]         : "No Value Returned";
-  $verSecurLevel   = array_key_exists("vpc_VerSecurityLevel", $_GET) ? $_GET["vpc_VerSecurityLevel"] : "No Value Returned";
-  $enrolled        = array_key_exists("vpc_3DSenrolled", $_GET)      ? $_GET["vpc_3DSenrolled"]      : "No Value Returned";
-  $xid             = array_key_exists("vpc_3DSXID", $_GET)           ? $_GET["vpc_3DSXID"]           : "No Value Returned";
-  $acqECI          = array_key_exists("vpc_3DSECI", $_GET)           ? $_GET["vpc_3DSECI"]           : "No Value Returned";
-  $authStatus      = array_key_exists("vpc_3DSstatus", $_GET)        ? $_GET["vpc_3DSstatus"]        : "No Value Returned";
-
-  $secure_secret = variable_get('uc_migs_secure_hash_secret', '');
-  
-  $vpc_Txn_Secure_Hash = $_GET["vpc_SecureHash"];
-  $errorExists = FALSE ;
-
-  if ( empty($secure_secret) ) {
-    // Secure Hash was not validated, none there to validate
-    drupal_set_title('Transaction could not be processed');
-    watchdog( 'uc_migs', 'Unable to process response from MIGS gateway, as MIGS Secure Hash Secret is not configured. Please correct your !settings.', array('!settings' => l('payment method settings', 'admin/store/settings/payment/edit/methods')), WATCHDOG_WARNING);
-    return theme('uc_migs_order_incomplete', 'Site configuration error', $receiptNo, $orderInfo, $order);
-  }
-  
-  if ( $txnResponseCode != 7 && $txnResponseCode != '' ) {
-    $md5HashData = $secure_secret;
-    // sort all the incoming vpc response fields and leave out any
-    // with no value, or which are "vpc_SecureHash" or "q"
-    foreach($_GET as $key => $value) {
-      if ($key != "vpc_SecureHash" && $key != "q" && strlen($value) > 0) { // strlen()>0 not empty() because of '0'
-        $md5HashData .= $value;
-      }
-    }
-    // validate secure hash
-    if (strtoupper($vpc_Txn_Secure_Hash) != strtoupper(md5($md5HashData))) {
-      // Secure Hash validation failed
-      drupal_set_title('Transaction could not be processed');
-      watchdog( 'uc_migs', 'MIGS gateway rejected payment due to validation failure. $_GET was: <pre>@get</pre>', array('@get' => print_r($_GET,1)), WATCHDOG_WARNING );
-      return theme('uc_migs_order_incomplete', 'Transaction could not be validated');    
-    }
-    else {
-      // validation success; fall thru to next phase 
-    }
-  } else {
-    // Secure Hash was not validated, none there to validate
-    drupal_set_title('Transaction could not be processed');
-    $order = uc_order_load($orderInfo);
-    watchdog( 'uc_migs', 'MIGS gateway rejected payment, and returned data did not contain a secure hash. $_GET was: <pre>@get</pre>', array('@get' => print_r($_GET,1)), WATCHDOG_WARNING );
-    return theme('uc_migs_order_incomplete', 'Transaction could not be validated', $receiptNo, $orderInfo, $order);  
-  }
-
-  $order = uc_order_load($orderInfo);
-  if ($order === FALSE) {
-    drupal_set_title('Transaction could not be processed');
-    watchdog('uc_migs', 'MIGS gateway unable to find order to process. The Order ID returned from MIGS was @order_id, and $_GET was <pre>@get</pre>', array('@order_id' => $orderInfo, '@get' => print_r($_GET,1)), WATCHDOG_WARNING);
-    return theme('uc_migs_order_incomplete', 'Order could not be found');
-  }
-
-  switch (uc_order_status_data($order->order_status, 'state')) {
-    case 'in_checkout' :
-      watchdog('uc_migs', 'MIGS payment notification received for order @order_id.', array('@order_id'=>$orderInfo));
-      break ;
-    case 'post_checkout' :
-      if ( variable_get('uc_migs_repeat_checkouts', FALSE) ) {
-        watchdog('uc_migs', 'MIGS payment repeat notification received for order @order_id.', array('@order_id'=>$orderInfo));
-        drupal_set_message('Permitting repeat checkout via MIGS.'); // it's for debug! localize the nag away if you must.
-        break ;
-      }
-
-      drupal_set_title('Order Already Completed');
-      // this happens when people reload the completion page. It's OK,
-      // but we'll make a note of it anyway.
-      watchdog('uc_migs', 'MIGS payment notification received for already checked out order @order_id.', array('@order_id'=>$orderInfo));
-      return theme('uc_migs_order_incomplete', 'Order already completed', $receiptNo, $orderInfo, $order);    
-    default:
-      drupal_set_title('Order Error');
-      watchdog('uc_migs', 'MIGS payment notification received for order @order_id which is not yet in checkout status.', array('@order_id'=>$orderInfo));
-      return theme('uc_migs_order_incomplete', 'Order not in checkout', $receiptNo, $orderInfo, $order);
-  }
-  
-  if ( $txnResponseCode != "0") {
-    // validation success, payment fail
-    drupal_set_title(t('Order Error'));
-    $errmsg = _uc_migs_get_response_description($txnResponseCode) ;
-    watchdog( 'uc_migs', 'A payment via MIGS failed. The error message was: <strong>%errmsg</strong>.<hr />$_GET was: <pre>@get</pre><hr />$order was: <pre>@order</pre>', array('@get' => print_r($_GET,1), '@order' => print_r($order,1), '%errmsg' => $errmsg), WATCHDOG_WARNING );
-    uc_order_comment_save($order->order_id, 0, t('Error processing payment for this order.'), 'admin');
-    return theme('uc_migs_order_incomplete', $errmsg, $receiptNo, $orderInfo, $order) ;
-  } else {
-    // success
-    $targs = array(
-      '@order'   => $orderInfo, 
-      '@receipt' => $receiptNo, 
-      '@trans'   => $transactionNo,
-      '@amount'  => uc_currency_format($amount/100),
-    ) ;
-    $comment = t('A payment of @amount for order #@order was made via MIGS by credit card with receipt number: @receipt and transaction number: @trans', $targs);
-    // we also attach the MIGS receipt number to the order so we can
-    // use it in an order token. Some MIGS banks require this number
-    // be displayed to the purchaser in order to more readily process
-    // complaints.
-    $order->data['migs_receiptno'] = $receiptNo ;
-    uc_order_save($order);
-    // uc_order_save() saves order_status so it needs to happen before uc_payment_enter()
-    uc_payment_enter($order->order_id, 'migs', $amount/100, 0, NULL, $comment);
-    
-    // Empty the cart...
-    uc_cart_empty($cart_id);
-    
-    $do_login = variable_get('uc_new_customer_login', FALSE );
-    $output = uc_cart_complete_sale($order, $do_login);
-    $output = str_replace('[site-url]', url(), $output);
-    
-    // Add a comment to let sales team know this came in through the site.
-    uc_order_comment_save($order->order_id, 0, t('A payment has been accepted.'), 'admin');    
-  }
-  return $output;
-}
-
-/**
- * This method uses the QSI Response code retrieved from the Digital
- * Receipt and returns an appropriate description for the QSI Response Code
- *
- * @param $responseCode String containing the QSI Response Code
- *
- * @return String containing the appropriate description
- */
-function _uc_migs_get_response_description($responseCode) {
-  switch ($responseCode) {
-    case "0" :
-      $result = "Transaction Successful"; 
-      break;
-    case "?" : 
-      $result = "Transaction status is unknown"; 
-      break;
-    case "1" : 
-      $result = "Transaction could not be processed";
-      break;
-    case "2" :
-      $result = "Bank Declined Transaction";
-      break ;
-    case "3" :
-      $result = "No Reply from Bank";
-      break ;
-    case "4" :
-      $result = "Expired Card";
-      break ;
-    case "5" :
-      $result = "Insufficient funds";
-      break ;
-    case "6" :
-      $result = "Error Communicating with Bank";
-      break ;
-    case "7" :
-      $result = "Payment Server System Error";
-      break ;
-    case "8" :
-      $result = "Transaction Type Not Supported";
-      break ;
-    case "9" :
-      $result = "Bank declined transaction (Do not contact Bank)";
-      break ;
-    case "A" :
-      $result = "Transaction Aborted";
-      break ;
-    case "C" :
-      $result = "Transaction Cancelled";
-      break ;
-    case "D" :
-      $result = "Deferred transaction has been received and is awaiting processing";
-      break ;
-    case "F" :
-      $result = "3D Secure Authentication failed";
-      break ;
-    case "I" :
-      $result = "Card Security Code verification failed";
-      break ;
-    case "L" :
-      $result = "Shopping Transaction Locked (Please try the transaction again later)";
-      break ;
-    case "N" :
-      $result = "Cardholder is not enrolled in Authentication scheme";
-      break ;
-    case "P" :
-      $result = "Transaction has been received by the Payment Adaptor and is being processed";
-      break ;
-    case "R" :
-      $result = "Transaction was not processed - Reached limit of retry attempts allowed";
-      break ;
-    case "S" :
-      $result = "Duplicate SessionID (OrderInfo)";
-      break ;
-    case "T" :
-      $result = "Address Verification Failed";
-      break ;
-    case "U" :
-      $result = "Card Security Code Failed";
-      break ;
-    case "V" :
-      $result = "Address Verification and Card Security Code Failed";
-      break ;
-    default  :
-      $result = "Unable to be determined"; 
-  }
-  return $result;
-}
-
-/**
- * Helper function to return a named array of CC provider logos to display.
- *
- * Note that you need not override this; you can simply alter the
- * variables or replace the files in the default location.
- */
-function _uc_migs_cc_logos() {
-  $cc_logos = array(
-    'MasterCard' => variable_get('uc_migs_logo_mastercard', file_directory_path() .'/uc_migs/mastercard.gif'),
-    'Visa' => variable_get('uc_migs_logo_visa', file_directory_path() .'/uc_migs/visa.gif'),
-  ) ;
-  return $cc_logos ;
-}
-
-/**
- * Allow people to override the logo display.
- *
- * return theme('imagecache', 'cc_logo', $filename, $alttext) ;
- *
- * I like to then do something like the above in my theme. Of course,
- * you can always modify the provider logos, but why open Photoshop
- * when you have Drupal? 'cc_logo' is a scale preset with height=50.
- */
-function theme_uc_migs_cc_logo($filename, $alttext='') {
-  return theme('image', $filename, $alttext) ;
-}
-
-/**
- * Implementation of hook_token_values(). (token.module)
- */
-function uc_migs_token_values($type, $object = NULL) {
-  switch ($type) {
-    case 'order':
-      $order = $object;
-      if ( isset($order->data['migs_receiptno']) ) {
-        $values['order-migs-receiptno'] = $order->data['migs_receiptno'] ;
-      }
-      break;
-  }
-  return $values;
-}
-
-/**
- * Implementation of hook_token_list(). (token.module)
- */
-function uc_migs_token_list($type = 'all') {
-  if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
-    $tokens['order']['order-migs-receiptno'] = t('MIGS Receipt Number (if payment processed via uc_migs)');
-  }
-  return $tokens;
-}
-
-/**
- * Preprocess function for uc_migs_order_incomplete()
- */
-function template_preprocess_uc_migs_order_incomplete(&$variables) {
-  if ( empty($variables['order_info']) ) {
-    $variables['order_id'] = 'Unknown Order ID' ;
-  }
-  else {
-    $variables['order_id'] = t('Order #@order_info', array('@order_info' => $variables['order_info']));
-  }
-  if ( empty($variables['response_receiptno']) ) {
-    $variables['receipt_no'] = 'Unknown Receipt No' ;
-  }
-  else {
-    $variables['receipt_no'] = t('Receipt #@receipt_no', array('@receipt_no' => $variables['response_receiptno']));
-  }
-  // by doing this, we make it possible for sites to reveal more
-  // detailed error messages via themeing, and still conceal them 
-  // by default which keeps MIGS happy (by default)
-  switch ( $variables['response_message'] ) {
-    // these variables indicate a site config issue or other
-    // issue we don't want to share with the great unwashed
-    case 'Site configuration error' :
-    case 'Transaction could not be validated' :
-    case 'Transaction could not be processed' :
-    case 'Order could not be found' :
-      $variables['error_message'] = 'Transaction error.' ;
-      break ;
-      
-    // however, if the problem is with the buyer's CC, we want to tell them what's wrong
-    default :
-      $variables['error_message'] = $variables['response_message'] ;
-  }
-}
-
-/**
- * Implementation of hook_ucga_display from UC Google Analytics module
- */
-function uc_migs_ucga_display() {
-  if (arg(0) == 'cart' && arg(1) == 'migs' && arg(2) == 'complete') {
-    return TRUE;
-  }
-  return FALSE;
-}
+<?php
+
+/**
+ * @file
+ * Integrates MIGS 3rd party payment system.
+ *
+ * This a payment module intended for the MIGS (MasterCard Internet
+ * Gateway Service) for what they call the "3rd party" integration
+ * method, or the Virtual Payment Client (VPC) which is the interface
+ * that processes the payment on their secure server.
+ *
+ * See http://drupal.org/project/uc_migs for the most up to date version.
+ *
+ * Currently maintained by Giant Robot Ltd - www.giantrobot.co.nz 
+ * Feedback welcome via issue queue or twitter.com/grobot
+ *
+ * Thanks also to previous authors & contributors:
+ *  T-Rex Art    - http://www.trexart.com.au
+ *  Open Web Lab - http://www.openweblab.com
+core = 7.x
+; Modified by PeterMoulding.com for Drupal 7, Ubercart 3.0, and CBA.
+; Modified for a new install with no attempt at an upgrade.
+
+file_directory_path() has been removed: http://drupal.org/update/modules/6/7#file_directory_path
+Ubercart 3.0-beta4, 2011-7-25
+-----------------------------
+- API:
+    * Removed ability to disable order logging.
+    * Removed the ability to disable payment logging or tracking.
+    * Off-site payment methods can specify simpler redirect forms.
+    * Payment gateways are now only used and supported by uc_credit.
+    * Ordered product titles increased from 128 to 255 characters.
+    * The following hooks are now keyed by ID, though backwards compatibility is kept:
+      * hook_uc_payment_method()
+
+ */
+
+/*******************************************************************************
+ * Hook Functions (Drupal)
+ ******************************************************************************/
+
+/** 
+ * Implementation of hook_theme()
+ */
+function uc_migs_theme() {
+  return array(
+    // Because we use uc_cart_complete_sale, we don't have an
+    // overridable theme function for uc completion. But we do have
+    // one for incomplete transactions!
+    'uc_migs_order_incomplete' => array(
+      'arguments' => array(
+        'response_message'   => '', // will be replaced with error description
+        'response_receiptno' => '', // RRN from MIGS
+        'order_info'         => '', // order ID from Ubercart
+        'order'              => NULL, // order, if applicable
+      ),
+      'template'  => 'uc-migs-order-incomplete',
+    ),
+    'uc_migs_cc_logo' => array(
+      'variables' => array(
+        'path' => '',
+        'alt'  => '',
+      ),
+    ),
+  );
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function uc_migs_menu() {
+  $items['cart/migs/complete'] = array(
+    'title' => 'Order complete',
+    'page callback' => 'uc_migs_complete',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function uc_migs_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
+    $order = uc_order_load($order_id);
+
+    if ($order->payment_method == 'migs') {
+      unset($form['submit']);
+      $form['#prefix'] = '<table width="100%" style="display: inline; padding-top: 1em;"><tr><td>';
+      $form['#suffix'] = '</td><td>'. drupal_get_form('uc_migs_form', $order) .'</td></tr></table>';
+    }
+  }
+}
+
+
+/*******************************************************************************
+ * Hook Functions (Ubercart)
+ ******************************************************************************/
+
+/**
+ * Implementation of hook_payment_method().
+Ubercart 3.0-beta4, 2011-7-25
+    * The following hooks are now keyed by ID, though backwards compatibility is kept:
+      * hook_uc_payment_method()
+ */
+function uc_migs_uc_payment_method() {
+  $path = base_path() . drupal_get_path('module', 'uc_migs');
+  $title = variable_get('uc_migs_method_title', t('Credit card on a secure server.'));
+  if ( variable_get('uc_migs_method_title_icons', TRUE) ) {
+    $title .= '<br />' ;
+    foreach ( _uc_migs_cc_logos() as $alt => $path ) {
+      $title .= theme('uc_migs_cc_logo', array('alt' => $alt, 'path' => $path)) ;
+    }    
+  }
+  $methods[] = array(
+    'id' => 'migs',
+    'name' => t('MIGS 3rd party'),
+    'title' => $title,
+    'review' => variable_get('uc_migs_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
+    'desc' => t('Redirect to MIGS to pay by credit card or eCheck.'),
+    'callback' => 'uc_payment_method_migs',
+    'weight' => 3,
+    'checkout' => TRUE,
+    'backend' => TRUE,
+  );
+
+  return $methods;
+}
+
+
+/*******************************************************************************
+ * Callback Functions, Forms, and Tables
+ ******************************************************************************/
+
+/**
+ * Callback for MIGS payment method settings.
+ */
+function uc_payment_method_migs($op, &$arg1) {
+  switch ($op) {
+    case 'settings':
+      $form['uc_migs_mid'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Merchant Id'),
+        '#description' => t('Your MIGS merchant id.'),
+        '#default_value' => variable_get('uc_migs_mid', ''),
+        '#size' => 16,
+      );
+      $form['uc_migs_access_code'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Access code'),
+        '#description' => t('The access code is provided to you when you registered your merchant profile.'),
+        '#default_value' => variable_get('uc_migs_access_code', ''),
+        '#size' => 16,
+      );
+      $form['uc_migs_secure_hash_secret'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Secure hash secret'),
+        '#description' => t('The secure hash secret is used to check the integrity of the transaction request.'),
+        '#default_value' => variable_get('uc_migs_secure_hash_secret', ''),
+        '#size' => 32,
+      );
+      $form['uc_migs_version'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Version number'),
+        '#description' => t('The version number of the API being used. Currently developed for version 1. Most likely do not change this.'),
+        '#default_value' => variable_get('uc_migs_version', '1'),
+        '#size' => 5,
+      );
+      $form['uc_migs_locale'] = array(
+        '#type' => 'select',
+        '#title' => t('Language preference'),
+        '#description' => t('Adjust language on MIGS pages.'),
+        '#options' => array(
+          'en' => t('English'),
+        ),
+        '#default_value' => variable_get('uc_migs_locale', 'en'),
+      );
+      $form['uc_migs_method_title'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Payment method title'),
+        '#default_value' => variable_get('uc_migs_method_title', t('Credit card on a secure server:')),
+      );
+      $form['uc_migs_method_title_icons'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Show credit card icons beside the payment method title.'),
+        '#default_value' => variable_get('uc_migs_method_title_icons', TRUE),
+      );
+      foreach ( _uc_migs_cc_logos() as $cc_logo ) {
+        if ( !file_exists($cc_logo) ) {
+          $missing_cc_logos[] = $cc_logo ;
+        }
+      }
+      if ( $missing_cc_logos ) {
+        $form['uc_migs_method_title_icons']['#prefix'] = "<p class='error'>Please upload appropriate logos to have the correct credit card icons beside the payment method (if the next checkbox is enabled). The missing file(s) are: ". 
+          theme('item_list', $missing_cc_logos, '', 'ul', array('class' => 'error')) . '</p>' ;
+      }
+      $form['uc_migs_method_title_icons']['#description'] = 'The following logos can be displayed:<br />' ;
+      foreach ( _uc_migs_cc_logos() as $k => $v ) {
+        $form['uc_migs_method_title_icons']['#description'] .= theme('uc_migs_cc_logo', $v, $k) ;
+      }
+      $form['uc_migs_checkout_button'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Order review submit button text'),
+        '#description' => t('Provide MIGS specific text for the submit button on the order review page.'),
+        '#default_value' => variable_get('uc_migs_checkout_button', t('Submit Order')),
+      );
+      $form['uc_migs_repeat_checkouts'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Permit repeated checkouts by reloading order confirmation page. Recommended for debugging only.'),
+        '#default_value' => variable_get('uc_migs_repeat_checkouts', FALSE),
+      );
+      return $form;
+  }
+}
+
+/**
+ * Form to build the submission to MIGS server
+ */
+function uc_migs_form(&$form_state, $order) {
+  $vpc_Amount = round($order->order_total * 100);
+  $vpc_OrderInfo = $order->order_id;
+  $vpc_MerchTxnRef = $vpc_OrderInfo . '_' . time();
+  $data = array(
+    'vpc_MerchTxnRef' => $vpc_MerchTxnRef,
+    'vpc_OrderInfo' => $vpc_OrderInfo,
+    'vpc_Amount' => $vpc_Amount,
+  );
+  foreach ($data as $name => $value) {
+    $form[$name] = array(
+      '#type' => 'value', 
+      '#value' => $value
+    );
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => variable_get('uc_migs_checkout_button', t('Submit Order')),
+  );
+  return $form;
+}
+
+/**
+ * Submit form and redirect to MIGS
+ */
+function uc_migs_form_submit($form, &$form_state) {
+  $vpc_AccessCode  = variable_get('uc_migs_access_code', '');
+  $vpc_Amount      = $form_state['values']['vpc_Amount'];
+  $vpc_Locale      = variable_get('uc_migs_locale', 'en');
+  $vpc_Merchant    = variable_get('uc_migs_mid', '');
+  $vpc_OrderInfo   = $form_state['values']['vpc_OrderInfo'];
+  $vpc_ReturnURL   = url('cart/migs/complete/'. uc_cart_get_id(), array('absolute' => TRUE));
+  $vpc_Version     = variable_get('uc_migs_version', '1');
+  $vpc_MerchTxnRef = $form_state['values']['vpc_MerchTxnRef'] ;
+
+  $secure_secret = variable_get('uc_migs_secure_hash_secret', '');
+  
+  $md5HashData = $secure_secret . $vpc_AccessCode . $vpc_Amount . 'pay' . $vpc_Locale . $vpc_MerchTxnRef . $vpc_Merchant . $vpc_OrderInfo . $vpc_ReturnURL . $vpc_Version;
+  
+  // add the start of the vpcURL querystring parameters
+  $vpcURL = 'https://migs.mastercard.com.au/vpcpay?';
+
+  // urlencode overkill!
+  $vpcURL .= urlencode('vpc_AccessCode') . '=' . urlencode($vpc_AccessCode) .
+    '&' . urlencode('vpc_Amount') . "=" . urlencode($vpc_Amount) .
+    '&' . urlencode('vpc_Command') . "=" . urlencode('pay') .
+    '&' . urlencode('vpc_Locale') . "=" . urlencode($vpc_Locale) .
+    '&' . urlencode('vpc_MerchTxnRef') . "=" . urlencode($vpc_MerchTxnRef) .
+    '&' . urlencode('vpc_Merchant') . "=" . urlencode($vpc_Merchant) .
+    '&' . urlencode('vpc_OrderInfo') . "=" . urlencode($vpc_OrderInfo) .
+    '&' . urlencode('vpc_ReturnURL') . "=" . urlencode($vpc_ReturnURL) .
+    '&' . urlencode('vpc_Version') . '=' . urlencode($vpc_Version) .
+    '&' . urlencode('vpc_SecureHash') . '=' . strtoupper(md5($md5HashData));
+  
+  // redirect to MIGS URL for payment
+  $form_state['redirect'] = $vpcURL;
+}
+
+/**
+ * Callback for MIGS order completion.
+ */
+function uc_migs_complete($cart_id = 0) {
+  // Standard Receipt Data
+  $amount          = $_GET["vpc_Amount"];
+  $locale          = $_GET["vpc_Locale"];
+  $batchNo         = $_GET["vpc_BatchNo"];
+  $command         = $_GET["vpc_Command"];
+  $message         = $_GET["vpc_Message"]; 
+  $version         = $_GET["vpc_Version"];
+  $cardType        = $_GET["vpc_Card"];
+  $orderInfo       = $_GET["vpc_OrderInfo"];
+  $receiptNo       = $_GET["vpc_ReceiptNo"];
+  $merchantID      = $_GET["vpc_Merchant"];
+  $authorizeID     = $_GET["vpc_AuthorizeId"];
+  $merchTxnRef     = $_GET["vpc_MerchTxnRef"];
+  $transactionNo   = $_GET["vpc_TransactionNo"];
+  $acqResponseCode = $_GET["vpc_AcqResponseCode"];
+  $txnResponseCode = check_plain($_GET["vpc_TxnResponseCode"]);
+  $plain_message   = check_plain($_GET['vpc_Message']);
+
+  // 3-D Secure Data
+  $verType         = array_key_exists("vpc_VerType", $_GET)          ? $_GET["vpc_VerType"]          : "No Value Returned";
+  $verStatus       = array_key_exists("vpc_VerStatus", $_GET)        ? $_GET["vpc_VerStatus"]        : "No Value Returned";
+  $token           = array_key_exists("vpc_VerToken", $_GET)         ? $_GET["vpc_VerToken"]         : "No Value Returned";
+  $verSecurLevel   = array_key_exists("vpc_VerSecurityLevel", $_GET) ? $_GET["vpc_VerSecurityLevel"] : "No Value Returned";
+  $enrolled        = array_key_exists("vpc_3DSenrolled", $_GET)      ? $_GET["vpc_3DSenrolled"]      : "No Value Returned";
+  $xid             = array_key_exists("vpc_3DSXID", $_GET)           ? $_GET["vpc_3DSXID"]           : "No Value Returned";
+  $acqECI          = array_key_exists("vpc_3DSECI", $_GET)           ? $_GET["vpc_3DSECI"]           : "No Value Returned";
+  $authStatus      = array_key_exists("vpc_3DSstatus", $_GET)        ? $_GET["vpc_3DSstatus"]        : "No Value Returned";
+
+  $secure_secret = variable_get('uc_migs_secure_hash_secret', '');
+  
+  $vpc_Txn_Secure_Hash = $_GET["vpc_SecureHash"];
+  $errorExists = FALSE ;
+
+  if ( empty($secure_secret) ) {
+    // Secure Hash was not validated, none there to validate
+    drupal_set_title('Transaction could not be processed');
+    watchdog( 'uc_migs', 'Unable to process response from MIGS gateway, as MIGS Secure Hash Secret is not configured. Please correct your !settings.', array('!settings' => l('payment method settings', 'admin/store/settings/payment/edit/methods')), WATCHDOG_WARNING);
+    return theme('uc_migs_order_incomplete', 'Site configuration error', $receiptNo, $orderInfo, $order);
+  }
+  
+  if ( $txnResponseCode != 7 && $txnResponseCode != '' ) {
+    $md5HashData = $secure_secret;
+    // sort all the incoming vpc response fields and leave out any
+    // with no value, or which are "vpc_SecureHash" or "q"
+    foreach($_GET as $key => $value) {
+      if ($key != "vpc_SecureHash" && $key != "q" && strlen($value) > 0) { // strlen()>0 not empty() because of '0'
+        $md5HashData .= $value;
+      }
+    }
+    // validate secure hash
+    if (strtoupper($vpc_Txn_Secure_Hash) != strtoupper(md5($md5HashData))) {
+      // Secure Hash validation failed
+      drupal_set_title('Transaction could not be processed');
+      watchdog( 'uc_migs', 'MIGS gateway rejected payment due to validation failure. $_GET was: <pre>@get</pre>', array('@get' => print_r($_GET,1)), WATCHDOG_WARNING );
+      return theme('uc_migs_order_incomplete', 'Transaction could not be validated');    
+    }
+    else {
+      // validation success; fall thru to next phase 
+    }
+  } else {
+    // Secure Hash was not validated, none there to validate
+    drupal_set_title('Transaction could not be processed');
+    $order = uc_order_load($orderInfo);
+    watchdog( 'uc_migs', 'MIGS gateway rejected payment, and returned data did not contain a secure hash. $_GET was: <pre>@get</pre>', array('@get' => print_r($_GET,1)), WATCHDOG_WARNING );
+    return theme('uc_migs_order_incomplete', 'Transaction could not be validated', $receiptNo, $orderInfo, $order);  
+  }
+
+  $order = uc_order_load($orderInfo);
+  if ($order === FALSE) {
+    drupal_set_title('Transaction could not be processed');
+    watchdog('uc_migs', 'MIGS gateway unable to find order to process. The Order ID returned from MIGS was @order_id, and $_GET was <pre>@get</pre>', array('@order_id' => $orderInfo, '@get' => print_r($_GET,1)), WATCHDOG_WARNING);
+    return theme('uc_migs_order_incomplete', 'Order could not be found');
+  }
+
+  switch (uc_order_status_data($order->order_status, 'state')) {
+    case 'in_checkout' :
+      watchdog('uc_migs', 'MIGS payment notification received for order @order_id.', array('@order_id'=>$orderInfo));
+      break ;
+    case 'post_checkout' :
+      if ( variable_get('uc_migs_repeat_checkouts', FALSE) ) {
+        watchdog('uc_migs', 'MIGS payment repeat notification received for order @order_id.', array('@order_id'=>$orderInfo));
+        drupal_set_message('Permitting repeat checkout via MIGS.'); // it's for debug! localize the nag away if you must.
+        break ;
+      }
+
+      drupal_set_title('Order Already Completed');
+      // this happens when people reload the completion page. It's OK,
+      // but we'll make a note of it anyway.
+      watchdog('uc_migs', 'MIGS payment notification received for already checked out order @order_id.', array('@order_id'=>$orderInfo));
+      return theme('uc_migs_order_incomplete', 'Order already completed', $receiptNo, $orderInfo, $order);    
+    default:
+      drupal_set_title('Order Error');
+      watchdog('uc_migs', 'MIGS payment notification received for order @order_id which is not yet in checkout status.', array('@order_id'=>$orderInfo));
+      return theme('uc_migs_order_incomplete', 'Order not in checkout', $receiptNo, $orderInfo, $order);
+  }
+  
+  if ( $txnResponseCode != "0") {
+    // validation success, payment fail
+    drupal_set_title(t('Order Error'));
+    $errmsg = _uc_migs_get_response_description($txnResponseCode) ;
+    watchdog( 'uc_migs', 'A payment via MIGS failed. The error message was: <strong>%errmsg</strong>.<hr />$_GET was: <pre>@get</pre><hr />$order was: <pre>@order</pre>', array('@get' => print_r($_GET,1), '@order' => print_r($order,1), '%errmsg' => $errmsg), WATCHDOG_WARNING );
+    uc_order_comment_save($order->order_id, 0, t('Error processing payment for this order.'), 'admin');
+    return theme('uc_migs_order_incomplete', $errmsg, $receiptNo, $orderInfo, $order) ;
+  } else {
+    // success
+    $targs = array(
+      '@order'   => $orderInfo, 
+      '@receipt' => $receiptNo, 
+      '@trans'   => $transactionNo,
+      '@amount'  => uc_currency_format($amount/100),
+    ) ;
+    $comment = t('A payment of @amount for order #@order was made via MIGS by credit card with receipt number: @receipt and transaction number: @trans', $targs);
+    // we also attach the MIGS receipt number to the order so we can
+    // use it in an order token. Some MIGS banks require this number
+    // be displayed to the purchaser in order to more readily process
+    // complaints.
+    $order->data['migs_receiptno'] = $receiptNo ;
+    uc_order_save($order);
+    // uc_order_save() saves order_status so it needs to happen before uc_payment_enter()
+    uc_payment_enter($order->order_id, 'migs', $amount/100, 0, NULL, $comment);
+    
+    // Empty the cart...
+    uc_cart_empty($cart_id);
+    
+    $do_login = variable_get('uc_new_customer_login', FALSE );
+    $output = uc_cart_complete_sale($order, $do_login);
+    $output = str_replace('[site-url]', url(), $output);
+    
+    // Add a comment to let sales team know this came in through the site.
+    uc_order_comment_save($order->order_id, 0, t('A payment has been accepted.'), 'admin');    
+  }
+  return $output;
+}
+
+/**
+ * This method uses the QSI Response code retrieved from the Digital
+ * Receipt and returns an appropriate description for the QSI Response Code
+ *
+ * @param $responseCode String containing the QSI Response Code
+ *
+ * @return String containing the appropriate description
+ */
+function _uc_migs_get_response_description($responseCode) {
+  switch ($responseCode) {
+    case "0" :
+      $result = "Transaction Successful"; 
+      break;
+    case "?" : 
+      $result = "Transaction status is unknown"; 
+      break;
+    case "1" : 
+      $result = "Transaction could not be processed";
+      break;
+    case "2" :
+      $result = "Bank Declined Transaction";
+      break ;
+    case "3" :
+      $result = "No Reply from Bank";
+      break ;
+    case "4" :
+      $result = "Expired Card";
+      break ;
+    case "5" :
+      $result = "Insufficient funds";
+      break ;
+    case "6" :
+      $result = "Error Communicating with Bank";
+      break ;
+    case "7" :
+      $result = "Payment Server System Error";
+      break ;
+    case "8" :
+      $result = "Transaction Type Not Supported";
+      break ;
+    case "9" :
+      $result = "Bank declined transaction (Do not contact Bank)";
+      break ;
+    case "A" :
+      $result = "Transaction Aborted";
+      break ;
+    case "C" :
+      $result = "Transaction Cancelled";
+      break ;
+    case "D" :
+      $result = "Deferred transaction has been received and is awaiting processing";
+      break ;
+    case "F" :
+      $result = "3D Secure Authentication failed";
+      break ;
+    case "I" :
+      $result = "Card Security Code verification failed";
+      break ;
+    case "L" :
+      $result = "Shopping Transaction Locked (Please try the transaction again later)";
+      break ;
+    case "N" :
+      $result = "Cardholder is not enrolled in Authentication scheme";
+      break ;
+    case "P" :
+      $result = "Transaction has been received by the Payment Adaptor and is being processed";
+      break ;
+    case "R" :
+      $result = "Transaction was not processed - Reached limit of retry attempts allowed";
+      break ;
+    case "S" :
+      $result = "Duplicate SessionID (OrderInfo)";
+      break ;
+    case "T" :
+      $result = "Address Verification Failed";
+      break ;
+    case "U" :
+      $result = "Card Security Code Failed";
+      break ;
+    case "V" :
+      $result = "Address Verification and Card Security Code Failed";
+      break ;
+    default  :
+      $result = "Unable to be determined"; 
+  }
+  return $result;
+}
+
+/**
+ * Helper function to return a named array of CC provider logos to display.
+ *
+ * Note that you need not override this; you can simply alter the
+ * variables or replace the files in the default location.
+ Drupal 7:
+ file_directory_path() has been removed: http://drupal.org/update/modules/6/7#file_directory_path
+
+ */
+function _uc_migs_cc_logos() {
+//  $files_path = variable_get('file_public_path', conf_path() . '/files');
+  $cc_logos = array(
+//    'MasterCard' => variable_get('uc_migs_logo_mastercard', file_directory_path() .'/uc_migs/mastercard.gif'),
+    'MasterCard' => uc_migs_credit_image('mastercard'),
+//    'Visa' => variable_get('uc_migs_logo_visa', file_directory_path() .'/uc_migs/visa.gif'),
+    'Visa' => uc_migs_credit_image('visa'),
+  ) ;
+  return $cc_logos ;
+}
+
+
+function uc_migs_credit_image($type) {
+  static $cc_types = array('visa', 'mastercard', 'discover', 'amex');
+  static $path;
+  if(!isset($path)) { $path = base_path() . drupal_get_path('module', 'uc_credit'); }
+  if(in_array($type, $cc_types)) {
+    if (variable_get('uc_credit_' . $type, TRUE)) {
+      return $path . '/images/' . $type . '.gif';
+    }
+  }
+  return '';
+}
+
+
+/**
+ * Allow people to override the logo display.
+ *
+ * return theme('imagecache', 'cc_logo', $filename, $alttext) ;
+ *
+ * I like to then do something like the above in my theme. Of course,
+ * you can always modify the provider logos, but why open Photoshop
+ * when you have Drupal? 'cc_logo' is a scale preset with height=50.
+ */
+function theme_uc_migs_cc_logo($variables) {
+//  return theme('image', $filename, $alttext) ;
+  return theme('image', $variables) ;
+}
+
+/**
+ * Implementation of hook_token_values(). (token.module)
+ */
+function uc_migs_token_values($type, $object = NULL) {
+  switch ($type) {
+    case 'order':
+      $order = $object;
+      if ( isset($order->data['migs_receiptno']) ) {
+        $values['order-migs-receiptno'] = $order->data['migs_receiptno'] ;
+      }
+      break;
+  }
+  return $values;
+}
+
+/**
+ * Implementation of hook_token_list(). (token.module)
+ */
+function uc_migs_token_list($type = 'all') {
+  if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
+    $tokens['order']['order-migs-receiptno'] = t('MIGS Receipt Number (if payment processed via uc_migs)');
+  }
+  return $tokens;
+}
+
+/**
+ * Preprocess function for uc_migs_order_incomplete()
+ */
+function template_preprocess_uc_migs_order_incomplete(&$variables) {
+  if ( empty($variables['order_info']) ) {
+    $variables['order_id'] = 'Unknown Order ID' ;
+  }
+  else {
+    $variables['order_id'] = t('Order #@order_info', array('@order_info' => $variables['order_info']));
+  }
+  if ( empty($variables['response_receiptno']) ) {
+    $variables['receipt_no'] = 'Unknown Receipt No' ;
+  }
+  else {
+    $variables['receipt_no'] = t('Receipt #@receipt_no', array('@receipt_no' => $variables['response_receiptno']));
+  }
+  // by doing this, we make it possible for sites to reveal more
+  // detailed error messages via themeing, and still conceal them 
+  // by default which keeps MIGS happy (by default)
+  switch ( $variables['response_message'] ) {
+    // these variables indicate a site config issue or other
+    // issue we don't want to share with the great unwashed
+    case 'Site configuration error' :
+    case 'Transaction could not be validated' :
+    case 'Transaction could not be processed' :
+    case 'Order could not be found' :
+      $variables['error_message'] = 'Transaction error.' ;
+      break ;
+      
+    // however, if the problem is with the buyer's CC, we want to tell them what's wrong
+    default :
+      $variables['error_message'] = $variables['response_message'] ;
+  }
+}
+
+/**
+ * Implementation of hook_ucga_display from UC Google Analytics module
+ */
+function uc_migs_ucga_display() {
+  if (arg(0) == 'cart' && arg(1) == 'migs' && arg(2) == 'complete') {
+    return TRUE;
+  }
+  return FALSE;
+}
