<?php
// Procedure pour créer les comptes DIRECT des clients

ini_set('soap.wsdl_cache_ttl', 1);

define('WS_GATEWAY','https://test-ws.hipay.com/soap/');
define('WS_LOGIN', 'd4a919efc4843389def7d56922c56c58'); // hipay@lrqdo.fr
define('WS_PSW', 'a2e33c6fdad9cbd3685cbf3fe8c868e0'); // hipay@lrqdo.fr
define('WEBSITE', '7432'); // hipay@lrqdo.fr		

$new_account = 'merchant' . time().'@lrqdo.com';

// STEP 1 : soap flow options
$options = array(
	'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
	'cache_wsdl' => WSDL_CACHE_NONE,
	'soap_version' => SOAP_1_1,
	'encoding' => 'UTF-8',
	'trace' => 1
);

// STEP 2 : Soap client initialization
$client = new SoapClient(WS_GATEWAY . 'user-account-v2?wsdl', $options);

// STEP 3 : Check is the email is free in Hipay
$result = $client->isAvailable(array('parameters'=>array(
	'wsLogin'		=> WS_LOGIN,
	'wsPassword'	=> WS_PSW,
	'email'			=> $new_account
)));
var_dump($result);
// STEP 4 : Soap call on createWithWebsite method of user-account-v2 webservice if email available
if ($result->isAvailableResult->isAvailable) {

	$queryparameters = array('parameters'=>array(
	'wsLogin'				=> 	WS_LOGIN,
	'wsPassword'			=> 	WS_PSW,
	'websiteId'				=> 	WEBSITE,
	'email'					=> 	$new_account,
	'firstname'				=> 	'James',
	'lastname'				=> 	'Dupont',
	'currency'				=> 	'EUR',
	'locale' 				=> 	'en_GB',
	'ipAddress'				=> 	'127.0.0.1',
	'entity'				=>	'direct',
	'websiteBusinessLineId'	=> 	12,
	'websiteTopicId' 		=> 	13,
	'websiteContactEmail'	=> 	'jdupont@free.fr',
	'websiteName'			=> 	'merchant.com',
	'websiteUrl'			=> 	'http://www.merchant.com',
	'websiteMerchantPassword' => 'passwdtest'
	));

	// STEP 5 : Soap call on createWithWebsite method of user-account-v2 webservice
	$result2 = $client->createWithWebsite($queryparameters);
	if ($result2->createWithWebsiteResult->userAccountId > 0) {
		header("Location: get_card.php?marchand=" . $result2->createWithWebsiteResult->userAccountId . "&websiteid=" . $result2->createWithWebsiteResult->websiteId);
	}
	else {
		echo "Account not created!";
	}
}
else {
	echo $request['email'] . " is not available";
//	print_r($result);
}

//echo $client->__getLastRequest();
//echo $client->__getLastResponse();


?>