You are here

function uc_payment_process_payment in Ubercart 7.3

Processes 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.

2 calls to uc_payment_process_payment()
uc_credit_terminal_form_submit in payment/uc_credit/uc_credit.admin.inc
Submit handler for credit terminal form.
uc_credit_uc_order in payment/uc_credit/uc_credit.module
Implements hook_uc_order().

File

payment/uc_payment/uc_payment.module, line 379

Code

function uc_payment_process_payment($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 = _uc_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' => _uc_payment_method_data($method, 'name'),
      )));
    }
    return FALSE;
  }

  // Find the default gateway if requested.
  if ($default) {
    $default = variable_get('uc_payment_' . $method . '_gateway', '');
  }

  // If we only found one gateway for this payment method...
  if (count($gateways) == 1) {
    $gateway = reset($gateways);
  }
  elseif ($default && isset($gateways[$default])) {

    // The default gateway was forced.
    $gateway = $gateways[$default];
  }
  elseif ($selected && isset($gateways[$selected])) {

    // A specific gateway was selected.
    $gateway = $gateways[$selected];
  }
  else {

    // No gateway available.
    $gateway = array(
      $method => '',
    );
  }

  // Check to see if the function exists and process the payment.
  if (function_exists($gateway[$method])) {

    // Reset the entity cache, so the latest data saved in the credit card cache
    // is guaranteed to be available in the charge function.
    uc_order_load($order_id, TRUE);
    $result = $gateway[$method]($order_id, $amount, $data);
  }
  else {

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

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

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

    // Otherwise display the failure message in the logs.
    watchdog('uc_payment', 'Payment failed for order @order_id: @message', array(
      '@order_id' => $order_id,
      '@message' => $result['message'],
    ), WATCHDOG_WARNING, l(t('view order'), 'admin/store/orders/' . $order_id));
  }

  // 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'];
}