<?php
	session_start();

	/**
	 * @author Jan Wehrs (jan.wehrs@billpay.de)
	 * @copyright Copyright 2010 Billpay GmbH
	 * @license commercial 
	 */
	require_once '../../api/ipl_xml_api.php';
	require_once '../../api/php5/ipl_preauthorize_request.php';
	
	// Generate a random suffix to create unique customer
	$randSuffix = rand(1, 15000000000);
	
	$req = new ipl_preauthorize_request($_SESSION['api-url-base'], IPL_CORE_PAYMENT_TYPE_INVOICE);
	
	// UN-COMMENT THIS FOR A DIRECT DEBIT REQUEST
	//$req = new ipl_preauthorize_request($_SESSION['api-url-base'], IPL_CORE_PAYMENT_TYPE_DIRECT_DEBIT);
	// UN-COMMENT THIS FOR A RATE PAYMENT REQUEST
	//$req = new ipl_preauthorize_request($_SESSION['api-url-base'], IPL_CORE_PAYMENT_TYPE_RATE_PAYMENT);
	
	$req->set_default_params($_SESSION['mid'], $_SESSION['pid'], $_SESSION['security-key']);
	$req->set_customer_details("123456", "e", "Herr", "", "Herbert $randSuffix", "Billpay $randSuffix",
		"Teststrasse $randSuffix", "123", "", "12345", "Teststadt $randSuffix", "DEU", "testing@billpay.de", 
	       "0302333459", "01775112389", "19850911", "de", "85.214.7.23", "p");
	$req->set_shipping_details(true);
	// UN-COMMENT THIS FOR A DIRECT DEBIT REQUEST OR RATE PAYMENT 
	//$req->set_bank_account("Herbert_$randSuffix Tester_$randSuffix", "4973024088", "10050000");
	// UN-COMMENT THIS FOR A RATE PAYMENT REQUEST
	//$req->set_rate_request(6, 28599);
	
	$req->set_total(500, 595, "Express-Versand", 500, 650, 17800, 27455, "EUR", "");
	$req->add_article("article001", 12, "Kaffetasse 08/15", "Eine Kaffetasse aus Porzellan", 1200, 1950);
	$req->add_article("article002", 4, "Kuchenteller 07-11", "Kuchenteller - weiss", 850, 1000);
	$req->add_order_history("1233343321", "20080101 10:23:30", 12300, "EUR", 4, 0);
	$req->set_terms_accepted(true);
	
	try {
		$req->send();
		
		if ($req->has_error()) {
			echo "Error occured!<br />";
			echo "Error code: " . $req->get_error_code() . "<br />";
			echo "Customer msg: " . utf8_decode($req->get_customer_error_message()) . "<br />";
			echo "Merchant msg: " . utf8_decode($req->get_merchant_error_message()) . "<br />";
		}
		
		echo "Status: " . $req->get_status() . "<br />";
		
		if (!$req->has_error()) {
			echo "TX ID: 	" . $req->get_bptid() . "<br />";
			echo "<input type=\"button\" value=\"Capture\" onclick=\"window.location.href='capture.php?tx_id=" . $req->get_bptid() . "'\" />";
		}
		
		echo "<br /><br />";
		echo "<strong>Request XML:</strong> " . htmlentities($req->get_request_xml());
		echo "<br /><br />";
		echo "<strong>Response XML:</strong> " . htmlentities($req->get_response_xml());
	}
	catch(Exception $e) {
		echo $e->getMessage();
	}

?>
