VK_DEST: | ';
$op .= ' | ';
$op .= '';
$op .= 'VK_SND_ID: | ';
$op .= ' | ';
$op .= '
';
$op .= 'VK_PRIVKEY: | ';
$op .= ' | ';
$op .= '
';
$op .= 'VK_PASS: | ';
$op .= '';
$op .= ' |
';
$op .= 'VK_PUBKEY | ';
$op .= ' | ';
$op .= '
';
return $op;
}
function submit_banklinksampo() {
if ($_POST['banklinksampo_vk_dest'] != null) {
update_option('banklinksampo_vk_dest', $_POST['banklinksampo_vk_dest']);
}
if ($_POST['banklinksampo_vk_snd_id'] != null) {
update_option('banklinksampo_vk_snd_id', $_POST['banklinksampo_vk_snd_id']);
}
if ($_POST['banklinksampo_vk_privkey'] != null) {
update_option('banklinksampo_vk_privkey', $_POST['banklinksampo_vk_privkey']);
}
if ($_POST['banklinksampo_vk_pass'] != null) {
update_option('banklinksampo_vk_pass', $_POST['banklinksampo_vk_pass']);
}
if ($_POST['banklinksampo_vk_pubkey'] != null) {
update_option('banklinksampo_vk_pubkey', $_POST['banklinksampo_vk_pubkey']);
}
}
function gateway_banklinksampo($seperator, $sessionid) {
global $wpdb, $wpsc_cart;
$purchase_log = $wpdb->get_row(
"SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS.
"` WHERE `sessionid`= ".$sessionid." LIMIT 1"
,ARRAY_A) ;
$currency_code = $wpdb->get_results("SELECT `code` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`='".get_option('currency_type')."' LIMIT 1",ARRAY_A);
$local_currency_code = $currency_code[0]['code'];
$curr=new CURRENCYCONVERTER();
$decimal_places = 2;
$total_price = 0;
$return = get_option('home').'/validateSampoPayment.php';
$macFields = Array(
'VK_SERVICE' => '1002',
'VK_VERSION' => '008',
'VK_SND_ID' => get_option('banklinksampo_vk_snd_id'),
'VK_STAMP' => $sessionid,
'VK_AMOUNT' => round($wpsc_cart->total_price, 2),
'VK_CURR' => $local_currency_code,
'VK_REF' => generateRefNum($purchase_log['id']),
'VK_MSG' => 'Arve nr. '.$purchase_log['id'].' tasu',
'VK_RETURN' => $return,
);
$key = openssl_pkey_get_private (get_option('banklinksampo_vk_privkey'), get_option('banklinksampo_vk_pass'));
$signature = "";
$macString = generateMACString($macFields);
if (!openssl_sign ($macString, $signature, $key)) {
die ("Unable to generate signature");
}
$macFields['VK_MAC'] = base64_encode ($signature);
# Enter your code submit to the gateway...
$post = '';
$post .= '";
$post .= "";
echo $post;
}
if (!function_exists('generateMACString')) {
function generateMACString($macFields) {
$VK_variableOrder = Array(
1001 => Array(
'VK_SERVICE','VK_VERSION','VK_SND_ID',
'VK_STAMP','VK_AMOUNT','VK_CURR',
'VK_ACC','VK_NAME','VK_REF','VK_MSG'
),
1002 => Array(
'VK_SERVICE','VK_VERSION','VK_SND_ID',
'VK_STAMP','VK_AMOUNT','VK_CURR',
'VK_REF','VK_MSG'
),
1101 => Array(
'VK_SERVICE','VK_VERSION','VK_SND_ID',
'VK_REC_ID','VK_STAMP','VK_T_NO','VK_AMOUNT','VK_CURR',
'VK_REC_ACC','VK_REC_NAME','VK_SND_ACC','VK_SND_NAME',
'VK_REF','VK_MSG','VK_T_DATE'
),
1901 => Array(
'VK_SERVICE','VK_VERSION','VK_SND_ID',
'VK_REC_ID','VK_STAMP','VK_REF','VK_MSG'
),
);
$requestNum = $macFields['VK_SERVICE'];
$data = '';
foreach ((array)$VK_variableOrder[$requestNum] as $kaey) {
$v = $macFields[$kaey];
$data .= str_pad (strlen ($v), 3, '0', STR_PAD_LEFT) . $v;
}
return $data;
}
}
if (!function_exists('generateRefNum')) {
function generateRefNum($stamp) {
$chcs = array(7, 3, 1);
$sum = 0;
$pos = 0;
for ($i = 0; $i < strlen($stamp); $i++) {
$x = (int)(substr($stamp,strlen($stamp) - 1 - $i, 1));
$sum = $sum + ($x * $chcs[$pos]);
if ($pos == 2) {
$pos = 0;
} else {
$pos = $pos + 1;
}
}
$x = 10 - ($sum % 10);
if ($x != 10) {
$sum = $x;
} else {
$sum = 0;
}
return $stamp . $sum;
}
}
if (!function_exists('validateBanklinkPayment')) {
function validateBanklinkPayment($params, $pubkey) {
$orderId = $params['VK_STAMP'];
$result = array('orderNr' => $orderId, 'payment' => 'failed', 'auto' => false);
$vk_bank_id = $params['VK_SND_ID'];
$vk_service = $params['VK_SERVICE'];
$macFields = Array ();
foreach ((array)$params as $f => $v) {
if (substr ($f, 0, 3) == 'VK_') {
$macFields[$f] = $v;
}
}
$key = openssl_pkey_get_public($pubkey);
if (!openssl_verify (generateMACString($macFields), base64_decode($macFields['VK_MAC']), $key)) {
//invalid signature here
return $result;
} else {
//if signature correct then
if ($vk_service == '1101') {
//if payd, update order
$result['payment'] = 'success';
if ($params['VK_AUTO'] == 'Y') {
$result['auto'] = true;
}
} else if ($vk_service == '1901') {
//if not payd leave order intact
if ($params['VK_AUTO'] == 'Y') {
$result['auto'] = true;
}
$result['payment'] = 'cancelled';
}
return $result;
}
}
}
function validateSampoPayment() {
global $wpdb, $wpsc_cart;
if (isset($_GET['validateSampo'])) {
//processed tuleb panna 2 peale.
$validationResult = validateBanklinkPayment($_REQUEST, get_option('banklinksampo_vk_pubkey'));
if ($validationResult['payment'] == 'success') {
if ($validationResult['auto']) {
//in here the order should me marked payd
$purchase_log = $wpdb->get_row(
"SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS.
"` WHERE `sessionid`= ".$wpdb->escape($_REQUEST['VK_STAMP'])." LIMIT 1"
,ARRAY_A);
if (isset($purchase_log['processed']) && $purchase_log['processed'] ==1) {
//in here mark order payd
$wpdb->query("UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET
`processed` = '2',
`transactid` = '".$wpdb->escape($_REQUEST['VK_STAMP'])."',
`date` = '".time()."'
WHERE `sessionid` = ".$wpdb->escape($_REQUEST['VK_STAMP'])." LIMIT 1");
transaction_results($_REQUEST['VK_STAMP'], false, $_REQUEST['VK_STAMP']);
}
}
//and always tell the user, that everything is superb....
$trUrl = get_option('transact_url');
if (strpos($trUrl, "?") > 0) {
$trUrl .= "&sessionid=".$_REQUEST['VK_STAMP'];
} else {
$trUrl .= "?sessionid=".$_REQUEST['VK_STAMP'];
}
wp_redirect($trUrl);
die();
} else if ($validationResult['payment'] == 'cancelled') {
//in here guide the user nicely to the cart and go out again.
$log_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`='{$wpdb->escape($_REQUEST['VK_STAMP'])}' LIMIT 1");
if ($log_id > 0) {
$wpdb->query("DELETE FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id`='$log_id' LIMIT 1");
}
$trUrl = get_option('shopping_cart_url');
wp_redirect($trUrl);
die();
} else {
//wrong signature, send to front page
wp_redirect(get_option('home'));
die();
}
}
}
if (isset($_GET['validateSampo'])) {
add_action('init', 'validateSampoPayment');
}
?>