You are here

function commerce_authnet_aim_void_form_submit in Commerce Authorize.Net 7

Submit handler: process the void request.

File

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

Code

function commerce_authnet_aim_void_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];

  // Build a name-value pair array for this transaction.
  $nvp = array(
    'x_type' => 'VOID',
    'x_trans_id' => $transaction->remote_id,
  );

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

  // Update and save the transaction based on the response.
  $transaction->payload[REQUEST_TIME] = $response;

  // If we got an approval response code...
  if ($response[0] == 1) {
    drupal_set_message(t('Transaction successfully voided.'));

    // Set the remote and local status accordingly.
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    $transaction->remote_status = $response[11];

    // Update the transaction message to show that it has been voided.
    $transaction->message .= '<br />' . t('Voided: @date', array(
      '@date' => format_date(REQUEST_TIME, 'short'),
    ));

    // Zero out transaction amount before saving,
    // so original amount is not saved as a positive revenue transaction.
    // Authorize.Net settles these as "0.00" (see the payload on a void response).
    $transaction->amount = '000';
  }
  else {
    drupal_set_message(t('Void failed: @reason', array(
      '@reason' => check_plain($response[3]),
    )), 'error');
  }
  commerce_payment_transaction_save($transaction);
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}