You are here

function uc_credit_order in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_credit/uc_credit.module \uc_credit_order()

Implementation of hook_order().

File

payment/uc_credit/uc_credit.module, line 291
Defines the credit card payment method and hooks in payment gateways.

Code

function uc_credit_order($op, &$arg1, $arg2) {

  // Set up the encryption key and object for saving and loading.
  if ($arg1->payment_method == 'credit' && ($op == 'save' || $op == 'load')) {

    // Log an error if encryption isn't configured properly.
    if (!uc_credit_encryption_key()) {
      watchdog('uc_credit', t('Credit card encryption must be setup to process credit cards.'));
    }
  }
  switch ($op) {
    case 'submit':
      if ($arg1->payment_method == 'credit') {

        // Clear out that session variable denoting this as a CC paid order.
        unset($_SESSION['cc_pay']);

        // Process CC transactions when an order is submitted after review.
        if (variable_get('uc_credit_checkout_process', TRUE)) {

          // Stuff the transaction type into the data array.
          $gateway_id = uc_credit_default_gateway();
          $data = array(
            'txn_type' => variable_get('uc_pg_' . $gateway_id . '_cc_txn_type', UC_CREDIT_AUTH_CAPTURE),
          );

          // Attempt to process the CC payment.
          $pass = uc_payment_process('credit', $arg1->order_id, $arg1->order_total, $data, TRUE, NULL, FALSE);

          // If the payment failed, store the data back in the session and
          // halt the checkout process.
          if (!$pass) {
            $message = variable_get('uc_credit_fail_message', t('We were unable to process your credit card payment. Please verify your card details and try again.  If the problem persists, contact us to complete your order.'));
            return array(
              array(
                'pass' => FALSE,
                'message' => $message,
              ),
            );
          }
        }
        elseif (variable_get('uc_credit_debug', FALSE)) {

          // If we aren't set to process transactions immediately and we're in
          // debug mode, store the CC data in the order so it can be viewed
          // later for test processing.
          $cc_data = $arg1->payment_details;
          _save_cc_data_to_order($cc_data, $arg1->order_id);
        }
      }
      break;
    case 'save':
      if ($arg1->payment_method == 'credit') {

        // Build an array of CC data to store with the order.
        if (!empty($arg1->payment_details)) {

          // Check for debug mode.
          if (variable_get('uc_credit_debug', FALSE) && arg(1) != 'checkout') {

            // If enabled, store the full payment details.
            $cc_data = $arg1->payment_details;
          }
          else {

            // Otherwise, save only some limited, PCI compliant data.
            $cc_data = array(
              'cc_number' => substr($arg1->payment_details['cc_number'], -4),
              'cc_type' => $arg1->payment_details['cc_type'],
            );
          }
          _save_cc_data_to_order($cc_data, $arg1->order_id);
        }
      }
      break;
    case 'load':
      if ($arg1->payment_method == 'credit') {

        // Load the CC details from the credit cache if available.
        $arg1->payment_details = uc_credit_cache('load');

        // Otherwise load any details that might be stored in the data array.
        if (empty($arg1->payment_details) && isset($arg1->data['cc_data'])) {
          $arg1->payment_details = uc_credit_cache('save', $arg1->data['cc_data']);
        }
      }
      break;
    case 'delete':
      db_query("DELETE FROM {uc_payment_credit} WHERE order_id = %d", $arg1->order_id);
      break;
  }
}