You are here

function _payment_method_adjustment in Ubercart Payment Method Adjustments 7

Same name and namespace in other branches
  1. 6 uc_pma.module \_payment_method_adjustment()
2 calls to _payment_method_adjustment()
uc_pma_adjustment_callback in ./uc_pma.module
Payment ajustment line item callback
uc_pma_order in ./uc_pma.module
Implements hook_order().

File

./uc_pma.module, line 221
This module hooks into a few different parts of Ubercart to allow store administrators to associate a fee or discount with payment methods.

Code

function _payment_method_adjustment($order) {
  if (empty($order->payment_method)) {
    return 0;
  }

  // In case the variable is set to '', check if $adjustment is empty and substitute 0.
  $pm_adjustment = variable_get('uc_payment_method_' . $order->payment_method . '_adjustment', '0');
  $adjustment_data = array(
    'name' => _uc_payment_method_data($order->payment_method, 'name'),
    'adjustment' => empty($pm_adjustment) ? '0' : $pm_adjustment,
  );
  $subtotal = 0;
  foreach ($order->products as $item) {
    $item_total = $item->qty ? $item->qty * $item->price : $item->price;
    $subtotal += $item_total;
  }
  $adjust_types = variable_get('uc_pma_adjustments_apply_to', array());
  if (!empty($adjust_types)) {
    foreach ($order->line_items as $line_item) {
      if (in_array($line_item['type'], $adjust_types)) {
        $subtotal += $line_item['amount'];
      }
    }
  }
  $discount = FALSE;
  $percent = FALSE;
  $adjustment = array();
  $adjustment = preg_replace('/[^-0-9' . variable_get('uc_currency_dec', '.') . ']+%/', '', $adjustment_data['adjustment']);
  $ret['value'] = $adjustment;
  $ret['description'] = t('@name fee', array(
    '@name' => $adjustment_data['name'],
  ));
  if (strstr($adjustment, '-')) {
    $discount = TRUE;
  }
  if (strstr($adjustment, '%')) {
    $percent = TRUE;
    $adjustment = str_replace('%', '', $adjustment);
    $adjustment /= 100;
  }
  if ($percent) {
    $ret['value'] = $adjustment * $subtotal;
  }
  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;
}