Skip to content

Commit

Permalink
Merge pull request #4 from JoaoRibeiroDeveloper/UpdatePdfXml23
Browse files Browse the repository at this point in the history
Add Token generation for XML and PDF url
  • Loading branch information
webmaniabr authored Jul 9, 2024
2 parents 12644bc + cef12ed commit ccd5863
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 2.3/admin/controller/extension/module/webmaniabrnfe.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ public function emitirNfe(){
'n_serie' => (int) $response->serie,
'url_xml' => (string) $response->xml,
'url_danfe' => (string) $response->danfe,
'documento' => (string) ($data['cliente']['cpf'] ?? $data['cliente']['cnpj'] ?? ''),
'data' => date('d/m/Y'),
);

Expand Down Expand Up @@ -580,6 +581,12 @@ function displayMessageCertificado(){

}

function get_value_from_fields( $data ){

return $this->NFeFunctions->get_value_from_fields($data["key"], $data["custom_fields_ids"], $data["custom_fields_customer"]);

}

function is_cpf( $cpf = null ){

return $this->NFeFunctions->is_cpf( $cpf );
Expand Down Expand Up @@ -684,4 +691,14 @@ public function get_order_transporte_info( $order_id ){

}

public function createSecureTokenDFe( $data ){

$password = preg_replace("/[^0-9]/", '', $data['password']);
$key = hash('sha256', $password . ':' . $data['uuid'], true);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-CBC'));
$encryptedData = openssl_encrypt(time(), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
$tokenData = json_encode(['data' => base64_encode($encryptedData), 'iv' => base64_encode($iv)]);
return urlencode(base64_encode($tokenData));

}
}
32 changes: 32 additions & 0 deletions 2.3/admin/controller/extension/nfe/xml/nfe.ocmod.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,38 @@
if (!$nfe_info) {
$nfe_info = @unserialize(utf8_decode(base64_decode($query->rows[0]['nfe_info'])));
}
if (!$nfe_info) {
$nfe_info = array();
}
$custom_fields_customer = !empty($order_info['custom_field']) && is_array($order_info['custom_field']) ? $order_info['custom_field'] : null;
if(!$custom_fields_customer){
$customer_info = $this->model_customer_customer->getCustomer($order_info['customer_id']);
$custom_fields_customer = @unserialize($customer_info['custom_field']);
if (!$custom_fields_customer) $custom_fields_customer = json_decode($customer_info['custom_field'], true);
}
$custom_fields_ids = $this->load->controller('extension/module/webmaniabrnfe/getCustomFieldsIds');
$route_get_value_from_fields = 'extension/module/webmaniabrnfe/get_value_from_fields';
$data_get_value_from_fields = ['key' => 'tipo_pessoa', 'custom_fields_ids' => $custom_fields_ids, 'custom_fields_customer' => $custom_fields_customer,];
$document_type = $this->load->controller($route_get_value_from_fields, $data_get_value_from_fields);
$document = '';
if($document_type == $this->load->controller($route_get_value_from_fields, array_merge($data_get_value_from_fields, ['key' => 'pessoa_fisica']))){
$document = $this->load->controller($route_get_value_from_fields, array_merge($data_get_value_from_fields, ['key' => 'cpf']));
}elseif($document_type == $this->load->controller($route_get_value_from_fields, array_merge($data_get_value_from_fields, ['key' => 'pessoa_juridica']))){
$document = $this->load->controller($route_get_value_from_fields, array_merge($data_get_value_from_fields, ['key' => 'cnpj']));
}
foreach ($nfe_info as $key => $nfe) {
$password = !empty($nfe['documento']) ? $nfe['documento'] : $document;
if(!empty($password)){
$tokens = [
"danfe" => $this->load->controller('extension/module/webmaniabrnfe/createSecureTokenDFe', ["password" => $password, "uuid" => $nfe['uuid']]),
"xml" => $this->load->controller('extensionmodule/webmaniabrnfe/createSecureTokenDFe', ["password" => $password, "uuid" => $nfe['uuid']]),
];
$nfe_info[$key]['url_danfe'] = $nfe_info[$key]['url_danfe'] . '?token=' . $tokens['danfe'];
$nfe_info[$key]['url_xml'] = $nfe_info[$key]['url_xml'] . '?token=' . $tokens['xml'];
}
}
$data['nfe_info'] = $nfe_info;
$data['url'] = $url;
}else{
Expand Down

0 comments on commit ccd5863

Please sign in to comment.