You are here

function commerce_sagepay_abort_transaction in Drupal Commerce SagePay Integration 7

2 calls to commerce_sagepay_abort_transaction()
commerce_sagepay_abort_form_submit in includes/commerce_sagepay_abort.inc
Submit handler: process an abort transaction request.
commerce_sagepay_abort_order_transaction in ./commerce_sagepay.rules.inc
Rules action callback for commerce_sagepay_abort_transaction action

File

includes/commerce_sagepay_abort.inc, line 75

Code

function commerce_sagepay_abort_transaction($transaction) {

  // Set up the SagePay query array.
  $query = array(
    'VPSProtocol' => SAGEPAY_PROTOCOL,
    'TxType' => 'ABORT',
    'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
    'VendorTxCode' => $transaction->payload['VendorTxCode'],
    'VPSTxId' => $transaction->remote_id,
    'SecurityKey' => $transaction->payload['SecurityKey'],
    'TxAuthNo' => $transaction->payload['TxAuthNo'],
  );

  // Determine the URL to send the request to based on the transaction mode.
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $url = SAGEPAY_SHARED_ABORT_TRANSACTION_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $url = SAGEPAY_SHARED_ABORT_TRANSACTION_TEST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $url = SAGEPAY_SHARED_ABORT_TRANSACTION_SIMULATION;
      break;
  }

  // Send the request to SagePay and process the response.
  $query = _commerce_sagepay_array_to_post($query);
  $response = _commerce_sagepay_request_post($url, $query);

  // Update and save the transaction based on the response.
  $transaction->payload[REQUEST_TIME] = $response;
  switch ($response['Status']) {
    case 'OK':
      drupal_set_message(t('Payment aborted successfully.'));

      // Update the transaction amount to the actual capture amount.
      $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      $transaction->remote_status = SAGEPAY_REMOTE_STATUS_CANCELLED;

      // Append an abort indication to the transaction message.
      $transaction->message .= '<br />' . t('Aborted: @date', array(
        '@date' => format_date(REQUEST_TIME, 'short'),
      ));
      commerce_payment_transaction_save($transaction);
      break;
    default:

      // Display an error message but leave the transaction pending.
      drupal_set_message(t('Transaction Abort failed, so the transaction will remain in a pending status.'), 'error');
      drupal_set_message(check_plain($response['StatusDetail']), 'error');
  }
}