Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaniabr committed May 11, 2023
1 parent 4d671b0 commit b7b2e45
Show file tree
Hide file tree
Showing 67 changed files with 3,127 additions and 1 deletion.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# MDFe-PHP-SDK

<p align="center">
<img src="https://wmbr.s3.amazonaws.com/img/logo_webmaniabr_github2.png">
</p>

# MDF-e PHP SDK
SDK de comunicação com API 2.0 da Webmania para MDF-e.
Através do emissor de Nota Fiscal de Serviço da Webmania®, você conta com a emissão e arquivamento das seus MDF-e, encerramento, cancelamento e impressão dos documentos. Realize a integração do seu sistema com esta SDK para o MDF-e.

- Emissor de Nota Fiscal Webmania®: [Saiba mais](https://webmaniabr.com/nota-fiscal-eletronica/)
- Documentação REST API: [Visualizar](https://webmaniabr.com/docs/rest-api-mdfe/)

## Requisitos

- Contrate um dos planos de MDF-e da Webmania® para obter suas credenciais de acesso: [Conheça os Planos (Teste 30 dias grátis!)](https://webmaniabr.com/nota-fiscal-eletronica/#plans-section).
- Obtenha o [Composer](https://getcomposer.org/) e instale o pacote da SDK e suas dependências.

## Endpoints

A SDK possui os recursos necessários para utilizar os endpoints de Emissão, Consulta, Encerramento, Cancelamento e Inclusão de Condutor para o MDF-e.

## Utilização
Instale o módulo da Webmania® via composer ou baixe nosso repositório e utilize as classes de emissão mencionadas mais abaixo:

```php
composer require webmaniabr/mdfe
```

Após executar o composer, adicione o require no topo do seu arquivo, dessa forma as classes da SDK serão carregadas automaticamente.

```php
require_once __DIR__ . '/vendor/autoload.php';
```

Para emissão, deve ser usada a classe MDFe

```php
\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->emitente = \Webmaniabr\Mdfe\Enums\TipoEmitente::PRESTADOR_DE_SERVICO;
$MDFe->transportador = \Webmaniabr\Mdfe\Enums\TipoTransportador::TAC;
$MDFe->ufCarregamento = \Webmaniabr\Mdfe\Enums\UF::GOIAS;
$MDFe->ufDescarregamento = \Webmaniabr\Mdfe\Enums\UF::PARANA;
$MDFe->valorCarga = 500;
...
echo $MDFe->emitir()->getMessage();
```

## Suporte

Qualquer dúvida entre em contato na nossa [Central de Ajuda](https://ajuda.webmaniabr.com) ou acesse o [Painel de Controle](https://webmaniabr.com/painel/) para conversar em tempo real no Chat ou Abrir um chamado.
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "webmaniabr/mdfe",
"type": "library",
"license": "GPL-3.0-or-later",
"description": "PHP SDK da API de MDF-e da Webmania",
"keywords": [ "documento fiscal", "api", "mdfe", "mdf-e", "webmaniabr", "webmania", "transporte", "ferroviario", "aereo", "aquaviario", "rodoviario"],
"homepage": "https://webmaniabr.com/docs/rest-api-mdfe/",
"prefer-stable": true,
"authors": [
{
"name": "Equipe Webmania"
}
],
"require": {
"php": ">=5.2.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"php": ">=5.2.0",
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
"Webmaniabr\\Mdfe\\": "src/"
}
}
}
8 changes: 8 additions & 0 deletions sample/Cancelamento.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require('../vendor/autoload.php');

\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->chave = '00000000000000000000000000000000000000000000';
$MDFe->cancelar('Erro de emissão');
8 changes: 8 additions & 0 deletions sample/Consulta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require('../vendor/autoload.php');

\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->chave = '00000000000000000000000000000000000000000000';
$MDFe->consultar();
27 changes: 27 additions & 0 deletions sample/Emissao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require('../vendor/autoload.php');

\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->emitente = \Webmaniabr\Mdfe\Enums\TipoEmitente::PRESTADOR_DE_SERVICO;
$MDFe->transportador = \Webmaniabr\Mdfe\Enums\TipoTransportador::TAC;
$MDFe->ufCarregamento = \Webmaniabr\Mdfe\Enums\UF::GOIAS;
$MDFe->ufDescarregamento = \Webmaniabr\Mdfe\Enums\UF::PARANA;
$MDFe->valorCarga = 500;
$MDFe->unidade = \Webmaniabr\Mdfe\Enums\Unidade::KG;
$MDFe->pesoBruto = 50000;
$Carregamento = $MDFe->newCarregamento();
$Carregamento->codigoMunicipio = '5208707';
$Carregamento->nomeMunicipio = 'Goiânia';
$Descarregamento = $MDFe->newDescarregamento();
$Descarregamento->codigoMunicipio = '4106902';
$Descarregamento->nomeMunicipio = 'Curitiba';
$DocumentoFiscal = $Descarregamento->newDocumentoFiscal();
$DocumentoFiscal->chave = '00000000000000000000000000000000000000000000';
$MDFe->Percurso->GO()->MG()->SP()->PR();
$Rodoviario = $MDFe->Rodoviario();
$Rodoviario->rntrc = '12313';
$ValePedagio = $Rodoviario->newValePedagio();
$ValePedagio->valor = 40;
$MDFe->emitir();
8 changes: 8 additions & 0 deletions sample/Encerramento.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require('../vendor/autoload.php');

\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->chave = '00000000000000000000000000000000000000000000';
$MDFe->encerrar(\Webmaniabr\Mdfe\Enums\UF::PARANA, "Curitiba");
11 changes: 11 additions & 0 deletions sample/IncluirCondutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require('../vendor/autoload.php');

\Webmaniabr\Mdfe\Api\Connection::getInstance()->setBearerToken('SEU_TOKEN_AUTENTICACAO');
$MDFe = new \Webmaniabr\Mdfe\Models\MDFe();
$MDFe->chave = '00000000000000000000000000000000000000000000';
$Condutor = new \Webmaniabr\Mdfe\Models\Modalidades\Rodoviario\Condutor();
$Condutor->nome = 'Fulano de Tal';
$Condutor->cpf = "000.000.000-00";
$MDFe->incluirCondutor($Condutor);
79 changes: 79 additions & 0 deletions src/Api/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Webmaniabr\Mdfe\Api;

use Webmaniabr\Mdfe\Interfaces\APIConnection;

class Connection implements APIConnection
{
/**
* Bearer token de autenticação na API.
* @var string
*/
private string $bearerToken = '';

/**
* Informações de comunicação com proxy.
* @see https://docs.guzzlephp.org/en/stable/request-options.html#proxy
* @var array
*/
private array $proxy = [];

private static Connection $instance;

private function __construct() { }

/**
* @return $this
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Define o token de autenticação.
* @param string $token
*/
public function setBearerToken(string $token)
{
$this->bearerToken = $token;
}

/**
* Retorna o token de autenticação.
* @return string
*/
public function getBearerToken() : string
{
return $this->bearerToken;
}

/**
* Define as informações do proxy.
* @param array $proxy
*/
public function setProxy(array $proxy)
{
$this->proxy = $proxy;
}

/**
* {@inheritDoc}
*/
public function getDomain() : string
{
return 'https://api.webmaniabr.com';
}

/**
* {@inheritDoc}
*/
public function getProxy() : array
{
return $this->proxy;
}
}
44 changes: 44 additions & 0 deletions src/Api/Endpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Webmaniabr\Mdfe\Api;

class Endpoint
{
private string $url;
private string $method;
private array $headers = [];

public function __construct(string $url, string $method, array $headers)
{
$this->url = $url;
$this->method = $method;
$this->headers = $headers;
}

/**
* Retorna a URL do endpoint.
* @return string
*/
public function getUrl() : string
{
return $this->url;
}

/**
* Retorna o método HTTP da requisição.
* @return string
*/
public function getMethod() : string
{
return $this->method;
}

/**
* Retorna os cabeçalhos para a requisição.
* @return array
*/
public function getHeaders() : array
{
return $this->headers;
}
}
10 changes: 10 additions & 0 deletions src/Api/Exceptions/APIException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Webmaniabr\Mdfe\Api\Exceptions;

use Exception;

class APIException extends Exception
{

}
32 changes: 32 additions & 0 deletions src/Api/Http/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Webmaniabr\Mdfe\Api\Http;

use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use Webmaniabr\Mdfe\Api\Connection;
use Webmaniabr\Mdfe\Api\Responses\FactoryResponse;
use Webmaniabr\Mdfe\Interfaces\APIOperation;
use Webmaniabr\Mdfe\Interfaces\APIResponse;

class Client
{
/**
* Envia a requisição de uma Operação da MDF-e.
* @param APIOperation $operation
* @return APIResponse
*/
public function send(APIOperation $operation) : APIResponse
{
$endpoint = $operation->getEndpoint();
$url = Connection::getInstance()->getDomain() . $endpoint->getUrl();
$request = new Request($endpoint->getMethod(), $url, $endpoint->getHeaders(), $operation->getContentToSend());
try {
$response = (new GuzzleHttpClient())->sendAsync($request)->wait();
} catch (ClientException $ex) {
$response = $ex->getResponse();
}
return FactoryResponse::getResponseInstance($response);
}
}
Loading

0 comments on commit b7b2e45

Please sign in to comment.