| 1 |
<?php
|
| 2 |
// $Id: uc_paygate.module,v 1.3 2009/10/07 14:34:17 maxheadroom Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Integrates paygate.co.za's redirected payment service.
|
| 7 |
*
|
| 8 |
* Development by Web-n-things.
|
| 9 |
* Updated/Ported by Jonathan Wagener - Amoebasys (www.amoebasys.com).
|
| 10 |
* Core: Drupal 6
|
| 11 |
*/
|
| 12 |
|
| 13 |
/*******************************************************************************
|
| 14 |
* Hook Functions (Drupal)
|
| 15 |
******************************************************************************/
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_menu().
|
| 19 |
*/
|
| 20 |
function uc_paygate_menu() {
|
| 21 |
$items['cart/paygate/complete'] = array(
|
| 22 |
'title' => 'PayGate transaction result',
|
| 23 |
'page callback' => 'uc_paygate_complete',
|
| 24 |
'access arguments' => array('uc_paygate_completion_access'),
|
| 25 |
'type' => MENU_CALLBACK,
|
| 26 |
);
|
| 27 |
|
| 28 |
return $items;
|
| 29 |
}
|
| 30 |
|
| 31 |
function uc_paygate_completion_access() {
|
| 32 |
return TRUE;
|
| 33 |
}
|
| 34 |
/**
|
| 35 |
* Implementation of hook_form_alter().
|
| 36 |
*/
|
| 37 |
function uc_paygate_form_alter(&$form, $form_state, $form_id) {
|
| 38 |
// normally a switch is used because you may want to alter more than
|
| 39 |
// one form and it is easy to add a new case for each form.
|
| 40 |
switch ($form_id) {
|
| 41 |
// this is our form_id.
|
| 42 |
case 'uc_cart_checkout_review_form':
|
| 43 |
if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
|
| 44 |
$order = uc_order_load($order_id);
|
| 45 |
|
| 46 |
if ($order->payment_method == 'paygate') {
|
| 47 |
unset($form['submit']);
|
| 48 |
$form['#prefix'] = '<table style="display: inline; padding-top: 1em;"><tr><td>';
|
| 49 |
$form['#suffix'] = '</td><td>'. drupal_get_form('uc_paygate_form', $order) .'</td></tr></table>';
|
| 50 |
}
|
| 51 |
}
|
| 52 |
break;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
/*******************************************************************************
|
| 58 |
* Hook Functions (Ubercart)
|
| 59 |
******************************************************************************/
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Implementation of hook_payment_method().
|
| 63 |
*/
|
| 64 |
function uc_paygate_payment_method() {
|
| 65 |
$path = base_path() . drupal_get_path('module', 'uc_paygate');
|
| 66 |
$title = variable_get('uc_paygate_method_title', t('Secure payment using PayGate:'));
|
| 67 |
$title .= '<br /><img src="'. $path .'/paygate_small.gif" style="position: relative; left: 2.5em;">';
|
| 68 |
|
| 69 |
$methods[] = array(
|
| 70 |
'id' => 'paygate',
|
| 71 |
'name' => 'PayGate',
|
| 72 |
'title' => $title,
|
| 73 |
'desc' => 'Redirect to PayGate for payment.',
|
| 74 |
'callback' => 'uc_payment_method_paygate',
|
| 75 |
'weight' => 3,
|
| 76 |
'checkout' => TRUE,
|
| 77 |
'no_gateway' => TRUE,
|
| 78 |
);
|
| 79 |
|
| 80 |
return $methods;
|
| 81 |
}
|
| 82 |
|
| 83 |
|
| 84 |
/*******************************************************************************
|
| 85 |
* Callback Functions, Forms, and Tables
|
| 86 |
******************************************************************************/
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Callback for paygate payment method settings.
|
| 90 |
*/
|
| 91 |
function uc_payment_method_paygate($op, &$arg1) {
|
| 92 |
switch ($op) {
|
| 93 |
case 'cart-details':
|
| 94 |
|
| 95 |
return;
|
| 96 |
|
| 97 |
case 'cart-process':
|
| 98 |
$_SESSION['pay_method'] = $_POST['pay_method'];
|
| 99 |
return;
|
| 100 |
|
| 101 |
case 'settings':
|
| 102 |
$form['paygate_settings'] = array(
|
| 103 |
'#type' => 'fieldset',
|
| 104 |
'#title' => t('Paygate settings'),
|
| 105 |
);
|
| 106 |
$form['paygate_settings']['paygate_id'] = array(
|
| 107 |
'#type' => 'textfield',
|
| 108 |
'#title' => t('PayGate ID'),
|
| 109 |
'#default_value' => variable_get('paygate_id', ''),
|
| 110 |
'#description' => t('The login ID used for the PayGate service.'),
|
| 111 |
);
|
| 112 |
$form['paygate_settings']['paygate_checksum_key'] = array(
|
| 113 |
'#type' => 'textfield',
|
| 114 |
'#title' => t('Checksum key'),
|
| 115 |
'#default_value' => variable_get('paygate_checksum_key', ''),
|
| 116 |
'#description' => t('Your PayGate key for sending encrypting data.'),
|
| 117 |
);
|
| 118 |
$form['paygate_settings']['paygate_test_id'] = array(
|
| 119 |
'#type' => 'textfield',
|
| 120 |
'#title' => t('Test PayGate ID'),
|
| 121 |
'#default_value' => variable_get('paygate_test_id', '10011013800'),
|
| 122 |
'#description' => t('The login ID used for the Paygate service in Test Mode.'),
|
| 123 |
);
|
| 124 |
$form['paygate_settings']['paygate_checksum_test_key'] = array(
|
| 125 |
'#type' => 'textfield',
|
| 126 |
'#title' => t('Test Checksum key'),
|
| 127 |
'#default_value' => variable_get('paygate_checksum_test_key', 'secret'),
|
| 128 |
'#description' => t('Your PayGate key for sending encrypting data in Test Mode.'),
|
| 129 |
);
|
| 130 |
$form['paygate_settings']['paygate_transaction_mode'] = array(
|
| 131 |
'#type' => 'select',
|
| 132 |
'#title' => t('Transaction mode'),
|
| 133 |
'#description' => t('Transaction mode used for processing orders. Be sure that you do your testing in Test mode.'),
|
| 134 |
'#options' => array(
|
| 135 |
'production' => t('Production'),
|
| 136 |
'test' => t('Test'),
|
| 137 |
),
|
| 138 |
'#default_value' => variable_get('paygate_transaction_mode', 'test'),
|
| 139 |
);
|
| 140 |
|
| 141 |
$form['paygate_settings']['paygate_return_url'] = array(
|
| 142 |
'#type' => 'textfield',
|
| 143 |
'#title' => t('Return URL'),
|
| 144 |
'#default_value' => variable_get('paygate_return_url', url('cart/paygate/complete', array('absolute' => TRUE))),
|
| 145 |
'#description' => t('The URL to where PayGate must return after a transaction. This normally not needed to be changed'),
|
| 146 |
);
|
| 147 |
|
| 148 |
$form['paygate_settings']['uc_paygate_method_title'] = array(
|
| 149 |
'#type' => 'textfield',
|
| 150 |
'#title' => t('Payment method title'),
|
| 151 |
'#default_value' => variable_get('uc_paygate_method_title', t('Secure payment using PayGate:')),
|
| 152 |
);
|
| 153 |
$form['paygate_settings']['uc_paygate_method_title_icons'] = array(
|
| 154 |
'#type' => 'checkbox',
|
| 155 |
'#title' => t('Show icons beside the payment method title.'),
|
| 156 |
'#default_value' => variable_get('uc_paygate_method_title_icons', TRUE),
|
| 157 |
);
|
| 158 |
$form['paygate_settings']['uc_paygate_checkout_button'] = array(
|
| 159 |
'#type' => 'textfield',
|
| 160 |
'#title' => t('Order review submit button text'),
|
| 161 |
'#description' => t('Provide PayGate specific text for the submit button on the order review page.'),
|
| 162 |
'#default_value' => variable_get('uc_paygate_checkout_button', t('Submit Order')),
|
| 163 |
);
|
| 164 |
|
| 165 |
$form['paygate_settings']['uc_checkout_review_instructions'] = array(
|
| 166 |
'#type' => 'textarea',
|
| 167 |
'#title' => t('Checkout review instructions'),
|
| 168 |
'#description' => t('Provide instructions for customers at the top of the checkout review screen.'),
|
| 169 |
'#default_value' => variable_get('uc_checkout_review_instructions', uc_get_message('review_instructions')),
|
| 170 |
'#rows' => 3,
|
| 171 |
);
|
| 172 |
$form['uc_checkout_review_instructions_format'] = filter_form(variable_get('uc_checkout_review_instructions_format', 3), NULL, array('uc_checkout_review_instructions_format'));
|
| 173 |
|
| 174 |
return $form;
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
// Form to build the submission to paygate.co.za
|
| 179 |
function uc_paygate_form($form_state, $order) {
|
| 180 |
global $user;
|
| 181 |
|
| 182 |
if (variable_get('uc_currency_code', 'ZAR') != 'ZAR') {
|
| 183 |
drupal_set_message(t('PayGate can only use South African Rand (ZAR) as currency. Please contact the site owner about this problem at !site_mail', array('!site_mail' => variable_get('site_mail', ''))), 'error');
|
| 184 |
return;
|
| 185 |
}
|
| 186 |
|
| 187 |
if (variable_get('paygate_transaction_mode', '') == "Production") {
|
| 188 |
$PAYGATE_ID=variable_get('paygate_id', '');
|
| 189 |
$checksum_key = variable_get('paygate_checksum_key', '');
|
| 190 |
}
|
| 191 |
else {
|
| 192 |
$PAYGATE_ID=variable_get('paygate_test_id', '');
|
| 193 |
$checksum_key = variable_get('paygate_checksum_test_key', '');
|
| 194 |
}
|
| 195 |
|
| 196 |
$REFERENCE = $order->order_id;
|
| 197 |
$AMOUNT = ($order->order_total)*100; //Paygate requires amount to be in cents
|
| 198 |
$CURRENCY = 'ZAR';
|
| 199 |
$RETURN_URL = variable_get('paygate_return_url', url('cart/paygate/complete', array('absolute' => TRUE)));
|
| 200 |
$TRANSACTION_DATE = date('Y-m-d H:i');
|
| 201 |
$EMAIL = substr($order->primary_email, 0, 64);
|
| 202 |
|
| 203 |
$checksum_data = $PAYGATE_ID ."|". $REFERENCE ."|". $AMOUNT ."|". $CURRENCY ."|". $RETURN_URL ."|". $TRANSACTION_DATE ."|". $EMAIL ."|". $checksum_key;
|
| 204 |
$CHECKSUM = md5($checksum_data);
|
| 205 |
|
| 206 |
$data = array(
|
| 207 |
'PAYGATE_ID' => $PAYGATE_ID,
|
| 208 |
'REFERENCE' => $REFERENCE,
|
| 209 |
'AMOUNT' => $AMOUNT,
|
| 210 |
'CURRENCY' => $CURRENCY,
|
| 211 |
'RETURN_URL' => $RETURN_URL,
|
| 212 |
'TRANSACTION_DATE' => $TRANSACTION_DATE,
|
| 213 |
'EMAIL' => $EMAIL,
|
| 214 |
'CHECKSUM' => $CHECKSUM
|
| 215 |
);
|
| 216 |
|
| 217 |
$form['#action'] = 'https://www.paygate.co.za/paywebv2/process.trans';
|
| 218 |
|
| 219 |
foreach ($data as $name => $value) {
|
| 220 |
$form[$name] = array('#type' => 'hidden', '#value' => $value);
|
| 221 |
}
|
| 222 |
|
| 223 |
$form['submit'] = array(
|
| 224 |
'#type' => 'submit',
|
| 225 |
'#value' => variable_get('uc_paygate_checkout_button', t('Submit Order')),
|
| 226 |
);
|
| 227 |
return $form;
|
| 228 |
}
|
| 229 |
|
| 230 |
function uc_paygate_complete($cart_id = 0) {
|
| 231 |
if (!$_POST) {
|
| 232 |
return;
|
| 233 |
}
|
| 234 |
if (variable_get(paygate_transaction_mode, '') == "Production") {
|
| 235 |
$checksum_key = variable_get('paygate_checksum_key', '');
|
| 236 |
}
|
| 237 |
else {
|
| 238 |
$checksum_key = variable_get('paygate_checksum_test_key', '');
|
| 239 |
}
|
| 240 |
$s_front = url();
|
| 241 |
|
| 242 |
$PAYGATE_ID=$_POST['PAYGATE_ID'];
|
| 243 |
$REFERENCE=$_POST['REFERENCE'];
|
| 244 |
$AMOUNT=$_POST['AMOUNT'];
|
| 245 |
$TRANSACTION_STATUS=$_POST['TRANSACTION_STATUS'];
|
| 246 |
$RESULT_CODE=$_POST['RESULT_CODE'];
|
| 247 |
$RESULT_DESC=$_POST['RESULT_DESC'];
|
| 248 |
$AUTH_CODE=$_POST['AUTH_CODE'];
|
| 249 |
$TRANSACTION_ID=$_POST['TRANSACTION_ID'];
|
| 250 |
$CHECKSUM=$_POST['CHECKSUM'];
|
| 251 |
$RISK_INDICATOR=$_POST['RISK_INDICATOR'];
|
| 252 |
|
| 253 |
$checksum_source = $PAYGATE_ID ."|". $REFERENCE ."|". $TRANSACTION_STATUS ."|". $RESULT_CODE ."|". $AUTH_CODE ."|". $AMOUNT ."|". $RESULT_DESC ."|". $TRANSACTION_ID ."|";
|
| 254 |
if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR ."|";
|
| 255 |
$checksum_source .= $checksum_key;
|
| 256 |
|
| 257 |
$test_checksum = md5($checksum_source);
|
| 258 |
|
| 259 |
if ( $test_checksum != $CHECKSUM) {
|
| 260 |
watchdog('Paygate', t('Checksum failed for order !order_id.', array('!order_id' => check_plain($TRANSACTION_ID))));
|
| 261 |
drupal_set_message('Checksum error! Contact Site Owner.', 'error');
|
| 262 |
}
|
| 263 |
|
| 264 |
$page = variable_get('uc_cart_checkout_complete_page', '');
|
| 265 |
if (!empty($page)) {
|
| 266 |
drupal_goto(variable_get('uc_cart_checkout_complete_page', ''));
|
| 267 |
}
|
| 268 |
|
| 269 |
switch ($TRANSACTION_STATUS) {
|
| 270 |
case '0':
|
| 271 |
drupal_set_message(t('Transaction can not be completed due to an unexpected error: <br> Order no.: !reference <br>Returned result: <strong> !result_desc. </strong><br /> Please try again or contact the site owner at !site_mail.', array('!result_desc' => check_plain($RESULT_DESC), '!reference' => check_plain($REFERENCE), '!site_mail' => variable_get('site_mail', ''))), 'error');
|
| 272 |
|
| 273 |
watchdog('Paygate', t('Transaction could not be completed.<br> Transaction ID: !order_id <br> Order no.: !reference <br>Returned result: !result_desc.'), array('!order_id' => check_plain($TRANSACTION_ID), '!result_desc' => check_plain($RESULT_DESC), '!reference' => check_plain($REFERENCE)), 'WATCHDOG_WARNING');
|
| 274 |
|
| 275 |
$output = '<br /><a href="'. $s_front .'">Click to return to the front page.</a>';
|
| 276 |
break;
|
| 277 |
|
| 278 |
case '1':
|
| 279 |
$order = uc_order_load($REFERENCE);
|
| 280 |
$output = uc_cart_complete_sale($order, variable_get('uc_new_customer_login', FALSE));
|
| 281 |
uc_payment_enter($order->order_id, 'PayGate', $AMOUNT/100, 0, NULL, check_plain($RESULT_DESC));
|
| 282 |
// Add a comment to let sales team know this came in through the site.
|
| 283 |
uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin');
|
| 284 |
watchdog('Paygate', t('Receiving new order notification for order !order_id.', array('!order_id' => check_plain($TRANSACTION_ID))));
|
| 285 |
break;
|
| 286 |
|
| 287 |
case '2':
|
| 288 |
drupal_set_message(t('Transaction was not authorised: <br> Order no.: !reference <br>Returned result: <strong> !result_desc. </strong><br /> Please contact your banking institution for further details.'), array('!result_desc' => check_plain($RESULT_DESC), '!reference' => check_plain($REFERENCE), '!site_mail' => variable_get('site_mail', '')), 'error');
|
| 289 |
|
| 290 |
watchdog('Paygate', t('Transaction was not authorised.<br> Transaction ID: !order_id <br> Order no.: !reference <br> Returned result: !result_desc. <br>'), array('!order_id' => check_plain($TRANSACTION_ID), '!result_desc' => check_plain($RESULT_DESC), '!reference' => check_plain($REFERENCE), '!site_mail' => variable_get('site_mail', '')), 'WATCHDOG_WARNING');
|
| 291 |
|
| 292 |
$output = '<br /><a href="'. $s_front .'">Click to return to the front page.</a>';
|
| 293 |
break;
|
| 294 |
|
| 295 |
default:
|
| 296 |
break;
|
| 297 |
}
|
| 298 |
|
| 299 |
return $output;
|
| 300 |
|
| 301 |
}
|
| 302 |
|