You are here

function commerce_sagepay_cancel_transaction in Drupal Commerce SagePay Integration 7

2 calls to commerce_sagepay_cancel_transaction()
commerce_sagepay_cancel_form_submit in includes/commerce_sagepay_cancel.inc
Submit handler.
commerce_sagepay_cancel_order_transaction in ./commerce_sagepay.rules.inc
Rules action callback for commerce_sagepay_cancel_transaction action

File

includes/commerce_sagepay_cancel.inc, line 72

Code

function commerce_sagepay_cancel_transaction($transaction) {

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

  // Determine the correct url to send the request to based on the mode.
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $url = SAGEPAY_SHARED_CANCEL_TRANSACTION_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $url = SAGEPAY_SHARED_CANCEL_TRANSACTION_TEST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $url = SAGEPAY_SHARED_CANCEL_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 cancelled successfully.'));
      $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      $transaction->remote_status = SAGEPAY_REMOTE_STATUS_CANCELLED;

      // Append a capture indication to the result message.
      $transaction->message .= '<br />' . t('Cancelled: @date', array(
        '@date' => format_date(REQUEST_TIME, 'short'),
      ));
      commerce_payment_transaction_save($transaction);
      break;
    default:
      drupal_set_message(t('Transaction Cancel failed.'), 'error');
      drupal_set_message(check_plain($response['StatusDetail']), 'error');
  }
}