You are here

function commerce_amex_3d_secure_callback in Commerce American Express Payment Gateway (Amex) 7

Process callback response from merchant server

1 string reference to 'commerce_amex_3d_secure_callback'
commerce_amex_menu in ./commerce_amex.module
Implements hook_menu

File

./commerce_amex.module, line 834
Implements American Express payment gateway for use in Drupal Commerce.

Code

function commerce_amex_3d_secure_callback($order, $transaction) {
  $process_transaction = FALSE;

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

  // check for 3d secure response field
  if (!isset($_POST['PaRes'])) {
    watchdog('commerce_amex', 'Invalid data received in 3D Secure response', array(), WATCHDOG_ERROR);
    return drupal_not_found();
  }
  $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
  $data = new stdClass();
  $data->apiOperation = 'PROCESS_ACS_RESULT';
  $data->{'3DSecure'}['paRes'] = $_POST['PaRes'];
  $url = $payment_method['settings']['txn_url'] . AMEX_TXN_PATH . $payment_method['settings']['merchant_id'] . '/3DSecureId/' . $transaction->transaction_id;
  $result = _commerce_amex_post_request($url, $payment_method['settings']['merchant_id'], $payment_method['settings']['password'], $data);

  // Process error and return to payment form.
  if (isset($result->result) && $result->result == 'ERROR') {
    $transaction = _commerce_amex_error_process($result, $transaction);
    drupal_set_message(t('There was an error verifying your card with 3D Secure'), 'error');
    commerce_payment_redirect_pane_previous_page($order);
    drupal_goto('checkout/' . $order->order_id);
  }
  $transaction->remote_status = $result->response->{'3DSecure'}->gatewayCode;
  switch ($result->response->{'3DSecure'}->gatewayCode) {
    case 'AUTHENTICATION_SUCCESSFUL':
    case 'AUTHENTICATION_ATTEMPTED':
      $process_transaction = TRUE;
      break;
    case 'AUTHENTICATION_FAILED':
    case 'INVALID_SIGNATURE_ON_AUTHENTICATION_RESPONSE':
      drupal_set_message(t('3D Secure Authentication Failed.'), 'error');
      break;
    case 'AUTHENTICATION_NOT_AVAILABLE_NO_ERROR_DETAILS':
    case 'AUTHENTICATION_NOT_AVAILABLE_ERROR_DETAILS_PROVIDED':
      if ($payment_method['settings']['txn_3d_secure'] == 1 || $payment_method['settings']['txn_3d_secure'] == 3) {
        $process_transaction = TRUE;
      }
      else {
        drupal_set_message(t('Authentication not availiable.'), 'error');
      }
      break;
    case 'ERROR_PARSING_AUTHENTICATION_RESPONSE':
      drupal_set_message(t('There was an error proccessing your security details.'), 'error');
      break;
    case 'ACS_SESSION_TIMEOUT':
      drupal_set_message(t('The 3DSecure session timed out.'), 'error');
      break;
  }
  if ($process_transaction) {
    $transaction = _commerce_amex_process_transaction($order, $transaction, TRUE);
    if (isset($transaction->payload->result) && $transaction->payload->result == 'SUCCESS') {
      commerce_payment_redirect_pane_next_page($order);
      drupal_goto('checkout/' . $order->order_id);
    }
    else {
      commerce_payment_redirect_pane_previous_page($order);
      drupal_goto('checkout/' . $order->order_id);
    }
  }
  else {
    commerce_payment_redirect_pane_previous_page($order);
    drupal_goto('checkout/' . $order->order_id);
  }
}