<?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', '94667602.stage-secure-gateway.hipay-tpp.com');
define('API_PASSWORD', 'Test_Xedrj6Lh06XQ4TzcjQdekm7Z');


// 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(
	'cardtoken' => 'ce5f096fa6bc05989c170e7c96f94432660491bd',
	'email' => 'nerijus@seo.lt',
	'orderid' => 'test4',
	'operation' => 'sale',
	'payment_product' => 'visa',
	'description' => 'Balance Refill',
	'currency' => 'USD',
	'amount' => '11.76',
	'shipping' => 0.0,
	'tax' => 0.0,
	'cid' => 'e3372f15-2bcb-491a-b065-faafe38f8e0c',
	'ipaddr' => '54.93.195.246',
	'accept_url' => 'http://18f69673.ngrok.io/payment/accept',
	'decline_url' => 'http://18f69673.ngrok.io/payment/decline',
	'pending_url' => 'http://18f69673.ngrok.io/payment/pending',
	'exception_url' => 'http://18f69673.ngrok.io/payment/exception',
	'cancel_url' => 'http://18f69673.ngrok.io/payment/cancel',
	'notify_url' => 'http://18f69673.ngrok.io/ipn',
	'http_accept' => '*/*',
	'http_user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
	'language' => 'en_US',
	'firstname' => 'John Doe',
	'lastname' => '',
	'custom_data' => '{"VERIFIED":"Y"}',
	);

// 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>";

?>
