<?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 . '/withdrawal.json';

// Define credentials
//define('API_USERNAME', '7bbddd211f30e16c1835f19d2a573c34');
//define('API_PASSWORD', '6128e02501d4ce174a96995a67e537f4');
$credentials = API_USERNAME . ':' . API_PASSWORD;


// Create query parameters
$queryParameters = array(
            'amount'                   	=> "17853.15370",
            'label'                 	=> "Withdrawal asked by the merchant through Brendon",
//            'merchant_unique_id'       	=> "",
//            'php-auth-subaccount-id'   	=> "10662472",
//            'php-auth-subaccount-login'	=> ""
            );

//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'],
    'php-auth-subaccount-id: 10662472'
);


// 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;

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