You are here

function commerce_payment_redirect_pane_checkout_form in Commerce Core 7

Payment redirect pane: form callback.

File

modules/payment/includes/commerce_payment.checkout_pane.inc, line 317
Callback functions for the Payment module's checkout panes.

Code

function commerce_payment_redirect_pane_checkout_form(&$form, &$form_state, $checkout_pane, $order) {

  // First load the order's specified payment method instance.
  if (!empty($order->data['payment_method'])) {
    $payment_method = commerce_payment_method_instance_load($order->data['payment_method']);
  }
  else {
    $payment_method = FALSE;
  }

  // If the payment method doesn't exist or does not require a redirect...
  if (!$payment_method || !$payment_method['offsite']) {
    if (!$payment_method) {
      $log = t('Customer skipped the Payment page because no payment was required.');
    }
    else {
      $log = t('Customer skipped the Payment page because payment was already submitted.');
    }

    // Advance the customer to the next step of the checkout process.
    commerce_payment_redirect_pane_next_page($order, $log);
    drupal_goto(commerce_checkout_order_uri($order));
  }

  // If the user came to the cancel URL...
  if (arg(3) == 'back' && arg(4) == $order->data['payment_redirect_key']) {

    // Perform any payment cancellation functions if necessary.
    if ($callback = commerce_payment_method_callback($payment_method, 'redirect_form_back')) {
      $callback($order, $payment_method);
    }

    // Send the customer to the previous step of the checkout process.
    commerce_payment_redirect_pane_previous_page($order, t('Customer canceled payment at the payment gateway.'));
    drupal_goto(commerce_checkout_order_uri($order));
  }

  // If the user came to the return URL...
  if (arg(3) == 'return' && arg(4) == $order->data['payment_redirect_key']) {

    // Check for a validate handler on return.
    $validate_callback = commerce_payment_method_callback($payment_method, 'redirect_form_validate');

    // If there is no validate handler or if there is and it isn't FALSE...
    if (!$validate_callback || $validate_callback($order, $payment_method) !== FALSE) {

      // Perform any submit functions if necessary.
      if ($callback = commerce_payment_method_callback($payment_method, 'redirect_form_submit')) {
        $callback($order, $payment_method);
      }

      // Send the customer on to the next checkout page.
      commerce_payment_redirect_pane_next_page($order, t('Customer successfully submitted payment at the payment gateway.'));
      drupal_goto(commerce_checkout_order_uri($order));
    }
    else {

      // Otherwise display the failure message and send the customer back.
      drupal_set_message(t('Payment failed at the payment server. Please review your information and try again.'), 'error');
      commerce_payment_redirect_pane_previous_page($order, t('Customer payment submission failed at the payment gateway.'));
      drupal_goto(commerce_checkout_order_uri($order));
    }
  }

  // If the function to build the redirect form exists...
  if ($callback = commerce_payment_method_callback($payment_method, 'redirect_form')) {

    // Generate a key to use in the return URL from the redirected service if it
    // does not already exist.
    if (empty($order->data['payment_redirect_key'])) {
      $order->data['payment_redirect_key'] = drupal_hash_base64(time());
      commerce_order_save($order);
    }

    // If the payment method has the 'offsite_autoredirect' option enabled, add
    // the redirection behavior.
    if (!empty($payment_method['offsite_autoredirect'])) {
      $form['#attached']['js'][] = drupal_get_path('module', 'commerce_payment') . '/commerce_payment.js';
      $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>';
    }

    // Merge the new form into the current form array, preserving the help text
    // if it exists. We also add a wrapper so the form can be easily submitted.
    $form += drupal_get_form($callback, $order, $payment_method);
    $form['#prefix'] = '<div class="payment-redirect-form">';
    $form['#suffix'] = '</div>';
  }
  else {

    // Alert the administrator that the module does not provide a required form.
    drupal_set_message(t('The %title payment method indicates it is offsite but does not define the necessary form to process the redirect.', array(
      '%title' => $payment_method['title'],
    )), 'error');
  }
}