uc_payment_order_pane.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Contains the callbacks for the payment order pane supplied with
  5. * Ubercart and their corresponding helper functions.
  6. *
  7. * Order panes are defined using hook_uc_order_pane() and use a callback to
  8. * handle the different processes involved in order viewing/editing. The
  9. * payment order pane is defined in uc_payment_uc_order_pane() in
  10. * uc_payment.module.
  11. */
  12. /**
  13. * Handles the Payment order pane.
  14. */
  15. function uc_order_pane_payment($op, $order, &$form = NULL, &$form_state = NULL) {
  16. switch ($op) {
  17. case 'view':
  18. $build['balance'] = array('#markup' => t('Balance: @balance', array('@balance' => uc_currency_format(uc_payment_balance($order)))));
  19. if (user_access('view payments')) {
  20. $build['view_payments'] = array(
  21. '#markup' => ' (' . l(t('View'), 'admin/store/orders/' . $order->order_id . '/payments') . ')',
  22. );
  23. }
  24. $method_name = _uc_payment_method_data($order->payment_method, 'review');
  25. if (empty($method_name)) {
  26. $method_name = _uc_payment_method_data($order->payment_method, 'name');
  27. }
  28. $build['method'] = array(
  29. '#markup' => t('Method: @payment_method', array('@payment_method' => $method_name)),
  30. '#prefix' => '<br />',
  31. );
  32. $func = _uc_payment_method_data($order->payment_method, 'callback');
  33. if (function_exists($func)) {
  34. $method_output = $func('order-view', $order);
  35. if (!empty($method_output)) {
  36. $build['output'] = $method_output + array(
  37. '#prefix' => '<br />',
  38. );
  39. }
  40. }
  41. return $build;
  42. case 'customer':
  43. $method_name = _uc_payment_method_data($order->payment_method, 'review');
  44. if (empty($method_name)) {
  45. $method_name = _uc_payment_method_data($order->payment_method, 'name');
  46. }
  47. $build['method'] = array('#markup' => t('Method: @payment_method', array('@payment_method' => $method_name)));
  48. $func = _uc_payment_method_data($order->payment_method, 'callback');
  49. if (function_exists($func)) {
  50. $method_output = $func('customer-view', $order);
  51. if (!empty($method_output)) {
  52. $build['output'] = $method_output + array(
  53. '#prefix' => '<br />',
  54. );
  55. }
  56. }
  57. return $build;
  58. case 'edit-form':
  59. $methods = _uc_payment_method_list();
  60. $options = array();
  61. foreach ($methods as $id => $method) {
  62. $options[$id] = $method['name'];
  63. }
  64. $form['payment']['payment_method'] = array(
  65. '#type' => 'select',
  66. '#title' => t('Payment method'),
  67. '#default_value' => $order->payment_method,
  68. '#options' => !empty($options) ? $options : array(t('None available')),
  69. '#disabled' => empty($options),
  70. '#ajax' => array(
  71. 'callback' => 'uc_payment_order_pane_ajax_callback',
  72. 'progress' => array('type' => 'throbber'),
  73. 'wrapper' => 'payment-details',
  74. ),
  75. );
  76. $form['payment']['payment_details'] = array(
  77. '#tree' => TRUE,
  78. '#prefix' => '<div id="payment-details">',
  79. '#suffix' => '</div>',
  80. );
  81. $method = isset($form_state['values']['payment_method']) ? $form_state['values']['payment_method'] : $order->payment_method;
  82. $func = _uc_payment_method_data($method, 'callback');
  83. if (function_exists($func) && $details = $func('order-details', $order)) {
  84. if (is_array($details)) {
  85. $form['payment']['payment_details'] += $details;
  86. }
  87. else {
  88. $form['payment']['payment_details']['#markup'] = $details;
  89. }
  90. }
  91. return $form;
  92. case 'edit-theme':
  93. return drupal_render($form['payment']);
  94. case 'edit-process':
  95. $changes = array();
  96. $changes['payment_method'] = $form_state['values']['payment_method'];
  97. $changes['payment_details'] = isset($form_state['values']['payment_details']) ? $form_state['values']['payment_details'] : array();
  98. $func = _uc_payment_method_data($form_state['values']['payment_method'], 'callback');
  99. if (function_exists($func) && ($return = $func('edit-process', $order, $form, $form_state)) != NULL && is_array($return)) {
  100. $changes['payment_details'] = array_merge($changes['payment_details'], $return);
  101. }
  102. if (!isset($order->payment_details)) {
  103. $order->payment_details = array();
  104. }
  105. return $changes;
  106. }
  107. }
  108. /**
  109. * AJAX callback to render the payment method pane.
  110. */
  111. function uc_payment_order_pane_ajax_callback($form, &$form_state) {
  112. $commands[] = ajax_command_replace('#payment-details', trim(drupal_render($form['payment']['payment_details'])));
  113. $commands[] = ajax_command_prepend('#payment-details', theme('status_messages'));
  114. return array('#type' => 'ajax', '#commands' => $commands);
  115. }