commit 6d42a8d257c489aaa5d82d1de027a826b13f033a
Author: Chris Burgess <chris@webscope.co.nz>
Date:   Wed Mar 7 12:45:16 2012 +1300

    Issue #1170614.
    
    Patch based on http://drupal.org/files/uc_pma-7.x-1.x-dev_0.zip by @tomasbarej.

diff --git a/uc_pma.info b/uc_pma.info
index 376ad86..fc02cb5 100755
--- a/uc_pma.info
+++ b/uc_pma.info
@@ -1,6 +1,11 @@
+; $Id$
 name = Payment Method Adjustments
 description = Associate a fee or discount with a payment method at checkout.
 dependencies[] = uc_cart
 dependencies[] = uc_payment
 package = Ubercart - payment
-core = 6.x
+core = 7.x
+
+version = "7.x-1.0"
+core = "7.x"
+project = "uc_pma"
\ No newline at end of file
diff --git a/uc_pma.module b/uc_pma.module
index 870077b..63ad265 100755
--- a/uc_pma.module
+++ b/uc_pma.module
@@ -1,36 +1,22 @@
 <?php
+// $Id$
 
 /**
  * @file This module hooks into a few different parts of Ubercart to allow store
  *   administrators to associate a fee or discount with payment methods.
  *
  * Module compiled by Ryan from a schnazzy patch by cYu.
+ * Ported on Drupal 7.x by Tomas Barej
  */
 
 /**
- * Implementation of hook_menu().
- */
-function uc_pma_menu() {
-  $items['cart/checkout/method_items'] = array(
-    'title' => 'Return payment method subtotal adjustments',
-    'page callback' => 'uc_pma_method_items',
-    'access arguments' => array('access content'),
-    'type' => MENU_CALLBACK,
-  );
-
-  return $items;
-}
-
-/**
  * Implementation of hook_theme().
  */
 function uc_pma_theme() {
   return array(
     'uc_pma_payment_method_table' => array(
+      'render element' => 'form',
       'file' => 'uc_pma.module',
-      'arguments' => array(
-        'form' => NULL,
-      ),
     ),
   );
 }
@@ -40,11 +26,16 @@ function uc_pma_theme() {
  */
 function uc_pma_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'uc_payment_methods_form') {
-    $form['methods_info']['#value'] .=
-       '<div>'
+    $methods_info = '<div>'
       . t('The adjustment field can be used to associate a fee or discount with a payment method.  Specify a value that is a flat amount or a percentage of the subtotal to be added or subtracted when a payment method is chosen at checkout. Examples: 3%, 1.00, -5.5%, -2')
       .'</div><br />';
 
+    if(isset($form['methods_info'])){
+      $form['methods_info']['#value'] .= $methods_info;
+    }else{
+      $form['methods_info']['#value'] = $methods_info;
+    }
+
     $form['pmtable']['#theme'] = 'uc_pma_payment_method_table';
 
     foreach (element_children($form['pmtable']) as $method_id) {
@@ -64,9 +55,30 @@ function uc_pma_form_alter(&$form, &$form_state, $form_id) {
       }
     }
 
-    drupal_add_js(drupal_get_path('module', 'uc_pma') .'/uc_pma.js');
-    drupal_add_js("\$(document).ready( function () { update_method_line_item('". $form['panes']['payment']['payment_method']['#default_value'] ."'); } );", 'inline');
+    //replace default payment pane ajax behaviour with own
+    $form['panes']['payment']['payment_method']['#ajax'] = array(
+      'callback' => 'uc_pma_payment_method_ajax',
+      'progress' => array(
+        'type' => 'throbber',
+      ),
+    );
+  }
+}
+
+/**
+ * AJAX callback for payment method details on the checkout form and adding a line item of payment method adjustment.
+ */
+function uc_pma_payment_method_ajax($form, $form_state) {
+  if (isset($form['panes']['payment']['details'])) {
+    $commands[] = ajax_command_replace('#payment-details', drupal_render($form['panes']['payment']['details']));
+  }
+
+  if (isset($form['panes']['payment']['line_items'])) {
+    $commands[] = ajax_command_replace('#line-items-div', drupal_render($form['panes']['payment']['line_items']));
+    $commands[] = ajax_command_prepend('#line-items-div', theme('status_messages'));
   }
+
+  return array('#type' => 'ajax', '#commands' => $commands);
 }
 
 
@@ -75,23 +87,58 @@ function uc_pma_form_alter(&$form, &$form_state, $form_id) {
  ******************************************************************************/
 
 /**
- * Implementation of hook_line_item().
+ * Implementation of hook_uc_line_item().
  */
-function uc_pma_line_item() {
+function uc_pma_uc_line_item() {
   $items[] = array(
-    'id' => 'payment_method',
-    'title' => t('Payment method'),
-    'weight' => 6,
-    'stored' => true,
-    'default' => FALSE,
+    'id'         => 'payment',
+    'title'      => t('Payment method'),
+    'weight'     => 6,
+    'stored'     => FALSE,
     'calculated' => TRUE,
-    'display_only' => FALSE,
+    'add_list'   => TRUE,
+    'callback'   => 'uc_pma_adjustment_callback',
   );
 
   return $items;
 }
 
 /**
+ * Payment ajustment line item callback
+ */
+function uc_pma_adjustment_callback($op, &$order) {
+  switch ($op) {
+    case 'load':
+      $adjustment_order = $order;
+
+      if(empty($adjustment_order->payment_method)){ //if payment_method isn't set we pick the default one
+        $methods = _uc_payment_method_list();
+        $default = NULL;
+
+        if(count($methods) > 0){
+          foreach ($methods as $id => $method) {
+            if ($method['checkout'] && !isset($method['express'])) {
+              if(is_null($default)) $default = $id;
+            }
+          }
+          $adjustment_order->payment_method = $default;
+        }
+      }
+
+      $adjustment = _payment_method_adjustment($adjustment_order);
+      if(isset($adjustment['description']) && isset($adjustment['value'])){
+        $lines[] = array(
+          'id' => 'payment_method',
+          'title' => $adjustment['description'],
+          'amount' => $adjustment['value'],
+        );
+      }
+
+      return $lines;
+  }
+}
+
+/**
  * Implementation of hook_order().
  */
 function uc_pma_order($op, &$arg1) {
@@ -108,47 +155,40 @@ function uc_pma_order($op, &$arg1) {
 }
 
 // Retheme the payment method settings table to include the adjustment field.
-function theme_uc_pma_payment_method_table($form) {
-  $header = array(t('Enabled'), t('Payment method'), t('Weight'), t('Adjustment'), t('Default gateway'));
+function theme_uc_pma_payment_method_table($variables) {
+  $form = $variables['form'];
+
+  drupal_add_tabledrag('uc-payment-methods', 'order', 'sibling', 'uc-payment-method-weight');
+
+  $header = array(t('Payment method'), t('List position'), t('Adjustment'), t('Operations'));
 
+  $rows = array();
   foreach (element_children($form) as $method) {
-    $rows[] = array(
-      array('data' => drupal_render($form[$method]['uc_payment_method_'. $method .'_checkout']), 'align' => 'center'),
-      drupal_render($form[$method]['name']),
-      drupal_render($form[$method]['uc_payment_method_'. $method .'_weight']),
-      drupal_render($form[$method]['uc_payment_method_'. $method .'_adjustment']),
-      drupal_render($form[$method]['uc_payment_'. $method .'_gateway']),
+    $row = array(
+      drupal_render($form[$method]['uc_payment_method_' . $method . '_checkout']),
+      drupal_render($form[$method]['uc_payment_method_' . $method . '_weight']),
+      drupal_render($form[$method]['uc_payment_method_' . $method . '_adjustment']),
+      drupal_render($form[$method]['settings']),
     );
-  }
 
-  if (empty($rows)) {
     $rows[] = array(
-      array('data' => t('No payment methods found.'), 'colspan' => 5),
+      'data' => $row,
+      'class' => array('draggable'),
     );
   }
 
-  return theme('table', $header, $rows);
-}
-
-function uc_pma_method_items($payment_method) {
-  drupal_set_header("Content-Type: text/javascript; charset=utf-8");
-  $order = $_POST['order'];
-  if ($order = unserialize(rawurldecode($order))) {
-    $order->payment_method = $payment_method;
-    $adjustment = _payment_method_adjustment($order);
-    print $adjustment['value'] .'|'. $adjustment['description'];
-  }
-  exit();
+  return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'uc-payment-methods')));
 }
 
 function payment_method_adjustment_description($method_id) {
-  $name = _payment_method_data($method_id, 'name');
+  $name = _uc_payment_method_data($method_id, 'name');
   $adjustment = variable_get('uc_payment_method_'. $method_id .'_adjustment', '');
   if (empty($name) || empty($adjustment)) {
     return;
   }
 
-  $adjustment = ereg_replace('[^-0-9'. variable_get('uc_currency_dec', '.') .'%]+', '', $adjustment);
+  //TODO: use of @ sign suppresses ereg deprecated errors, needs to be rewritten using preg_replace.
+  $adjustment = @ereg_replace('[^-0-9'. variable_get('uc_currency_dec', '.') .'%]+', '', $adjustment);
 
   if (!strstr($adjustment, '%')) {
     $adjustment = uc_currency_format(str_replace(variable_get('uc_currency_dec', '.'), ".", $adjustment));
@@ -165,8 +205,9 @@ function payment_method_adjustment_description($method_id) {
 }
 
 function _payment_method_adjustment($order) {
+  if(empty($order->payment_method)) return 0;
   $adjustment_data = array(
-    'name' => _payment_method_data($order->payment_method, 'name'),
+    'name' => _uc_payment_method_data($order->payment_method, 'name'),
     'adjustment' => variable_get('uc_payment_method_'. $order->payment_method .'_adjustment', ''),
   );
 
@@ -180,7 +221,9 @@ function _payment_method_adjustment($order) {
   $percent = FALSE;
 
   $adjustment = array();
-  $adjustment = ereg_replace('[^-0-9'. variable_get('uc_currency_dec', '.') .'%]+', '', $adjustment_data['adjustment']);
+
+  //TODO: use of @ sign suppresses ereg deprecated errors, needs to be rewritten using preg_replace.
+  $adjustment = @ereg_replace('[^-0-9'. variable_get('uc_currency_dec', '.') .'%]+', '', $adjustment_data['adjustment']);
   $ret['value'] = $adjustment;
   $ret['description'] = t('@name fee', array('@name' => $adjustment_data['name']));
 
@@ -199,6 +242,7 @@ function _payment_method_adjustment($order) {
   if ($discount) {
     $ret['description'] = t('@name discount', array('@name' => $adjustment_data['name']));
   }
+  $ret['description'] = ucfirst($ret['description']); //force first letter of line item to be uppercase
   $ret['value'] = number_format(str_replace(variable_get('uc_currency_dec', '.'), ".", $ret['value']), 2);
 
   return $ret;
