baks-dev/wildberries-package
Модуль baks-dev/wildberries-package для PHP 8.4+ и Composer: установка и упаковка заказов Wildberries. Включает установку конфигурации/ресурсов, миграции Doctrine и тесты PHPUnit (группа wildberries-package).
Install the Package
composer require baks-dev/wildberries-package
Set Up Configuration & Assets Run the CLI command to install default configurations and assets:
php artisan baks:assets:install
This generates:
config/wildberries.phpresources/views/wildberries/ and public/wildberries/Publish Configuration (Optional) If you need to customize the package’s behavior:
php artisan vendor:publish --tag=wildberries-config
Run Database Migrations Check for new migrations and apply them:
php artisan doctrine:migrations:diff
php artisan doctrine:migrations:migrate
First Use Case: Packaging an Order Use the package’s service class to package an order:
use BaksDev\WildberriesPackage\Facades\WildberriesPackage;
$order = app('App\Models\Order'); // Your order model
$packaging = WildberriesPackage::package($order);
// Generate shipment label
$label = $packaging->generateLabel();
customer_name, items, shipping_address).$packaging = WildberriesPackage::package($order);
$label = $packaging->generateLabel();
$barcode = $packaging->generateBarcode();
$order->shipment_label = $label->getData();
$order->save();
$packaging->sendToWildberries();
use BaksDev\WildberriesPackage\Services\WildberriesApi;
$api = app(WildberriesApi::class);
$response = $api->createShipment($shipmentData);
try {
$response = $api->createShipment($shipmentData);
} catch (\BaksDev\WildberriesPackage\Exceptions\ApiException $e) {
// Log error and retry or notify admin
\Log::error('Wildberries API Error: ' . $e->getMessage());
}
use BaksDev\WildberriesPackage\Events\OrderPackaged;
OrderPackaged::listen(function (OrderPackaged $event) {
// Custom logic after order is packaged
\Log::info('Order packaged: ' . $event->order->id);
});
EventServiceProvider:
protected $listen = [
OrderPackaged::class => [
'App\Listeners\NotifyWarehouse',
],
];
# Package all orders ready for shipment
php artisan wildberries:package-ready-orders
# Generate labels for a specific order
php artisan wildberries:generate-label 123
Order and Shipment models extend or use the package’s entities if provided.php artisan vendor:publish --tag=wildberries-migrations
config/wildberries.php:
'api' => [
'endpoint' => env('WILDBERRIES_API_ENDPOINT', 'https://api.wildberries.ru'),
'token' => env('WILDBERRIES_API_TOKEN'),
],
'packaging' => [
'label_template' => 'wildberries::label.template',
'barcode_format' => 'CODE128',
],
php artisan test --group=wildberries-package
use BaksDev\WildberriesPackage\Services\WildberriesApi;
$api = Mockery::mock(WildberriesApi::class);
$api->shouldReceive('createShipment')
->once()
->andReturn(['success' => true]);
$this->app->instance(WildberriesApi::class, $api);
namespace App\Wildberries;
use BaksDev\WildberriesPackage\Contracts\LabelTemplate;
class CustomLabelTemplate implements LabelTemplate
{
public function render(array $data): string
{
// Custom logic
return 'Custom label content';
}
}
$this->app->bind(
\BaksDev\WildberriesPackage\Contracts\LabelTemplate::class,
App\Wildberries\CustomLabelTemplate::class
);
PHP Version Requirements
Database Schema Conflicts
Order or Shipment models, the package’s migrations might conflict.php artisan vendor:publish --tag=wildberries-migrations
Then merge changes manually.Wildberries API Rate Limits
config/wildberries.php:
'api' => [
'retry_attempts' => 3,
'retry_delay' => 1000, // ms
],
Asset Overwrites
baks:assets:install will overwrite existing files in resources/views/wildberries/ and public/wildberries/.Event Listener Conflicts
OrderPackaged or similar events, the package’s events might conflict.Barcode Generation Dependencies
milbo/barcode) for barcode generation.Enable Debug Mode
Set debug to true in config/wildberries.php to get verbose logs:
'debug' => env('WILDBERRIES_DEBUG', false),
Log API Responses Enable logging for API interactions:
'api' => [
'log_responses' => true,
],
Check logs in storage/logs/wildberries.log.
Validate Order Data Ensure your order data matches the package’s expected structure:
$validator = \Validator::make($order->toArray(), [
'customer_name' => 'required|string',
'items' => 'required|array',
'shipping_address' => 'required|array',
]);
Check for Deprecated Methods
If upgrading from an older version, scan for @deprecated tags in the package’s code and update your usage.
PackagingService to add custom rules:
namespace App\Services;
use BaksDev\WildberriesPackage\Services\PackagingService as BasePackagingService;
class CustomPackagingService extends BasePackagingService
{
public function getCustomPackagingRules()
{
return [
'rule1' => function ($item) {
return $item['weight'] > 1000; // Custom rule
},
];
How can I help you explore Laravel packages today?