You are here

function commerce_sagepay_release_transaction in Drupal Commerce SagePay Integration 7

2 calls to commerce_sagepay_release_transaction()
commerce_sagepay_release_form_submit in includes/commerce_sagepay_release.inc
Submit handler: Release a transaction.
commerce_sagepay_release_order_transaction in ./commerce_sagepay.rules.inc
Rules action callback for commerce_sagepay_release_transaction action

File

includes/commerce_sagepay_release.inc, line 110

Code

function commerce_sagepay_release_transaction($transaction, $amount) {

  // Set up a new transaction id for the release transaction.
  $release_transaction_id = _commerce_sagepay_vendor_tx_code($transaction);

  // Create the query to send to SagePay.
  $query = array(
    'VPSProtocol' => SAGEPAY_PROTOCOL,
    'TxType' => 'RELEASE',
    'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
    'VendorTxCode' => $transaction->payload['VendorTxCode'],
    'ReleaseAmount' => $amount,
    'Description' => t('Release deferred transaction.'),
    'VPSTxId' => $transaction->remote_id,
    'SecurityKey' => $transaction->payload['SecurityKey'],
    'TxAuthNo' => $transaction->payload['TxAuthNo'],
  );

  // Determine the url to send the transaction to based on the transaction mode.
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $url = SAGEPAY_SHARED_RELEASE_TRANSACTION_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $url = SAGEPAY_SHARED_RELEASE_TRANSACTION_TEST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $url = SAGEPAY_SHARED_RELEASE_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.
  $response['VendorTxId'] = $release_transaction_id;
  $transaction->payload[REQUEST_TIME] = $response;
  switch ($response['Status']) {
    case 'OK':
      drupal_set_message(t('Payment Authorised successfully.'));
      $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $transaction->remote_status = SAGEPAY_REMOTE_STATUS_PAYMENT;
      break;
    default:

      // Display an error message but leave the transaction pending.
      $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      drupal_set_message(t('Transaction release failed.'), 'error');
      drupal_set_message(check_plain($response['StatusDetail']), 'error');
  }
  $transaction_message = 'Status @status, @statusdetail. ';
  $transaction->message = $transaction_message;
  $transaction->message_variables = array(
    '@status' => $response['Status'],
    '@statusdetail' => $response['StatusDetail'],
  );
  commerce_payment_transaction_save($transaction);
}