a-posokhov/russianpost-sdk

俄罗斯邮政(pochta.ru)SDK

dev-master 2024-08-16 10:27 UTC

This package is auto-updated.

Last update: 2024-09-16 10:40:12 UTC


README

Latest Version on Packagist Build Status StyleCI Quality Score Code Coverage Total Downloads Software License

目录

仅在有与俄罗斯邮政签订的合同的情况下才能使用批量跟踪API和发送API。

单个跟踪API可以使用合同或简单注册(但每日请求量有限 - 100个请求/日)。

安装

最低要求 - PHP 7.1+,ext-soap,ext-json。

请使用包管理器 Composer 进行安装

composer require appwilio/russianpost-sdk

在使用 Laravel 框架时,SDK会自动注册可用服务。

日志记录

可以通过连接任何实现PSR-3标准(例如,Monolog)的日志记录器来记录请求和响应

$log = (new Logger('pochta.ru'))
    ->pushHandler(new StreamHandler('path/to/your.log', Logger::INFO));

// SingleAccessClient, PacketAccessClient, DispatchingClient
$client->setLogger($log);

在使用 Laravel 框架时,应在容器中添加名为 appwilio.russianpost.logger 的日志记录器

$this->app->singleton('appwilio.russianpost.logger', function () {
    return (new Logger('pochta.ru'))
        ->pushHandler(new StreamHandler('path/to/your.log', Logger::INFO));
});

追踪

文档

Laravel中的配置

请将以下键添加到 services.php

// ...
'russianpost' => [
    'tracking' => [
        'login' => \env('RUSSIAN_POST_TRACKING_LOGIN'),
        'password' => \env('RUSSIAN_POST_TRACKING_PASSWORD'),
    ],
]
// ...

如果您已经缓存了这些设置,请务必重新生成设置缓存!

单次访问

$tracker = new SingleAccessClient($login, $password);

如果找不到货签号(跟踪码)信息,则会抛出带有相应消息的异常 Appwilio\RussianPostSDK\Tracking\Exceptions\SingleAccessException

获取货签号(跟踪码)数据

$response = $tracker->getTrackingEvents('29014562148754');

对象 $response 实现了接口 \IteratorAggregate,因此可以直接在循环中使用它

foreach ($response as $events) {
    $parameters = $events->getOperationParameters();
    
    echo $parameters->getOperationId();
    echo $parameters->getAttributeId();
    echo $parameters->getPerformedAt()->format('d.m.Y в h:i:s'); // 17.09.2019 в 17:20:48
}

获取货签号(跟踪码)的代收货款信息

$response = $tracker->getCashOnDeliveryEvents('29014562148754');

对象 $response 实现了接口 \IteratorAggregate,因此可以直接在循环中使用它

foreach ($response as $event) {
    $parameters = $event->getOperationParameters();
    
    echo $parameters->getTransferNumber();
    echo $parameters->getPayment(); // 7410
    echo $parameters->getPerformedAt()->format('d.m.Y в h:i:s'); // 17.09.2019 в 17:20:48
}

批量访问

$tracker = new PacketAccessClient($login, $password);

获取货签号(跟踪码)数据

$ticket = $tracker->getTicket(['29014562148754', 'RA325487125CN']); // максимум 3 000 треков

建议在请求信息前等待15分钟。

$response = $tracker->getTrackingEvents($ticket->getId());

echo $response->getPreparedAt()->format('d.m.Y в h:m:s');

对象 $response 实现了接口 \IteratorAggregate,因此可以直接在循环中使用它

foreach ($response as $item) {
    echo $item->getBarcode();
    
    foreach ($item as $events) {
        echo $events->getOperationId();
        echo $events->getAttributeId();
        echo $events->getPerformedAt()->format('d.m.Y в h:m:s'); // 17.09.2019 в 17:20:48
    }
}

发送

文档

配置

use GuzzleHttp\Client as GuzzleClient;

$dispatching = new DispatchingClient(
    new Authentication($login, $password, $token),
    new GuzzleClient()
);

Laravel中的配置

请将以下键添加到 services.php

// ...
'russianpost' => [
    'dispatching' => [
        'token' => \env('RUSSIAN_POST_TRACKING_TOKEN'),
        'login' => \env('RUSSIAN_POST_TRACKING_LOGIN'),
        'password' => \env('RUSSIAN_POST_TRACKING_PASSWORD'),
    ],
]
// ...

如果您已经缓存了这些设置,请务必重新生成设置缓存!

计算运费

$response = $dispatching->services->calculate(
    CalculationRequest::create('123456', 200)
        ->ofMailType(MailType::PARCEL_POSTAL)
        ->ofMailCategory(MailCategory::ORDINARY)
        ->ofEntriesType(MailEntryType::SALE_OF_GOODS)
        ->fragile()
        ->withSmsNotice();
);

echo $response->getTotal()->getRate();
echo $response->getTotal()->getVAT(); // НДС

数据标准化和验证

姓名标准化

$response = $dispatching->services->normalizeFio(
    NormalizeFioRequest::one('иванов иван иванович')
);

if ($response[0]->isUseful()) {
    echo $response[0]->getFirstName().' '.$response[0]->getLastName(); // Иван Иванов
}

地址标准化

$response = $dispatching->services->normalizeAddress(
    NormalizeAddressRequest::one('Москва варшавское шоссе 37-45')
);

电话标准化

$response = $dispatching->services->normalizePhone(NormalizePhoneRequest::one('89001234567'));

检查收件人信誉

$response = $dispatching->services->checkRecipient(
    CheckRecipientRequest::one('Москва, Варшавское шоссе, 37-45')
);

$response[0]->isFraud(); // ненадёжный
$response[0]->isReliable(); // надёжный
$response = $dispatching->services->checkRecipient(
    CheckRecipientRequest::create()
        ->addRecipient('123456 Москва, Варшавское шоссе, 37-45')
        ->addRecipient('654321 Владивосток, пер. Староконный, 12-98');
);

foreach ($response as $recipient) {
    echo $recipient->getAddress.': '.$recipiend->isReliable();
}

文件

$file = $dispatching->documents->orderF7Form('12345678');

echo $file->getClientFilename(); // f7p.pdf

// Сохранение
$file->moveTo("storage/printforms/12345678-{$file->getClientFilename()}");

// Перенаправление в браузер (Laravel)
return \response()->streamDownload(function () use ($file) {
    (string) $file->getStream();
}, $file->getClientName(), ['Content-Type' => $file->getClientMediaType()]);

订单表单F7п

$pdf = $dispatching->documents->orderF7Form(
    '12345678', new \DateTime('2019-01-01'), Documents::PRINT_TYPE_THERMO
);

订单表单F112ЭК

$pdf = $dispatching->documents->orderF112Form('12345678', new \DateTime('2019-01-01'));

订单文档包(形成批次前)

$zip = $dispatching->documents->orderFormsBundleBacklog('12345678', new \DateTime('2019-01-01'));

订单文档包(形成批次后)

$zip = $dispatching->documents->orderFormBundle(
    '12345678', new \DateTime('2019-01-01'), Documents::PRINT_TYPE_THERMO
);

批次文档包

$zip = $dispatching->documents->batchFormBundle('87654321');

批次内容检查报告

$pdf = $dispatching->documents->batchCheckingForm('87654321');

批次表单F103

$pdf = $dispatching->documents->batchF103Form('87654321');

运行测试

$ vendor/bin/phpunit

作者

许可证

本SDK遵循MIT许可证MIT进行分发。