<?php
//environnement
define('URL_TEST', 'https://stage-secure-gateway.hipay-tpp.com/rest/v1/hpayment');
define('URL_PROD', 'https://secure-gateway.hipay-tpp.com/rest/v1/hpayment');

// 02. Identifiant API du compte HiPay pour s'authentifier auprès de l'API HiPay

//Mes ID
define('API_USERNAME', '94677480.stage-secure-gateway.hipay-tpp.com');
define('API_PASSWORD', 'Test_MH94eehPwhjuVqxYYjTUDzr4');

//ID DU MARCHAND
/*define('API_USERNAME', '');
define('API_PASSWORD', '');*/

// 03. Création des 2 variables pour stocker les infors sur l'environnement, l'API et les identifiants API
$resource = URL_TEST;
$credentials = API_USERNAME . ':' . API_PASSWORD;

$template = 'basic-js';
$time_limit_to_pay = '';
$css = '';
$language = 'fr_FR';
$merchant_display_name = 'Boutik';
$orderid = 'order'.rand(0, 1000);
$display_selector = '1';
$payment_product_list = '';
$operation = 'Sale'; //Sale or Authorization or JToken
$eci = '7';
$multi_use = '';
$description = 'Embrayage moto';
$long_description = 'Embrayage moto Yamaha M1';
$authentication_indicator = '';
$currency = 'EUR';
$amount = '89';
$shipping = '';
$tax = '';
$tax_rate = '';
$custom_data = '';
$email = 'yan.cy@gmail.com';
$phone = '0678908765';
$birthdate = '19850423';
$gender = 'M';
$firstname = 'Marc';
$lastname = 'DUPONT';
$streetaddress = '7 rue de paris';
$streetaddress2 = '';
$city = 'Paris';
$state = '';
$zipcode = '75001';
$country = 'FR';
$shipto_firstname = 'Luca';
$shipto_lastname = 'MARINI';
$shipto_streetaddress = '2 rue de paris';
$shipto_streetaddress2 = '';
$shipto_city = 'Gonesse';
$shipto_state = '';
$shipto_zipcode = '95500';
$shipto_country = 'FR';
$ipaddr = '127.0.0.1';
$cid = '123456';
$accept_url = 'https://yannoo.fr/responsepage.php?accept';
$decline_url = 'https://yannoo.fr/responsepage.php?decline';
$pending_url = 'https://yannoo.fr/responsepage.php?pending';
$exception_url = 'https://yannoo.fr/responsepage.php?exception';
/*$accept_url = '';
$decline_url = '';
$pending_url = '';
$exception_url = '';*/

$data = array(
  'template' => $template,
  'time_limit_to_pay	' => $time_limit_to_pay,
  'css' => $css,
  'language' => $language,
  'merchant_display_name' => $merchant_display_name,
  'orderid' => $orderid,
  'display_selector' => $display_selector,
  'payment_product_list' => $payment_product_list,
  'operation' => $operation,
  'eci' => $eci,
  'multi_use' => $multi_use,
  'description' => $description,
  'long_description' => $long_description,
  'authentication_indicator	' => $authentication_indicator,
  'currency' => $currency,
  'amount' => $amount,
  'shipping' => $shipping,
  'tax' => $tax,
  'tax_rate' => $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"}',
  'email' => $email,
  'phone' => $phone,
  'birthdate' => $birthdate,
  'gender' => $gender,
  'firstname' => $firstname,
  'lastname' => $lastname,
  'streetaddress' => $streetaddress,
  'streetaddress2' => $streetaddress2,
  'city' => $city,
  'state' => $state,
  'zipcode' => $zipcode,
  'country' => $country,
  'shipto_firstname' => $shipto_firstname,
  'shipto_lastname' => $shipto_lastname,
  'shipto_streetaddress' => $shipto_streetaddress,
  'shipto_streetaddress2' => $shipto_streetaddress2,
  'shipto_city' => $shipto_city,
  'shipto_state' => $shipto_state,
  'shipto_zipcode' => $shipto_zipcode,
  'shipto_country' => $shipto_country,
  'ipaddr' => $ipaddr,
  'cid' => $cid,
  'accept_url' => $accept_url,
  'decline_url' => $decline_url,
  'pending_url' => $pending_url,
  'exception_url' => $exception_url,
  'cancel_url' => $exception_url,
  'country' => $country,
);


// 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 => $data
);


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 "<br/><a href='".$url."' target='_blank'>Payer par carte</a>";
