| 1 |
<?php |
<?php |
| 2 |
// $Id: uc_paygate.module,v 1.1 2008/09/12 16:28:57 maxheadroom Exp $ |
// $Id: uc_paygate.module,v 1.2.2.2 2009/10/07 14:17:31 maxheadroom Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Integrates paygate.co.za's redirected payment service. |
* Integrates paygate.co.za's redirected payment service. |
| 7 |
* |
* |
| 8 |
* Development by Web-n-things. |
* Development by Web-n-things. |
| 9 |
|
* Updated/Ported by Jonathan Wagener - Amoebasys (www.amoebasys.com). |
| 10 |
|
* Core: Drupal 6 |
| 11 |
*/ |
*/ |
| 12 |
|
|
| 13 |
/******************************************************************************* |
/******************************************************************************* |
| 17 |
/** |
/** |
| 18 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 19 |
*/ |
*/ |
| 20 |
function uc_paygate_menu($may_cache) { |
function uc_paygate_menu() { |
| 21 |
if ($may_cache) { |
$items['cart/paygate/complete'] = array( |
| 22 |
$items[] = array( |
'title' => 'PayGate transaction result', |
| 23 |
'path' => 'cart/paygate/complete', |
'page callback' => 'uc_paygate_complete', |
| 24 |
'title' => t('PayGate transaction result'), |
'access arguments' => 'uc_paygate_completion_access', |
|
'callback' => 'uc_paygate_complete', |
|
|
'access' => user_access('access content'), |
|
| 25 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 26 |
); |
); |
|
} |
|
| 27 |
|
|
| 28 |
return $items; |
return $items; |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
|
function uc_paygate_completion_access() { |
| 32 |
|
return TRUE; |
| 33 |
|
} |
| 34 |
/** |
/** |
| 35 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 36 |
*/ |
*/ |
| 37 |
function uc_paygate_form_alter($form_id, &$form) { |
function uc_paygate_form_alter(&$form, $form_state, $form_id) { |
| 38 |
if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) { |
// normally a switch is used because you may want to alter more than |
| 39 |
$order = uc_order_load($order_id); |
// one form and it is easy to add a new case for each form. |
| 40 |
|
switch ($form_id) { |
| 41 |
if ($order->payment_method == 'paygate') { |
// this is our form_id. |
| 42 |
unset($form['submit']); |
case 'uc_cart_checkout_review_form': |
| 43 |
$form['#prefix'] = '<table style="display: inline; padding-top: 1em;"><tr><td>'; |
if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) { |
| 44 |
$form['#suffix'] = '</td><td>'. drupal_get_form('uc_paygate_form', $order) .'</td></tr></table>'; |
$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 |
|
|
| 67 |
$title .= '<br /><img src="'. $path .'/paygate_small.gif" style="position: relative; left: 2.5em;">'; |
$title .= '<br /><img src="'. $path .'/paygate_small.gif" style="position: relative; left: 2.5em;">'; |
| 68 |
|
|
| 69 |
$methods[] = array( |
$methods[] = array( |
| 70 |
'id' => 'paygate', |
'id' => 'paygate', |
| 71 |
'name' => t('PayGate'), |
'name' => 'PayGate', |
| 72 |
'title' => $title, |
'title' => $title, |
| 73 |
'desc' => t('Redirect to PayGate for payment.'), |
'desc' => 'Redirect to PayGate for payment.', |
| 74 |
'callback' => 'uc_payment_method_paygate', |
'callback' => 'uc_payment_method_paygate', |
| 75 |
'weight' => 3, |
'weight' => 3, |
| 76 |
'checkout' => TRUE, |
'checkout' => TRUE, |
| 77 |
'no_gateway' => TRUE, |
'no_gateway' => TRUE, |
| 78 |
); |
); |
| 79 |
|
|
| 80 |
return $methods; |
return $methods; |
| 141 |
$form['paygate_settings']['paygate_return_url'] = array( |
$form['paygate_settings']['paygate_return_url'] = array( |
| 142 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 143 |
'#title' => t('Return URL'), |
'#title' => t('Return URL'), |
| 144 |
'#default_value' => variable_get('paygate_return_url', url('cart/paygate/complete/'. uc_cart_get_id(), NULL, NULL, TRUE)), |
'#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.'), |
'#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( |
$form['paygate_settings']['uc_paygate_method_title'] = array( |
| 176 |
} |
} |
| 177 |
|
|
| 178 |
// Form to build the submission to paygate.co.za |
// Form to build the submission to paygate.co.za |
| 179 |
function uc_paygate_form($order) { |
function uc_paygate_form($form_state, $order) { |
| 180 |
global $user; |
global $user; |
| 181 |
|
|
| 182 |
if (variable_get('uc_currency_code', 'ZAR') != 'ZAR'){ |
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'); |
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; |
return; |
| 185 |
} |
} |
| 186 |
|
|
| 187 |
if (variable_get(paygate_transaction_mode,'') == "Production"){ |
if (variable_get('paygate_transaction_mode', '') == "Production") { |
| 188 |
$PAYGATE_ID=variable_get('paygate_id', ''); |
$PAYGATE_ID=variable_get('paygate_id', ''); |
| 189 |
$checksum_key = variable_get('paygate_checksum_key', ''); |
$checksum_key = variable_get('paygate_checksum_key', ''); |
| 190 |
}else{ |
} |
| 191 |
|
else { |
| 192 |
$PAYGATE_ID=variable_get('paygate_test_id', ''); |
$PAYGATE_ID=variable_get('paygate_test_id', ''); |
| 193 |
$checksum_key = variable_get('paygate_checksum_test_key', ''); |
$checksum_key = variable_get('paygate_checksum_test_key', ''); |
| 194 |
} |
} |
| 196 |
$REFERENCE = $order->order_id; |
$REFERENCE = $order->order_id; |
| 197 |
$AMOUNT = ($order->order_total)*100; //Paygate requires amount to be in cents |
$AMOUNT = ($order->order_total)*100; //Paygate requires amount to be in cents |
| 198 |
$CURRENCY = 'ZAR'; |
$CURRENCY = 'ZAR'; |
| 199 |
$RETURN_URL = url('cart/paygate/complete/', NULL, NULL, TRUE); |
$RETURN_URL = variable_get('paygate_return_url', url('cart/paygate/complete', array('absolute' => TRUE))); |
| 200 |
$TRANSACTION_DATE = date('Y-m-d H:i'); |
$TRANSACTION_DATE = date('Y-m-d H:i'); |
| 201 |
$EMAIL = substr($order->primary_email, 0, 64); |
$EMAIL = substr($order->primary_email, 0, 64); |
| 202 |
|
|
| 203 |
$checksum_data = $PAYGATE_ID."|".$REFERENCE."|".$AMOUNT."|".$CURRENCY."|".$RETURN_URL."|".$TRANSACTION_DATE."|".$EMAIL."|".$checksum_key; |
$checksum_data = $PAYGATE_ID ."|". $REFERENCE ."|". $AMOUNT ."|". $CURRENCY ."|". $RETURN_URL ."|". $TRANSACTION_DATE ."|". $EMAIL ."|". $checksum_key; |
| 204 |
$CHECKSUM = md5($checksum_data); |
$CHECKSUM = md5($checksum_data); |
| 205 |
|
|
| 206 |
$data = array( |
$data = array( |
| 207 |
'PAYGATE_ID'=>$PAYGATE_ID, |
'PAYGATE_ID' => $PAYGATE_ID, |
| 208 |
'REFERENCE'=>$REFERENCE, |
'REFERENCE' => $REFERENCE, |
| 209 |
'AMOUNT'=>$AMOUNT, |
'AMOUNT' => $AMOUNT, |
| 210 |
'CURRENCY'=>$CURRENCY, |
'CURRENCY' => $CURRENCY, |
| 211 |
'RETURN_URL'=>$RETURN_URL, |
'RETURN_URL' => $RETURN_URL, |
| 212 |
'TRANSACTION_DATE'=>$TRANSACTION_DATE, |
'TRANSACTION_DATE' => $TRANSACTION_DATE, |
| 213 |
'EMAIL'=>$EMAIL, |
'EMAIL' => $EMAIL, |
| 214 |
'CHECKSUM'=>$CHECKSUM |
'CHECKSUM' => $CHECKSUM |
| 215 |
); |
); |
| 216 |
|
|
| 217 |
$form['#action'] = 'https://www.paygate.co.za/paywebv2/process.trans'; |
$form['#action'] = 'https://www.paygate.co.za/paywebv2/process.trans'; |
| 224 |
'#type' => 'submit', |
'#type' => 'submit', |
| 225 |
'#value' => variable_get('uc_paygate_checkout_button', t('Submit Order')), |
'#value' => variable_get('uc_paygate_checkout_button', t('Submit Order')), |
| 226 |
); |
); |
|
|
|
| 227 |
return $form; |
return $form; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
function uc_paygate_complete($cart_id = 0) { |
function uc_paygate_complete($cart_id = 0) { |
| 231 |
if (!$_POST){ |
if (!$_POST) { |
| 232 |
return; |
return; |
| 233 |
} |
} |
| 234 |
if (variable_get(paygate_transaction_mode,'') == "Production"){ |
if (variable_get(paygate_transaction_mode, '') == "Production") { |
| 235 |
$checksum_key = variable_get('paygate_checksum_key', ''); |
$checksum_key = variable_get('paygate_checksum_key', ''); |
| 236 |
}else{ |
} |
| 237 |
|
else { |
| 238 |
$checksum_key = variable_get('paygate_checksum_test_key', ''); |
$checksum_key = variable_get('paygate_checksum_test_key', ''); |
| 239 |
} |
} |
| 240 |
$s_front = url(); |
$s_front = url(); |
| 250 |
$CHECKSUM=$_POST['CHECKSUM']; |
$CHECKSUM=$_POST['CHECKSUM']; |
| 251 |
$RISK_INDICATOR=$_POST['RISK_INDICATOR']; |
$RISK_INDICATOR=$_POST['RISK_INDICATOR']; |
| 252 |
|
|
| 253 |
$checksum_source = $PAYGATE_ID."|".$REFERENCE."|".$TRANSACTION_STATUS."|".$RESULT_CODE."|".$AUTH_CODE."|".$AMOUNT."|".$RESULT_DESC."|".$TRANSACTION_ID."|"; |
$checksum_source = $PAYGATE_ID ."|". $REFERENCE ."|". $TRANSACTION_STATUS ."|". $RESULT_CODE ."|". $AUTH_CODE ."|". $AMOUNT ."|". $RESULT_DESC ."|". $TRANSACTION_ID ."|"; |
| 254 |
if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR."|"; |
if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR ."|"; |
| 255 |
$checksum_source .= $checksum_key; |
$checksum_source .= $checksum_key; |
| 256 |
|
|
| 257 |
$test_checksum = md5($checksum_source); |
$test_checksum = md5($checksum_source); |
| 258 |
|
|
| 259 |
if ( $test_checksum != $CHECKSUM){ |
if ( $test_checksum != $CHECKSUM) { |
| 260 |
watchdog('Paygate', t('Checksum failed for order !order_id.', array('!order_id' => check_plain($TRANSACTION_ID)))); |
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'); |
drupal_set_message('Checksum error! Contact Site Owner.', 'error'); |
| 262 |
} |
} |
| 268 |
|
|
| 269 |
switch ($TRANSACTION_STATUS) { |
switch ($TRANSACTION_STATUS) { |
| 270 |
case '0': |
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'); |
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'); |
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>'; |
$output = '<br /><a href="'. $s_front .'">Click to return to the front page.</a>'; |
| 276 |
break; |
break; |
| 277 |
|
|
| 278 |
case '1': |
case '1': |
| 285 |
break; |
break; |
| 286 |
|
|
| 287 |
case '2': |
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'); |
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'); |
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>'; |
$output = '<br /><a href="'. $s_front .'">Click to return to the front page.</a>'; |
| 293 |
break; |
break; |
| 294 |
|
|
| 295 |
default: |
default: |