<?php
// Define endpoint
define('API_ENDPOINT', 'https://merchant.hipaywallet.com/api'); // HiPay PRO production platform
//define('API_ENDPOINT', 'https://test-merchant.hipaywallet.com/api'); // HiPay PRO test platform
$resource = API_ENDPOINT . '/user-account/is-available.json';

// Define credentials
define('API_USERNAME', '3203bd12d7b22789be639223204f6d1f');
define('API_PASSWORD', 'e9af26baa7bfc838ab5550f1882851d6');
$credentials = API_USERNAME . ':' . API_PASSWORD;


// Create query parameters
$queryParameters = array(
            'user_email'               	=> $_REQUEST['email'],
            'entity'                 	=> "wallet"
            );

//print_r($queryParameters);
// Create a new cURL resourse
$curl = curl_init();

// Define HTTP Headers
$header = array(
    'User-Agent: ' .$_SERVER['HTTP_USER_AGENT'],
    'Accept: application/json', // 'text/xml' 'application/json'
    'Accept-Language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'],
    'Accept-Charset:' .  $_SERVER['HTTP_ACCEPT_CHARSET']
);


// Create cURL options array
$options = array(
	CURLOPT_URL => $resource,
	CURLOPT_USERPWD => $credentials,
	CURLOPT_HTTPHEADER => $header,
	CURLOPT_RETURNTRANSFER =>true,
	CURLOPT_FAILONERROR => false,
	CURLOPT_HEADER =>false,
	CURLOPT_POST => true,
	CURLOPT_POSTFIELDS => http_build_query($queryParameters),
	CURLOPT_SSL_VERIFYPEER  => false,
);

foreach ($options as $option => $value) {
	curl_setopt($curl, $option, $value);
}

// Execute the given cURL session
if (false === ($result = curl_exec($curl))) {
	//throw new RuntimeException(curl_error($curl), curl_errno($curl));
	echo "<br/><strong>Error: </strong><br/>";
	var_dump(curl_error($curl), curl_errno($curl));
	exit();
}

echo "<br/><strong>Return: </strong><br/>" . $result . "<br/>";

// Clean the answer 
$status = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = json_decode($result);
var_dump($response);
if (floor($status/100) != 2) {
	//throw new RuntimeException($response->message, $response->code);
	var_dump($response->message, $response->code);
	exit();
}

// Close cURL
curl_close($curl);

// Get payment page URL
//$url = $response->forwardUrl;

print_r($response);

// Redirect customer to payment page
//header('Location: ' . $url);
?>