uc_payment.admin.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * @file
  4. * Payment administration menu items.
  5. */
  6. /**
  7. * Displays an overview of the available payment methods.
  8. *
  9. * @see theme_uc_payment_methods_table()
  10. */
  11. function uc_payment_methods_form($form, &$form_state) {
  12. $methods = _uc_payment_method_list();
  13. $form['pmtable'] = array(
  14. '#theme' => 'uc_payment_method_table',
  15. );
  16. foreach ($methods as $id => $method) {
  17. $form['pmtable'][$id]['uc_payment_method_' . $id . '_checkout'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => check_plain($method['name']),
  20. '#default_value' => variable_get('uc_payment_method_' . $id . '_checkout', $method['checkout']),
  21. );
  22. $form['pmtable'][$id]['uc_payment_method_' . $id . '_weight'] = array(
  23. '#type' => 'weight',
  24. '#default_value' => variable_get('uc_payment_method_' . $id . '_weight', $method['weight']),
  25. '#attributes' => array('class' => array('uc-payment-method-weight')),
  26. );
  27. if (empty($method['no_gateway'])) {
  28. $gateways = _uc_payment_gateway_list($id, TRUE);
  29. $options = array();
  30. foreach ($gateways as $gateway_id => $gateway) {
  31. $options[$gateway_id] = $gateway['title'];
  32. }
  33. if ($options) {
  34. $form['pmtable'][$id]['uc_payment_method_' . $id . '_checkout']['#title'] .= ' (' . t('includes %gateways', array('%gateways' => implode(', ', $options))) . ')';
  35. }
  36. }
  37. $links = array();
  38. $null = NULL;
  39. $method_settings = $method['callback']('settings', $null, array(), $form_state);
  40. if (is_array($method_settings)) {
  41. $links['settings'] = array(
  42. 'title' => t('settings'),
  43. 'href' => 'admin/store/settings/payment/method/' . $id,
  44. );
  45. }
  46. $links['conditions'] = array(
  47. 'title' => t('conditions'),
  48. 'href' => 'admin/store/settings/payment/manage/uc_payment_method_' . $id,
  49. );
  50. $form['pmtable'][$id]['settings'] = array(
  51. '#theme' => 'links',
  52. '#links' => $links,
  53. '#attributes' => array('class' => array('links', 'inline')),
  54. );
  55. }
  56. return system_settings_form($form);
  57. }
  58. /**
  59. * Themes the table that displays available payment methods.
  60. *
  61. * @see uc_payment_methods_form()
  62. * @ingroup themeable
  63. */
  64. function theme_uc_payment_method_table($variables) {
  65. $form = $variables['form'];
  66. drupal_add_tabledrag('uc-payment-methods', 'order', 'sibling', 'uc-payment-method-weight');
  67. $header = array(t('Payment method'), t('List position'), t('Operations'));
  68. $rows = array();
  69. foreach (element_children($form) as $method) {
  70. $row = array(
  71. drupal_render($form[$method]['uc_payment_method_' . $method . '_checkout']),
  72. drupal_render($form[$method]['uc_payment_method_' . $method . '_weight']),
  73. drupal_render($form[$method]['settings']),
  74. );
  75. $rows[] = array(
  76. 'data' => $row,
  77. 'class' => array('draggable'),
  78. );
  79. }
  80. return theme('table', array(
  81. 'header' => $header,
  82. 'rows' => $rows,
  83. 'attributes' => array('id' => 'uc-payment-methods'),
  84. 'empty' => t('No payment methods are available. Modules providing payment methods must first be enabled on the !modules administration page under the "Ubercart - payment" fieldset.', array('!modules' => l(t('Modules'), 'admin/modules'))),
  85. ));
  86. }
  87. /**
  88. * Displays settings for a single payment method.
  89. */
  90. function uc_payment_method_settings_form($form, &$form_state, $method_id) {
  91. $callback = _uc_payment_method_data($method_id, 'callback');
  92. $null = NULL;
  93. if (function_exists($callback)) {
  94. $form = $callback('settings', $null, array(), $form_state);
  95. }
  96. else {
  97. drupal_not_found();
  98. drupal_exit();
  99. }
  100. return system_settings_form($form);
  101. }
  102. /**
  103. * Displays a list of payments attached to an order.
  104. *
  105. * @see uc_payment_by_order_form_validate()
  106. * @see uc_payment_by_order_form_submit()
  107. * @ingroup forms
  108. */
  109. function uc_payment_by_order_form($form, &$form_state, $order) {
  110. $form['#attached']['css'][] = drupal_get_path('module', 'uc_payment') . '/uc_payment.css';
  111. $total = $order->order_total;
  112. $payments = uc_payment_load_payments($order->order_id);
  113. $form['order_total'] = array(
  114. '#type' => 'item',
  115. '#title' => t('Order total'),
  116. '#theme' => 'uc_price',
  117. '#price' => $total,
  118. );
  119. $form['payments'] = tapir_get_table('uc_payments_table');
  120. $form['payments']['#weight'] = 10;
  121. if ($payments !== FALSE) {
  122. foreach ($payments as $payment) {
  123. $form['payments'][$payment->receipt_id]['received'] = array(
  124. '#markup' => format_date($payment->received, 'custom', variable_get('date_format_uc_store', 'm/d/Y') . '<b\r />H:i:s'),
  125. );
  126. $form['payments'][$payment->receipt_id]['user'] = array(
  127. '#markup' => theme('uc_uid', array('uid' => $payment->uid)),
  128. );
  129. $form['payments'][$payment->receipt_id]['method'] = array(
  130. '#markup' => ($payment->method == '') ? t('Unknown') : $payment->method,
  131. );
  132. $form['payments'][$payment->receipt_id]['amount'] = array(
  133. '#theme' => 'uc_price',
  134. '#price' => $payment->amount,
  135. );
  136. $total -= $payment->amount;
  137. $form['payments'][$payment->receipt_id]['balance'] = array(
  138. '#theme' => 'uc_price',
  139. '#price' => $total,
  140. );
  141. $form['payments'][$payment->receipt_id]['comment'] = array(
  142. '#markup' => ($payment->comment == '') ? '-' : filter_xss_admin($payment->comment),
  143. );
  144. if (user_access('delete payments')) {
  145. $action_value = l(t('Delete'), 'admin/store/orders/' . $order->order_id . '/payments/'
  146. . $payment->receipt_id . '/delete');
  147. }
  148. else {
  149. $action_value = '-';
  150. }
  151. $form['payments'][$payment->receipt_id]['action'] = array(
  152. '#markup' => $action_value,
  153. );
  154. }
  155. }
  156. $form['balance'] = array(
  157. '#type' => 'item',
  158. '#title' => t('Current balance'),
  159. '#theme' => 'uc_price',
  160. '#price' => $total,
  161. );
  162. $form['order_id'] = array(
  163. '#type' => 'hidden',
  164. '#value' => $order->order_id,
  165. );
  166. if (user_access('manual payments')) {
  167. $form['payments']['new']['received'] = array(
  168. '#type' => 'date',
  169. '#default_value' => array(
  170. 'month' => format_date(REQUEST_TIME, 'custom', 'n'),
  171. 'day' => format_date(REQUEST_TIME, 'custom', 'j'),
  172. 'year' => format_date(REQUEST_TIME, 'custom', 'Y'),
  173. ),
  174. );
  175. $form['payments']['new']['user'] = array(
  176. '#markup' => '-',
  177. );
  178. $methods = _uc_payment_method_list();
  179. foreach ($methods as $id => $method) {
  180. $options[$id] = $method['name'];
  181. }
  182. $form['payments']['new']['method'] = array(
  183. '#type' => 'select',
  184. '#title' => t('Method'),
  185. '#title_display' => 'invisible',
  186. '#options' => $options,
  187. );
  188. $form['payments']['new']['amount'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Amount'),
  191. '#title_display' => 'invisible',
  192. '#size' => 6,
  193. );
  194. $form['payments']['new']['balance'] = array(
  195. '#markup' => '-',
  196. );
  197. $form['payments']['new']['comment'] = array(
  198. '#type' => 'textfield',
  199. '#title' => t('Comment'),
  200. '#title_display' => 'invisible',
  201. '#size' => 32,
  202. '#maxlength' => 256,
  203. );
  204. $form['payments']['new']['action'] = array('#type' => 'actions');
  205. $form['payments']['new']['action']['action'] = array(
  206. '#type' => 'submit',
  207. '#value' => t('Enter'),
  208. );
  209. }
  210. return $form;
  211. }
  212. /**
  213. * Form validation for uc_payment_by_order_form().
  214. *
  215. * @see uc_payment_by_order_form()
  216. * @see uc_payment_by_order_form_submit()
  217. */
  218. function uc_payment_by_order_form_validate($form, &$form_state) {
  219. if (!is_numeric($form_state['values']['payments']['new']['amount'])) {
  220. form_set_error('payments][new][amount', t('You must enter a number for the amount.'));
  221. }
  222. return TRUE;
  223. }
  224. /**
  225. * Form submission handler for uc_payment_by_order_form().
  226. *
  227. * @see uc_payment_by_order_form()
  228. * @see uc_payment_by_order_form_validate()
  229. */
  230. function uc_payment_by_order_form_submit($form, &$form_state) {
  231. global $user;
  232. $payment = $form_state['values']['payments']['new'];
  233. $received = strtotime($payment['received']['year'] . '-' . $payment['received']['month'] . '-' . $payment['received']['day'] . ' 00:00:00');
  234. // If the value entered is today, use the exact timestamp instead
  235. $startofday = mktime(0, 0, 0);
  236. if ($received == $startofday) {
  237. $received = REQUEST_TIME;
  238. }
  239. uc_payment_enter($form_state['values']['order_id'], $payment['method'], $payment['amount'], $user->uid, '', $payment['comment'], $received);
  240. drupal_set_message(t('Payment entered.'));
  241. }
  242. /**
  243. * Confirmation form to delete a payment from an order.
  244. *
  245. * @see uc_payment_delete_confirm_form_submit()
  246. * @ingroup forms
  247. */
  248. function uc_payment_delete_confirm_form($form, &$form_state, $order, $payment) {
  249. // Make sure the payment is for the specified order.
  250. if ($payment->order_id != $order->order_id) {
  251. drupal_set_message(t('An error loading the payment information occurred.'));
  252. drupal_goto('admin/store/orders/' . $order->order_id . '/payments');
  253. }
  254. $desc = '<strong>' . t('Payment information:') . '</strong> '
  255. . t('@method payment of @amount received on @date.', array('@method' => $payment->method, '@amount' => uc_currency_format($payment->amount), '@date' => format_date($payment->received, 'short')));
  256. $form['order_id'] = array(
  257. '#type' => 'value',
  258. '#value' => $order->order_id
  259. );
  260. $form['receipt_id'] = array(
  261. '#type' => 'value',
  262. '#value' => $payment->receipt_id,
  263. );
  264. return confirm_form($form, t('Are you sure you want to delete this payment?'), 'admin/store/orders/' . $order->order_id . '/payments', $desc, t('Delete'));
  265. }
  266. /**
  267. * Form submission handler for uc_payment_delete_confirm_form().
  268. *
  269. * @see uc_payment_delete_confirm_form()
  270. */
  271. function uc_payment_delete_confirm_form_submit($form, &$form_state) {
  272. uc_payment_delete($form_state['values']['receipt_id']);
  273. drupal_set_message(t('Payment deleted.'));
  274. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/payments';
  275. }