Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Drupal\webform_pagamentos\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class SettingsForm extends ConfigFormBase {

protected function getEditableConfigNames(): array {
return ['webform_pagamentos.settings'];
}

public function getFormId(): string {
return 'webform_pagamentos_settings';
}

public function buildForm(array $form, FormStateInterface $form_state): array {

$config = $this->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);
}

}
119 changes: 119 additions & 0 deletions src/Service/GerarBoleto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Drupal\webform_pagamentos\Service;
use Uspdev\Boleto;


class GerarBoleto {

public function generate(array $settings, array $submission_data): array {

$codigoFonteRecursoSET = $settings['codigoFonteRecurso'] ?? '';
$estruturaHierarquicaSET = $settings['estruturaHierarquica'] ?? '';
$dataVencimentoSET = $settings['dataVencimento'] ?? '';
$informacoesSacadoSET = $settings['informacoesSacado'] ?? '';
$instrucoesObjetoCobrancaSET = $settings['instrucoesObjetoCobranca'] ?? '';
$valorDocumentoSET = $settings['valorDocumento'] ?? '';
$nomeSacadoSET = $settings['nomeSacado'] ?? '';
$codigoEmailSET = $settings['codigoEmail'] ?? '';
$cpfCnpjSET = $settings['cpfCnpj'] ?? '';
$numeroUspSacadoSET = $settings['numeroUspSacado'] ?? '';


//GUARDANDO PARA CASO PRECISE ADICIONAR INFORMAÇÕES COLOCADAS PELO USUÁRIO:

// $codigoFonteRecurso = $submission_data[$codigoFonteRecursoSET] ?? '';
// $estruturaHierarquica = $submission_data[$estruturaHierarquicaSET] ?? '';
// $dataVencimento = $submission_data[$dataVencimentoSET] ?? '';
// $informacoesSacado = $submission_data[$informacoesSacadoSET] ?? '';
// $instrucoesObjetoCobranca = $submission_data[$instrucoesObjetoCobrancaSET] ?? '';
// $valorDocumento = $submission_data[$valorDocumentoSET] ?? '';
// $nomeSacado = $submission_data[$nomeSacadoSET] ?? '';
// $codigoEmail = $submission_data[$codigoEmailSET] ?? '';
// $cpfCnpj = $submission_data[$cpfCnpjSET] ?? '';
// $numeroUspSacado = $submission_data[$numeroUspSacadoSET] ?? '';

///
///Recupera USER E LOGIN a partir do Settings no menuzinho lá de configuração do Drupal ;)

$config = \Drupal::config('webform_pagamentos.settings');
$user = $config->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);
// }
5 changes: 5 additions & 0 deletions webform_pagamentos.links.menu.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions webform_pagamentos.module
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
7 changes: 7 additions & 0 deletions webform_pagamentos.routing.yml
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 3 additions & 0 deletions webform_pagamentos.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
webform_pagamentos.gerar_boleto:
class: Drupal\webform_pagamentos\Service\GerarBoleto