{"id":33985475,"url":"https://github.com/melhorenvio/melhor-envio-sdk-php","last_synced_at":"2025-12-13T04:56:19.964Z","repository":{"id":42126821,"uuid":"466142423","full_name":"melhorenvio/melhor-envio-sdk-php","owner":"melhorenvio","description":null,"archived":false,"fork":false,"pushed_at":"2023-08-17T09:19:57.000Z","size":48,"stargazers_count":11,"open_issues_count":2,"forks_count":6,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-06T16:07:50.859Z","etag":null,"topics":["api-docs","integracoes","tecnologia"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/melhorenvio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-04T13:54:12.000Z","updated_at":"2024-07-25T14:37:00.000Z","dependencies_parsed_at":"2023-01-24T22:00:16.756Z","dependency_job_id":null,"html_url":"https://github.com/melhorenvio/melhor-envio-sdk-php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/melhorenvio/melhor-envio-sdk-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melhorenvio%2Fmelhor-envio-sdk-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melhorenvio%2Fmelhor-envio-sdk-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melhorenvio%2Fmelhor-envio-sdk-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melhorenvio%2Fmelhor-envio-sdk-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/melhorenvio","download_url":"https://codeload.github.com/melhorenvio/melhor-envio-sdk-php/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melhorenvio%2Fmelhor-envio-sdk-php/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27700379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-docs","integracoes","tecnologia"],"created_at":"2025-12-13T04:56:18.793Z","updated_at":"2025-12-13T04:56:19.957Z","avatar_url":"https://github.com/melhorenvio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Melhor Envio SDK - Integração com Melhor Envio\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/melhorenvio/melhor-envio-sdk-php.svg?style=flat-square)](https://packagist.org/packages/melhorenvio/melhor-envio-sdk-php)\n[![Total Downloads](https://img.shields.io/packagist/dt/melhorenvio/melhor-envio-sdk-php.svg?style=flat-square)](https://packagist.org/packages/melhorenvio/melhor-envio-sdk-php)\n\nAgora ficou mais fácil ter o serviço do Melhor Envio no seu projeto de e-commerce.\n\n## Indice\n\n* [Instalação](#instalacao)\n* [Cofiguração Inicial](##configuração-inicial)\n* [Exemplos de uso](##Criando-a-instância-do-Melhor-Envio)\n* [Mais exemplos](##Mais-Exemplos)\n* [Testes](##Testes)\n* [Changelog](##Changelog)\n* [Contribuindo](##Contribuindo)\n* [Segurança](##Segurança)\n* [Créditos](##Créditos)\n* [Licença](##Licença)\n\n### require \n* PHP \u003e= 7.4\n* Ext-json = *\n* Guzzlehttp/guzzle \u003e= 6.5\n\n### require-dev\n* phpunit/phpunit \u003e= 5\n\n\n## Instalação\n\nVocê pode instalar o pacote via composer, rodando o seguinte comando:\n\n```bash\ncomposer require melhorenvio/melhor-envio-sdk-php\n```\n\n## Configuração inicial\n### Obtendo link de autorização de conta do Melhor Envio\n```php\nrequire \"./vendor/autoload.php\";\n\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\Event;\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\OAuth2;\nuse MelhorEnvio\\Resources\\Shipment\\Product;\n\n$provider = new OAuth2(\n    $appData['client_id'],\n    $appData['client_secret'],\n    $appData['redirect_uri']\n);\n\n$provider-\u003esetScopes('shipping-calculate');\n$linkAuthorize = $provider-\u003egetAuthorizationUrl();\n\necho $linkAuthorize;\n```\n### Obtendo Access Token e Refresh Token\nPara maiores informações sobre autenticação, acessar a documentação do Auth SDK:\nhttps://packagist.org/packages/melhorenvio/auth-sdk-php\n```php\nrequire \"./vendor/autoload.php\";\n\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\Event;\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\OAuth2;\nuse MelhorEnvio\\Resources\\Shipment\\Product;\n\n$provider = new OAuth2(\n    $appData['client_id'],\n    $appData['client_secret'],\n    $appData['redirect_uri']\n);\n\n$code = $_GET['code'];\n\n$tokens = $provider-\u003egetAccessToken($code);\nvar_dump($tokens);\ndie;\n```\n\n### Realizando cotações\nPara maiores informações sobre cotações, acessar a documentação do Shipment SDK:\nhttps://packagist.org/packages/melhorenvio/shipment-sdk-php\n\n```php\nrequire \"./vendor/autoload.php\";\n\nuse MelhorEnvio\\Enums\\Environment;\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\Event;\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\OAuth2;\nuse MelhorEnvio\\MelhorEnvioSdkPhp\\Shipment;\nuse MelhorEnvio\\Resources\\Shipment\\Product;\n\nEvent::listen('refresh', function ($token, $refreshToken) {\n    // Put here trading rule to save accessToken e refreshToken.\n});\n\n$oAuth2 = new OAuth2(\n    CLIENT_ID,\n    TEST_CLIENT_SECRET,\n    TEST_REDIRECT_URI\n);\n\n$this-\u003eshipment = new Shipment(\n    $oAuth2,\n    ACCESS_TOKEN,\n    REFRESH_TOKEN\n);\n\n$calculator = $shipment-\u003ecalculator();\n\n$calculator-\u003epostalCode('01010010', '20271130');\n\n$calculator-\u003esetOwnHand();\n$calculator-\u003esetReceipt(false);\n$calculator-\u003esetCollect(false);\n\n$calculator-\u003eaddProducts(\n    new Product(uniqid(), 40, 30, 50, 10.00, 100.0, 1),\n    new Product(uniqid(), 5, 1, 10, 0.1, 50.0, 1)\n);\n\n$quotations = $calculator-\u003ecalculate();\n\nvar_dump($quotations);\n```\n\n### Recebendo Access Tokens e Refresh Tokens atualizados\nO Access Token gerado pelo Melhor Envio tem a validade de 1(um) mês, após esse período é possível atualizar o token de forma automatiza com o refresh token, por isso é necessário sempre manter atulizado os access tokens e refresh tokens, visando isso, o Melhor Envio SDK possui um evento de listerner de receber com os dados de tokens atualizados.  \nVocê deverá implementar a lógica para persistir esses dados na sua plataforma, veja um exemplo abaixo:\n```php\nEvent::listen('refresh', function ($token, $refreshToken) {\n    // Aqui deve ser inserido a sua lógica de persitir as informações na sua plataforma, o código abaixo é apenas um exemplo, o mesmo deve ser subistituido para a sua realidade.\n    Credentials::update([\n       'access_token' =\u003e $token,\n       'refresh_token' =\u003e $refreshToken \n   ]) \n});\n```\n\n## Criando a instância do Melhor Envio\n\n\n### Mais exemplos\n\n[Aqui você pode acessar mais exemplos de implementação](/examples)\n\n### Testes\n\nDentro do projeto você encontrará alguns documentos de teste baseados em testes unitários\n\n\nVocê pode usar na aplicação tanto o comando:\n``` bash\ncomposer test\n```\nQuanto o comando:\n```bash\nvendor/bin/phpunit tests \n```\n\n### Changelog\n\nConsulte [CHANGELOG](CHANGELOG.md) para mais informações de alterações recentes.\n\n## Contribuindo\n\nConsulte [CONTRIBUTING](CONTRIBUTING.md) para mais detalhes.\n\n### Segurança\n\nSe você descobrir algum problema de segurança, por favor, envie um e-mail para tecnologia@melhorenvio.com, ao invés de usar um *issue tracker*.\n\n## Créditos\n\n- [Vinícius Schlee Tessmann](https://github.com/viniciustessmann)\n\n## Licença\n\nMelhor Envio. Consulte [Arquivo de lincença](LICENSE.md) para mais informações.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelhorenvio%2Fmelhor-envio-sdk-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmelhorenvio%2Fmelhor-envio-sdk-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelhorenvio%2Fmelhor-envio-sdk-php/lists"}