function commerce_sagepay_authorise_form_submit in Drupal Commerce SagePay Integration 7
Submit handler: process a prior authorization capture.
Parameters
array $form: The Form array.
array $form_state: The Form state array.
File
- includes/
commerce_sagepay_authorise.inc, line 117
Code
function commerce_sagepay_authorise_form_submit($form, &$form_state) {
$transaction = $form_state['transaction'];
$amount = $form_state['values']['amount'];
// Create a new transaction number for the authorisation transaction.
$authorise_transaction_id = _commerce_sagepay_vendor_tx_code($transaction);
// Assemble the query array to send to SagePay.
$query = array(
'VPSProtocol' => SAGEPAY_PROTOCOL,
'TxType' => 'AUTHORISE',
'Vendor' => variable_get(SAGEPAY_SETTING_VENDOR_NAME),
'VendorTxCode' => $authorise_transaction_id,
'Amount' => $amount,
'Description' => t('Authorise registered transaction.'),
'RelatedVPSTxId' => $transaction->remote_id,
'RelatedVendorTxCode' => $transaction->payload['VendorTxCode'],
'RelatedSecurityKey' => $transaction->payload['SecurityKey'],
'ApplyAVSCV2' => $form_state['values']['cv2_check'],
);
// Determine the correct url to send the request depending on the Mode.
switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
case SAGEPAY_TXN_MODE_LIVE:
$url = SAGEPAY_SHARED_AUTHORISE_TRANSACTION_LIVE;
break;
case SAGEPAY_TXN_MODE_TEST:
$url = SAGEPAY_SHARED_AUTHORISE_TRANSACTION_TEST;
break;
case SAGEPAY_TXN_MODE_SIMULATION:
$url = SAGEPAY_SHARED_AUTHORISE_TRANSACTION_SIMULATION;
break;
}
// Send the request to SagePay adn 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'] = $authorise_transaction_id;
$transaction->payload[REQUEST_TIME] = $response;
$transaction->payload['VendorTxId'] = $authorise_transaction_id;
// Create a new transaction for the Authorise.
$authorise_transaction = commerce_payment_transaction_new($transaction->payment_method, $transaction->order_id);
$authorise_transaction->instance_id = $transaction->instance_id;
$authorise_transaction->amount = $amount * 100;
$authorise_transaction->currency_code = $transaction->currency_code;
$authorise_transaction->remote_id = $authorise_transaction_id;
$authorise_transaction->payload = $response;
switch ($response['Status']) {
case 'OK':
drupal_set_message(t('Payment Authorised successfully.'));
$authorise_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
break;
default:
// Display an error message but leave the transaction pending.
$authorise_transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
drupal_set_message(t('Transaction authorise failed.'), 'error');
drupal_set_message(check_plain($response['StatusDetail']), 'error');
}
$transaction_message = 'Status @status, @statusdetail. ';
$authorise_transaction->message = $transaction_message;
$authorise_transaction->message_variables = array(
'@status' => $response['Status'],
'@statusdetail' => $response['StatusDetail'],
);
commerce_payment_transaction_save($authorise_transaction);
$form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}