'admin/store/settings/payment', 'title' => t('Payment settings'), 'callback' => 'uc_payment_settings_overview', 'access' => user_access('administer store'), 'description' => t('Configure the payment settings.'), ); $items[] = array( 'path' => 'admin/store/settings/payment/overview', 'title' => t('Overview'), 'access' => user_access('administer store'), 'description' => t('View the payment settings.'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items[] = array( 'path' => 'admin/store/settings/payment/edit', 'title' => t('Edit'), 'callback' => 'drupal_get_form', 'callback arguments' => array('uc_payment_settings_form'), 'access' => user_access('administer store'), 'description' => t('Edit the payment settings.'), 'type' => MENU_LOCAL_TASK, 'weight' => -5, ); $items[] = array( 'path' => 'admin/store/settings/payment/edit/basic', 'title' => t('Payment settings'), 'access' => user_access('administer store'), 'description' => t('Edit the basic payment settings.'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items[] = array( 'path' => 'admin/store/settings/payment/edit/methods', 'title' => t('Payment methods'), 'access' => user_access('administer store'), 'callback' => 'drupal_get_form', 'callback arguments' => array('uc_payment_methods_form'), 'description' => t('Edit the payment method settings.'), 'type' => MENU_LOCAL_TASK, 'weight' => -5, ); $items[] = array( 'path' => 'admin/store/settings/payment/edit/gateways', 'title' => t('Payment gateways'), 'access' => user_access('administer store'), 'callback' => 'drupal_get_form', 'callback arguments' => array('uc_payment_gateways_form'), 'description' => t('Edit the payment gateway settings.'), 'type' => MENU_LOCAL_TASK, 'weight' => 0, ); $items[] = array( 'path' => 'admin/store/payment_methods', 'title' => t('Payment methods autocomplete'), 'callback' => '_uc_payment_methods_autocomplete', 'access' => user_access('edit orders'), 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'cart/checkout/line_items', 'title' => t('Return order totals'), 'callback' => 'uc_payment_get_totals', 'access' => user_access('access content'), 'type' => MENU_CALLBACK, ); } else { $items[] = array( 'path' => 'cart/checkout/payment_details/'. arg(3), 'title' => t('Payment details'), 'description' => t('Add the payment details to the checkout pane.'), 'callback' => 'uc_payment_get_details', 'callback arguments' => array(arg(3)), 'access' => user_access('access content'), 'type' => MENU_CALLBACK, ); if (is_numeric(arg(3))) { $items[] = array( 'path' => 'admin/store/orders/'. arg(3) .'/payment_details/'. arg(5), 'title' => t('Payment details'), 'description' => t('Add the payment details to the order pane.'), 'callback' => 'uc_payment_get_details', 'callback arguments' => array(arg(5), 'order'), 'access' => user_access('edit orders'), 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'admin/store/orders/'. arg(3) .'/receive_check', 'title' => t('Receive Check'), 'callback' => 'drupal_get_form', 'callback arguments' => array('uc_payment_receive_check_form', arg(3)), 'access' => user_access('view all orders'), 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'admin/store/orders/'. arg(3) .'/payments/select/'. arg(6), 'title' => t('Select Payment Gateway'), 'callback' => 'uc_payment_gateway_select', 'callback arguments' => array(arg(3), arg(6)), 'access' => user_access('view all orders'), 'type' => MENU_CALLBACK, ); if (variable_get('uc_payment_tracking', TRUE)) { $items[] = array( 'path' => 'admin/store/orders/'. arg(3) .'/payments', 'title' => t('payments'), 'callback' => 'uc_payment_by_order', 'callback arguments' => array(arg(3)), 'access' => user_access('view payments'), 'weight' => 5, 'type' => MENU_LOCAL_TASK ); } if (is_numeric(arg(5)) && arg(6) == 'delete' && variable_get('uc_payment_deleting', TRUE)) { $items[] = array( 'path' => 'admin/store/orders/'. arg(3) .'/payments/'. arg(5) .'/delete', 'title' => t('Delete payment?'), 'description' => t('Delete payment?'), 'callback' => 'uc_payment_delete_confirm', 'callback arguments' => array(arg(3), arg(5)), 'access' => user_access('delete payments'), 'type' => MENU_CALLBACK, ); } } } return $items; } /** * Implementation of hook_token_values(). (token.module) */ function uc_payment_token_values($type, $object = NULL) { switch ($type) { case 'uc_order': $order = $object; $values['order-payment-method'] = _payment_method_data($order->payment_method, 'name'); break; } return $values; } /** * Implementation of hook_token_list(). (token.module) */ function uc_payment_token_list($type = 'all') { if ($type == 'uc_order' || $type == 'ubercart' || $type == 'all') { $tokens['uc_order']['order-payment-method'] = t('The payment method of the order.'); } return $tokens; } /** * Implementation of hook_perm(). */ function uc_payment_perm() { return array('view payments', 'manual payments', 'delete payments'); } /******************************************************************************* * Hook Functions (TAPIr) ******************************************************************************/ /** * Implementation of hook_table_settings(). */ function uc_payment_table_settings() { $tables[] = array( 'id' => 'uc_payments_table', 'description' => t('The payments table on the order payments screen.'), 'path' => 'admin/store/settings/tables', 'access' => 'view all orders', 'preview' => FALSE, ); return $tables; } /******************************************************************************* * Hook Functions (Ubercart) ******************************************************************************/ /** * Implementation of hook_order(). */ function uc_payment_order($op, &$arg1) { switch ($op) { case 'submit': $func = _payment_method_data($arg1->payment_method, 'callback'); if (function_exists($func)) { /*if (substr(PHP_VERSION, 0, 1) == 4) { return $func('order-submit', &$arg1); } else { return uc_func_call($func, 'order-submit', $arg1); }*/ return $func('order-submit', $arg1); } break; case 'load': $func = _payment_method_data($arg1->payment_method, 'callback'); if (function_exists($func)) { /*if (substr(PHP_VERSION, 0, 1) == 4) { $func('order-load', &$arg1); } else { uc_func_call($func, 'order-load', $arg1); }*/ $func('order-load', $arg1); } break; case 'save': $func = _payment_method_data($arg1->payment_method, 'callback'); if (function_exists($func)) { /*if (substr(PHP_VERSION, 0, 1) == 4) { $func('order-save', &$arg1); } else { uc_func_call($func, 'order-save', $arg1); }*/ $func('order-save', $arg1); } break; case 'delete': db_query("DELETE FROM {uc_payment_receipts} WHERE order_id = %d", $arg1); // Call each payment method to delete method specific data from the database. $methods = _payment_method_list(); foreach ($methods as $method) { $func = $method['callback']; if (function_exists($func)) { $func('order-delete', $arg1); } } break; //#mic_start ADD--> case 'total': $csubtotal = 0; $citems = uc_cart_get_contents(); foreach ($citems as $item) { $ctotal = ($item->qty) ? $item->qty * $item->price : $item->price; $csubtotal += $ctotal; } $text_of_payment = ''; $cost_of_payment = 0; $cost_of_payment = variable_get('uc_payment_method_'. $arg1->payment_method .'_cost', ''); $mode = variable_get('uc_payment_method_'. $arg1->payment_method .'_mode', ''); db_query("DELETE FROM {uc_order_line_items} WHERE order_id = %d AND type = '%s'", $arg1->order_id, 'Cost of Payment'); if ($cost_of_payment != ""){ if ($mode == 'percent_plus') { $cost_of_payment = intval($cost_of_payment); $cost_of_payment = ($csubtotal / 100) * $cost_of_payment; } elseif ($mode == 'percent_minus') { $cost_of_payment = intval($cost_of_payment); $cost_of_payment = ($csubtotal / 100) * $cost_of_payment; $cost_of_payment *= -1; } elseif ($mode == 'static') { $cost_of_payment = $cost_of_payment; } $text_of_payment = variable_get('uc_payment_method_'. $arg1->payment_method .'_text', ''); uc_order_line_item_add($arg1->order_id, 'Cost of Payment', $text_of_payment, $cost_of_payment, 1); } return $cost_of_payment; //#mic_end <-- } } /** * Implementation of hook_checkout_pane(). */ function uc_payment_checkout_pane() { $panes[] = array( 'id' => 'payment', 'title' => t('Payment Method'), 'desc' => t('Select a payment method from the enabled payment modules.'), 'callback' => 'uc_checkout_pane_payment', 'weight' => 6, ); return $panes; } /** * Implementation of hook_order_pane(). */ function uc_payment_order_pane() { $panes[] = array( 'id' => 'payment', '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'), //, 'invoice', 'customer'), ); return $panes; } /** * Implementation of hook_payment_method(). */ function uc_payment_payment_method() { $methods[] = array( 'id' => 'check', 'name' => t('Check'), 'title' => t('Check or Money Order'), 'desc' => t('Pay by mailing a check or money order.'), 'callback' => 'uc_payment_method_check', 'weight' => 1, 'checkout' => TRUE, 'backend' => TRUE, ); $methods[] = array( 'id' => 'cod', 'name' => t('COD'), 'title' => t('Cash on Delivery'), 'desc' => t('Pay cash on delivery on pick-up.'), 'callback' => 'uc_payment_method_cod', 'weight' => 1, 'checkout' => FALSE, 'backend' => TRUE, ); $methods[] = array( 'id' => 'other', 'name' => t('Other'), 'title' => t('Other'), 'desc' => t('A generic payment method type.'), 'callback' => 'uc_payment_method_other', 'weight' => 10, 'checkout' => FALSE, 'backend' => TRUE, ); return $methods; } /******************************************************************************* * Callback Functions, Forms, and Tables ******************************************************************************/ /** * Display the payment settings overview. */ function uc_payment_settings_overview() { $sections[] = array( 'edit' => 'admin/store/settings/payment/edit', 'title' => t('Payment settings'), 'items' => array( t('Payment tracking is !status.', array('!status' => variable_get('uc_payment_tracking', TRUE) ? t('enabled') : t('disabled'))), t('Payments !status be deleted by approved users.', array('!status' => variable_get('uc_payment_deleting', TRUE) ? t('may') : t('may not'))), t('Payments are !status in the order logs.', array('!status' => variable_get('uc_payment_logging', TRUE) ? t('tracked') : t('not tracked'))), ), ); $methods = _payment_method_list(); foreach ($methods as $method) { $items[] = t('!title is !status for checkout.', array('!title' => $method['name'], '!status' => $method['checkout'] ? t('enabled') : t('disabled'))); } $sections[] = array( 'edit' => 'admin/store/settings/payment/edit/methods', 'title' => t('Payment methods'), 'items' => $items, ); $items = array(); $gateways = _payment_gateway_list(); foreach ($gateways as $gateway) { $items[] = t('!title is !status.', array('!title' => $gateway['title'], '!status' => $gateway['enabled'] ? t('enabled') : t('disabled'))); } $sections[] = array( 'edit' => 'admin/store/settings/payment/edit/gateways', 'title' => t('Payment gateways'), 'items' => $items, ); $output = theme('uc_settings_overview', $sections); return $output; } function uc_payment_settings_form() { $form['uc_payment_tracking'] = array( '#type' => 'checkbox', '#title' => t('Enable payment tracking.'), '#default_value' => variable_get('uc_payment_tracking', TRUE), ); $form['uc_payment_deleting'] = array( '#type' => 'checkbox', '#title' => t('Allow payments to be deleted by users with permission.'), '#default_value' => variable_get('uc_payment_deleting', TRUE), ); $form['uc_payment_logging'] = array( '#type' => 'checkbox', '#title' => t('Log payments entered and deleted to order log.'), '#default_value' => variable_get('uc_payment_logging', TRUE), ); return system_settings_form($form); } function uc_payment_methods_form() { $methods = _payment_method_list(); $form['methods_info'] = array( '#value' => '
| '. $line[2] .': | ' .''. uc_currency_format($line[1]) .' |
| '. t('Order total:') .' | ' .''. uc_currency_format($grand_total) .' |
| '. $title .': | ' . drupal_render($arg1['payment'][$field]) .' |
| ' . t('Description:') .' | ' . drupal_render($form['pm_other_description']) .' |
'. t('Orders totalling more than !number are not eligible for COD.', array('!number' => uc_currency_format($max))) .'
'; } if (variable_get('uc_cod_delivery_date', FALSE)) { $details .= uc_strip_form(drupal_get_form('uc_payment_method_cod_form', $arg1)); } return $details; case 'cart-process': if (variable_get('uc_cod_delivery_date', FALSE)) { $arg1->payment_details['delivery_month'] = intval($_POST['cod_delivery_month']); $arg1->payment_details['delivery_day'] = intval($_POST['cod_delivery_day']); $arg1->payment_details['delivery_year'] = intval($_POST['cod_delivery_year']); } return TRUE; case 'cart-review': if (variable_get('uc_cod_delivery_date', FALSE)) { $date = uc_date_format($arg1->payment_details['delivery_month'], $arg1->payment_details['delivery_day'], $arg1->payment_details['delivery_year']); $review[] = array('title' => t('Delivery Date'), 'data' => $date); } return $review; case 'order-view': if (variable_get('uc_cod_delivery_date', FALSE)) { $output = t('Desired delivery date:') .'| '); $month = (!empty($order->payment_details['delivery_month'])) ? $order->payment_details['delivery_month'] : date('n'); $form['cod_delivery_month'] = uc_select_month(NULL, $month); $form['table2'] = array('#value' => ' | '); $day = (!empty($order->payment_details['delivery_day'])) ? $order->payment_details['delivery_day'] : date('j'); $form['cod_delivery_day'] = uc_select_day(NULL, $day); $form['table3'] = array('#value' => ' | '); $year = (!empty($order->payment_details['delivery_year'])) ? $order->payment_details['delivery_year'] : date('Y'); $form['cod_delivery_year'] = uc_select_year(NULL, $year, date('Y'), date('Y') + 1); $form['table4'] = array('#value' => ' |
' . uc_address_format(variable_get('uc_store_name', ''), NULL, variable_get('uc_store_company', ''), variable_get('uc_store_street1', ''), variable_get('uc_store_street2', ''), variable_get('uc_store_city', ''), variable_get('uc_store_zone', ''), variable_get('uc_store_postal_code', ''), variable_get('uc_store_country', 840)) .'
'. variable_get('uc_check_policy', '') .'
'; } else { $details = t('Checks should be made out to:') .'' . uc_address_format(variable_get('uc_check_mailing_name', ''), NULL, variable_get('uc_check_mailing_company', ''), variable_get('uc_check_mailing_street1', ''), variable_get('uc_check_mailing_street2', ''), variable_get('uc_check_mailing_city', ''), variable_get('uc_check_mailing_zone', ''), variable_get('uc_check_mailing_postal_code', ''), variable_get('uc_store_country', 840)) .'
'. variable_get('uc_check_policy', '') .'
'; } return $details; case 'cart-review': if (!variable_get('uc_check_mailing_street1', FALSE)) { $review[] = array('title' => t('Mail to'), 'data' => uc_address_format(variable_get('uc_store_name', ''), NULL, variable_get('uc_store_company', ''), variable_get('uc_store_street1', ''), variable_get('uc_store_street2', ''), variable_get('uc_store_city', ''), variable_get('uc_store_zone', ''), variable_get('uc_store_postal_code', ''), variable_get('uc_store_country', 840))); } else { $review[] = array('title' => t('Mail to'), 'data' => uc_address_format(variable_get('uc_check_mailing_name', ''), NULL, variable_get('uc_check_mailing_company', ''), variable_get('uc_check_mailing_street1', ''), variable_get('uc_check_mailing_street2', ''), variable_get('uc_check_mailing_city', ''), variable_get('uc_check_mailing_zone', ''), variable_get('uc_check_mailing_postal_code', ''), variable_get('uc_store_country', 840))); } return $review; case 'order-view': if (!variable_get('uc_payment_tracking', TRUE)) { return ''; } $result = db_query("SELECT clear_date FROM {uc_payment_check} WHERE " ."order_id = %d ", $arg1->order_id); if ($check = db_fetch_object($result)) { $output = t('Clear Date:') .' '. date('m/d/Y', $check->clear_date); } else { $output = l(t('Receive Check'), 'admin/store/orders/'. $arg1->order_id .'/receive_check'); } $output .= ''. t('Use the form to enter the check into the payments system and set the expected clear date.') .'
'; $output .= ''. t('Order Balance:') .' ' . drupal_render($form['balance']) .'
'; $output .= drupal_render($form['check_exists']) . drupal_render($form['amount']) . drupal_render($form['comment']); $output .= '| '. t('Expected clear date:') .' | ||
| '. drupal_render($form['clear_month']) .' | '. drupal_render($form['clear_day']) . ' | ' . drupal_render($form['clear_year']) .' |
'. drupal_get_form('uc_payment_by_order_form', $order_id) .'
'; return $output; } function uc_payment_by_order_form($order_id) { $order = uc_order_load($order_id); if ($order === FALSE) { drupal_set_message('Order '. $order_id .' does not exist.'); drupal_goto('admin/store/orders'); } $total = $order->order_total; $payments = uc_payment_load_payments($order_id); $form['order_total'] = array('#value' => uc_currency_format($total)); $form['payments'] = array('#tree' => TRUE); if ($payments !== FALSE) { foreach ($payments as $payment) { $form['payments'][$payment->receipt_id]['received'] = array( '#value' => date('m/d/YH:i:s', $payment->received), ); $form['payments'][$payment->receipt_id]['user'] = array( '#value' => uc_get_initials($payment->uid), ); $form['payments'][$payment->receipt_id]['method'] = array( '#value' => ($payment->method == '') ? t('Unknown') : $payment->method, ); $form['payments'][$payment->receipt_id]['amount'] = array( '#value' => uc_currency_format($payment->amount), ); $total -= $payment->amount; $form['payments'][$payment->receipt_id]['balance'] = array( '#value' => uc_currency_format($total), ); $form['payments'][$payment->receipt_id]['comment'] = array( '#value' => ($payment->comment == '') ? '-' : $payment->comment, ); if (variable_get('uc_payment_deleting', TRUE) && user_access('delete payments')) { $action_value = l(t('Delete'), 'admin/store/orders/'. $order_id .'/payments/' . $payment->receipt_id .'/delete'); } else { $action_value = '-'; } $form['payments'][$payment->receipt_id]['action'] = array( '#value' => $action_value, ); } } $form['balance'] = array('#value' => uc_currency_format($total)); $form['order_id'] = array( '#type' => 'hidden', '#value' => $order_id, ); if (user_access('manual payments')) { $form['payments']['new']['received'] = array( '#value' => '-', ); $form['payments']['new']['user'] = array( '#value' => '-', ); $methods = _payment_method_list(); foreach ($methods as $method) { $options[$method['id']] = $method['name']; } $form['payments']['new']['method'] = array( '#type' => 'select', '#options' => $options, ); $form['payments']['new']['amount'] = array( '#type' => 'textfield', '#size' => 6, ); $form['payments']['new']['balance'] = array( '#value' => '-', ); $form['payments']['new']['comment'] = array( '#type' => 'textfield', '#size' => 32, '#maxlength' => 256, ); $form['payments']['new']['action'] = array( '#type' => 'submit', '#value' => t('Enter'), ); } return $form; } function uc_payment_by_order_form_validate($form_id, $form_values) { if (!is_numeric($form_values['payments']['new']['amount'])) { form_set_error('payments][new][amount', t('You must enter a number for the amount.')); } return TRUE; } function uc_payment_by_order_form_submit($form_id, $form_values) { global $user; $payment = $form_values['payments']['new']; uc_payment_enter($form_values['order_id'], $payment['method'], $payment['amount'], $user->uid, '', $payment['comment']); drupal_set_message(t('Payment entered.')); } function theme_uc_payment_by_order_form($form) { $output = ''. t('Order total:') .' '. drupal_render($form['order_total'])
.'
'. t('Current Balance:') .' '
. drupal_render($form['balance']) .'
'. tapir_get_table('uc_payments_table', $form) .'
' . ''. drupal_render($form['form_id']) . drupal_render($form['form_token']) .'
'; return $output; } function uc_payments_table($op, $form) { switch ($op) { case 'fields': $fields[] = array('name' => 'received', 'title' => t('Received'), 'weight' => 0, 'enabled' => TRUE); $fields[] = array('name' => 'user', 'title' => t('User'), 'weight' => 1, 'enabled' => TRUE); $fields[] = array('name' => 'method', 'title' => t('Method'), 'weight' => 2, 'enabled' => TRUE); $fields[] = array('name' => 'amount', 'title' => t('Amount'), 'weight' => 3, 'enabled' => TRUE); $fields[] = array('name' => 'balance', 'title' => t('Balance'), 'weight' => 4, 'enabled' => TRUE); $fields[] = array('name' => 'comment', 'title' => t('Comment'), 'weight' => 5, 'enabled' => TRUE); $fields[] = array('name' => 'action', 'title' => t('Action'), 'weight' => 6, 'enabled' => TRUE); return $fields; case 'data': foreach (element_children($form['payments']) as $i) { $data['received'][] = drupal_render($form['payments'][$i]['received']); $data['user'][] = drupal_render($form['payments'][$i]['user']); $data['method'][] = drupal_render($form['payments'][$i]['method']); $data['amount'][] = drupal_render($form['payments'][$i]['amount']); $data['balance'][] = drupal_render($form['payments'][$i]['balance']); $data['comment'][] = drupal_render($form['payments'][$i]['comment']); $data['action'][] = drupal_render($form['payments'][$i]['action']); } return $data; } } /** * Delete a payment from an order. */ function uc_payment_delete_confirm($order_id, $receipt_id) { $payment = uc_payment_load($receipt_id); if ($payment->order_id != $order_id) { drupal_set_message(t('An error loading the payment information occurred.')); drupal_goto('admin/store/orders/'. $order_id .'/payments'); } $output = ''. t('Payment information:') .'
'
. t('!method payment of !amount received on !date.',
array('!method' => $payment->method,
'!amount' => uc_currency_format($payment->amount),
'!date' => date('m/d/Y', $payment->received))) .'
' . drupal_get_form('uc_payment_delete_confirm_form', $order_id, $receipt_id); return $output; } function uc_payment_delete_confirm_form($order_id, $receipt_id) { $form['order_id'] = array( '#type' => 'hidden', '#value' => $order_id ); $form['receipt_id'] = array( '#type' => 'hidden', '#value' => $receipt_id, ); $form['cancel'] = array( '#type' => 'submit', '#value' => t('Cancel'), ); $form['delete'] = array( '#type' => 'submit', '#value' => t('Delete'), ); return $form; } function uc_payment_delete_confirm_form_submit($form_id, $form_values) { if ($form_values['op'] == t('Delete')) { uc_payment_delete($form_values['receipt_id']); drupal_set_message(t('Payment deleted.')); } drupal_goto('admin/store/orders/'. $form_values['order_id'] .'/payments'); } /** * Select a payment gateway to process a payment when multiple gateways * exist for a given payment method. */ function uc_payment_gateway_select($url_order_id) { $gateways = _payment_gateway_list($_SESSION['uc_payment_method'], TRUE); foreach($gateways as $gateway) { $options[$gateway['id']] = $gateway['title']; } $output = t('Please choose a payment gateway to use for that payment.'); $output .= drupal_get_form('uc_payment_gateway_select_form', $options, $_SESSION['uc_payment_method'], $_SESSION['uc_payment_order_id'], $_SESSION['uc_payment_amount'], $_SESSION['uc_payment_data']); return $output; } function uc_payment_gateway_select_form($options, $method, $order_id, $amount, $data) { $form['method'] = array( '#type' => 'hidden', '#value' => $method, ); $form['order_id'] = array( '#type' => 'hidden', '#value' => $order_id, ); $form['amount'] = array( '#type' => 'hidden', '#value' => $amount, ); $form['p_data'] = array( '#type' => 'hidden', '#value' => $data, ); $form['p_selected'] = array( '#type' => 'select', '#title' => t('Use Gateway'), '#options' => $options, '#default_value' => variable_get('uc_payment_'. $method .'_gateway', ''), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Process'), ); return $form; } function uc_payment_gateway_select_form_submit($form_id, $form_values) { unset($_SESSION['uc_payment_method']); unset($_SESSION['uc_payment_order_id']); unset($_SESSION['uc_payment_amount']); unset($_SESSION['uc_payment_data']); uc_payment_process($form_values['method'], $form_values['order_id'], $form_values['amount'], unserialize($form_values['p_data']), FALSE, $form_values['p_selected']); drupal_goto('admin/store/orders/'. $form_values['order_id']); } /******************************************************************************* * Module and Helper Functions ******************************************************************************/ /** * Process a payment through an enabled payment gateway. */ function uc_payment_process($method, $order_id, $amount, $data = NULL, $default = FALSE, $selected = NULL, $redirect = TRUE) { $gateways = _payment_gateway_list($method, TRUE); if (!is_array($gateways) || count($gateways) == 0) { if (!$default) { drupal_set_message(t('You are not able to process payments of type !type. ', array('!type' => ''. _payment_method_data($method, 'name') .''))); } return FALSE; } if (count($gateways) == 1) { if (function_exists($gateways[0][$method])) { $result = $gateways[0][$method]($order_id, $amount, $data); } else { if (user_access('administer store')) { drupal_set_message(t("Attempted to process payment but the gateway's function was not found.")); } } } else { $func = FALSE; foreach($gateways as $gateway) { if ($default && $gateway['id'] == variable_get('uc_payment_'. $method .'_gateway', '')) { $func = $gateway[$method]; } if (!is_null($selected) && $gateway['id'] == $selected) { $func = $gateway[$method]; } } if ($func !== FALSE) { if (function_exists($func)) { $result = $func($order_id, $amount, $data); } else { drupal_set_message(t('An error has occurred with your payment gateway. The charge function could not be found.')); $result['success'] = FALSE; } } else { $_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 ($result['success'] === TRUE) { if (!empty($result['uid'])) { $result['uid'] = 0; } uc_payment_enter($order_id, $method, $amount, $result['uid'], $result['data'], $result['comment']); } if (!empty($result['message']) && !$default) { drupal_set_message($result['message']); } if ($redirect) { drupal_goto('admin/store/orders/'. $order_id); } return $result['success']; } /** * Enter a payment for an order. */ function uc_payment_enter($order_id, $method, $amount, $uid, $data, $comment) { $method_name = _payment_method_data($method, 'name'); if (is_null($method_name)) { $method_name = t('Other'); } if (is_array($data)) { $data = serialize($data); } if (variable_get('uc_payment_logging', TRUE)) { global $user; $log_message = t('!method payment for !amount entered by !user.', array('!method' => $method_name, '!amount' => uc_currency_format($amount), '!user' => uc_get_initials($user->uid))); uc_order_log_changes($order_id, array($log_message)); } db_query("INSERT INTO {uc_payment_receipts} (receipt_id, order_id, method, " ."amount, uid, data, comment, received) VALUES (%d, %d, '%s', %f, " ."%d, '%s', '%s', %d)", db_next_id('{uc_payment_receipts}_receipt_id'), $order_id, $method_name, $amount, $uid, $data, $comment, time()); } /** * Delete a payment from the database. */ function uc_payment_delete($receipt_id) { if (!is_numeric($receipt_id)) { return FALSE; } if (variable_get('uc_payment_logging', TRUE)) { global $user; $payment = uc_payment_load($receipt_id); $log_message = t('!method payment for !amount deleted by !user.', array('!method' => $payment->method, '!amount' => uc_currency_format($payment->amount), '!user' => uc_get_initials($user->uid))); uc_order_log_changes($payment->order_id, array($log_message)); } db_query("DELETE FROM {uc_payment_receipts} WHERE receipt_id = %d", $receipt_id); } /** * Return the balance of payments on an order. */ 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; } /** * Load a single payment from the database by receipt_id. */ function uc_payment_load($receipt_id) { if (!is_numeric($receipt_id)) { return FALSE; } $result = db_query("SELECT * FROM {uc_payment_receipts} WHERE receipt_id = %d ", $receipt_id); $payment = db_fetch_object($result); return $payment; } /** * Load an array of all the payments for order $order_id. */ function uc_payment_load_payments($order_id, $action = NULL) { $payments = array(); $result = db_query("SELECT * FROM {uc_payment_receipts} WHERE order_id = %d " ."ORDER BY received ASC", $order_id); while ($payment = db_fetch_object($result)) { $payments[] = $payment; } if (count($payments) == 0) { $payments = FALSE; } return $payments; } /** * Sort the payment methods by weight using this function with usort(). */ function _payment_method_sort($a, $b) { if ($a['weight'] == $b['weight']) { return 0; } return ($a['weight'] > $b['weight']) ? 1 : -1; } /** * Build a list of payment methods defined in the enabled modules. */ function _payment_method_list($action = NULL) { static $methods; if (count($methods) > 0 && $action !== 'rebuild') { return $methods; } $methods = module_invoke_all('payment_method'); for ($i = 0; $i < count($methods); $i++) { $methods[$i]['checkout'] = variable_get('uc_payment_method_'. $methods[$i]['id'] .'_checkout', $methods[$i]['checkout']); $methods[$i]['backend'] = variable_get('uc_payment_method_'. $methods[$i]['id'] .'_backend', $methods[$i]['backend']); $methods[$i]['weight'] = variable_get('uc_payment_method_'. $methods[$i]['id'] .'_weight', $methods[$i]['weight']); } usort($methods, 'uc_weight_sort'); return $methods; } /** * Return data from a payment method by method ID and the array key. */ function _payment_method_data($method_id, $key) { $methods = _payment_method_list(); foreach ($methods as $method) { if ($method['id'] == $method_id) { return $method[$key]; } } } /** * Build a list of payment gateways defined in the enabled modules. */ function _payment_gateway_list($filter = NULL, $enabled_only = FALSE) { $gateways = module_invoke_all('payment_gateway'); for ($i = 0, $j = count($gateways); $i < $j; $i++) { $gateways[$i]['enabled'] = variable_get('uc_pg_'. $gateways[$i]['id'] .'_enabled', TRUE); if ($filter != NULL) { if (!isset($gateways[$i][$filter]) || !function_exists($gateways[$i][$filter])) { unset($gateways[$i]); } } if ($enabled_only) { if (!variable_get('uc_pg_'. $gateways[$i]['id'] .'_enabled', TRUE)) { unset($gateways[$i]); } } } return $gateways; } /** * Return data from a payment gateway by gateway ID and the array key. */ function _payment_gateway_data($method_id, $key) { $gateways = _payment_gateway_list(); foreach ($gateways as $gateway) { if ($gateway['id'] == $gateway_id) { return $gateway[$key]; } } } /** * Retrieve a pipe delimited string of autocomplete suggestions for payment methods */ function _uc_payment_methods_autocomplete($string = '') { $matches = array(); $methods = _payment_method_list(); foreach($methods as $method) { if ($method['backend'] == TRUE) { $matches[$method['name']] = check_plain($method['name']); } } print drupal_to_js($matches); exit(); }