You are here

function commerce_authnet_aim_credit_form_submit in Commerce Authorize.Net 7

Submit handler: process a credit via AIM.

File

includes/commerce_authnet.admin.inc, line 258
Administrative forms for the Authorize.Net module.

Code

function commerce_authnet_aim_credit_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $amount = number_format($form_state['values']['amount'], 2, '.', '');
  $order = $form_state['order'];
  $payment_method = $form_state['payment_method'];

  // Determine the last 4 credit card digits from the previous transaction.
  $transaction_payload = end($transaction->payload);

  // If this payment transaction does not have credit card digits, we will check
  // for a previous transaction that has the information.
  if (empty($transaction_payload[50])) {

    // We reverse the array because Voids do not contain credit card digits,
    // and we will have to cycle through fewer transactions this way.
    $transaction_payloads = array_reverse($transaction->payload);
    foreach ($transaction_payloads as $transaction_id => $payload) {
      if ($payload[0] == 1 && !empty($payload[50])) {

        // This payload has the last four digits.
        $transaction_payload = $payload;
        break;
      }
    }
  }
  $credit_card = !empty($transaction_payload[50]) ? substr($transaction_payload[50], 4, 8) : '';

  // Make sure that the last 4 digits are available and valid.
  if (!intval($credit_card) || strlen($credit_card) != 4) {
    drupal_set_message(t('The credit could not be attempted, because the last 4 digits of the credit card were not found in the transaction data. Please login to your Authorize.Net merchant interface to issue the credit.'));
    return FALSE;
  }
  else {

    // Build a name-value pair array for this transaction.
    $nvp = array(
      'x_type' => 'CREDIT',
      'x_trans_id' => $transaction->remote_id,
      'x_amount' => $amount,
      'x_card_num' => $credit_card,
      'x_invoice_num' => $order->order_number,
      'x_email' => substr($order->mail, 0, 255),
      'x_cust_id' => substr($order->uid, 0, 20),
      'x_customer_ip' => substr(ip_address(), 0, 15),
    );

    // Submit the request to Authorize.Net.
    $response = commerce_authnet_aim_request($form_state['payment_method'], $nvp);

    // If the credit succeeded...
    if ($response[0] == 1) {
      $credit_amount = commerce_currency_decimal_to_amount($amount, $transaction->currency_code);
      drupal_set_message(t('Credit for @amount issued successfully', array(
        '@amount' => commerce_currency_format($credit_amount, $transaction->currency_code),
      )));

      // Create a new transaction to record the credit.
      $credit_transaction = commerce_payment_transaction_new('authnet_aim', $order->order_id);
      $credit_transaction->instance_id = $payment_method['instance_id'];
      $credit_transaction->remote_id = $response[6];
      $credit_transaction->amount = $credit_amount * -1;
      $credit_transaction->currency_code = $transaction->currency_code;
      $credit_transaction->payload[REQUEST_TIME] = $response;
      $credit_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $credit_transaction->remote_status = $response[11];
      $credit_transaction->message = t('Credited to @remote_id.', array(
        '@remote_id' => $transaction->remote_id,
      ));

      // Save the credit transaction.
      commerce_payment_transaction_save($credit_transaction);
    }
    else {

      // Save the failure response message to the original transaction.
      $transaction->payload[REQUEST_TIME] = $response;

      // Display a failure message and response reason from Authorize.net.
      drupal_set_message(t('Credit failed: @reason', array(
        '@reason' => $response[3],
      )), 'error');

      // Add an additional helper message if the transaction hadn't settled yet.
      if ($response[2] == 54) {
        drupal_set_message(t('The transaction must be setted before a credit can be issued. This usually takes 24 hours'), 'error');
      }
      commerce_payment_transaction_save($transaction);
    }
  }
  $form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id . '/payment';
}