You are here

function commerce_sagepay_repeat_form_submit in Drupal Commerce SagePay Integration 7

Submit handler: Repeat a transaction.

Parameters

array $form: The Form array.

array $form_state: The Form state array.

File

includes/commerce_sagepay_repeat.inc, line 96

Code

function commerce_sagepay_repeat_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $amount = $form_state['values']['amount'];
  $repeat_transaction_id = 'REPEAT_' . _commerce_sagepay_vendor_tx_code($transaction);
  $query = array(
    'VPSProtocol' => SAGEPAY_PROTOCOL,
    'TxType' => 'REPEAT',
    'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
    'VendorTxCode' => $repeat_transaction_id,
    'Amount' => $amount,
    'Currency' => $transaction->currency_code,
    'Description' => t('Repeat payment against order %order_id', array(
      '%order_id' => $transaction->order_id,
    )),
    'RelatedVPSTxId' => $transaction->remote_id,
    'RelatedVendorTxCode' => $transaction->payload['VendorTxCode'],
    'RelatedSecurityKey' => $transaction->payload['SecurityKey'],
    'RelatedTxAuthNo' => $transaction->payload['TxAuthNo'],
  );
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $url = SAGEPAY_SHARED_REPEAT_TRANSACTION_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $url = SAGEPAY_SHARED_REPEAT_TRANSACTION_TEST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $url = SAGEPAY_SHARED_REPEAT_TRANSACTION_SIMULATION;
      break;
  }
  $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;

  // Create a new transaction for the repeat order.
  $repeat_transaction = commerce_payment_transaction_new($transaction->payment_method, $transaction->order_id);
  $repeat_transaction->instance_id = $transaction->instance_id;
  $repeat_transaction->amount = $amount * 100;
  $repeat_transaction->currency_code = $transaction->currency_code;
  $repeat_transaction->remote_id = $repeat_transaction_id;
  $repeat_transaction->payload = $response;
  $repeat_transaction->remote_status = SAGEPAY_REMOTE_STATUS_PAYMENT;
  switch ($response['Status']) {
    case 'OK':
      drupal_set_message(t('Payment repeated successfully.'));
      $repeat_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      break;
    default:

      // Display an error message but leave the transaction pending.
      $repeat_transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      drupal_set_message(t('Transaction repeat failed.'), 'error');
      drupal_set_message(check_plain($response['StatusDetail']), 'error');
  }
  $transaction_message = 'Status @status, @statusdetail. ';
  $repeat_transaction->message = $transaction_message;
  $repeat_transaction->message_variables = array(
    '@status' => $response['Status'],
    '@statusdetail' => $response['StatusDetail'],
  );
  commerce_payment_transaction_save($repeat_transaction);
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}