View source
<?php
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_constants');
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_utils');
function commerce_sagepay_menu() {
$items = array();
$items['admin/commerce/config/sagepay'] = array(
'title' => 'SagePay settings',
'description' => 'Configure the settings for the SagePay payment gateway integration.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_settings_form',
),
'access arguments' => array(
'administer sagepay',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/commerce_sagepay.admin.inc',
);
$items['commerce-sagepay-server/vps-callback/%/%'] = array(
'page callback' => 'commerce_sagepay_server_handle_callback',
'page arguments' => array(
2,
3,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'includes/commerce_sagepay_server.inc',
);
$items['commerce_sagepay/sagepay_waiting_page'] = array(
'page callback' => 'commerce_sagepay_server_waiting_page',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'includes/commerce_sagepay_server.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-release'] = array(
'title' => 'Release',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_release_form',
3,
5,
),
'access callback' => 'commerce_sagepay_release_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_release.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-refund'] = array(
'title' => 'Refund',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_refund_form',
3,
5,
),
'access callback' => 'commerce_sagepay_refund_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_refund.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-abort'] = array(
'title' => 'Abort',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_abort_form',
3,
5,
),
'access callback' => 'commerce_sagepay_abort_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_abort.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-void'] = array(
'title' => 'Void',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_void_form',
3,
5,
),
'access callback' => 'commerce_sagepay_void_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_void.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-authorise'] = array(
'title' => 'Authorise',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_authorise_form',
3,
5,
),
'access callback' => 'commerce_sagepay_authorise_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_authorise.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-cancel'] = array(
'title' => 'Cancel',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_cancel_form',
3,
5,
),
'access callback' => 'commerce_sagepay_cancel_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_cancel.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-repeat'] = array(
'title' => 'Repeat',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_repeat_form',
3,
5,
),
'access callback' => 'commerce_sagepay_repeat_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_repeat.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-repeat-defer'] = array(
'title' => 'Repeat Deferred',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_sagepay_repeat_deferred_form',
3,
5,
),
'access callback' => 'commerce_sagepay_repeat_deferred_access',
'access arguments' => array(
3,
5,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_sagepay_repeat_deferred.inc',
);
return $items;
}
function commerce_sagepay_permission() {
$permissions = array(
'administer sagepay' => array(
'title' => t('Administer SagePay'),
'description' => t('Configure SagePay Settings.'),
'restrict access' => TRUE,
),
'sagepay abort payment' => array(
'title' => t('Abort Payment'),
'description' => t('Allow users to Abort a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay authorise payment' => array(
'title' => t('Authorise Payment'),
'description' => t('Allow users to Authorise a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay cancel payment' => array(
'title' => t('Cancel Payment'),
'description' => t('Allow users to Cancel a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay refund payment' => array(
'title' => t('Refund Payment'),
'description' => t('Allow users to Refund a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay release payment' => array(
'title' => t('Release Payment'),
'description' => t('Allow users to Release a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay repeat payment' => array(
'title' => t('Repeat Payment'),
'description' => t('Allow users to Repeat a SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay repeat deferred payment' => array(
'title' => t('Repeat Deferred Payment'),
'description' => t('Allow users to Repeat a Deferred SagePay Payment'),
'restrict access' => TRUE,
),
'sagepay void payment' => array(
'title' => t('Void Payment'),
'description' => t('Allow users to Void a SagePay Payment'),
'restrict access' => TRUE,
),
);
return $permissions;
}
function commerce_sagepay_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['commerce_sagepay_form'] = array(
'base' => 'commerce_sagepay_form',
'title' => t('SagePay Form Integration'),
'display_title' => t('Credit or Debit card (Form)'),
'short_title' => t('SagePay'),
'description' => t('Integration with SagePay using Form method.'),
'active' => FALSE,
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
'callbacks' => array(),
'file' => 'includes/commerce_sagepay_form.inc',
);
$payment_methods['commerce_sagepay_server'] = array(
'title' => t('SagePay Server Integration'),
'short_title' => t('SagePay Server'),
'display_title' => t('Credit or Debit card (Server)'),
'description' => t('Integration with SagePay using Server method.'),
'active' => TRUE,
'offsite' => TRUE,
'terminal' => FALSE,
'callbacks' => array(),
'offsite_autoredirect' => FALSE,
'file' => 'includes/commerce_sagepay_server.inc',
);
$payment_methods['commerce_sagepay_direct'] = array(
'base' => 'commerce_sagepay_direct',
'title' => t('SagePay Direct Integration'),
'display_title' => t('Credit or Debit card (Direct)'),
'short_title' => t('SagePay'),
'description' => t('Integration with SagePay using Direct method.'),
'active' => FALSE,
'terminal' => TRUE,
'offsite' => FALSE,
'offsite_autoredirect' => FALSE,
'callbacks' => array(),
'file' => 'includes/commerce_sagepay_direct.inc',
);
return $payment_methods;
}
function commerce_sagepay_release_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if ($transaction->remote_status != SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED && $transaction->remote_status != SAGEPAY_REMOTE_STATUS_DEFERRED) {
return FALSE;
}
if (time() - $transaction->created > 86400 * 90) {
return FALSE;
}
if (!user_access('sagepay release payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_refund_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if (in_array($transaction->remote_status, array(
SAGEPAY_REMOTE_STATUS_FAIL,
SAGEPAY_REMOTE_STATUS_INVALID,
SAGEPAY_REMOTE_STATUS_STARTED,
SAGEPAY_REMOTE_STATUS_UNKNOWN,
SAGEPAY_REMOTE_STATUS_REFUNDED,
SAGEPAY_REMOTE_STATUS_REGISTERED,
SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED,
))) {
return FALSE;
}
if ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
return FALSE;
}
if (!user_access('sagepay refund payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_abort_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if ($transaction->remote_status != SAGEPAY_REMOTE_STATUS_DEFERRED && $transaction->remote_status != SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED) {
return FALSE;
}
if (!user_access('sagepay abort payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_void_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if ($transaction->remote_status != 'PAYMENT' && $transaction->remote_status != 'RELEASED' && $transaction->remote_status != 'DEFERRED') {
return FALSE;
}
if ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
return FALSE;
}
if (!user_access('sagepay void payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_cancel_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if (!in_array($transaction->remote_status, array(
SAGEPAY_REMOTE_STATUS_AUTHENTICATE,
SAGEPAY_REMOTE_STATUS_REGISTERED,
))) {
return FALSE;
}
if ($transaction->remote_status != 'AUTHENTICATE' && $transaction->remote_status != 'REGISTERED') {
return FALSE;
}
if (!user_access('sagepay cancel payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_authorise_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if ($transaction->remote_status != 'AUTHENTICATE' && $transaction->remote_status != 'REGISTERED') {
return FALSE;
}
if (time() - $transaction->created > 86400 * 90) {
return FALSE;
}
if (!user_access('sagepay authorise payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_repeat_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if (in_array($transaction->remote_status, array(
SAGEPAY_REMOTE_STATUS_FAIL,
SAGEPAY_REMOTE_STATUS_INVALID,
SAGEPAY_REMOTE_STATUS_STARTED,
SAGEPAY_REMOTE_STATUS_UNKNOWN,
SAGEPAY_REMOTE_STATUS_REFUNDED,
SAGEPAY_REMOTE_STATUS_REGISTERED,
))) {
return FALSE;
}
if ($transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
return FALSE;
}
if (!user_access('sagepay repeat payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_repeat_deferred_access($order, $transaction) {
global $user;
if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
return FALSE;
}
if (in_array($transaction->remote_status, array(
SAGEPAY_REMOTE_STATUS_FAIL,
SAGEPAY_REMOTE_STATUS_INVALID,
SAGEPAY_REMOTE_STATUS_STARTED,
SAGEPAY_REMOTE_STATUS_UNKNOWN,
SAGEPAY_REMOTE_STATUS_REFUNDED,
SAGEPAY_REMOTE_STATUS_REGISTERED,
))) {
return FALSE;
}
if ($transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
return FALSE;
}
if (!user_access('sagepay repeat deferred payment', $user)) {
return FALSE;
}
return commerce_payment_transaction_access('update', $order, $user);
}
function commerce_sagepay_page_build($page) {
$url = explode('/', current_path());
if (count($url) > 2) {
if ($url[0] == 'checkout') {
drupal_add_js(drupal_get_path('module', 'commerce_sagepay') . '/js/commerce_sagepay_server_iframe.js', array(
'type' => 'file',
'scope' => 'header',
'weight' => -100,
));
}
}
}
function commerce_sagepay_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'commerce_checkout_form_') === 0) {
if (!isset($form['commerce_payment'])) {
return;
}
$instance_id = $form['commerce_payment']['payment_method']['#default_value'];
$payment_method = commerce_payment_method_instance_load($instance_id);
if (strpos($payment_method['instance_id'], 'sagepay_direct') > 0) {
drupal_add_css(drupal_get_path('module', 'commerce_sagepay') . '/css/sagepay_direct.css');
if (commerce_checkout_page_load(substr($form_id, 23))) {
if (!empty($form['commerce_payment'])) {
$enabled_cards = _commerce_sagepay_populate_card_names(variable_get(SAGEPAY_SETTING_ENABLED_CARDS));
if (empty($enabled_cards)) {
$enabled_cards = _commerce_sagepay_all_card_names();
}
$card_images = theme('commerce_sagepay_card_logos', array(
'enabled_cards' => $enabled_cards,
));
$payment_details = $form['commerce_payment']['payment_details'];
if (array_key_exists('credit_card', $payment_details)) {
array_unshift($form['commerce_payment']['payment_details']['credit_card'], array(
'logos' => array(
'#markup' => $card_images,
),
));
$form['commerce_payment']['payment_details']['credit_card']['type']['#options'] = $enabled_cards;
$empty_option = array(
0 => '-',
);
$start_months = $form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'];
$start_years = $form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'];
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'] = $empty_option + $start_months;
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#default_value'] = 0;
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#required'] = FALSE;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'] = $empty_option + $start_years;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#default_value'] = 0;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#required'] = FALSE;
}
}
}
}
}
}
function commerce_sagepay_theme($existing, $type, $theme, $path) {
return array(
'commerce_sagepay_card_logos' => array(
'variables' => array(
'enabled_cards' => array(),
),
),
'commerce_sagepay_card_logo' => array(
'variables' => array(
'card' => NULL,
'name' => NULL,
'img' => NULL,
'href' => NULL,
),
),
);
}
function theme_commerce_sagepay_card_logos($variables) {
if (!isset($variables['enabled_cards'])) {
return '<div />';
}
$output = '<div class="card_type_images">';
foreach ($variables['enabled_cards'] as $card => $name) {
if ($name != '0') {
$output .= theme('commerce_sagepay_card_logo', array(
'card' => $card,
));
$output .= ' ';
}
}
$output .= '</div>';
return $output;
}
function theme_commerce_sagepay_card_logo($variables) {
$output = '';
if (!empty($variables['card'])) {
if (!empty($variables['img'])) {
$src = $variables['img'];
}
else {
$path = '/' . drupal_get_path('module', 'commerce_sagepay');
$src = $path . '/img/' . $variables['card'] . '.png';
}
$img = '<img src="' . $src . '" />';
if (!empty($variables['href'])) {
$output .= l($img, $variables['href'], array(
'html' => TRUE,
));
}
else {
$output .= $img;
}
}
return $output;
}
function commerce_sagepay_enable() {
drupal_set_message(t('Sage Pay installation complete, please configure your account in the <a href="/admin/commerce/config/sagepay">settings page</a>'));
}
function commerce_sagepay_sagepay_order_data_alter(&$query, $order) {
switch ($query['BillingCountry']) {
case 'SG':
$query['BillingCity'] = 'Singapore';
break;
case 'GI':
$query['BillingCity'] = 'Gibraltar';
break;
case 'HK':
$query['BillingCity'] = 'Hong Kong';
break;
case 'NR':
$query['BillingCity'] = 'Nauru';
break;
case 'SM':
$query['BillingCity'] = 'San Marino';
break;
case 'AE':
$query['BillingCity'] = 'United Arab Emirates';
break;
}
switch ($query['DeliveryCountry']) {
case 'SG':
$query['DeliveryCity'] = 'Singapore';
break;
case 'GI':
$query['DeliveryCity'] = 'Gibraltar';
break;
case 'HK':
$query['DeliveryCity'] = 'Hong Kong';
break;
case 'NR':
$query['DeliveryCity'] = 'Nauru';
break;
case 'SM':
$query['DeliveryCity'] = 'San Marino';
break;
case 'AE':
$query['DeliveryCity'] = 'United Arab Emirates';
break;
}
if (empty($query['BillingSurname'])) {
$query['BillingSurname'] = '.';
}
if (empty($query['DeliverySurname'])) {
$query['DeliverySurname'] = '.';
}
}