<?
include "config.php";



class APayments 
{ 
  var $PROCES_DETAILS; 
  
  function Breakout($errno, $key="") 
  { 
    switch($errno) 
    { 
      case 1: 
        $this->PROCES_DETAILS["Error"]  = "Error with var $key"; 
      break; 
      
      case 2: 
        $this->PROCES_DETAILS["Error"]  = "No vars"; 
      break; 
      
      case 3: 
        $this->PROCES_DETAILS["Error"]  = "Could not connect to e-gold server"; 
      break; 
    } 
  } 
  
  function Check_vars($vars) 
  { 
    if($vars["ERROR"]) 
      $this->PROCES_DETAILS["Error"]  = $vars["ERROR"]; 
    elseif($vars["PAYMENT_BATCH_NUM"] && $vars["PAYMENT_AMOUNT"]) 
      $this->PROCES_DETAILS["Batch"]  = $vars["PAYMENT_BATCH_NUM"]; 
    else 
      $this->PROCES_DETAILS["Error"]  = "Unknown error"; 
  } 
  
  function Output($output) 
  { 
    if(preg_match_all("/<input type=hidden name=([^']*?) value=\"([^']*?)\">/", $output, $matches)) 
    { 
      $num  = count($matches[1]); 
      
      for($i=0;$i<$num;$i++) 
      { 
        $output_vars[$matches[1][$i]]  = $matches[2][$i]; 
      } 
      
      $this->Check_vars($output_vars); 
    } 
    else 
    { 
      $this->Breakout(2); 
    } 
  } 
  
  function Pay($data) 
  { 
    $vars    = ""; 
    
    foreach($data AS $key => $value) 
    { 
      if($key && $value) 
        $vars    .= "$key=$value&"; 
      else 
        $this->Breakout(1, $key); 
    } 
    
    if($vars) 
      $vars    = substr($vars, 0, strlen($vars) - 1); 
    else 
      $this->Breakout(2); 
    
    $cu      = curl_init(); 
    
    curl_setopt($cu, CURLOPT_URL,        "https://www.e-gold.com/acct/confirm.asp"); 
    curl_setopt($cu, CURLOPT_SSL_VERIFYHOST,  2); 
    curl_setopt($cu, CURLOPT_USERAGENT,      "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
    curl_setopt($cu, CURLOPT_POST,        1); 
    curl_setopt($cu, CURLOPT_POSTFIELDS,    $vars); 
    curl_setopt($cu, CURLOPT_RETURNTRANSFER,  1); 
    curl_setopt($cu, CURLOPT_TIMEOUT,      100); 
    
    $output    = curl_exec($cu); 
    $succeeded  = curl_errno($cu) == 0 ? true : false; 
    
    curl_close($cu); 
    
    if(!$succeeded) 
      $this->Breakout(3); 
    else 
      $this->Output($output); 
  } 
} 

$apayment  = new APayments; 

echo "начало1<br>";

//You can use it this way: 

$data = Array(); 

$data["AccountID"] = $account; 
$data["PassPhrase"] = $password; 
$data["Payee_Account"] = "1138414"; 
$data["Amount"] = "0.01"; 
$data["Memo"] = urlencode(stripslashes("Automatic Payment")); 
$data["PAY_IN"] = "1"; 
$data["WORTH_OF"] = "Gold"; 
$data["IGNORE_RATE_CHANGE"] = "Y"; 
echo "начало2<br>";
$apayment->Pay($data); 
//Pay($data);
echo $apayment->PROCES_DETAILS["Error"]; // possible error 
echo $apayment->PROCES_DETAILS["Batch"]; // batch nr 
echo "конец<br>";


?>