g0ddest/epay — Issue #1: epay class (supply pm only)
Author: g0ddest
Created: 2011-12-02 19:34:08+00:00
State: open

'pm', // For PerfectMoney (may be 'perfect money', 'PerfectMoney' and others)
* 'account' => '123', // Account ID
* 'passphrase' => 'some' // PassPhrase
* );
* //or simply:
* $in = array('pm','123','some'); // you should turn ON api on web-site
*
* $merch = new epay($in);
* if($merch)
* if(!$merch->errors) {
* print_r($merch->balance()); // return array with your pm balance if were not errors in processing
* $start = array(1, 5, 2011); $end = array(1, 6, 2011); // format: mm dd yyyy
* print_r($merch->history($start, $end)); // return array with history
* print_r($merch->voucherlist()); // return array with Voucher list
* print_r($merch->rates()); // return array with exchange rates, e.g. Array ( [USD2EUR] => 0.71 [USD2GOLD] => 0.00066 [date] => 5/24/11 12:03:34 AM )
* print_r($merch->vouchercreate('U1234567', '1.01')); // create voucher to U1234567 with amount 1.01
* print_r($merch->transfer(U1234567, U7654321, 19.95, 322223, 'Thanks for dinner.', 'mycode123', 3)); // tranfer funds to other account in mount in 19.95 with protection code mycode123
* }
*
*/

class epay{

public
$payment_method,
$pm_accountid, $pm_passphrase;
public
$error, $error_text;

public function __construct($input){

$this->error = false;

if(!is_array($input))
return false;

else{

(isset($input['method'])) ? $this->payment_method = $input['method'] : $this->payment_method = $input[0];
// Perfect Money part
if(strtolower($this->payment_method) === 'perfectmoney' || strtolower($this->payment_method) === 'perfect money')
$this->payment_method = 'pm';
if($this->payment_method == 'pm')
(isset($input['account'])) ? $this->pm_accountid = $input['account'] : $this->pm_accountid = $input[1];
(isset($input['passphrase'])) ? $this->pm_passphrase = $input['passphrase'] : $this->pm_passphrase = $input[2];
// end Perfect Money part

if($this->payment_method == '') $this->error_report(2);
}

}

function getsslcont($link, $return = 'string'){
/*if( $ch = curl_init($path) ){
curl_setopt($ch, CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$otvet = curl_exec($ch);
curl_close($ch);
return $otvet;
}
else
$this->error_report(3);*/
$f=fopen($link, 'rb');
if($f===false) $this->error_report(0);

if($return == 'array'){
$out=array();
while(!feof($f))
array_push($out, trim(fgets($f)));
}
else{
$out="";
while(!feof($f))
$out.=fgets($f);
}

fclose($f);
return $out;
}

public function balance(){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/balance.asp?AccountID='.$this->pm_accountid.'&PassPhrase='.$this->pm_passphrase;
$out = $this->getsslcont($f, 'string');
if(!preg_match_all("//", $out, $result, PREG_SET_ORDER))
$this->error_report(1);
foreach($result as $item){
$key=$item[1];
$ar[$key]=$item[2];
}
return $ar;
// end Perfect Money part

break;
}

}

}

public function history($start, $end){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/historycsv.asp?startmonth='.$start[0].'&startday='.$start[1].'&startyear='.$start[2].'&endmonth='.$end[0].'&endday='.$end[1].'&endyear='.$end[2].'&AccountID='.$this->pm_accountid.'&PassPhrase='.$this->pm_passphrase;
$lines = $this->getsslcont($f, 'array');

if($lines[0]!='Time,Type,Batch,Currency,Amount,Fee,Payer Account,Payee Account,Memo'){

$this->error_report(1);
return '';

}
else{

$ar=array();
$n=count($lines);
for($i=1; $i<$n; $i++){

$item=explode(",", $lines[$i], 9);
if(count($item)!=9) continue; // line is invalid - pass to next one
$item_named['Time']=$item[0];
$item_named['Type']=$item[1];
$item_named['Batch']=$item[2];
$item_named['Currency']=$item[3];
$item_named['Amount']=$item[4];
$item_named['Fee']=$item[5];
$item_named['Payer Account']=$item[6];
$item_named['Payee Account']=$item[7];
$item_named['Memo']=$item[8];
array_push($ar, $item_named);
}

}

return $ar;
// end Perfect Money part

break;

}

}

}

public function voucherlist(){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/evcsv.asp?AccountID='.$this->pm_accountid.'&PassPhrase='.$this->pm_passphrase;
$lines = $this->getsslcont($f, 'array');

if($lines[0]!='Created,e-Voucher number,Activation code,Currency,Batch,Payer Account,Payee Account,Activated,Amount'){

// print error message
echo $lines[0];

}else{

// do parsing
$ar=array();
$n=count($lines);
for($i=1; $i<$n; $i++){

$item=explode(",", $lines[$i], 9);
if(count($item)!=9) continue; // line is invalid - pass to next one
$item_named['Created']=$item[0];
$item_named['Number']=$item[1];
$item_named['Code']=$item[2];
$item_named['Currency']=$item[3];
$item_named['Batch']=$item[4];
$item_named['Payer Account']=$item[5];
$item_named['Payee Account']=$item[6];
$item_named['Activated']=$item[7];
$item_named['Amount']=$item[8];
array_push($ar, $item_named);
}

}

return $ar;
// end Perfect Money part

break;

}

}

}

public function rates(){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/rates.asp';
list($lines) = $this->getsslcont($f, 'array');
$rates = explode(",", $lines);
$ar['USD2EUR'] = trim($rates[1]);
$ar['USD2GOLD'] = trim($rates[2]);
$ar['date'] = trim($rates[0]);
return $ar;
// end Perfect Money part

break;

}

}

}

public function vouchercreate($to, $amount){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/evcsv.asp?AccountID='.$this->pm_accountid.'&PassPhrase='.$this->pm_passphrase.'&Payer_Account='.$to.'&Amount='.$amount;
$out = $this->getsslcont($f, 'string');
if(!preg_match_all("//", $out, $result, PREG_SET_ORDER))
$this->error_report(1);

$ar="";
foreach($result as $item){
$key=$item[1];
$ar[$key]=$item[2];
}
return $ar;
// end Perfect Money part

break;
}
}

}

public function transfer($from, $to, $amount, $id, $memo, $code, $period){

switch($this->payment_method){

case('pm'):{ // Perfect Money part

$f = 'https://perfectmoney.com/acct/confirm.asp?AccountID='.$this->pm_accountid.'&PassPhrase='.$this->pm_passphrase.'&Payer_Account='.$from.'&Payee_Account='.$to.'&PAYMENT_ID='.$id.'&Amount='.$amount;
if($memo != '') $f .= '&Memo='.$memo;
if($code != '') $f .= '&code='.$code;
if($period != '') $f .= '&period='.$period;
$out = $this->getsslcont($f, 'string');

if(!preg_match_all("//", $out, $result, PREG_SET_ORDER))
$this->error_report(1);

$ar="";
foreach($result as $item){
$key=$item[1];
$ar[$key]=$item[2];
}

return $ar;
// end Perfect Money part

break;
}
}

}

public function error_report($number){

$this->error = true;

switch($number){
case(0):
$this->error_text = 'Error openning url.';
break;

case(1):
$this->error_text = 'Ivalid output.';
break;

case(2):
$this->error_text = 'Unknown payment system.';
break;

case(3):
$this->error_text = 'Please enable cURL in PHP.';
break;

default:
$this->error_text = 'Unknown error';
}

$this->error_text = 'Error code number '.$number.'. '.$this->error_text;

}

}

?>