<?php

// 01. URL de l'environnement à appeler (TEST ou PRODUCTION)
define('API_ENDPOINT', 'https://stage-secure-gateway.hipay-tpp.com/rest/v1'); // TEST
// define('API_ENDPOINT', 'https://secure-gateway.hipay-tpp.com/rest/v1'); // PRODUCTION


// 02. Identifiant API du compte HiPay pour s'authentifier auprès de l'API HiPay
define('API_USERNAME', '94659043.stage-secure-gateway.hipay-tpp.com');
define('API_PASSWORD', 'Test_rU83KkyqylRjA4rwZm0wX3TM');


// 03. Création des 2 variables pour stocker les infors sur l'environnement, l'API et les identifiants API
$resource = API_ENDPOINT . '/hpayment';
$credentials = API_USERNAME . ':' . API_PASSWORD;



// 04. Construction de la requête
$request = array(
// 1. Design et paramétrage de la page
'template' =>'',
'time_to_limit_to_pay' =>'',
'css' =>'',
'language' =>'fr_FR',
'merchant_display_name' =>'',
'orderid'=>'lguillet-test1',
'display_selector'=>'',
'payment_product_list'=>'visa,mastercard',

// 2. Paramètrge du paiement
'operation'=> 'sale',
'eci'=>'7',
'multi_use'=>'',
'description'=> 'produit test 01',
'long_description'=> 'produit test 01',
'authentification_indicator'=>'',
'currency'=> 'EUR',
'amount'=> '150',
'shipping'=> '',
'tax'=> '',
'tax_rate'=> '',
'custom_data' =>
'{
	"shipping_method":"click and collect",
	"first_order":"0",
	"products_list":"First product, Second product, Third product",
	"_reporting_data_1":"my custom data 1"}',
	// 3. Informations du client
	'email'=> '',
	'phone'=> '',
	'birthdate'=> '',
	'gender'=> '',
	'firstname'=> '',
	'lastname'=> '',
	'country'=> '',
	'streetaddress'=> '',
	'streetaddress2'=> '',
	'city'=> '',
	'zipcode'=> '',
	'shipto_firstname'=> '',
	'shipto_lastname'=> '',
	'shipto_streetaddress'=> '',
	'shipto_streetaddress2'=> '',
	'shipto_city'=> '',
	'shipto_zipcode'=> '',
	'shipto_country'=> '',
	'cid'=> '123456',
	'ipaddr'=> '',
	'accept_url'=> '',
	'decline_url'=> '',
	'pending_url'=> '',
	'exception_url'=> '',
	'cancel_url'=> ''
	);


// 05. Initialisation de cURL
$curl = curl_init();

// 07. Définition des en-têtes
$header = array(
  'User-Agent: ' .$_SERVER['HTTP_USER_AGENT'],
  'Accept: application/json',
  'Accept-Language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'],
  'Accept-Charset:' .  $_SERVER['HTTP_ACCEPT_CHARSET']
);

// 07. Configuration des options de cURL
$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 => $request
);


foreach ($options as $option => $value) {
	curl_setopt($curl, $option, $value);
}

// 08. Exécution de cURL
if (false === ($result = '')) {
	echo "<br/><strong>Error: </strong><br/>";
	var_dump(curl_error($curl), curl_errno($curl));
	exit();
}


$status = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);

// 09. Affichage de la réponse API
$response = json_decode($result);
var_dump($response);
$url = $response->forwardUrl;

 // 10. Deconnexion de cURL
curl_close($curl);


?>
