You are here

function uc_payment_method_credit in Ubercart 7.3

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

Callback function for the Credit Card payment method.

1 string reference to 'uc_payment_method_credit'
uc_credit_uc_payment_method in payment/uc_credit/uc_credit.module
Implements hook_uc_payment_method().

File

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

Code

function uc_payment_method_credit($op, &$order, $form = NULL, &$form_state = NULL) {
  switch ($op) {
    case 'cart-details':
      $details = uc_payment_method_credit_form(array(), $form_state, $order);
      return $details;
    case 'cart-process':
      if (!isset($form_state['values']['panes']['payment']['details']['cc_number'])) {
        return;
      }

      // Fetch the CC details from the $_POST directly.
      $cc_data = $form_state['values']['panes']['payment']['details'];
      $cc_data['cc_number'] = str_replace(' ', '', $cc_data['cc_number']);
      array_walk($cc_data, 'check_plain');

      // Recover cached CC data in
      // $form_state['values']['panes']['payment']['details'] if it exists.
      if (isset($form_state['values']['panes']['payment']['details']['payment_details_data'])) {
        $cache = uc_credit_cache('save', $form_state['values']['panes']['payment']['details']['payment_details_data']);
      }

      // Account for partial CC numbers when masked by the system.
      if (substr($cc_data['cc_number'], 0, strlen(t('(Last4)'))) == t('(Last4)')) {

        // Recover the number from the encrypted data in the form if truncated.
        if (isset($cache['cc_number'])) {
          $cc_data['cc_number'] = $cache['cc_number'];
        }
        else {
          $cc_data['cc_number'] = '';
        }
      }

      // Account for masked CVV numbers.
      if (!empty($cc_data['cc_cvv']) && $cc_data['cc_cvv'] == str_repeat('-', strlen($cc_data['cc_cvv']))) {

        // Recover the number from the encrypted data in $_POST if truncated.
        if (isset($cache['cc_cvv'])) {
          $cc_data['cc_cvv'] = $cache['cc_cvv'];
        }
        else {
          $cc_data['cc_cvv'] = '';
        }
      }

      // Go ahead and put the CC data in the payment details array.
      $order->payment_details = $cc_data;

      // Default our value for validation.
      $return = TRUE;

      // Make sure an owner value was entered.
      if (variable_get('uc_credit_owner_enabled', FALSE) && empty($cc_data['cc_owner'])) {
        form_set_error('panes][payment][details][cc_owner', t('Enter the owner name as it appears on the card.'));
        $return = FALSE;
      }

      // Validate the CC number if that's turned on/check for non-digits.
      if (variable_get('uc_credit_validate_numbers', TRUE) && !_uc_credit_valid_card_number($cc_data['cc_number']) || !ctype_digit($cc_data['cc_number'])) {
        form_set_error('panes][payment][details][cc_number', t('You have entered an invalid credit card number.'));
        $return = FALSE;
      }

      // Validate the start date (if entered).
      if (variable_get('uc_credit_start_enabled', FALSE) && !_uc_credit_valid_card_start($cc_data['cc_start_month'], $cc_data['cc_start_year'])) {
        form_set_error('panes][payment][details][cc_start_month', t('The start date you entered is invalid.'));
        form_set_error('panes][payment][details][cc_start_year');
        $return = FALSE;
      }

      // Validate the card expiration date.
      if (!_uc_credit_valid_card_expiration($cc_data['cc_exp_month'], $cc_data['cc_exp_year'])) {
        form_set_error('panes][payment][details][cc_exp_month', t('The credit card you entered has expired.'));
        form_set_error('panes][payment][details][cc_exp_year');
        $return = FALSE;
      }

      // Validate the issue number (if entered).  With issue numbers, '01' is
      // different from '1', but is_numeric() is still appropriate.
      if (variable_get('uc_credit_issue_enabled', FALSE) && !_uc_credit_valid_card_issue($cc_data['cc_issue'])) {
        form_set_error('panes][payment][details][cc_issue', t('The issue number you entered is invalid.'));
        $return = FALSE;
      }

      // Validate the CVV number if enabled.
      if (variable_get('uc_credit_cvv_enabled', TRUE) && !_uc_credit_valid_cvv($cc_data['cc_cvv'])) {
        form_set_error('panes][payment][details][cc_cvv', t('You have entered an invalid CVV number.'));
        $return = FALSE;
      }

      // Validate the bank name if enabled.
      if (variable_get('uc_credit_bank_enabled', FALSE) && empty($cc_data['cc_bank'])) {
        form_set_error('panes][payment][details][cc_bank', t('You must enter the issuing bank for that card.'));
        $return = FALSE;
      }

      // Initialize the encryption key and class.
      $key = uc_credit_encryption_key();
      $crypt = new UbercartEncryption();

      // Store the encrypted details in the session for the next pageload.
      // We are using base64_encode() because the encrypt function works with a
      // limited set of characters, not supporting the full Unicode character
      // set or even extended ASCII characters that may be present.
      // base64_encode() converts everything to a subset of ASCII, ensuring that
      // the encryption algorithm does not mangle names.
      $_SESSION['sescrd'] = $crypt
        ->encrypt($key, base64_encode(serialize($order->payment_details)));

      // Log any errors to the watchdog.
      uc_store_encryption_errors($crypt, 'uc_credit');

      // If we're going to the review screen, set a variable that lets us know
      // we're paying by CC.
      if ($return) {
        $_SESSION['cc_pay'] = TRUE;
      }
      return $return;
    case 'cart-review':
      if (variable_get('uc_credit_type_enabled', FALSE)) {
        $review[] = array(
          'title' => t('Card type'),
          'data' => check_plain($order->payment_details['cc_type']),
        );
      }
      if (variable_get('uc_credit_owner_enabled', FALSE)) {
        $review[] = array(
          'title' => t('Card owner'),
          'data' => check_plain($order->payment_details['cc_owner']),
        );
      }
      $review[] = array(
        'title' => t('Card number'),
        'data' => uc_credit_display_number($order->payment_details['cc_number']),
      );
      if (variable_get('uc_credit_start_enabled', FALSE)) {
        $start = $order->payment_details['cc_start_month'] . '/' . $order->payment_details['cc_start_year'];
        $review[] = array(
          'title' => t('Start date'),
          'data' => strlen($start) > 1 ? $start : '',
        );
      }
      $review[] = array(
        'title' => t('Expiration'),
        'data' => $order->payment_details['cc_exp_month'] . '/' . $order->payment_details['cc_exp_year'],
      );
      if (variable_get('uc_credit_issue_enabled', FALSE)) {
        $review[] = array(
          'title' => t('Issue number'),
          'data' => $order->payment_details['cc_issue'],
        );
      }
      if (variable_get('uc_credit_bank_enabled', FALSE)) {
        $review[] = array(
          'title' => t('Issuing bank'),
          'data' => check_plain($order->payment_details['cc_bank']),
        );
      }
      return $review;
    case 'order-view':
      $build = array();

      // Add the hidden span for the CC details if possible.
      if (user_access('view cc details')) {
        $rows = array();
        if (!empty($order->payment_details['cc_type'])) {
          $rows[] = t('Card type') . ': ' . check_plain($order->payment_details['cc_type']);
        }
        if (!empty($order->payment_details['cc_owner'])) {
          $rows[] = t('Card owner') . ': ' . check_plain($order->payment_details['cc_owner']);
        }
        if (!empty($order->payment_details['cc_number'])) {
          $rows[] = t('Card number') . ': ' . uc_credit_display_number($order->payment_details['cc_number']);
        }
        if (!empty($order->payment_details['cc_start_month']) && !empty($order->payment_details['cc_start_year'])) {
          $rows[] = t('Start date') . ': ' . $order->payment_details['cc_start_month'] . '/' . $order->payment_details['cc_start_year'];
        }
        if (!empty($order->payment_details['cc_exp_month']) && !empty($order->payment_details['cc_exp_year'])) {
          $rows[] = t('Expiration') . ': ' . $order->payment_details['cc_exp_month'] . '/' . $order->payment_details['cc_exp_year'];
        }
        if (!empty($order->payment_details['cc_issue'])) {
          $rows[] = t('Issue number') . ': ' . check_plain($order->payment_details['cc_issue']);
        }
        if (!empty($order->payment_details['cc_bank'])) {
          $rows[] = t('Issuing bank') . ': ' . check_plain($order->payment_details['cc_bank']);
        }
        $build['cc_info'] = array(
          '#prefix' => '<a href="#" onclick="jQuery(this).hide().next().show();">' . t('Show card details') . '</a><div style="display: none;">',
          '#markup' => implode('<br />', $rows),
          '#suffix' => '</div>',
        );

        // Add the form to process the card if applicable.
        if (user_access('process credit cards')) {
          $build['terminal'] = drupal_get_form('uc_credit_order_view_form', $order->order_id);
        }
      }
      return $build;
    case 'customer-view':
      $build = array();
      if (!empty($order->payment_details['cc_number'])) {
        $build['#markup'] = t('Card number') . ':<br />' . uc_credit_display_number($order->payment_details['cc_number']);
      }
      return $build;
    case 'order-details':
      return t('Use the terminal available through the<br />%button button on the View tab to<br />process credit card payments.', array(
        '%button' => t('Process card'),
      ));
    case 'settings':
      form_load_include($form_state, 'inc', 'uc_credit', 'uc_credit.admin');
      return uc_credit_settings_form($form, $form_state);
  }
}