api_id = $api_id; $this->api_key = $api_key; // Init payment systems $this->RefreshPaymentSystems(); } /** * Call the API */ public function Query($method = '', $args = array()) { // Prepare request before send $this->Prepare($args); print_r($args); $url = $this->public_api.$method; $ch = curl_init(); if(strtolower((substr($url,0,5))=='https')) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_POST, 0); #curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $args); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (Windows; U; Windows NT 5.0; En; rv:1.8.0.2) Gecko/20070306 Firefox/1.0.0.4"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $result = curl_exec($ch); curl_close($ch); print_r($result.PHP_EOL); return json_decode($result); } /** * Prepare request */ private function Prepare(&$Params) { // Sort array by key (ASC) if(isset($Params['signature'])) { unset($Params['signature']); } $Params = array_reverse($Params, true); $Params['APIID'] = $this->api_id; $Params = array_reverse($Params, true); $SortedParams = array(); $a1 = array_keys($Params); foreach($a1 as $key) { $SortedParams[strtolower($key)] = $Params[$key]; } ksort($SortedParams); // Implode values and append API key $Imploded = implode(":", $SortedParams).":".$this->api_key; $SortedParams['signature'] = md5($Imploded); $Params = $SortedParams; } /** * Refresh payment systems collection */ public function RefreshPaymentSystems() { $PaySystemsResult = $this->Query("Rating_PaySystems"); if($PaySystemsResult->IsSuccess) { $this->PaymentSystems = array(); foreach($PaySystemsResult->Result as $PaySystem) { $this->PaymentSystems[$PaySystem->ID] = $PaySystem; } } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$PaySystemsResult->ErrorMessage); } } /** * Get payment systems list */ public function Rating_PaySystems() { $PaySystemsResult = $this->Query("Rating_PaySystems"); if($PaySystemsResult->IsSuccess) { return $PaySystemsResult->Result; } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$CurrencyResult->ErrorMessage); } } /** * Get exchangers list */ public function Rating_Exchangers($PaySystemID) { if(!array_key_exists($PaySystemID, $this->PaymentSystems)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$PaySystemID.' not found'); } $ExchangersResult = $this->Query("Rating_Exchangers", array('paySystemID' => $PaySystemID)); if($ExchangersResult->IsSuccess) { return $ExchangersResult->Result; } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$ExchangersResult->ErrorMessage); } } /** * Get available directions */ public function Rating_Exchangers_Directions($FromPaySystemID, $FromCurrencyName, $ToPaySystemID, $ToCurrencyName) { if(!array_key_exists($FromPaySystemID, $this->PaymentSystems)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$PaySystemID.' not found'); } if(!array_key_exists($ToPaySystemID, $this->PaymentSystems)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$ToPaySystemID.' not found'); } if(!in_array($FromCurrencyName, $this->PaymentSystems[$FromPaySystemID]->Currencies)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$FromCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system'); } if(!in_array($ToCurrencyName, $this->PaymentSystems[$ToPaySystemID]->Currencies)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$ToCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system'); } $DirectionsResult = $this->Query( "Rating_Exchangers_Directions", array( 'fromPaySystemID' => $FromPaySystemID, 'fromCurrencyName' => $FromCurrencyName, 'toPaySystemID' => $ToPaySystemID, 'toCurrencyName' => $ToCurrencyName ) ); if($DirectionsResult->IsSuccess) { return $DirectionsResult->Result; } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage); } } /** * Get available directions by payment system and currency code (FROM) */ public function Rating_Directions_From($FromPaySystemID, $FromCurrencyName) { if(!array_key_exists($FromPaySystemID, $this->PaymentSystems)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$FromPaySystemID.' not found'); } if(!in_array($FromCurrencyName, $this->PaymentSystems[$FromPaySystemID]->Currencies)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$FromCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system'); } $DirectionsResult = $this->Query( "Rating_Directions_From", array( 'fromPaySystemID' => $FromPaySystemID, 'fromCurrencyName' => $FromCurrencyName ) ); if($DirectionsResult->IsSuccess) { return $DirectionsResult->Result; } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage); } } /** * Get available directions by payment system and currency code (TO) */ public function Rating_Directions_To($ToPaySystemID, $ToCurrencyName) { if(!array_key_exists($ToPaySystemID, $this->PaymentSystems)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$ToPaySystemID.' not found'); } if(!in_array($ToCurrencyName, $this->PaymentSystems[$ToPaySystemID]->Currencies)) { throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$ToCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system'); } $DirectionsResult = $this->Query( "Rating_Directions_To", array( 'toPaySystemID' => $ToPaySystemID, 'toCurrencyName' => $ToCurrencyName ) ); if($DirectionsResult->IsSuccess) { return $DirectionsResult->Result; } else { throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage); } } } /** * Exceptions */ class OkChangerAPIException extends Exception {} class OkChangerAPIFailureException extends OkChangerAPIException {} class OkChangerAPIInvalidParameterException extends OkChangerAPIException {} ?>