You are here

function uc_payment_process in Ubercart 5

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

Process a payment through an enabled payment gateway.

Parameters

$method: The ID of the payment method to use to process the payment.

$order_id: The ID of the order associated with this payment.

$amount: The amount of the payment we're attempting to collect.

$data: An array of data passed on to the payment gateway module used to process the payment for the specified payment method.

$default: TRUE or FALSE to indicate we're forcing the use of the default gateway for the specified payment method. When TRUE, admin messages related to the payment will be hidden from display so customers don't see them.

$selected: The ID of a payment gateway to use to process the payment; normally comes from the payment gateway select form.

$redirect: TRUE or FALSE to indicate whether or not to redirect back to the admin order view page for the order referenced in $order_id.

Return value

TRUE or FALSE indicating whether or not the payment was processed.

3 calls to uc_payment_process()
uc_credit_order in payment/uc_credit/uc_credit.module
Implementation of hook_order().
uc_credit_terminal_form_submit in payment/uc_credit/uc_credit.module
uc_payment_gateway_select_form_submit in payment/uc_payment/uc_payment.module

File

payment/uc_payment/uc_payment.module, line 858

Code

function uc_payment_process($method, $order_id, $amount, $data = NULL, $default = FALSE, $selected = NULL, $redirect = TRUE) {
  $result = array();

  // Get an array of enabled payment gateways available for the payment method.
  $gateways = _payment_gateway_list($method, TRUE);

  // Fail if no gateways were found for the specified method.
  if (empty($gateways)) {

    // Display an error message if messages weren't silenced.
    if (!$default) {
      drupal_set_message(t('You are not able to process %type payments.', array(
        '%type' => _payment_method_data($method, 'name'),
      )));
    }
    return FALSE;
  }

  // If we only found one gateway for this payment method...
  if (count($gateways) == 1) {

    // Get the right key for the payment gateway in the array.
    $key = array_shift(array_keys($gateways));

    // If we can find a callback in the gateway for the payment method...
    if (function_exists($gateways[$key][$method])) {

      // Pass the payment data onto the callback and store the result.
      $result = $gateways[$key][$method]($order_id, $amount, $data);
    }
    else {

      // Otherwise display a failure message to administrators.
      if (user_access('administer store')) {
        drupal_set_message(t("Attempted to process a %type payment but the gateway's function was not found."));
      }
      $result['success'] = FALSE;
    }
  }
  else {

    // Otherwise attempt to find the appropriate gateway function in the array.
    $callback = FALSE;
    foreach ($gateways as $gateway) {

      // If we want the default gateway and this is it, store the callback
      // and continue.
      if ($default && $gateway['id'] == variable_get('uc_payment_' . $method . '_gateway', '')) {
        $callback = $gateway[$method];
        continue;
      }

      // If we want to use a specific gateway and this is it, store the callback.
      if (!empty($selected) && $gateway['id'] == $selected) {
        $callback = $gateway[$method];
      }
    }

    // If we found a callback...
    if ($callback !== FALSE) {

      // Check to see if the function exists and process the payment.
      if (function_exists($callback)) {
        $result = $callback($order_id, $amount, $data);
      }
      else {

        // Otherwise display an error message to administrators.
        if (user_access('administer store')) {
          drupal_set_message(t('An error has occurred with your payment gateway.  The charge function could not be found.'));
        }
        $result['success'] = FALSE;
      }
    }
    else {

      // Otherwise store the info that was passed to us in the session and
      // redirect to a form where we can choose a payment gateway.
      $_SESSION['uc_payment_method'] = $method;
      $_SESSION['uc_payment_order_id'] = $order_id;
      $_SESSION['uc_payment_amount'] = $amount;
      $_SESSION['uc_payment_data'] = serialize($data);
      drupal_goto('admin/store/orders/' . $order_id . '/payments/select/' . $method);
    }
  }

  // If the payment processed successfully...
  if ($result['success'] === TRUE) {

    // Log the payment to the order if not disabled.
    if ($result['log_payment'] !== FALSE) {
      uc_payment_enter($order_id, $method, $amount, empty($result['uid']) ? 0 : $result['uid'], $result['data'], $result['comment']);
    }
  }
  else {

    // Otherwise display the failue message in the logs.
    watchdog('uc_payment', t('Payment failed: @message', array(
      '@message' => $result['message'],
    )), WATCHDOG_WARNING);
  }

  // If we have a message for display and aren't simply charging with the
  // default gateway for a customer...
  if (!empty($result['message']) && !$default) {
    drupal_set_message($result['message']);
  }

  // Head back to the order if a redirect was specified.
  if ($redirect) {
    drupal_goto('admin/store/orders/' . $order_id);
  }
  return $result['success'];
}