diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php new file mode 100644 index 0000000..92241b5 --- /dev/null +++ b/src/Form/SettingsForm.php @@ -0,0 +1,59 @@ +config('webform_pagamentos.settings'); + + $form['user'] = [ + '#type' => 'textfield', + '#title' => $this->t('Usuário'), + '#default_value' => $config->get('user'), + '#required' => FALSE, + '#attributes' => [ + 'placeholder' => $this->t('Exemplo: fflch'), + ], + ]; + + $form['password'] = [ + '#type' => 'password', + '#title' => $this->t('Senha'), + '#default_value' => $config->get('password'), + '#required' => FALSE, + ]; + + return parent::buildForm($form, $form_state); + } + + public function submitForm(array &$form, FormStateInterface $form_state): void { + + $config = $this->configFactory() + ->getEditable('webform_pagamentos.settings'); + + $config->set('user', $form_state->getValue('user')); + + // Only update password if a new one was entered. + if ($password = $form_state->getValue('password')) { + $config->set('password', $password); + } + + $config->save(); + + parent::submitForm($form, $form_state); +} + +} diff --git a/src/Service/GerarBoleto.php b/src/Service/GerarBoleto.php new file mode 100644 index 0000000..aa1cd17 --- /dev/null +++ b/src/Service/GerarBoleto.php @@ -0,0 +1,119 @@ +get('user'); + $password = $config->get('password'); + + /// + /// + + + $boleto = new Boleto($user, $password); + /* array com campos mínimos para geração do boleto */ + $data = array( + 'codigoUnidadeDespesa' => 8, + 'codigoFonteRecurso' => $codigoFonteRecursoSET, + 'estruturaHierarquica' => $estruturaHierarquicaSET, + 'dataVencimentoBoleto' => $dataVencimentoSET, + 'valorDocumento' => $valorDocumentoSET, + 'tipoSacado' => 'PF', + 'cpfCnpj' => $cpfCnpjSET, + 'nomeSacado' => $nomeSacadoSET, + 'codigoEmail' => $codigoEmailSET, + 'informacoesBoletoSacado' => $informacoesSacadoSET, + 'instrucoesObjetoCobranca' => $instrucoesObjetoCobrancaSET, + ); + $gerar = $boleto->gerar($data); + dd($gerar); + $id = $gerar['value']; + $obter = $boleto->obter($id); + + header('Content-type: application/pdf'); + header('Content-Disposition: attachment; filename="boleto.pdf"'); + echo base64_decode($obter['value']); +} +}; + + + + + + + + +// // Inicialização do serviço com as credenciais criadas +// // [Ambiente de DEV] = ('consumerdi','teste1') +// // [Ambiente de PRD] = solicitar credenciais em https://servicos.sti.usp.br/ws-boleto/ +// $boleto = new Boleto('usuario','senha'); +// +// /* array com campos mínimos para geração do boleto */ +// $data = array( +// 'codigoUnidadeDespesa' => 8, +// 'codigoFonteRecurso' => 32, +// 'estruturaHierarquica' => '\FFLCH\SCINFOR', +// 'dataVencimentoBoleto' => '10/11/2018', +// 'valorDocumento' => 18.20, +// 'tipoSacado' => 'PF', +// 'cpfCnpj' => '99999999999', +// 'nomeSacado' => 'Fulano', +// 'codigoEmail' => 'fulano@usp.br', +// 'informacoesBoletoSacado' => 'Qualquer informações que queira colocar', +// 'instrucoesObjetoCobranca' => 'Não receber após vencimento!', +// ); +// +// // [Método Gerar] gerar boleto +// $gerar = $boleto->gerar($data); +// if($gerar['status']) { +// $id = $gerar['value']; +// +// // [Método Situacao] resgatar informações do boleto +// print_r($boleto->situacao($id)); +// +// // [Método Obter] recupera o arquivo PDF do boleto +// // (PDF no formato binário codificado para Base64) +// $obter = $boleto->obter($codigoIDBoleto); +// +// //redirecionando os dados binarios do pdf para o browser +// header('Content-type: application/pdf'); +// header('Content-Disposition: attachment; filename="boleto.pdf"'); +// echo base64_decode($obter['value']); +// +// // [Método Cancelar] cancelar boleto +// $boleto->cancelar($id); +// } diff --git a/webform_pagamentos.links.menu.yml b/webform_pagamentos.links.menu.yml new file mode 100644 index 0000000..d3888d4 --- /dev/null +++ b/webform_pagamentos.links.menu.yml @@ -0,0 +1,5 @@ +webform_pagamentos.settings: + title: 'Webform Pagamentos' + description: 'Configurações de autenticação do boleto USP' + parent: system.admin_config_services + route_name: webform_pagamentos.settings diff --git a/webform_pagamentos.module b/webform_pagamentos.module index ccab1ca..c6a51e5 100644 --- a/webform_pagamentos.module +++ b/webform_pagamentos.module @@ -204,4 +204,17 @@ function webform_pagamentos_webform_third_party_settings_form_alter(array &$form ]; } +function webform_pagamentos_webform_submission_insert(WebformSubmissionInterface $webform_submission) { + $settings = $webform_submission + ->getWebform() + ->getThirdPartySettings('webform_pagamentos'); + if (empty($settings['habilitar_boleto'])) { + return; + } + + $submission_data = $webform_submission->getData(); + + \Drupal::service('webform_pagamentos.gerar_boleto') + ->generate($settings, $webform_submission->getData()); +} diff --git a/webform_pagamentos.routing.yml b/webform_pagamentos.routing.yml new file mode 100644 index 0000000..d5b738c --- /dev/null +++ b/webform_pagamentos.routing.yml @@ -0,0 +1,7 @@ +webform_pagamentos.settings: + path: '/admin/config/webform-pagamentos/settings' + defaults: + _form: '\Drupal\webform_pagamentos\Form\SettingsForm' + _title: 'Webform Pagamentos' + requirements: + _permission: 'administer site configuration' diff --git a/webform_pagamentos.services.yml b/webform_pagamentos.services.yml new file mode 100644 index 0000000..3e69723 --- /dev/null +++ b/webform_pagamentos.services.yml @@ -0,0 +1,3 @@ +services: + webform_pagamentos.gerar_boleto: + class: Drupal\webform_pagamentos\Service\GerarBoleto