home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (uc_payment_checkout_pane.inc)

This example Drupal source code file (uc_payment_checkout_pane.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

arg1, array, foreach, form, if, options, php, result, return, review, the, title, true, type

The uc_payment_checkout_pane.inc Drupal example source code

<?php
// $Id: uc_payment_checkout_pane.inc,v 1.5.2.8 2009/09/21 14:34:46 islandusurper Exp $

/**
 * @file
 * Checkout pane functions for uc_payment.module.
 *
 * The checkout pane holds form to select the payment method. It also shows a
 * preview of the line items and order total.
 */

function uc_checkout_pane_payment($op, &$arg1, $arg2) {
  switch ($op) {
    case 'view':
      // Add the default payment message as a JS variable.
      // Add URL paths as JS variables (be nice to i18n, allow URL rewrite).
      drupal_add_js(array(
        'defPaymentMsg' => addslashes(variable_get('uc_default_payment_msg', t('Continue with checkout to complete payment.'))),
        'ucURL' => array(
          'adminOrders' => url('admin/store/orders/'),
          'checkoutPaymentDetails' => url('cart/checkout/payment_details/'),
          'checkoutLineItems' => url('cart/checkout/line_items'),
          'creditCardCVVInfo' => url('cart/checkout/credit/cvv_info'),
        ),
      ), 'setting');

      if (variable_get('uc_payment_show_order_total_preview', TRUE)) {
        drupal_add_js('misc/progress.js');

        $contents['current_total'] = array(
          '#type' => 'hidden',
          '#value' => $arg1->order_total > 0 ? $arg1->order_total : NULL,
        );
        $contents['shown_total'] = array(
          '#value' => '<div style="padding: .5em 1em; margin-bottom: 1em; border: dashed 1px #bbb;" id="line-items-div"><em>'. t('Javascript must be enabled to view the order total preview.') .'</em></div>',
          '#weight' => -20,
        );

        // let the script know we need to show a progressbar
        drupal_add_js(array('ucShowProgressBar' => TRUE), 'setting');
      }

      $methods = _payment_method_list();

      foreach ($methods as $method) {
        if ($method['checkout']) {
          $options[$method['id']] = $method['title'];
        }
      }

      if (is_array($options)) {
        if (isset($_POST['panes']) && in_array($_POST['panes']['payment']['payment_method'], array_keys($options))) {
          $default = $_POST['panes']['payment']['payment_method'];
        }
        else {
          $default = (count($options) == 1 || !isset($arg1->payment_method)) ? key($options) : $arg1->payment_method;
        }
      }

      if (count($options) > 1) {
        $description = t('Select a payment method from the following options.');
      }
      else {
        $description = '';
      }

      $contents['payment_method'] = array(
        '#type' => 'radios',
        '#title' => t('Payment method'),
        '#options' => $options,
        '#default_value' => $default,
        '#disabled' => count($options) == 1 ? TRUE : FALSE,
        '#required' => TRUE,
        '#attributes' => array('onclick' => "get_payment_details(Drupal.settings.ucURL.checkoutPaymentDetails + this.value);"),
        '#theme' => 'uc_payment_method_select',
      );
      $contents['details'] = array(
        '#value' => '<div id="payment_details" class="solid-border display-none"></div>',
      );

      return array('description' => $description, 'contents' => $contents);

    case 'process':
      $arg1->payment_method = $arg2['payment_method'];
      $func = _payment_method_data($arg1->payment_method, 'callback');
      if (function_exists($func)) {
        $result = $func('cart-process', $arg1);
        if ($result === FALSE) {
          return FALSE;
        }
      }
      return TRUE;

    case 'review':
      $line_items = $arg1->line_items;
      $items = _line_item_list();
      foreach ($items as $item) {
        if (isset($item['display_only']) && $item['display_only'] == TRUE) {
          $result = $item['callback']('display', $arg1);
          if (is_array($result)) {
            foreach ($result as $line) {
              $line_items[] = array(
                'title' => $line['title'],
                'amount' => $line['amount'],
                'weight' => $item['weight']
              );
            }
          }
        }
      }
      usort($line_items, 'uc_weight_sort');

      $context = array(
        'revision' => 'themed',
        'type' => 'line_item',
        'subject' => array(
          'order' => $arg1,
        ),
      );
      foreach ($line_items as $line_item) {
        $context['subject']['line_item'] = $line_item;
        $review[] = array('title' => $line_item['title'], 'data' => uc_price($line_item['amount'], $context));
      }
      $review_data = _payment_method_data($arg1->payment_method, 'review');
      if (empty($review_data)) {
        $review_data = _payment_method_data($arg1->payment_method, 'name');
      }
      $review[] = array('border' => 'top', 'title' => t('Paying by'), 'data' => $review_data);
      $func = _payment_method_data($arg1->payment_method, 'callback');
      if (function_exists($func)) {
        $result = $func('cart-review', $arg1);
        if (is_array($result)) {
          $review = array_merge($review, $result);
        }
      }
      return $review;

    case 'settings':
      $form['uc_payment_show_order_total_preview'] = array(
        '#type' => 'checkbox',
        '#title' => t('Show the order total preview on the payment pane.'),
        '#default_value' => variable_get('uc_payment_show_order_total_preview', TRUE),
      );
      return $form;
  }
}

/**
 * We need a theme function for the radios element in case another module alters
 * the default or available payment methods... we need the JS to grab the right
 * default payment details.
 */
function theme_uc_payment_method_select($form) {
  if (empty($form['#options'])) {
    drupal_set_message(t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.'), 'error');
    return;
  }

  // Perhaps instead this should be a normal JS function and we just print the
  // default payment method to a JS variable or use a selector.  -RS
  drupal_add_js(array('ucDefaultPayment' => $form['#default_value']), 'setting');

  return drupal_render($form);
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal uc_payment_checkout_pane.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.