-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathShipEngine.php
228 lines (205 loc) · 8.6 KB
/
ShipEngine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
namespace ShipEngine;
use Psr\Http\Client\ClientExceptionInterface;
use ShipEngine\Message\ShipEngineException;
use ShipEngine\ListCarriers\Result as ListCarriersResult;
/**
* Exposes the functionality of the ShipEngine API.
*
* @package ShipEngine
*/
final class ShipEngine
{
/**
* ShipEngine SDK Version
*/
public const VERSION = '1.0.2';
// /**
// *
// * @var ShipEngineClient
// */
// protected ShipEngineClient $client;
/**
* Global configuration for the ShipEngine API client, such as timeouts,
* retries, page size, etc. This configuration applies to all method calls,
* unless specifically overridden when calling a method.
*
* @var ShipEngineConfig
*/
public ShipEngineConfig $config;
/**
* Instantiates the ShipEngine class. The `apiKey` you pass in can be either
* a ShipEngine sandbox or production API Key. (sandbox keys start with "TEST_)
*
* @param mixed $config Can be either a string that is your `apiKey` or an `array` {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, eventListener:object}
*/
public function __construct($config = null)
{
$this->config = new ShipEngineConfig(
is_string($config) ? array('apiKey' => $config) : $config
);
}
/**
* Fetch the carrier accounts connected to your ShipEngine Account.
*
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array An array of **CarrierAccount** objects that correspond the to carrier accounts connected
* to a given ShipEngine account.
*/
public function listCarriers($config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->get(
'v1/carriers',
$config,
);
return $apiResponse;
}
/**
* Address validation ensures accurate addresses and can lead to reduced shipping costs by preventing address
* correction surcharges. ShipEngine cross references multiple databases to validate addresses and identify
* potential deliverability issues.
* See: https://shipengine.github.io/shipengine-openapi/#operation/validate_address
*
* @param array $params A list of addresses that are to be validated
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array An array of Address objects that correspond the to carrier accounts connected
* to a given ShipEngine account.
*/
public function validateAddresses($params, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->post(
'v1/addresses/validate',
$config,
$params
);
return $apiResponse;
}
/**
* When retrieving rates for shipments using the /rates endpoint, the returned information contains a rateId
* property that can be used to generate a label without having to refill in the shipment information repeatedly.
* See: https://shipengine.github.io/shipengine-openapi/#operation/create_label_from_rate
*
* @param string $rateId A rate identifier for the label
* @param array $params An array of label params that will dictate the label display and level of verification.
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array A label that correspond the to shipment details for a rate id
*/
public function createLabelFromRate($rateId, $params, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->post(
"v1/labels/rates/$rateId",
$config,
$params
);
return $apiResponse;
}
/**
* Purchase and print a label for shipment.
* https://shipengine.github.io/shipengine-openapi/#operation/create_label
*
* @param array $params An array of shipment details for the label creation.
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array A label that correspond the to shipment details
*/
public function createLabelFromShipmentDetails($params, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->post(
'v1/labels',
$config,
$params
);
return $apiResponse;
}
/**
* Void label with a Label Id.
* https://shipengine.github.io/shipengine-openapi/#operation/void_label
*
* @param string $labelId A label id
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array A voided label approval and message
*/
public function voidLabelWithLabelId($labelId, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->put(
"v1/labels/$labelId/void",
$config
);
return $apiResponse;
}
/**
* Given some shipment details and rate options, this endpoint returns a list of rate quotes.
* See: https://shipengine.github.io/shipengine-openapi/#operation/calculate_rates
*
* @param array $params An array of rate options and shipment details.
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array An array of Rate objects that correspond to the rate options and shipment details.
*/
public function getRatesWithShipmentDetails($params, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->post(
'v1/rates',
$config,
$params
);
return $apiResponse;
}
/**
* Retrieve the label's tracking information with Label Id
* See: https://shipengine.github.io/shipengine-openapi/#operation/get_tracking_log_from_label
*
* @param string $labelId A label id
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array An array of Tracking information corresponding to the Label Id.
*/
public function trackUsingLabelId($labelId, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->get(
"v1/labels/$labelId/track",
$config
);
return $apiResponse;
}
/**
* Retrieve the label's tracking information with Carrier Code and Tracking Number
* See: https://shipengine.github.io/shipengine-openapi/#operation/get_tracking_log
*
* @param string $carrierCode Carrier code used to retrieve tracking information
* @param string $trackingNumber The tracking number associated with a shipment
* @param array|ShipEngineConfig|null $config Optional configuration overrides for this method call {apiKey:string,
* baseUrl:string, pageSize:int, retries:int, timeout:int, client:HttpClient|null}
* @return array An array of Tracking information corresponding to the Label Id.
*/
public function trackUsingCarrierCodeAndTrackingNumber($carrierCode, $trackingNumber, $config = null): array
{
$config = $this->config->merge($config);
$client = new ShipEngineClient();
$apiResponse = $client->get(
"v1/tracking?carrier_code=$carrierCode&tracking_number=$trackingNumber",
$config
);
return $apiResponse;
}
}