<?php

/*
Stage/testing https://stage-secure-vault.allopass.com/rest/v1/
Production https://secure-vault.allopass.com/rest/v1/
*/

$testing = true;

if ($testing)
	$endpoint = 'https://stage-secure-vault.allopass.com/rest/v1/';
else 
	$endpoint = 'https://secure-vault.allopass.com/rest/v1/';
	

define('API_ENDPOINT', $endpoint);
define('API_USERNAME', '94651813.api.allopass.com');
define('API_PASSWORD', 'AtrumIJY1lSpqJgc11IOPxEh');

$credentials = API_USERNAME . ':' . API_PASSWORD;
//echo 'base64("<API login>:<API password>") = '.base64($credentials);


$action = 'token/create';

$resource = API_ENDPOINT . $action;

// create a new cURL resource
$curl = curl_init();

// request parameters
/*
// List of mandatory data elements
orderid
payment_product : The payment product (e.g., visa, mastercard, ideal).
description
currency 		: Base currency for this order (Default to EUR).
amount			: The total order amount. It should be calculated as a sum of the items purchased, plus the shipping fee (if present), plus the tax fee (if present).
cid 			: AN M Unique customer id.
*/

$card_number = '4111111111111111';
$card_expiry_month = '10';
$card_expiry_year = '2014';
$card_holder = 'Tno Token Create';
$cvc = '123';
// Multi_use : only boolean 
// 0 = Generate a single-use token
// 1 = Generate a multi-use token (default)
$multi_use = '0'; 

$data = array(
	'card_number' => $card_number,
	'card_expiry_month' => $card_expiry_month,
	'card_expiry_year' => $card_expiry_year,
	'card_holder' => $card_holder,
	'cvc' => $cvc,
	'multi_use' => $multi_use,
	);

// set appropriate options
$verbose = fopen('php://temp', 'rw+');
$options = array(
	CURLOPT_URL => $resource,
	CURLOPT_USERPWD => $credentials,
	CURLOPT_HTTPHEADER => array('Accept: application/json'),
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_FAILONERROR => false,
	CURLOPT_HEADER => false,
	CURLOPT_POST => true,
	CURLOPT_POSTFIELDS => http_build_query($data),
	//CURLOPT_POSTFIELDS => http_build_query($data),
	//CURLOPT_POSTFIELDS => json_encode($data),
	// WARNING : ONLY FOR TESTING PURPOSES! NEED TO BE SET TO TRUE IN PROD
	CURLOPT_SSL_VERIFYPEER => false, 
	CURLOPT_SSL_VERIFYHOST => false,
	CURLOPT_VERBOSE => true,
	CURLOPT_STDERR => $verbose
);
//print_r($options);


foreach ($options as $option => $value) {
	curl_setopt($curl, $option, $value);
}

$result = curl_exec($curl);

$status = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = json_decode($result);

//// Verbose below:
// Needs CURLOPT_VERBOSE => true
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
//// End of verbose

// execute the given cURL session
if (false === ($result)) {
	throw new RuntimeException(curl_error($curl), curl_errno($curl));
}





if (floor($status/100) != 2) {
	print_r($response);
	echo 'error : status = '.$status;
	//throw new RuntimeException($response->message.' : '.$response->description, $response->code);
}

//printf('Payment Reference: %s', $response->transactionReference);
print_r($response);
curl_close($curl);