✏️ 正在编辑: Safra.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/library/Core/Banco/Service/Safra.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Banco_Service_Safra implements Core_Banco_Service_ImportacaoDireta { private $_usuario; private $_senha; private $_bancoId; private $_url = ''; private $_client = null; private $_erros; public function __construct(Zend_Config $config, $bancoNome = null) { $this->_bancoId = Model_Banco::bancoPorNome($bancoNome); if(empty($this->_bancoId)){ throw new Yuppie_Exception("O banco Safra não foi configurado"); } $this->_url = 'https://epfweb.safra.com.br/WCF/SvcContratos.svc?wsdl'; $this->_usuario = $config->usuario; $this->_senha = $config->senha; $this->_erros = array(); } public function getErros() { return $this->_erros; } private function addErro($mensagem) { array_push($this->_erros, $mensagem); } protected function setClient() { $opts = array( 'http'=>array( 'user_agent' => 'PHP/SOAP' ) ); $context = stream_context_create($opts); ini_set('default_socket_timeout', 600); $client = new SoapClient($this->_url, array( 'trace' => true, 'soap_version' => SOAP_1_1, 'connection_timeout' => 50000, 'cache_wsdl' => WSDL_CACHE_BOTH, 'keep_alive' => true, 'stream_context' => $context, 'exceptions' => true, )); $this->_client = $client; } public function importar($dataImportacao = null) { $this->setClient(); $naoImportados = array(); $data = self::formatData($dataImportacao); $planilha = new Core_Banco_Service_Safra_WebService($this->_bancoId, null); $operacoes = array('DadosCadastrais', 'Emprestimo', 'Status'); foreach ( $operacoes as $operacao ){ $result = $this->getContratos($operacao,'Data', $data); if( count($result) ){ switch ($operacao){ case 'Emprestimo' : $metodo = 'importarContratosEmprestimos'; break; case 'Status' : $metodo = 'importarStatus'; break; default : $metodo = 'importar'; } $falhaImportacao = $planilha->$metodo($result); if(count($falhaImportacao)){ array_merge($naoImportados, $falhaImportacao); } } } return $naoImportados; } /** * Transforma a data para o padrão Y-m-d * * @param string $dataImportacao * @return string * * @throws Yuppie_Exception */ private static function formatData($dataImportacao) { if (empty($dataImportacao)){ $dt = new DateTime(); return $dt->sub(new DateInterval('P1D'))->format('Y-m-d'); } $data = Core_Util::convertDate($dataImportacao, 'd/m/Y'); if( is_array($data) ){ throw new Yuppie_Exception('Data inválida'); } return $data; } /** * Consulta os dados no webservice do banco * * @param $metodo * @param $tipo * @param $data * @return array * * @throws Core_Exception */ public function getContratos($metodo, $tipo, $data) { $response = $this->requestContratos($metodo, $tipo, $data); if( empty($response) || !empty($this->_client->__soap_fault) ){ switch ($metodo){ case 'DadosCadastrais': $mensagem = "Não foi possível importar os dados cadastrais"; break; case 'Emprestimo': $mensagem = "Não foi possível atualizar os dados de empréstimo"; break; case 'Status': $mensagem = "Não foi possível atualizar os dados de status de propostas"; break; } $this->addErro("Houve uma falha na comunicação com o serviço. $mensagem. Por favor tente novamente em alguns instantes."); return; } return $this->extrairDados($metodo, $response); } /** * Extrai os dados do xml retornado pelo webservice * * @param string $metodo * @param string $response * @return array */ private function extrairDados($metodo, $response) { $xml = simplexml_load_string($response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); $xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF'); if( $metodo == "DadosCadastrais" ){ $metodo = "Cadastro"; } $fieldsProposta = array(); $propostas = $xml->xpath("//a:Retorno{$metodo}"); foreach ( $propostas as $proposta ) { $proposta = $proposta[0]->children("a", true); $numeroProposta = $proposta->NrContrato->__toString(); $numeroProposta = ltrim($numeroProposta, '0'); foreach ( $proposta as $value ) { if( !isset($fieldsProposta[$numeroProposta][$value->getName()]) ){ if( $value->count() > 0 ){ foreach( $value as $child ){ if( $child->count() > 1 ){ foreach( $child as $node ){ $fieldsProposta[$numeroProposta][$node->getName()] = current($node); } }else{ $fieldsProposta[$numeroProposta][$value->getName()] = current($child); } } }else{ $fieldsProposta[$numeroProposta][$value->getName()] = $value->__toString(); } } } } return $fieldsProposta; } /** * Método para retornar o token conforme o tipo da consulta(Data, Proposta, CPF) * * @param string $tipo * @return string token * * @throws Core_Exception */ protected function getToken($tipo) { $result = $this->requestToken($tipo); $xml = simplexml_load_string($result, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); $xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF'); $token = $xml->xpath('//a:DsToken//text()'); $dsRetorno = $xml->xpath("//a:DsRetorno//text()"); $msg = $dsRetorno[0]->__toString(); if( $msg != "Ok" ){ throw new Core_Exception($msg); } return $token[0]->__toString(); } /** * Requisita o token no webservice * * @param $tipo * @return string xml */ protected function requestToken($tipo) { $request = "<x:Envelope xmlns:x='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/' xmlns:epf='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF'> <x:Header/> <x:Body> <tem:GerarToken> <tem:objLogin> <epf:DsSenha>{$this->_senha}'</epf:DsSenha> <epf:DsUsuario>{$this->_usuario}</epf:DsUsuario> <epf:IdTipoConsulta>{$tipo}</epf:IdTipoConsulta> </tem:objLogin> </tem:GerarToken> </x:Body> </x:Envelope>"; return $this->_client->__doRequest($request, $this->_url, 'http://tempuri.org/ISvcContratos/GerarToken', 1); } /** * Requisita os dados do contrato no webservice * * @param string $metodo * @param string $tipo * @param string $dataMovimento * @return string xml * * @throws Core_Exception */ protected function requestContratos($metodo, $tipo, $dataMovimento) { $token = $this->getToken($tipo); $request = "<x:Envelope xmlns:x='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/' xmlns:epf='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF'> <x:Header/> <x:Body> <tem:SelecionaContratos{$metodo}> <tem:objLoginToken> <epf:DsSenha>{$this->_senha}</epf:DsSenha> <epf:DsUsuario>{$this->_usuario}</epf:DsUsuario> <epf:IdTipoConsulta>{$tipo}</epf:IdTipoConsulta> <epf:DsToken>{$token}</epf:DsToken> </tem:objLoginToken> <tem:objFiltros> <epf:DtFiltro>{$dataMovimento}</epf:DtFiltro> </tem:objFiltros> </tem:SelecionaContratos{$metodo}> </x:Body> </x:Envelope>"; return $this->_client->__doRequest($request, $this->_url, "http://tempuri.org/ISvcContratos/SelecionaContratos{$metodo}", 1); } }
💾 保存文件
← 返回文件管理器