You are here

function sagepay_3d_secure_callback in Drupal Commerce SagePay Integration 7

Process callback response from merchant server.

Parameters

int $order_id: The order number being processed.

1 string reference to 'sagepay_3d_secure_callback'
sagepay_3d_secure_menu in modules/sagepay_3d_secure/sagepay_3d_secure.module
Implements hook_menu().

File

modules/sagepay_3d_secure/sagepay_3d_secure.module, line 222

Code

function sagepay_3d_secure_callback($order_id) {

  // If there's no data in the POST, return a page not found.
  if (empty($_POST)) {
    drupal_not_found();
  }

  // Attempt to reload the order.
  $order = commerce_order_load($order_id);

  // If the order doesn't exist, return a page not found.
  if (!isset($order)) {
    drupal_not_found();
  }

  // Check the order status - if it's already complete return a page not found.
  $order_status = commerce_order_status_load($order->status);
  if ($order_status['name'] != 'checkout_3d_secure') {
    drupal_not_found();
  }

  // Check for 3d secure response field.
  if (!isset($_POST['MD']) || !isset($_POST['PaRes'])) {
    watchdog('commerce_sagepay_direct', 'Invalid data received in 3D Secure response', array(), WATCHDOG_ERROR);
    drupal_not_found();
  }
  $md = check_plain($_POST['MD']);
  $pares = check_plain($_POST['PaRes']);

  // Assemble post request to send to SagePay.
  $post = 'MD=' . $md;
  $post .= '&PaRes=' . $pares;
  $payment_method_instance = commerce_payment_method_instance_load($order->data['payment_method']);

  // Check the transaction mode we are in to determine which server to send
  // the 3D Secure callback.
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $server_url = SAGEPAY_DIRECT_SERVER_3D_SECURE_CALLBACK_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $server_url = SAGEPAY_DIRECT_SERVER_3D_SECURE_CALLBACK_TEST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $server_url = SAGEPAY_DIRECT_SERVER_SIMULATION;
      break;
    default:
      $server_url = SAGEPAY_TXN_MODE_SIMULATION;
  }

  // Process the callback.
  $response = _commerce_sagepay_request_post($server_url, $post);

  // Process response.
  $success = commerce_sagepay_process_response($payment_method_instance, $order, $response);
  if ($success) {
    $checkout_pages = commerce_checkout_pages();
    $next_step = $checkout_pages['3d_secure']['next_page'];
    $order = commerce_order_status_update($order, 'checkout_' . $next_step);

    // Inform modules of checkout completion if the next page is Completed.
    if ($next_step == 'complete') {
      commerce_checkout_complete($order);
    }
    $redirect = 'checkout/' . $order->order_id . '/' . $next_step;
  }
  else {
    $redirect = 'checkout/' . $order->order_id . '/payment';
  }
  sagepay_3d_secure_clear_iframe($redirect);
}