canislupus/api-client-melhor-envio

0.1.1 2023-10-12 16:40 UTC

This package is auto-updated.

Last update: 2024-09-12 18:39:28 UTC


README

Cliente para integração com a API do sistema Melhor Envio. Conheça o Melhor Envio

Instalação

Composer

composer require canislupus/api-client-melhor-envio

Utilização

use CanisLupus\ApiClients\MelhorEnvio\v2\MelhorEnvioApiClient;
use CanisLupus\ApiClients\MelhorEnvio\v2\Enums\EnvironmentEnum;
use CanisLupus\ApiClients\MelhorEnvio\v2\MelhorEnvioApiConfig;
use CanisLupus\ApiClients\MelhorEnvio\v2\Exceptions\MelhorEnvioApiException;

$appName = 'Canis Lupus';
$appSupportEmail = 'leoaguiarpereira@gmail.com';
$token = '#TOKEN#';

// Inicialize o cliente passando um objeto de configuração
$clientMelhorEnvio = new MelhorEnvioApiClient(
    new MelhorEnvioApiConfig(EnvironmentEnum::Sandbox, $appName, $appSupportEmail, $token)
);

// Exemplo para Cotação de Frete
try {    
    $fretes = $clientMelhorEnvio->shipment->calculate([
        'from' => ['postal_code' => "36085400"],
        'to' => ['postal_code' => "36025007"],
        'products' => [
            [
                'id' => 1,
                'width' => 10,
                'height' => 10,
                'length' => 10,
                'weight' => 1,
                'insurance_value' => 200,
                'quantity' => 1
            ]
        ]
    ]);

    $fretesComValor = [];
    foreach ($fretes as $frete) {
        if ($frete->getError()) continue;

        $fretesComValor[] = [
            'empresa' => $frete->getCompany()->getName(),
            'servico' => $frete->getServiceName(),
            'preco' => $frete->getPrice(),
            'prazoEntrega' => $frete->getDeliveryTime() . ' dias úteis'
        ];
    }
    echo "<pre>";
    var_dump($fretesComValor);
    echo "</pre>";
    die;

} catch (MelhorEnvioApiException $e) {
    die($e->getMessage());
}

Mais exemplos

Mais exemplos podem ser encontrados na pasta docs/examples

Abrangência

Lista de API's

Licença

  • MIT License