uc_payment_checkout_pane.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Checkout pane functions for uc_payment.module.
  5. *
  6. * The checkout pane holds form to select the payment method. It also shows a
  7. * preview of the line items and order total.
  8. */
  9. function uc_checkout_pane_payment($op, &$order, $form = NULL, &$form_state = NULL) {
  10. switch ($op) {
  11. case 'view':
  12. $contents['#attached']['css'][] = drupal_get_path('module', 'uc_payment') . '/uc_payment.css';
  13. if (variable_get('uc_payment_show_order_total_preview', TRUE)) {
  14. $contents['line_items'] = array(
  15. '#theme' => 'uc_payment_totals',
  16. '#order' => $order,
  17. '#prefix' => '<div id="line-items-div">',
  18. '#suffix' => '</div>',
  19. '#weight' => -20,
  20. );
  21. }
  22. // Ensure that the form builder uses #default_value to determine which
  23. // button should be selected after an ajax submission. This is
  24. // necessary because the previously selected value may have become
  25. // unavailable, which would result in an invalid selection.
  26. unset($form_state['input']['panes']['payment']['payment_method']);
  27. $options = array();
  28. foreach (_uc_payment_method_list() as $id => $method) {
  29. $set = rules_config_load('uc_payment_method_' . $method['id']);
  30. if ($set && !$set->execute($order)) {
  31. continue;
  32. }
  33. if ($method['checkout'] && !isset($method['express'])) {
  34. $options[$id] = $method['title'];
  35. }
  36. }
  37. drupal_alter('uc_payment_method_checkout', $options, $order);
  38. $description = '';
  39. if (!$options) {
  40. $description = t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.');
  41. $options[''] = t('No payment methods available');
  42. }
  43. elseif (count($options) > 1) {
  44. $description = t('Select a payment method from the following options.');
  45. }
  46. if (!isset($options[$order->payment_method])) {
  47. $order->payment_method = key($options);
  48. }
  49. $contents['payment_method'] = array(
  50. '#type' => 'radios',
  51. '#title' => t('Payment method'),
  52. '#title_display' => 'invisible',
  53. '#options' => $options,
  54. '#default_value' => $order->payment_method,
  55. '#disabled' => count($options) == 1,
  56. '#required' => TRUE,
  57. '#ajax' => array(
  58. 'callback' => 'uc_payment_checkout_payment_details',
  59. 'wrapper' => 'payment-details',
  60. 'progress' => array(
  61. 'type' => 'throbber',
  62. ),
  63. ),
  64. );
  65. $contents['details'] = array(
  66. '#prefix' => '<div id="payment-details" class="clearfix payment-details-' . $order->payment_method . '">',
  67. '#markup' => t('Continue with checkout to complete payment.'),
  68. '#suffix' => '</div>',
  69. );
  70. $func = _uc_payment_method_data($order->payment_method, 'callback');
  71. if (function_exists($func) && $details = $func('cart-details', $order, $form, $form_state)) {
  72. unset($contents['details']['#markup']);
  73. $contents['details'] += $details;
  74. }
  75. return array('description' => $description, 'contents' => $contents);
  76. case 'process':
  77. if (empty($form_state['values']['panes']['payment']['payment_method'])) {
  78. form_set_error('panes][payment][payment_method', t('You cannot check out without selecting a payment method.'));
  79. return FALSE;
  80. }
  81. $order->payment_method = $form_state['values']['panes']['payment']['payment_method'];
  82. $func = _uc_payment_method_data($order->payment_method, 'callback');
  83. if (function_exists($func)) {
  84. $result = $func('cart-process', $order, $form, $form_state);
  85. if ($result === FALSE) {
  86. return FALSE;
  87. }
  88. }
  89. return TRUE;
  90. case 'review':
  91. $line_items = uc_order_load_line_items_display($order);
  92. foreach ($line_items as $line_item) {
  93. $review[] = array('title' => $line_item['title'], 'data' => theme('uc_price', array('price' => $line_item['amount'])));
  94. }
  95. $review_data = _uc_payment_method_data($order->payment_method, 'review');
  96. if (empty($review_data)) {
  97. $review_data = _uc_payment_method_data($order->payment_method, 'name');
  98. }
  99. $review[] = array('border' => 'top', 'title' => t('Paying by'), 'data' => $review_data);
  100. $func = _uc_payment_method_data($order->payment_method, 'callback');
  101. if (function_exists($func)) {
  102. $result = $func('cart-review', $order);
  103. if (is_array($result)) {
  104. $review = array_merge($review, $result);
  105. }
  106. }
  107. return $review;
  108. case 'settings':
  109. $form['uc_payment_show_order_total_preview'] = array(
  110. '#type' => 'checkbox',
  111. '#title' => t('Show the order total preview on the payment pane.'),
  112. '#default_value' => variable_get('uc_payment_show_order_total_preview', TRUE),
  113. );
  114. return $form;
  115. }
  116. }
  117. /**
  118. * AJAX callback for payment method details on the checkout form.
  119. */
  120. function uc_payment_checkout_payment_details($form, $form_state) {
  121. return $form['panes']['payment']['details'];
  122. }