function uc_payment_process in Ubercart 6.2
Same name and namespace in other branches
- 5 payment/uc_payment/uc_payment.module \uc_payment_process()
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.
3 calls to uc_payment_process()
- uc_credit_order in payment/
uc_credit/ uc_credit.module - Implements hook_order().
- uc_credit_terminal_form_submit in payment/
uc_credit/ uc_credit.admin.inc - Submit handler for credit terminal form.
- uc_payment_gateway_select_form_submit in payment/
uc_payment/ uc_payment.admin.inc - Submit handler for uc_payment_gateway_select_form().
File
- payment/
uc_payment/ uc_payment.module, line 511
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.
$gateways_keys = array_keys($gateways);
$key = array_shift($gateways_keys);
// 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.", array(
'%type' => _payment_method_data($method, 'name'),
)));
}
$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 (!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'];
}