You are here

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

Payment method callback: submit form submission.

File

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

Code

function commerce_amex_hosted_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {

  // Return an error if the enabling action's settings haven't been configured.
  if (empty($payment_method['settings']['password'])) {
    drupal_set_message(t('Payment gateway is not configured for use.'), 'error');
    return array();
  }

  // Create a form session with Amex.
  $session = _commerce_amex_request_session($payment_method);
  if (isset($session->result) && $session->result == "SUCCESS") {
    $session_id = $session->session;

    // Create a new payment transaction and setup the amount.
    $transaction = commerce_payment_transaction_new('amex_hosted', $order->order_id);
    $transaction->amount = $charge['amount'];
    $transaction->currency_code = $charge['currency_code'];
    $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
    $transaction->instance_id = $payment_method['instance_id'];
    $transaction->remote_id = $session_id;
    $transaction->data['cardonfile_selected'] = !empty($pane_values['cardonfile']) && $pane_values['cardonfile'] != 'new' ? $pane_values['cardonfile'] : NULL;
    commerce_payment_transaction_save($transaction);
    return TRUE;
  }
  elseif (isset($session->result) && $session->result == "ERROR") {
    $explanation = isset($session->error->explanation) ? $session->error->explanation : 'Unknown Error';
    watchdog('commerce_amex', 'Failed to connect to Amex for order %order_id: %explanation', array(
      '%order_id' => $order->order_id,
      '%explanation' => $explanation,
    ), WATCHDOG_ERROR);
    drupal_set_message(t('There was an error connecting to the payment server.'), 'error');
    commerce_payment_redirect_pane_previous_page($order);
  }
  else {
    watchdog('commerce_amex', 'Failed to connect to Amex for order %order_id', array(
      '%order_id' => $order->order_id,
    ), WATCHDOG_ERROR);
    drupal_set_message(t('There was an error connecting to the payment server.'), 'error');
    commerce_payment_redirect_pane_previous_page($order);
  }
}