<?php
// Script to create a signature
define('API_BASE_URL', 'http://api.allopass.com/rest');
define('API_KEY', '3e2963bd582b40a19b00016d4bc0c84e');			// HugAvenue
define('API_SECRET_KEY', '192ca2eb7836e7ad49ef7b371fcd539c');	// HugAvenue
define('API_HASH_FUNCTION', 'sha1');
date_default_timezone_set('UTC');

// STEPS 1 and 2: Construction of query parameters
$queryParameters = array(
'subscriber_reference' 	=> $_REQUEST['ref'],
'api_key' 				=> API_KEY,
'api_hash' 				=> API_HASH_FUNCTION,
'api_ts' 				=> time(),
'format'				=> 'xml'
);

// STEP 3 : Sort parameters by ascending alphabetical order by name of parameter
ksort($queryParameters);

/* STEP 4
* Prepare a string to hash
* with the hash function "API_HASH_FUNCTION"
*/
$stringToHash = '';
foreach ($queryParameters as $parameter =>$value) {
$stringToHash .= $parameter . (is_array($value) ? implode('', $value) : $value);
}
$stringToHash .= API_SECRET_KEY;

// STEP 5: Creation of signature
$signature = hash(API_HASH_FUNCTION, $stringToHash);

// STEP 6 : Generating URL
$queryParameters['api_sig'] = $signature;
$url = API_BASE_URL . '/subscription/cancellation?'. http_build_query($queryParameters);
//echo "\n" . $url . "\n";
header('Location: ' . $url);
exit();