function commerce_sagepay_refund_form_submit in Drupal Commerce SagePay Integration 7
Submit handler: process a refund request.
Parameters
array $form: The Form array.
array $form_state: The Form state array.
File
- includes/
commerce_sagepay_refund.inc, line 95
Code
function commerce_sagepay_refund_form_submit($form, &$form_state) {
$transaction = $form_state['transaction'];
$amount = $form_state['values']['amount'];
// Create a new transaction id for the refund.
$refund_transaction_id = _commerce_sagepay_vendor_tx_code($transaction, 'REFUND');
// Set up the request to send to SagePay.
$query = array(
'VPSProtocol' => SAGEPAY_PROTOCOL,
'TxType' => 'REFUND',
'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
'VendorTxCode' => $refund_transaction_id,
'Amount' => $amount,
'Currency' => $transaction->currency_code,
'Description' => t('Refund 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'],
);
// Determine the url to sent the request to based on the transaction mode.
switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
case SAGEPAY_TXN_MODE_LIVE:
$url = SAGEPAY_SHARED_REFUND_TRANSACTION_LIVE;
break;
case SAGEPAY_TXN_MODE_TEST:
$url = SAGEPAY_SHARED_REFUND_TRANSACTION_TEST;
break;
case SAGEPAY_TXN_MODE_SIMULATION:
$url = SAGEPAY_SHARED_REFUND_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;
// Create a new transaction for the refund.
$refund_transaction = commerce_payment_transaction_new($transaction->payment_method, $transaction->order_id);
$refund_transaction->instance_id = $transaction->instance_id;
$refund_transaction->amount = 0 - $amount * 100;
$refund_transaction->currency_code = $transaction->currency_code;
$refund_transaction->remote_id = $refund_transaction_id;
$refund_transaction->payload = $response;
$refund_transaction->remote_status = SAGEPAY_REMOTE_STATUS_REFUNDED;
switch ($response['Status']) {
case 'OK':
drupal_set_message(t('Payment refunded successfully.'));
$refund_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
break;
default:
// Display an error message but leave the transaction pending.
$refund_transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
drupal_set_message(t('Transaction refund failed.'), 'error');
drupal_set_message(check_plain($response['StatusDetail']), 'error');
}
$transaction_message = 'Status @status, @statusdetail. ';
$refund_transaction->message = $transaction_message;
$refund_transaction->message_variables = array(
'@status' => $response['Status'],
'@statusdetail' => $response['StatusDetail'],
);
commerce_payment_transaction_save($refund_transaction);
$form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}