marshmallow/keen-delivery

连接您的 Laravel 应用程序与 Keen Delivery

v2.0.0 2023-09-06 14:08 UTC

This package is auto-updated.

Last update: 2024-09-06 16:13:35 UTC


README

alt text

Laravel Keen Delivery

Latest Version on Packagist Total Downloads Issues Stars Forks

此软件包使您能够轻松连接到 Keen Delivery API。您只需将一个特质添加到您的模型中即可连接,该模型应该能够进行运输。软件包将处理其余部分。我们还添加了多个功能,使您能够通过 Laravel Nova 管理您的运输。

DeliveryActions

安装

您可以使用 composer 安装此软件包。

composer require marshmallow/keen-delivery

迁移

运行迁移。迁移将在您的数据库中创建一个表,其中将存储所有运输。我们还将存储 Keen Delivery API 的请求和响应。

php artisan migrate

发布配置

发布配置后,请根据您的规格更改配置。下面将解释所有配置值。

php artisan vendor:publish --provider="Marshmallow\KeenDelivery\ServiceProvider" --tag="config"

更新您的 .env 文件

将您的 Sendy 访问令牌添加到您的 .env 文件中。并添加 Sendy 商店 UUiD。

SENDY_ACCESS_TOKEN="*****"
SENDY_SHOP_ID="*****"

要启用旧的 Keen Delivery API,请将以下变量设置为 true。

KEEN_DELIVERY_LEGACY_ENABLED=true

将您的 Keen Delivery API 令牌添加到您的 .env 文件中。您也可以将其添加到您的配置中,但不建议这样做。

KEEN_DELIVERY_API_TOKEN="*****"

用法

将特质添加到您的模型中

首先,您需要将 KeenDelivery 特质添加到可以运输的模型中。

use Marshmallow\KeenDelivery\Traits\KeenDelivery;

class Order
{
    use KeenDelivery;
    // ...

实现抽象方法

接下来,您需要实现我们需要的所有方法,以便使用 KeenDelivery 创建运输。以下是一个示例。

use Marshmallow\KeenDelivery\Traits\KeenDelivery;

class Order
{
    use KeenDelivery;

    // ...

    /**
     * Keen Delivery
     */
    public function getDeliveryReference(): string
    {
        return __('Shipment for order: #:order_id', [
            'order_id' => $this->id,
        ]);
    }

    public function getDeliveryCompanyName(): ?string
    {
        return $this->shippingAddress()->company_name;
    }

    public function getDeliveryContactPerson(): ?string
    {
        return $this->shippingAddress()->name;
    }

    public function getDeliveryStreet(): string
    {
        return $this->shippingAddress()->address;
    }

    public function getDeliveryNumber(): string
    {
        return $this->shippingAddress()->house_number;
    }

    public function getDeliveryAddition(): ?string
    {
        return $this->shippingAddress()->house_number_addon;
    }

    public function getDeliveryZipCode(): string
    {
        return $this->shippingAddress()->postal_code;
    }

    public function getDeliveryCity(): string
    {
        return $this->shippingAddress()->city;
    }

    public function getDeliveryCountry(): string
    {
        return $this->shippingAddress()->country?->id ?? 'NL';
    }

    public function getDeliveryPhone(): ?string
    {
        return $this->customer->phone_number;
    }

    public function getDeliveryEmail(): ?string
    {
        return $this->customer->email;
    }

    public function getDeliveryComment(): ?string
    {
        return null;
    }

    public function getDeliveryPredict(): ?string
    {
        return '';
    }

    /**
     * Return the weight in kilo's
     */
    public function getDeliveryWeight(): ?int
    {
        return 1;
    }

    public function getCustomDeliveryData(): array
    {
        return [
            /**
             * Use email notifications for DPD
             */
            'predict' => 2,
        ];
    }

创建运输

完成此操作后,您可以创建您的运输标签,如下所示

$order->createShipment()

事件

此软件包将在某些操作上触发事件。以下是可以挂钩的事件概述。

在 Laravel Nova 中使用

如果您想在 Nova 中使用此软件包,您必须首先发布 Nova 资源。可以通过运行以下命令来完成此操作。如果该文件尚不存在,此命令将创建 app\Nova\Delivery.php 文件。

DeliveryDetail

php artisan marshmallow:resource Delivery KeenDelivery

更新您自己的资源

在此示例中,我们假设您想将您的订单与该软件包相关联。我们有一些有用的函数,在 Nova 中提供了大量信息和可能性。在此示例中,我们谈论的是订单,但是这可以应用于您的所有模型和资源。

显示运输

将以下关系添加到 App\Nova\Order,以便在订单的详细视图中显示运输。

DeliveryIndex

public function fields(Request $request)
{
    // ...
    MorphMany::make(__('Deliveries'), 'deliverable', Delivery::class),
}

在索引中显示跟踪信息

我们创建了一个助手,允许您将 跟踪 & 追踪 编号和下载按钮添加到资源的索引中,以下载运输标签。

OrderIndex

use Marshmallow\KeenDelivery\Facades\KeenDelivery;

public function fields(Request $request)
{
    // ...
    KeenDelivery::shipmentInfoField(),
}

过滤器

我们还有一个默认的过滤器。使用此过滤器,您可以过滤所有已成功提交给 Keen Delivery 的订单。您还可以过滤尚未或未成功提交给 Keen Delivery 的订单。

DeliveryFilters

public function filters(Request $request)
{
    return [
        new ShipmentNotifiedFilter,
    ];
}

操作

此软件包中有两个操作可以使您的生活更加轻松。

DeliveryActions

提交以运输

使用此操作,您可以从 Nova 将订单提交给 Keen Delivery。您还可以同时提交多个订单。

public function actions(Request $request)
{
    return [
        //
        new SubmitForShipment,
    ];
}

下载标签

使用此操作,您可以下载运输标签。您还可以一次性下载多个订单的运输标签。

public function actions(Request $request)
{
    return [
        //
        new DownloadLabels,
    ];
}

API

以下API方法对设置此包很有用。使用verifyApiToken,您可以检查您是否真的与Keen Deliver API建立了有效连接,使用listShippingMethods,您可以看到哪些运输方式可供您使用。

use Marshmallow\KeenDelivery\Facades\KeenDeliveryApi;

KeenDeliveryApi::verifyApiToken();
KeenDeliveryApi::listShippingMethods();

Sendy Api

以下API方法对设置此包很有用。使用me,您可以检查您是否真的与Keen Deliver API建立了有效连接,使用listShippingMethods,您可以看到哪些运输方式可供您使用。您还可以使用“shops”方法获取所有连接到您账户的商店。或者使用getShopUuid获取第一个商店的UUID。

use Marshmallow\KeenDelivery\Facades\SendyApi;

SendyApi::me();
SendyApi::getShopUuid();
SendyApi::listShops();
SendyApi::getShippingPreference();
SendyApi::listShippingPreferences();
SendyApi::listCarriers();
SendyApi::listShippingMethods();
SendyApi::listServices($carrierId);
SendyApi::getShipment($shipmentId);

承运商

以下表格显示了此包目前支持的承运商和服务。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全

如果您发现任何安全相关的问题,请通过电子邮件stef@marshmallow.dev联系,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件

资源

API文档:https://keendelivery.readthedocs.io/en/latest/

版权(c)2021 marshmallow。