<?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', '94681754.stage-secure-gateway.hipay-tpp.com');
define('API_PASSWORD', 'Test_KPyx5MIggiwu4F8cvtvxTVDg');


// 03. Création des 2 variables pour stocker les infos sur l'environnement, l'API et les identifiants API
$resource = API_ENDPOINT . '/order';
$credentials = API_USERNAME . ':' . API_PASSWORD;


// 04. Construction de la requête
$request = array(
// 1. Design et paramétrage de la page
'language' =>'fr_FR',

// 2. Paramètrge du paiement
'orderid'=>'HP-3x_Metropole-444',
'operation'=> 'Sale',
'eci'=> '7',
'description'=> 'produit_01',
'long_description' => 'produit_01',

'payment_product'=>'credit-long',     //'credit-long',  //'4xcb-no-fees', //-no-fees

'currency'=> 'EUR',
'amount'=> 1350,
'shipping'=> 10,
'tax'=> 20,
'tax_rate'=> 5,
'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'=> 'hp@hipay.com',
	'phone'=> '0625641987',
	'birthdate'=> '19880623',
	'gender'=> 'M',
	'firstname'=> 'Ludovic',
	'lastname'=> 'Guillet',
	'country'=> 'FR',
	'streetaddress'=> 'via Cosimo del Fante, 7',
	'streetaddress2'=> 'complément dadresse',
	'city'=> 'Paris',
	'zipcode'=> '75009',
	'shipto_firstname'=> 'ludo',
	'shipto_lastname'=> 'guillet',
	'shipto_streetaddress'=> 'via Cosimo del Fante, 7',
	'shipto_streetaddress2'=> 'complementaire',
	'shipto_city'=> 'Paris',
	'shipto_zipcode'=> '75009',
	'shipto_country'=> 'FR',
	'cid'=> '123456',
	'ipaddr'=> '127.0.0.1',
	'accept_url'=> 'http://google.fr/accept',
	'decline_url'=> 'http://google.fr/decline',
	'pending_url'=> 'http://google.fr/pending',
	'exception_url'=> 'http://google.fr/execeptiojn',
	'cancel_url'=> 'http://google.fr/cancel',
	'order_category_code' => '5261',
	'delivery_date'=>'2018-11-30', 
	'delivery_method'=>'{"mode":"TRAVEL","shipping":"EXPRESS"}',
	'basket'=>'[{"product_category": 8,
       "product_reference": "NF-a0351",
       "name": "My second product",
       "type": "good",
       "quantity": 1,
       "unit_price": 1350,
       "total_amount": 1350}]'
	   //  ,'payment_product_parameters' =>'{"merchant_promotion":"FIO10"}'
	   
	   
	);

// 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 = curl_exec($curl))) {
	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);

// 11. Afficher le bouton "Payer"
echo "<meta http-equiv='refresh' content='5; URL=".$url."'>";
//echo "<br/><a href='".$url."' target='_blank'>afficher paiement alternatif</a>";

?>
