rollun-com / rollun-usps
USPS API 服务
Requires
- php: ^7.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- code-tool/jaeger-client-php: ^2.16
- guzzlehttp/guzzle: ^6.5
- rollun-com/rollun-callback: ^5.2||^6
- rollun-com/rollun-datastore: ^7
- symfony/dotenv: ^4.2
- vinceg/usps-php-api: ^1.0
- zendframework/zend-component-installer: ^2.1.1
- zendframework/zend-config-aggregator: ^1.0
- zendframework/zend-servicemanager: ^3.3
- zendframework/zend-stdlib: ^3.1
Requires (Dev)
- phpunit/phpunit: ^7.0.1
- roave/security-advisories: dev-master
- rollun-com/rollun-logger: ^4.7||^5.0
- squizlabs/php_codesniffer: ^3.0
- zendframework/zend-expressive-tooling: ^1.0
- zfcampus/zf-development-mode: ^3.1
- dev-master
- v4.x-dev
- 4.0.0
- 3.3.10
- 3.3.9
- 3.3.8
- 3.3.7
- 3.3.6
- 3.3.5
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.0
- dev-dev
- dev-temp-rollback
- dev-add-usps-logs
- dev-fix-usps
- dev-change-usps-prices
- dev-remove-openapi-namespace
This package is auto-updated.
Last update: 2024-09-07 10:41:14 UTC
README
入门指南
该库提供了获取不同配送方式成本的功能。库中集成了USPS API,但所有计算均由库本身完成。
对于某些类的工作,需要定义PSR-3 logger,该logger将在容器中以 Psr\Log\LoggerInterface
的名字可用。
配送方式
- RM-DS
- RM-PickUp (USPS)
- PU-DS
- PU-PickUp (USPS)
- WPS-DS
- TR-DS
- SLT-DS
- AU-DS
- AU-DS-COVID19
DataStore: shipping-all-costs
该库提供了 shipping-all-costs DataStore,该DataStore返回所有配置的配送方法的成本。
查询示例
http://SOME_URL/api/datastore/shipping-all-costs?and(eq(ZipOrigination,10005),eq(ZipDestination,91730),eq(Width,2),eq(Length,2),eq(Height,5),eq(Pounds,2),ne(cost,null()))&sort(+cost)&limit(50)
http://SOME_URL/api/datastore/shipping-all-costs?and(eq(ZipOrigination,28790),eq(ZipDestination,91730),eq(Width,2),eq(Length,2),eq(Height,1),eq(Pounds,2),eq(attr_CommodityCode,301),ne(cost,null()))&sort(+cost)&limit(50)
http://SOME_URL/api/datastore/shipping-all-costs?ZipOrigination=91601&ZipDestination=91730&Width=1&Length=10&Height=5&Pounds=0.5&Click_N_Shipp=Priority%20Mail
http://SOME_URL/api/datastore/shipping-all-costs?ZipOrigination=91601&ZipDestination=91730&Width=1&Length=10&Height=5&Pounds=1&like(id,*FtCls*)&limit(2,1)&select(id)
可以向配送方法发送额外的属性。为此,在请求中指定 ...,eq(attr_CommodityCode,301),...
,这意味着所有配送方法都将获得CommodityCode值为301的属性。
添加自定义配送方式
该库提供了添加自定义配送方式的功能。所有可能的配送方式都必须在 RootShippingProvider 中声明,因为这里使用的是树形结构,而树的根是root。
以下是一个通过配置添加名为 FixedPrice1 的配送方式的示例。
<?php use rollun\Entity\Product\Container\Box; use rollun\Entity\Shipping\Method\FixedPrice; use service\Entity\Rollun\Shipping\Method\Provider\Root as RootShippingProvider; use rollun\Entity\Shipping\Method\Provider\PickUp\RmPickUp; return [ 'ShippingMethod' => [ 'Root' => [ 'class' => RootShippingProvider::class, // указываем элемент сервис менеджера 'shortName' => 'Root', 'shippingMethodList' => [ 'RM-PickUp', // добавляем первый способ доставки который по своей сути является еще одним ShippingMethodProvider со своими способами доставки 'FixedPrice1' // добавляем наш способ доставки ] ], 'RM-PickUp' => [ // объявляем ShippingMethodProvider 'class' => RmPickUp::class, // указываем элемент сервис менеджера 'shortName' => 'RM-PickUp', 'shippingMethodList' => [ 'Usps' // указываем способы доставки ] ], 'FixedPrice1' => [ // добавляем наш способ доставки 'class' => FixedPrice::class, // указываем элемент сервис менеджера 'shortName' => 'FP1', 'price' => 8, // характерно только для FixedPrice::class, указываем цену 'maxWeight' => 20, // характерно только для FixedPrice::class, максимально допустимый вес 'containerService' => 'Fixed Price Box 1' // // характерно только для FixedPrice::class, указываем контейнер ], ], 'Container' => [ 'Fixed Price Box 1' => [ // объявляем контейнер 'class' => Box::class, 'Length' => 10, 'Width' => 10, 'Height' => 10 ], ] ];
USPS API
要使用USPS API,需要在.env中指定 USPS_API_PASS(仅在开发模式下需要)USPS_API_PASS="112233445566"
该库会自行进行数据计算,同时也有机会检查内部计算与API响应是否一致。为此,只需运行 unit tests。更详细的内容请参考 phpunit
包装机制
rollun\Entity\Product\Container\Box
容器在检查是否可以将包裹包装到容器的方法中使用 PackagePacker。PackagePacker 是一个提供API的微服务,用于 https://github.com/betterwaysystems/packer。
请在env中指定 PACKAGE_PACKER_API_URL
,以便库可以获取API数据。
使用USPS API获取配送区域名称
该方法来自页面 https://postcalc.usps.com/DomesticZoneChart(第二个标签页)。该方法允许获取接收者zip相对于发送者zip的区域。
curl查询示例
curl 'https://postcalc.usps.com/DomesticZoneChart/GetZone?origin=10001&destination=10001&shippingDate=12%2F10%2F2020&_=1607610825151' \ -X 'GET' \ -H 'Accept: application/json, text/javascript, */*; q=0.01' \ -H 'Accept-Language: en-gb' \ -H 'Host: postcalc.usps.com' \ -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.3 Safari/605.1.15' \ -H 'Referer: https://postcalc.usps.com/DomesticZoneChart' \ -H 'Accept-Encoding: gzip, deflate, br' \ -H 'Connection: keep-alive' \ -H 'X-Requested-With: XMLHttpRequest'
必需参数
- origin - 发送者zip
- destination - 接收者zip
- shippingDate - 发送日期
- _ - 当前timestamp,带毫秒(例如 - 1607610825151)。没有此参数时,请求将正常工作,但USPS会发送它。
始终期望响应代码为200,即使参数不正确。
响应示例 -
{ "OriginError":"", "DestinationError":"", "ShippingDateError":"", "PageError":"", "EffectiveDate":"December 1, 2020", "ZoneInformation":"The Zone is 1. This is a Local zone. The destination ZIP Code is within the same NDC as the origin ZIP Code." }
ZoneInformation - 区域名称始终是第一行,格式(至少根据测试结果)如下。
The Zone is <zone_number>
错误响应示例,未指定日期 -
{ "OriginError":"", "DestinationError":"", "ShippingDateError":"Mailing date is invalid.", "PageError":"", "EffectiveDate":"", "ZoneInformation":"" }
USPS_PACKAGE_COSTS 生成器
生成器脚本,用于生成符合指定尺寸的盒子价格数组,位于 bin/UspsPackageCostsGenerator.php
。输入为csv格式,输出为所需数组(data目录中有示例),然后将其插入到类中。