<?php
 namespace App\CurrencyExchange; class System_NbsRS extends SystemAbstract { protected $referenceCurrency='RSD'; protected $systemAddress='https://webservices.nbs.rs/CommunicationOfficeService1_0/CurrentExchangeRateService.asmx?WSDL'; protected $auth_licenseNo = null; protected $auth_username = null; protected $auth_password = null; protected $rateUsed = null; protected $exchangeRateListTypeID=null; public function __construct($date='') { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $this->auth_licenseNo = $CFG['currencies']['System']['NbsRS']['auth_licenseNo']; $this->auth_username = $CFG['currencies']['System']['NbsRS']['auth_username']; $this->auth_password = $CFG['currencies']['System']['NbsRS']['auth_password']; switch ($CFG['currencies']['System']['NbsRS']['rateLoaded']) { case 'selling': $this->rateUsed = 'SellingRate'; $this->exchangeRateListTypeID = 1; break; case 'middle': $this->rateUsed = 'MiddleRate'; $this->exchangeRateListTypeID = 3; break; default: throw new \Exception(sprintf("[%s] Unknown loaded rate defined: %s", __METHOD__, $CFG['currencies']['System']['NbsRS']['rateLoaded'])); } parent::__construct($date); } public function getRates() { try { if ($this->proxyHost) { $client = new \soapclient($this->systemAddress, [ 'trace' => true, 'exceptions' => true, 'encoding' =>'utf-8', 'connection_timeout'=> 15, 'proxy_host' => $this->proxyHost, 'proxy_port' => $this->proxyPort, 'proxy_login' => $this->proxyUser, 'proxy_password' => $this->proxyPass, ] ); } else { $client = new \soapclient($this->systemAddress, [ 'trace' => true, 'exceptions' => true, 'encoding' =>'utf-8', 'connection_timeout'=> 15 ] ); } $ns="http://communicationoffice.nbs.rs"; $header = new \SoapHeader($ns, "AuthenticationHeader", ['UserName'=>$this->auth_username, 'Password'=>$this->auth_password, 'LicenceID'=>$this->auth_licenseNo]); $client->__setSoapHeaders($header); $response=$client->GetCurrentExchangeRate( ['exchangeRateListTypeID'=>$this->exchangeRateListTypeID]); } catch (\SoapFault $fault) { throw new Exception("An error occured during webservice request: ".$fault->faultstring); } if (!$response->GetCurrentExchangeRateResult->any) throw new Exception("The server content is not valid."); $fileContent=@simplexml_load_string($response->GetCurrentExchangeRateResult->any); if (!$fileContent) throw new Exception('The external system webservice reply was not a well formed XML.'); $result=[]; $items=$fileContent->ExchangeRateDataSet[0]; $rateUsed=$this->rateUsed; if ($items) { foreach ($items as $thisCurr) { $currCode=(String)$thisCurr->CurrencyCodeAlfaChar; $units=(float)$thisCurr->Unit; $rate=(float)$thisCurr->{$rateUsed}; if ($units>0) $result[$currCode]=$rate/$units; } } return $result; } } 