You are here

function commerce_payflow_form_alter in Commerce PayPal 7.2

Implements hook_form_alter().

File

modules/payflow/commerce_payflow.module, line 904
Implements PayPal Payments Advanced (U.S. only) and Payflow Link Hosted Checkout pages and Transparent Redirect.

Code

function commerce_payflow_form_alter(&$form, &$form_state, $form_id) {

  // If we're altering a checkout form that has the Payflow Link radio button...
  if (strpos($form_id, 'commerce_checkout_form_') === 0 && !empty($form['commerce_payment']['payment_method'])) {
    $payflow_link = FALSE;
    foreach ($form['commerce_payment']['payment_method']['#options'] as $key => &$value) {
      list($method_id, $rule_name) = explode('|', $key);

      // If we find Payflow Link, include its CSS on the form and exit the loop.
      if (in_array($method_id, array(
        'payflow_link',
        'paypal_ppa',
      ))) {

        // Merge in the default settings in case the payment method has not been
        // configured yet.
        $payment_method = commerce_payment_method_instance_load($key);
        $payment_method['settings'] += commerce_payflow_link_default_settings();
        $payment_icons = array_values($payment_method['settings']['payment_icons']);
        $payflow_link = TRUE;

        // Prepare an icons array that only includes enabled icons.
        $icons = commerce_paypal_icons($payment_icons);
        if (in_array('paypal', $payment_icons, TRUE)) {
          $value = t('Credit card, debit card, or PayPal:') . ' ' . implode(' ', $icons);
        }
        else {
          $value = t('Credit card or debit card:') . ' ' . implode(' ', $icons);
        }

        // Check to see if we should disable a corresponding Express Checkout
        // payment method option.
        if (!empty($payment_method['settings']['paypal_ec_disable']) && !empty($payment_method['settings']['paypal_ec_instance'])) {
          $ec_instance_id = 'paypal_ec|' . $payment_method['settings']['paypal_ec_instance'];

          // Remove the option from the list.
          unset($form['commerce_payment']['payment_method']['#options'][$ec_instance_id]);

          // Reset the default selection if that happened to be it.
          if ($form['commerce_payment']['payment_method']['#default_value'] == $ec_instance_id) {
            reset($form['commerce_payment']['payment_method']['#options']);
            $default_value = key($form['commerce_payment']['payment_method']['#options']);

            // Set the new default value of the payment method radio buttons.
            $form['commerce_payment']['payment_method']['#default_value'] = $default_value;

            // Update the payment details area of the form if necessary.
            $default_payment_method = commerce_payment_method_instance_load($default_value);
            if ($callback = commerce_payment_method_callback($default_payment_method, 'submit_form')) {
              $form['commerce_payment']['payment_details'] = $callback($default_payment_method, array(), commerce_checkout_pane_load('commerce_payment'), $form_state['order']);
            }
            else {
              $form['commerce_payment']['payment_details'] = array();
            }
          }
        }
      }
    }

    // If we did find Payflow Link, include its CSS now.
    if ($payflow_link) {

      // Include its CSS on the form.
      $form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_payflow') . '/theme/commerce_payflow.theme.css';
    }
  }

  // Alter the Payment checkout page form for Payflow Link.
  if ($form_id == 'commerce_checkout_form_payment' && !empty($form['commerce_payflow_payment_method'])) {
    switch ($form['commerce_payflow_payment_method']['#value']['settings']['redirect_mode']) {

      // Display the automatic redirection for POST redirects.
      case 'post':
        $form['help']['#markup'] = '<div class="checkout-help">' . t('Please wait while you are redirected to the payment server. If nothing happens within 10 seconds, please click on the button below.') . '</div>';
        break;

      // Clear the help text for embedded iframe payment.
      case 'iframe':
        unset($form['help']);
        break;
    }
  }

  // Alter the Review checkout page form for Payflow Link.
  if ($form_id == 'commerce_checkout_form_review' && !empty($form_state['order']->data['commerce_payflow'])) {
    drupal_add_js(array(
      'commercePayflow' => array(
        'page' => 'review',
      ),
    ), 'setting');
    $form['#attached']['js'][] = drupal_get_path('module', 'commerce_payflow') . '/commerce_payflow.js';

    // If the Payflow query variable is present, reshow the error message and
    // reload the page.
    if (!empty($_GET['payflow-page']) && $_GET['payflow-page'] == 'review') {
      drupal_set_message(t('Payment failed at the payment server. Please review your information and try again.'), 'error');
      drupal_goto('checkout/' . $form_state['order']->order_id . '/review');
    }
  }

  // Alter the Completion checkout page form for Payflow Link.
  if ($form_id == 'commerce_checkout_form_complete' && !empty($form_state['order']->data['commerce_payflow'])) {
    $form['#attached']['js'][] = drupal_get_path('module', 'commerce_payflow') . '/commerce_payflow.js';
  }
}