View source
<?php
require_once dirname(__FILE__) . '/uc_payment_checkout_pane.inc';
require_once dirname(__FILE__) . '/uc_payment_order_pane.inc';
function uc_payment_menu() {
$items['admin/store/settings/payment'] = array(
'title' => 'Payment methods',
'description' => 'Choose and configure available payment methods.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_payment_methods_form',
),
'access arguments' => array(
'administer store',
),
'file' => 'uc_payment.admin.inc',
);
$items['admin/store/settings/payment/method/%'] = array(
'title callback' => 'uc_payment_method_title',
'title arguments' => array(
5,
),
'description' => 'Configures a specific payment method.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_payment_method_settings_form',
5,
),
'access arguments' => array(
'administer store',
),
'file' => 'uc_payment.admin.inc',
);
$items += rules_ui()
->config_menu('admin/store/settings/payment');
$items['admin/store/orders/%uc_order/payments'] = array(
'title' => 'Payments',
'description' => 'Show payments made against an order.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_payment_by_order_form',
3,
),
'access arguments' => array(
'view payments',
),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
'file' => 'uc_payment.admin.inc',
);
$items['admin/store/orders/%uc_order/payments/%uc_payment/delete'] = array(
'title' => 'Delete payment?',
'description' => 'Delete payment?',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_payment_delete_confirm_form',
3,
5,
),
'access arguments' => array(
'delete payments',
),
'type' => MENU_CALLBACK,
'file' => 'uc_payment.admin.inc',
);
return $items;
}
function uc_payment_method_title($method_id) {
return t('!method settings', array(
'!method' => _uc_payment_method_data($method_id, 'name'),
));
}
function uc_payment_permission() {
return array(
'view payments' => array(
'title' => t('View payments'),
),
'manual payments' => array(
'title' => t('Manual payments'),
'description' => t('Enter payments manually.'),
),
'delete payments' => array(
'title' => t('Delete payments'),
),
);
}
function uc_payment_theme() {
return array(
'uc_payment_totals' => array(
'variables' => array(
'order' => NULL,
),
'file' => 'uc_payment.theme.inc',
),
'uc_payment_method_table' => array(
'render element' => 'form',
'file' => 'uc_payment.admin.inc',
),
);
}
function uc_payment_form_uc_cart_view_form_alter(&$form, &$form_state) {
$methods = _uc_payment_method_list();
foreach ($methods as $id => $method) {
if ($method['checkout'] && isset($method['express']) && ($express = $method['express'](array(), $form_state))) {
$form['actions']['checkout'][$id] = $express;
}
}
}
function uc_payment_form_uc_cart_checkout_review_form_alter(&$form, &$form_state) {
$order = $form_state['uc_order'];
if ($redirect = _uc_payment_method_data($order->payment_method, 'redirect')) {
unset($form['actions']['submit']);
$suffix = drupal_get_form($redirect, $order);
$form['#suffix'] = drupal_render($suffix);
}
}
function uc_payment_uc_order($op, $order) {
if (!isset($order->payment_method)) {
$order->payment_method = '';
}
switch ($op) {
case 'submit':
$func = _uc_payment_method_data($order->payment_method, 'callback');
if (function_exists($func)) {
return $func('order-submit', $order);
}
break;
case 'load':
$func = _uc_payment_method_data($order->payment_method, 'callback');
if (function_exists($func)) {
$func('order-load', $order);
}
break;
case 'save':
$func = _uc_payment_method_data($order->payment_method, 'callback');
if (function_exists($func)) {
$func('order-save', $order);
}
break;
case 'can_delete':
if (uc_payment_load_payments($order->order_id) !== FALSE) {
return FALSE;
}
break;
case 'delete':
db_delete('uc_payment_receipts')
->condition('order_id', $order->order_id)
->execute();
$methods = _uc_payment_method_list();
foreach ($methods as $method) {
$func = $method['callback'];
if (function_exists($func)) {
$func('order-delete', $order);
}
}
break;
}
}
function uc_payment_uc_checkout_pane() {
$panes['payment'] = array(
'title' => t('Payment method'),
'desc' => t('Select a payment method from the enabled payment modules.'),
'callback' => 'uc_checkout_pane_payment',
'weight' => 6,
);
return $panes;
}
function uc_payment_uc_order_pane() {
$panes['payment'] = array(
'callback' => 'uc_order_pane_payment',
'title' => t('Payment'),
'desc' => t('Specify and collect payment for an order.'),
'class' => 'pos-left',
'weight' => 4,
'show' => array(
'view',
'edit',
'customer',
),
);
return $panes;
}
function uc_payment_uc_order_state() {
$states['payment_received'] = array(
'title' => t('Payment received'),
'weight' => 10,
'scope' => 'general',
);
return $states;
}
function uc_payment_uc_payment_method() {
$methods['free_order'] = array(
'name' => t('Free order'),
'title' => t('No payment required'),
'desc' => t('Allow customers with !zero order totals to checkout without paying.', array(
'!zero' => uc_currency_format(0),
)),
'callback' => 'uc_payment_method_free_order',
'checkout' => TRUE,
'no_gateway' => TRUE,
'weight' => 0,
);
return $methods;
}
function uc_payment_uc_payment_method_checkout_alter(&$methods, $order) {
if (isset($methods['free_order'])) {
if ($order->order_total < 0.01) {
foreach (array_keys($methods) as $key) {
if ($key != 'free_order') {
unset($methods[$key]);
}
}
}
else {
unset($methods['free_order']);
}
}
}
function uc_payment_method_free_order($op, &$order) {
switch ($op) {
case 'cart-details':
return array(
'#markup' => t('Continue with checkout to complete your order.'),
);
case 'order-submit':
if ($order->order_total >= 0.01) {
return array(
array(
'pass' => FALSE,
'message' => t('We cannot process your order without payment.'),
),
);
}
uc_payment_enter($order->order_id, 'free_order', 0, 0, NULL, t('Checkout completed for a free order.'));
}
}
function uc_payment_get_totals($form, $form_state) {
$commands[] = ajax_command_replace('#line-items-div', trim(drupal_render($form['panes']['payment']['line_items'])));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function uc_payments_table() {
$table = array(
'#type' => 'tapir_table',
'#tree' => TRUE,
'#columns' => array(
'received' => array(
'cell' => t('Received'),
'weight' => 0,
),
'user' => array(
'cell' => t('User'),
'weight' => 1,
),
'method' => array(
'cell' => t('Method'),
'weight' => 2,
),
'amount' => array(
'cell' => t('Amount'),
'weight' => 3,
),
'balance' => array(
'cell' => t('Balance'),
'weight' => 4,
),
'comment' => array(
'cell' => t('Comment'),
'weight' => 5,
),
'action' => array(
'cell' => t('Action'),
'weight' => 6,
),
),
'#rows' => array(),
);
return $table;
}
function uc_payment_process_payment($method, $order_id, $amount, $data = NULL, $default = FALSE, $selected = NULL, $redirect = TRUE) {
$result = array();
$gateways = _uc_payment_gateway_list($method, TRUE);
if (empty($gateways)) {
if (!$default) {
drupal_set_message(t('You are not able to process %type payments.', array(
'%type' => _uc_payment_method_data($method, 'name'),
)));
}
return FALSE;
}
if ($default) {
$default = variable_get('uc_payment_' . $method . '_gateway', '');
}
if (count($gateways) == 1) {
$gateway = reset($gateways);
}
elseif ($default && isset($gateways[$default])) {
$gateway = $gateways[$default];
}
elseif ($selected && isset($gateways[$selected])) {
$gateway = $gateways[$selected];
}
else {
$gateway = array(
$method => '',
);
}
if (function_exists($gateway[$method])) {
uc_order_load($order_id, TRUE);
$result = $gateway[$method]($order_id, $amount, $data);
}
else {
$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 ($result['success'] === TRUE) {
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 {
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 (!empty($result['message']) && !$default) {
drupal_set_message($result['message']);
}
if ($redirect) {
drupal_goto('admin/store/orders/' . $order_id);
}
return $result['success'];
}
function uc_payment_enter($order_id, $method, $amount, $uid = 0, $data = NULL, $comment = '', $received = REQUEST_TIME) {
$method_name = _uc_payment_method_data($method, 'review');
if (empty($method_name)) {
$method_name = _uc_payment_method_data($method, 'name');
}
if (is_null($method_name)) {
$method_name = t('Other');
}
if (is_array($data)) {
$data = serialize($data);
}
$log_message = t('@method payment for @amount entered.', array(
'@method' => $method_name,
'@amount' => uc_currency_format($amount),
));
uc_order_log_changes($order_id, array(
$log_message,
));
$receipt_id = db_insert('uc_payment_receipts')
->fields(array(
'order_id' => $order_id,
'method' => $method_name,
'amount' => $amount,
'uid' => $uid,
'data' => $data,
'comment' => $comment,
'received' => $received,
))
->execute();
$order = uc_order_load($order_id, TRUE);
$account = user_load($uid);
if (module_exists('uc_cart')) {
uc_cart_complete_sale($order);
}
module_invoke_all('uc_payment_entered', $order, $method, $amount, $account, $data, $comment);
rules_invoke_event('uc_payment_entered', $order, $account);
return $receipt_id;
}
function uc_payment_delete($receipt_id) {
if (!is_numeric($receipt_id)) {
return FALSE;
}
$payment = uc_payment_load($receipt_id);
$log_message = t('@method payment for @amount deleted.', array(
'@method' => $payment->method,
'@amount' => uc_currency_format($payment->amount),
));
uc_order_log_changes($payment->order_id, array(
$log_message,
));
db_delete('uc_payment_receipts')
->condition('receipt_id', $receipt_id)
->execute();
}
function uc_payment_balance($order) {
$total = $order->order_total;
$payments = uc_payment_load_payments($order->order_id);
if ($payments === FALSE) {
return $total;
}
foreach ($payments as $payment) {
$total -= $payment->amount;
}
return $total;
}
function uc_payment_load($receipt_id) {
if (!is_numeric($receipt_id)) {
return FALSE;
}
$payment = db_query("SELECT * FROM {uc_payment_receipts} WHERE receipt_id = :id", array(
':id' => $receipt_id,
))
->fetchObject();
return $payment;
}
function uc_payment_load_payments($order_id) {
$payments = db_query("SELECT * FROM {uc_payment_receipts} WHERE order_id = :id ORDER BY received ASC", array(
':id' => $order_id,
))
->fetchAll();
if (count($payments) == 0) {
$payments = FALSE;
}
return $payments;
}
function _uc_payment_method_list($action = NULL) {
static $methods = array();
if (count($methods) > 0 && $action !== 'rebuild') {
return $methods;
}
foreach (module_invoke_all('uc_payment_method') as $id => $method) {
if (is_numeric($id)) {
$id = $method['id'];
}
$methods[$id] = array_merge($method, array(
'id' => $id,
'checkout' => variable_get('uc_payment_method_' . $id . '_checkout', $method['checkout']),
'weight' => variable_get('uc_payment_method_' . $id . '_weight', $method['weight']),
));
}
drupal_alter('uc_payment_method', $methods);
uasort($methods, 'uc_weight_sort');
return $methods;
}
function _uc_payment_method_data($method_id, $key) {
$methods = _uc_payment_method_list();
return isset($methods[$method_id][$key]) ? $methods[$method_id][$key] : NULL;
}
function _uc_payment_gateway_list($filter = NULL, $enabled_only = FALSE) {
$gateways = array();
foreach (module_invoke_all('uc_payment_gateway') as $id => $gateway) {
if (is_numeric($id)) {
$id = $gateway['id'];
}
$gateways[$id] = array_merge($gateway, array(
'id' => $id,
'enabled' => variable_get('uc_pg_' . $id . '_enabled', TRUE),
));
}
drupal_alter('uc_payment_gateway', $gateways);
foreach ($gateways as $id => $gateway) {
if ($filter && (!isset($gateway[$filter]) || !function_exists($gateway[$filter]))) {
unset($gateways[$id]);
continue;
}
if ($enabled_only && !$gateway['enabled']) {
unset($gateways[$id]);
}
}
return $gateways;
}
function _uc_payment_gateway_data($gateway_id, $key) {
$gateways = _uc_payment_gateway_list();
return isset($gateways[$gateway_id][$key]) ? $gateways[$gateway_id][$key] : NULL;
}
function uc_payment_method_options_list() {
$options = array();
foreach (_uc_payment_method_list() as $id => $method) {
$options[$id] = $method['name'];
}
return $options;
}
function uc_payment_views_api() {
return array(
'api' => '2.0',
'path' => drupal_get_path('module', 'uc_payment') . '/views',
);
}