You are here

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

Check if the card is registered for 3D Secure and redirect if required

Parameters

object $order:

object $payment_method:

object $transaction:

Return value

boolean

1 call to commerce_amex_3d_secure_check()
commerce_amex_hosted_redirect_form_submit in ./commerce_amex.module
Payment method callback: redirect form submit.

File

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

Code

function commerce_amex_3d_secure_check($order, $payment_method, $transaction) {
  $amount = commerce_currency_amount_to_decimal($transaction->amount, $transaction->currency_code);
  $order_amount = number_format($amount, 2, '.', '');
  $return = FALSE;

  // Build the data array.
  $data = new stdClass();
  $data->apiOperation = 'CHECK_3DS_ENROLLMENT';
  $data->transaction['amount'] = $order_amount;
  $data->transaction['currency'] = $transaction->currency_code;
  $data->sourceOfFunds['session'] = $transaction->remote_id;
  if (isset($transaction->data['cardonfile_selected']) && ($cardonfile = commerce_cardonfile_load($transaction->data['cardonfile_selected']))) {
    $data->sourceOfFunds['token'] = $cardonfile->remote_id;
  }
  $data->{'3DSecure'}['authenticationRedirect']['responseUrl'] = url('checkout/' . $order->order_id . '/amex/3d/' . $transaction->transaction_id, array(
    'absolute' => TRUE,
  ));

  // Build the URL and send the request to Amex.
  $url = $payment_method['settings']['txn_url'] . AMEX_TXN_PATH . $payment_method['settings']['merchant_id'] . '/3DSecureId/' . $transaction->transaction_id;
  $result = _commerce_amex_put_request($url, $payment_method['settings']['merchant_id'], $payment_method['settings']['password'], $data);

  // Process error and return false.
  if (isset($result->error) && $result->result == 'ERROR') {
    $transaction = _commerce_amex_error_process($result, $transaction);
    commerce_payment_redirect_pane_previous_page($order);
  }

  // If the card is enrolled print the auto redirect form and exit.
  if (isset($result->response)) {
    switch ($result->response->{'3DSecure'}->gatewayCode) {
      case 'CARD_ENROLLED':
        print $result->{'3DSecure'}->authenticationRedirect->simple->htmlBodyContent;
        drupal_exit();
        break;
      case 'NOT_ENROLLED_NO_ERROR_DETAILS':
      case 'NOT_ENROLLED_ERROR_DETAILS_PROVIDED':
      case 'CARD_DOES_NOT_SUPPORT_3DS':
      case 'ENROLLMENT_STATUS_UNDETERMINED_NO_ERROR_DETAILS':
      case 'ENROLLMENT_STATUS_UNDETERMINED_ERROR_DETAILS_PROVIDED':
      case 'INVALID_DIRECTORY_SERVER_CREDENTIALS':
      case 'ERROR_PARSING_CHECK_ENROLLMENT_RESPONSE':
      case 'ERROR_COMMUNICATING_WITH_DIRECTORY_SERVER':
      case 'MPI_PROCESSING_ERROR':
        if ($payment_method['settings']['txn_3d_secure'] < 3) {
          $return = TRUE;
        }
        else {
          drupal_set_message(t('A 3DSecure card is required'), 'error');
        }
        break;
    }
  }
  return $return;
}