<?php 
    define('API_BASE_URL', 'http://api.allopass.com/rest');
    define('API_KEY', 'de571f4eff4ff32866399a53f4ad1d88');
    define('API_SECRET_KEY', 'f76279773be6c07c3946e3837bbf15a6');
    define('API_HASH_FUNCTION', 'sha1');
    date_default_timezone_set('UTC');
            
    // STEPS 1 and 2: Construction of query parameters
    $queryParameters = array(
    'site_id'	=>277650,
    'product_name' =>'Abonnement 1 mois',
    'amount' => 48,
    'price_mode' => 'price',
    'price_policy' => 'high-only',
    'reference_currency' => 'EUR',
    'data' => 'transid',
    'merchant_transaction_id' => '2606124',
    'api_key'	=> API_KEY,
    'api_hash'	=> API_HASH_FUNCTION,
    'api_ts'	=> time(),
    'forward_url' => 'http://www.hugavenue.com/payment_allopass/result',
    'error_url' => 'http://www.hugavenue.com/payment_allopass/error',
    'notification_url' => 'http://www.hugavenue.com/payment_allopass/push',
    'forward_target' => 'parent'
    //'format'	=> 'json'
    );
    // 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 . '/transaction/prepare?'. http_build_query($queryParameters);
    $url = API_BASE_URL . '/onetime/discrete-button';
    
    $sock = curl_init($url); 
    //curl_setopt_array($sock, array(CURLOPT_POSTFIELDS=>http_build_query($queryParameters),CURLOPT_POST=>true,CURLOPT_HEADER	=>true, CURLOPT_RETURNTRANSFER =>true, CURLOPT_FOLLOWLOCATION =>false, CURLOPT_CONNECTTIMEOUT =>10, CURLOPT_LOW_SPEED_TIME =>10, CURLOPT_TIMEOUT	=>10)); 
    curl_setopt_array($sock, array(
	CURLOPT_HEADER =>true,
	CURLOPT_RETURNTRANSFER =>true,
	CURLOPT_FOLLOWLOCATION =>false,
	CURLOPT_CONNECTTIMEOUT =>10,
	CURLOPT_LOW_SPEED_TIME =>10,
	CURLOPT_TIMEOUT =>10
	));
    $response = curl_exec($sock);
    if (0< ($curlErrno = curl_errno($sock))) 
    { 
        trigger_error("CURL Error ($curlErrno): " . curl_error($sock), E_USER_NOTICE);
        header('Location: /error/unavailable.php'); 
        exit();
    } 
    $httpStatusCode = curl_getinfo($sock, CURLINFO_HTTP_CODE); 
    $httpHeaderSize = curl_getinfo($sock, CURLINFO_HEADER_SIZE);
    curl_close($sock);
    // Read the API response returned in the $response variable by the curl_exec () function
    $responseHeaders = array(); 
    $rawHeaders	= substr($response, 0, $httpHeaderSize - 4);
    $responseBody = substr($response, $httpHeaderSize);
    print_r($responseBody);
    ?>