<?

$url="https://secure.checkout2pay.com/payment/api.php";
$values=array(  // from an XML-request to the gateway
'request'=>"
<xml>
<request>
<merchant>
	<merchantID>5000</merchantID>
	<merchantPassword>xxx</merchantPassword>
	<merchantSiteID>6006</merchantSiteID>
</merchant>

<transaction>
	<transactionType>sale</transactionType>
	<transactionAmount>56.00</transactionAmount>
	<transactionOrderID>#2234223</transactionOrderID>
	<transactionAffiliateID></transactionAffiliateID>
	<transactionDescription>Order #2234223</transactionDescription>
</transaction>

<customer>
	<customerCardNumber>4444333322221111</customerCardNumber>
	<customerCardType>visa</customerCardType>
	<customerExpireMonth>07</customerExpireMonth>
	<customerExpireYear>2008</customerExpireYear>
	<customerCVC2>124</customerCVC2>
	<customerCardHolder>Alex Test</customerCardHolder>
	<customerFullName>Alex Test</customerFullName>
	<customerPhone>+7 834 2567798</customerPhone>
	<customerAddress>Fireside Ct 18-270</customerAddress>
	<customerCity>Viena</customerCity>
	<customerStateCode>VA</customerStateCode>
	<customerZip>43321</customerZip>
	<customerCountryCode>US</customerCountryCode>
	<customerEmail>alex@kochetov.com</customerEmail>
	<customerShippingFullName></customerShippingFullName>
	<customerShippingPhone></customerShippingPhone>
	<customerShippingAddress></customerShippingAddress>
	<customerShippingCity></customerShippingCity>
	<customerShippingStateCode></customerShippingStateCode>
	<customerShippingCountryCode></customerShippingCountryCode>
	<customerIP>127.0.0.1</customerIP>
	<customerBrowser>Mozilla Firefox</customerBrowser>
	<customerLanguage>En Ru</customerLanguage>
	<customerScreenResolution>1024x768</customerScreenResolution>
</customer>

</request>
</xml>
"
);

	foreach ($values as $key=>$value) { //prepare valid POST data
		$params.="$key=".urlencode($value)."&";
	}


	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

	ob_start();
	curl_exec($ch);   // sending XML to the gateway
	$result = ob_get_contents();  // getting response from gateway
	ob_end_clean();
	curl_close($ch);

	$requestxml=$result;
	$p = xml_parser_create();
	if (!  xml_parse_into_struct($p, $requestxml, $vals, $index)) { //parse response
		throw new Exception("Can not parse response"); //exception if invalid response recieved
	}
	xml_parser_free($p);


	if (is_array($vals)) foreach ($vals as $key=>$value) {
		if ($value[type]=='complete') {
			$transinfo[$value[tag]]=trim($value[value]);
		}
	}


	mydump($transinfo);
	if (($transinfo[RESULTSTATUS]=='approved' || $transinfo[RESULTSTATUS]=='test' ) && $transinfo[TRANSACTIONTYPE]=='sale') { // process transaction
		echo "Transaction approved<br>\n";
	} else {
		echo "Transaction declined<br>\n";
	}





function mydump($mix, $screen=true, $file=true) {
    ob_start();
    print_r($mix);

    $text=(ob_get_contents());
    ob_end_clean();


    if ($screen) {
        $text=htmlentities($text);
        echo "<div align=left><pre>$text<br></pre></div>";
    }

}

?>
