muscobytes / laravel-cdek-webhook
在您的Laravel应用程序中轻松创建CDEK端点。
1.0.5
2023-05-30 06:51 UTC
Requires
- spatie/laravel-data: ^3
- spatie/laravel-enum: ^3.0
Requires (Dev)
- orchestra/testbench: ^8.5
README
安装
composer require muscobytes/laravel-cdek-webhook
配置
配置包包括确定处理CDEK请求的URL。这可以通过两种方式完成:通过.env文件设置变量或在config/cdek.php文件中定义变量。
.env
在.env文件中需要添加以下行
CDEK_WEBHOOK_URL=/api/cdek/webhook
config/cdek.php
要通过配置文件定义URL,需要发布配置文件
php artisan vendor:publish --provider="Muscobytes\CdekWebhook\CdekWebhookServiceProvider" --tag="config"
在config/cdek.php文件中需要为webhook_url键定义值
<?php return [ 'webhook_url' => env('CDEK_WEBHOOK_URL', '/api/cdek/webhook') ];
使用
当对配置中定义的URL执行请求时,将启动一个事件调用。
DownloadPhotoEvent::classOrderStatusEvent::classPrealertEvent::classPrintFormEvent::class
对于上述每个事件,您都可以创建自己的事件处理器。
例如,对于DownloadPhotoEvent::class事件
php artisan make:listener DownloadPhotoListener
在创建的app/Listeners/DownloadPhotoListener.php文件中需要定义handle方法
<?php namespace App\Listeners; use App\Jobs\CreateOrderFromPostingJob; use Muscobytes\OzonSeller\Events\DownloadPhotoEvent; class DownloadPhotoEventListener { public function handle( DownloadPhotoEvent $event ): void { /** @var \Muscobytes\CdekWebhook\Messages\DownloadPhotoMessage $message */ $message = $event->getMessage(); // Ваш код } }
DownloadPhotoEvent对象的$message属性包含来自CDEK的请求数据DTO。
- 对于
DownloadPhotoEvent这是Muscobytes\CdekWebhook\Messages\DownloadPhotoMessage - 对于
OrderStatusEvent这是Muscobytes\CdekWebhook\Messages\OrderStatusMessage - 对于
PrealertEvent这是Muscobytes\CdekWebhook\Messages\PrealertClosedMessage - 对于
PrintFormEvent这是Muscobytes\CdekWebhook\Messages\PrintFormMessage
DTO属性的集合与在CDEK文档中描述的字段集合相匹配。
为了注册事件处理器,需要在app/Providers/EventServiceProvider.php文件中的$listen数组中添加事件类,并将事件处理器类分配给它
class EventServiceProvider extends ServiceProvider { protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], OrderStatusEvent::class => [ OrderStatusEventListener::class ] ];