sanmai / shipandco-sdk
Ship&co API SDK
Fund package maintenance!
sanmai
Installs: 12 390
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- doctrine/annotations: ^1.14 || ^2.0
- guzzlehttp/guzzle: ^6.3 || ^7.0
- jms/serializer: ^3.9
- sanmai/json-serializer: ^0.1.1 || ^0.2.4
- sanmai/pipeline: ^5 || ^6
- sanmai/sdk-common: ^0.5.4
- sanmai/version-info: ^0.2.0
- symfony/http-foundation: ^4.4.7 || ^5.0.7
Requires (Dev)
- doctrine/cache: <2
- ergebnis/composer-normalize: >=2.23
- friendsofphp/php-cs-fixer: ^3
- infection/infection: >=0.16
- laravel/framework: ^6.20.26 || ^8.40
- phan/phan: ^1 <1.3 || >1.3.0
- php-coveralls/php-coveralls: ^2.4.1
- phpstan/phpstan: >=0.10
- phpunit/phpunit: ^9.3
- psr/log: ^1.1
- vimeo/psalm: >=3.0.16
Suggests
- doctrine/cache: For metadata caching
- monolog/monolog: For advanced logging and debugging
Conflicts
- doctrine/annotations: <1.10.3
README
Features:
- Shipments
- Create Shipment
- List Shipments
- Get Shipment
- Delete Shipment
- Rate
- Carrier
- Register Carrier
- List Carriers
- Update Carrier
- Delete Carrier
- Tracking
- Address
- Register Address
- List Addresses
- Warehouse
- Register Warehouse
- List Warehouses
- Sub User
- Register Sub User
- List Sub User
- Get Sub User
- Refresh Sub User
- Delete Sub User
This library is far from offering a complete set of API methods, but it should do the most important bits.
Something is amiss? Let us know, or, even better, send a PR!
Ship&co API documentation for your reference. Please note this is not in any way an official SDK of Ship&co, therefore most likely they won't be able to answer any questions about this SDK.
Installation
composer require sanmai/shipandco-sdk
This SDK requires at least PHP 7.3. It was tested to work under PHP 7.3, 7.4, and 8.0.
Overview
Major parts are:
- Client. Client is the object you send all requests through.
- Requests. There are several request objects for most requests the API offers. They follow a fluent interface paradigm, where if the original request has a certain property, a request here will have it too. More on this below.
- Responses. After sending a request you'll have a response object, which could be an actual reponse, or an error response. All responses follow the same fluent paradigm.
Usage
First, you need to acquire an access token as outlined in the documentation.
Next, instantiate a client using a convenient builder:
$builder = new \ShipAndCoSDK\ClientBuilder(); $builder->setToken($token); $client = $builder->build();
The builder has several more convenience methods to set timeouts and enable caching. Check with the source code.
Error handling
Error handling is built upon error responses. Exceptions are almost never thrown. If there's an exception, this has to be a truly exceptional situation, even a bug.
$request = new \ShipAndCoSDK\Requests\RatesRequest(); // This requests requires several properties to be set, therefore we'll have an error here. $response = $client->sendRatesRequest($request); if (\count($response) > 0) { // Will not be printed because count() is zero here. echo 'Rates received: ', \count($response), "\n"; } if ($response->hasErrors()) { // Check for exact errors foreach ($response->getMessages() as $message) { if ($message->getErrorCode() !== '') { // That's the error: echo "{$message->getErrorCode()}: {$message->getMessage()}\n"; } } /** @var \ShipAndCoSDK\Responses\Bad\ErrorResponse $response */ // To get more specific error details use response-specific fields. E.g.: foreach ($response->details as $detail) { echo "Error code: {$detail->code}\n\tMessage: {$detail->message}\n\tField: {$detail->field}\n"; } }
Requests
For every implemented request there's a runnable example. E.g. running this:
export SHIPANDCO_ACCESS_TOKEN=... (your token)
php examples/030_CarriersRequest.php
...Will output your registered carriers.
Examples come with a debugging output enabled so that you can experiment freely.
Obviosly, to run examples you'll need to have the library checked out:
git clone https://github.com/sanmai/shipandco-sdk.git
cd shipandco-sdk
composer install
List Carriers
$request = new \ShipAndCoSDK\Requests\CarriersRequest(); $response = $client->sendCarriersRequest($request); \var_dump(\count($response)); // Should print the number of configured carriers foreach ($response as $value) { echo "{$value->id}\t{$value->type}\t{$value->state}\t{$value->created_at->format('Y-m-d')}\n"; foreach ($value->credentials as $key => $value) { echo "\t$key =\t$value\n"; } }
List Addresses
$request = new \ShipAndCoSDK\Requests\AddressesRequest(); $response = $client->sendAddressesRequest($request); \var_dump(\count($response)); // Should print the number of addresses returned foreach ($response as $value) { echo "{$value->id}\t{$value->created_at->format('Y-m-d')}\n"; foreach ($value->address as $key => $value) { echo "\t$key =\t$value\n"; } }
List Warehouses
$request = new \ShipAndCoSDK\Requests\WarehousesRequest(); $response = $client->sendWarehousesRequest($request); \var_dump(\count($response)); // Should print the number of warehouses returned foreach ($response as $value) { echo "{$value->id}\t{$value->created_at->format('Y-m-d')}\t{$value->company}\n"; foreach ($value->address as $key => $value) { echo "\t$key =\t$value\n"; } }
List Rates
$request = new \ShipAndCoSDK\Requests\RatesRequest(); $request->from_address->country = 'JP'; $request->from_address->full_name = 'Yamada Taro'; $request->from_address->company = 'World Company'; $request->from_address->email = 'ytaro@example.com'; $request->from_address->phone = '08012341234'; $request->from_address->country = 'JP'; $request->from_address->address1 = 'OSAKAFU'; $request->from_address->address2 = 'OTECHO'; $request->from_address->province = 'OSAKA'; $request->from_address->zip = '5670883'; $request->from_address->city = 'IBARAKI SHI'; $request->to_address->full_name = 'John Doe'; $request->to_address->company = 'ACME'; $request->to_address->email = 'john@example.net'; $request->to_address->phone = '0901231234'; $request->to_address->country = 'PT'; $request->to_address->address1 = 'Rua Maria Matos, 32'; $request->to_address->address2 = ''; $request->to_address->province = 'SETUBAL'; $request->to_address->zip = '2820-344'; $request->to_address->city = 'CHARNECA DA CAPARICA'; $product = $request->addProduct(); $product->quantity = 1; $product->name = 'Example'; $product->price = 1000; $parcel = $request->addParcel(); $parcel->weight = 200; $parcel->width = 10; $parcel->height = 10; $parcel->depth = 10; $request->customs->duty_paid = false; $request->customs->content_type = 'MERCHANDISE'; $request->setup->date = new DateTime('+1 week'); $response = $client->sendRatesRequest($request); \var_dump(\count($response)); foreach ($response as $rate) { echo "{$rate->carrier}\t{$rate->service}\t{$rate->price} {$rate->currency}\n"; foreach ($rate->surcharges as $surcharge) { echo "\t{$surcharge->type}\t{$surcharge->price}\n"; } }
Create Shipment
As far as shipments go, it's important to understand that requests for domestic shipments over Japan (Sagawa, Yamato, YuPack) are a bit different from an International shipments:
- For domestic shipments addresses (to and from) should be in Japanese. The opposite is true for International shipments.
- No city field is needed for domestic shipments (city goes to
address_1
). - No need for most details about product: on the contrary, for International shipments, details about products are used for customs clearance purpose.
- For domestic shipments, if it's not a cash on delivery shipment, product name only should work (just to show on the label wha'ts inside the box).
This SDK poses no restrictions on the number of fields specified in a request (other than that they should exist), thus it is a user responsibility (yours) to set correct fields for each type of request.
$request = new \ShipAndCoSDK\Requests\CreateShipmentRequest(); $request->to_address->country = 'JP'; $request->to_address->full_name = 'TEST TARO'; $request->to_address->phone = '1111111111'; $request->to_address->country = 'JP'; $request->to_address->address1 = '京都市中京区八百屋町117'; $request->to_address->zip = '604-8072'; $request->to_address->city = '京都府'; $request->from_address->full_name = 'テスト'; $request->from_address->phone = '08012341234'; $request->from_address->country = 'JP'; $request->from_address->address1 = 'OSAKAFU'; $request->from_address->province = 'OSAKA'; $request->from_address->zip = '1234567'; $request->from_address->city = 'IBARAKI SHI'; $product = $request->addProduct(); $product->name = 'Blue Basketball'; $product->quantity = 2; $product->price = 4850; $product->origin_country = 'JP'; $product = $request->addProduct(); $product->name = 'Orange Basketball'; $product->quantity = 1; $product->price = 7850; $product->origin_country = 'JP'; $parcel = $request->addParcel(); $parcel->amount = 1; // That's the default. $parcel->weight = 2000; $parcel->width = 10; $parcel->height = 10; $parcel->depth = 10; // $parcel->package = 'fedex_envelope'; $request->customs->duty_paid = false; $request->customs->content_type = 'MERCHANDISE'; $request->setup->carrier = 'sagawa'; $request->setup->service = 'sagawa_regular'; $request->setup->currency = 'JPY'; $request->setup->shipment_date = new DateTime('+1 day'); $request->setup->date = new DateTime('+2 day'); $request->setup->time = '16-18'; $request->setup->insurance = 0; $request->setup->ref_number = ''; $request->setup->delivery_note = ''; $request->setup->signature = false; $request->setup->care->fragile = false; $request->setup->care->side_up = false; $request->setup->care->valuable_goods = false; $request->setup->pack_size = '0'; $request->setup->pack_amount = 3; $request->setup->cash_on_delivery->amount = 1000; $request->setup->cash_on_delivery->tax = 100; $request->setup->return_label = false; $request->setup->print_start_location = 1; $request->setup->test = true; $response = $client->sendCreateShipmentRequest($request); if ($response->hasErrors()) { // Check for exact errors foreach ($response->getMessages() as $message) { if ($message->getErrorCode() !== '') { // That's the error echo "{$message->getErrorCode()}: {$message->getMessage()}\n"; } } return; // This is a failure. } /** @var $response \ShipAndCoSDK\Responses\ShipmentResponse */ echo "{$response->id}\t{$response->state}\n"; echo "Shipping Label: {$response->delivery->label}\n"; foreach ($response->delivery->tracking_numbers as $trackingNumber) { echo "Tracking Number: {$trackingNumber}\n"; }
To safeguard you against unexpected billing charges, shipment requests are using the test environment by default (thus creating dummy labels). Make sure to set test
to false
in setup
section to receive live labels.
Just like so:
$request->setup->test = false;
License
This project is licensed under the terms of the MIT license.