View source
<?php
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_common');
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_utils');
function commerce_sagepay_form_redirect_form($form, &$form_state, $order, $payment_method) {
if (!variable_get(SAGEPAY_SETTING_VENDOR_NAME)) {
drupal_set_message(t("SagePay Form Integration is not configured for use.\n <a href=\"/admin/commerce/config/sagepay\">Set a vendor name on the settings page.</a>"), 'error');
return array();
}
if (!variable_get(SAGEPAY_SETTING_ENCRYPTION_KEY)) {
drupal_set_message(t('SagePay Form Integration is not configured for use.
<a href=\\"/admin/commerce/config/sagepay\\">Set an encryption key on the settings page.</a>'), 'error');
return array();
}
$settings = array(
'cancel_return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
'absolute' => TRUE,
)),
'return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
'absolute' => TRUE,
)),
'payment_method' => $payment_method['instance_id'],
);
return commerce_sagepay_order_form($form, $form_state, $order, $settings);
}
function commerce_sagepay_form_redirect_form_validate($order, $payment_method) {
$encrypted_response = $_GET['crypt'];
switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
case SAGEPAY_TXN_MODE_LIVE:
$enc_key = variable_get(SAGEPAY_SETTING_ENCRYPTION_KEY);
break;
default:
$enc_key = variable_get(SAGEPAY_SETTING_TEST_ENCRYPTION_KEY);
}
if (!isset($enc_key)) {
watchdog('commerce_sagepay_form', 'Cannot load SagePay key from payment method in order to decrypt response', array(), WATCHDOG_ERROR);
return FALSE;
}
$unencrypted_response = _commerce_sagepay_decode_and_decrypt($encrypted_response, $enc_key);
$tokens = _commerce_sagepay_form_get_tokens($unencrypted_response);
if (empty($tokens)) {
watchdog('commerce_sagepay_form', 'Response could not be converted to tokens.', array(), WATCHDOG_ERROR);
return FALSE;
}
$vendor_tx_code = isset($tokens['VendorTxCode']) ? $tokens['VendorTxCode'] : FALSE;
if (empty($vendor_tx_code)) {
watchdog('commerce_sagepay_form', 'No VendorTxCode returned.', array(), WATCHDOG_ERROR);
return FALSE;
}
$order_id = _commerce_sagepay_vendor_tx_code_to_order_id($vendor_tx_code);
if ($order_id != $order->order_id) {
watchdog('commerce_sagepay_form', 'Returned order id does not match order for this session', array(), WATCHDOG_ERROR);
return FALSE;
}
$def_charge = array(
'amount' => 0,
'currency_code' => '',
);
switch ($tokens['Status']) {
case 'ABORT':
watchdog('commerce_sagepay', 'ABORT error from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ALERT);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Your SagePay transaction was aborted.'), 'error');
return FALSE;
case 'NOTAUTHED':
watchdog('commerce_sagepay', 'NOTAUTHED error from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ALERT);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Your transaction was not authorised by SagePay'), 'error');
return FALSE;
case 'REJECTED':
watchdog('commerce_sagepay', 'REJECTED error from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ALERT);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Your transaction was rejected by SagePay'), 'error');
return FALSE;
case 'MALFORMED':
watchdog('commerce_sagepay', 'MALFORMED error from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ALERT);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Sorry the transaction has failed.'), 'error');
return FALSE;
case 'INVALID':
watchdog('commerce_sagepay', 'INVALID error from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ERROR);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Sorry the transaction has failed.'), 'error');
return FALSE;
case 'ERROR':
watchdog('commerce_sagepay', 'System ERROR from SagePay for order %order_id with message %msg', array(
'%order_id' => $order_id,
'%msg' => $tokens['StatusDetail'],
), WATCHDOG_ERROR);
commerce_sagepay_transaction($payment_method, $order, $def_charge, $tokens, COMMERCE_PAYMENT_STATUS_FAILURE, SAGEPAY_REMOTE_STATUS_FAIL);
drupal_set_message(t('Sorry an error occurred while processing your transaction.'), 'error');
return FALSE;
case 'OK':
watchdog('commerce_sagepay', 'OK Payment callback received from SagePay for order %order_id with status code %status', array(
'%order_id' => $order_id,
'%status' => $tokens['Status'],
));
$remote_status = SAGEPAY_REMOTE_STATUS_OK;
$transaction_status = COMMERCE_PAYMENT_STATUS_SUCCESS;
break;
case 'AUTHENTICATED':
watchdog('commerce_sagepay', 'AUTHENTICATED Payment callback received from SagePay for order %order_id with status code %status', array(
'%order_id' => $order_id,
'%status' => $tokens['Status'],
));
$remote_status = SAGEPAY_REMOTE_STATUS_AUTHENTICATE;
$transaction_status = COMMERCE_PAYMENT_STATUS_SUCCESS;
break;
case 'REGISTERED':
watchdog('commerce_sagepay', 'REGISTERED Payment callback received from SagePay for order %order_id with status code %status', array(
'%order_id' => $order_id,
'%status' => $tokens['Status'],
));
$remote_status = SAGEPAY_REMOTE_STATUS_REGISTERED;
$transaction_status = COMMERCE_PAYMENT_STATUS_PENDING;
break;
default:
watchdog('commerce_sagepay', 'Unrecognised Status response from SagePay for order %order_id (%response_code)', array(
'%order_id' => $order_id,
'%response_code' => $tokens['Status'],
), WATCHDOG_ERROR);
return FALSE;
}
$charge = array();
$currency = commerce_currency_load($order->commerce_order_total['und'][0]['currency_code']);
$charge['amount'] = str_replace($currency['thousands_separator'], '', $tokens['Amount']);
$charge['amount'] = str_replace($currency['decimal_separator'], '.', $charge['amount']);
$charge['amount'] = $charge['amount'] * 100;
$charge['currency_code'] = $order->commerce_order_total['und'][0]['currency_code'];
commerce_sagepay_transaction($payment_method, $order, $charge, $tokens, $transaction_status, $remote_status);
return TRUE;
}