You are here

function uc_payment_method_cod in Ubercart 6.2

Same name and namespace in other branches
  1. 5 payment/uc_payment_pack/uc_payment_pack.module \uc_payment_method_cod()
  2. 7.3 payment/uc_payment_pack/uc_payment_pack.module \uc_payment_method_cod()

Payment method callback for the "Cash on Delivery" payment method.

1 string reference to 'uc_payment_method_cod'
uc_payment_pack_payment_method in payment/uc_payment_pack/uc_payment_pack.module
Implements hook_payment_method().

File

payment/uc_payment_pack/uc_payment_pack.module, line 156
Provides the Check/Money Order, COD, and "Other" payment methods.

Code

function uc_payment_method_cod($op, &$order) {
  switch ($op) {
    case 'cart-details':
      $details = variable_get('uc_cod_policy', t('Full payment is expected upon delivery or prior to pick-up.'));
      if (($max = variable_get('uc_cod_max_order', 0)) > 0 && is_numeric($max)) {
        $context = array(
          'revision' => 'formatted-original',
          'type' => 'amount',
        );
        $details .= '<p>' . t('Orders totalling more than !number are <b>not eligible</b> for COD.', array(
          '!number' => uc_price($max, $context),
        )) . '</p>';
      }
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        $details .= uc_strip_form(drupal_get_form('uc_payment_method_cod_form', $order));
      }
      return $details;
    case 'cart-process':
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        $order->payment_details = array(
          'delivery_month' => intval($_POST['cod_delivery_month']),
          'delivery_day' => intval($_POST['cod_delivery_day']),
          'delivery_year' => intval($_POST['cod_delivery_year']),
        );
      }
      return TRUE;
    case 'cart-review':
      $review = array();
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        $date = uc_date_format($order->payment_details['delivery_month'], $order->payment_details['delivery_day'], $order->payment_details['delivery_year']);
        $review[] = array(
          'title' => t('Delivery Date'),
          'data' => $date,
        );
      }
      return $review;
    case 'order-view':
    case 'customer-view':
      $output = '';
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        $output = t('Desired delivery date:') . '<br />' . uc_date_format($order->payment_details['delivery_month'], $order->payment_details['delivery_day'], $order->payment_details['delivery_year']);
      }
      return $output;
    case 'order-details':
      $details = '';
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        $details .= uc_strip_form(drupal_get_form('uc_payment_method_cod_form', $order));
      }
      return $details;
    case 'edit-process':
      if (variable_get('uc_cod_delivery_date', FALSE)) {
        return array(
          'payment_details' => array(
            'delivery_month' => intval($_POST['cod_delivery_month']),
            'delivery_day' => intval($_POST['cod_delivery_day']),
            'delivery_year' => intval($_POST['cod_delivery_year']),
          ),
        );
      }
      return;
    case 'order-load':
      $result = db_query("SELECT * FROM {uc_payment_cod} WHERE order_id = %d", $order->order_id);
      if ($row = db_fetch_object($result)) {
        $order->payment_details = array(
          'delivery_month' => $row->delivery_month,
          'delivery_day' => $row->delivery_day,
          'delivery_year' => $row->delivery_year,
        );
      }
      break;
    case 'order-submit':
      if ($order->payment_method == 'cod' && ($max = variable_get('uc_cod_max_order', 0)) > 0 && is_numeric($max) && $order->order_total > $max) {
        $result[] = array(
          'pass' => FALSE,
          'message' => t('Your final order total exceeds the maximum for COD payment.  Please go back and select a different method of payment.'),
        );
        $_SESSION['expanded_panes'][] = 'payment';
        return $result;
      }

    // TODO: This falls through to the order-save case - is this deliberate?
    // If so, it should be documented.
    case 'order-save':
      if (isset($order->payment_details['delivery_month']) && isset($order->payment_details['delivery_day']) && isset($order->payment_details['delivery_year'])) {
        db_query("DELETE FROM {uc_payment_cod} WHERE order_id = %d", $order->order_id);
        db_query("INSERT INTO {uc_payment_cod} VALUES (%d, %d, %d, %d)", $order->order_id, $order->payment_details['delivery_month'], $order->payment_details['delivery_day'], $order->payment_details['delivery_year']);
      }
      break;
    case 'order-delete':
      db_query("DELETE FROM {uc_payment_cod} WHERE order_id = %d", $order->order_id);
      break;
    case 'settings':
      $form['uc_cod_policy'] = array(
        '#type' => 'textarea',
        '#title' => t('Policy message'),
        '#default_value' => variable_get('uc_cod_policy', t('Full payment is expected upon delivery or prior to pick-up.')),
        '#description' => t('Help message shown at checkout.'),
      );
      $form['uc_cod_max_order'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum order total eligible for COD'),
        '#default_value' => variable_get('uc_cod_max_order', 0),
        '#description' => t('Set to 0 for no maximum order limit.'),
      );
      $form['uc_cod_delivery_date'] = array(
        '#type' => 'checkbox',
        '#title' => t('Let customers enter a desired delivery date.'),
        '#default_value' => variable_get('uc_cod_delivery_date', FALSE),
      );
      return $form;
  }
}