1 | <?php |
||
54 | class Pakkelabels |
||
55 | { |
||
56 | |||
57 | /** |
||
58 | * API Endpoint URL |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | const API_ENDPOINT = 'https://app.pakkelabels.dk/api/public/v2'; |
||
63 | |||
64 | /** |
||
65 | * API Endpoint URL |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | const VERSION = '1.1'; |
||
70 | |||
71 | /** |
||
72 | * API user |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $api_user; |
||
77 | |||
78 | /** |
||
79 | * API key |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $api_key; |
||
84 | |||
85 | /** |
||
86 | * Token |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $token; |
||
91 | |||
92 | /** |
||
93 | * Constructor |
||
94 | * |
||
95 | * @param string $api_user |
||
96 | * @param string $api_key |
||
97 | * |
||
98 | * @throws \PakkelabelsException |
||
99 | */ |
||
100 | 1 | public function __construct($api_user, $api_key) |
|
101 | { |
||
102 | 1 | $this->api_user = $api_user; |
|
103 | 1 | $this->api_key = $api_key; |
|
104 | 1 | $this->login(); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Login |
||
109 | * |
||
110 | * @return void |
||
111 | * @throws \PakkelabelsException |
||
112 | */ |
||
113 | 1 | protected function login() |
|
114 | { |
||
115 | 1 | $result = $this->makeApiCall( |
|
116 | 1 | 'users/login', |
|
117 | 1 | true, |
|
118 | 1 | array('api_user' => $this->api_user, 'api_key' => $this->api_key) |
|
119 | 1 | ); |
|
120 | $this->token = $result['token']; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Get balance |
||
125 | * |
||
126 | * @return void |
||
127 | * @throws \PakkelabelsException |
||
128 | */ |
||
129 | public function balance() |
||
130 | { |
||
131 | $result = $this->makeApiCall('users/balance'); |
||
132 | return $result['balance']; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get PDF |
||
137 | * |
||
138 | * @return base64 encoded string |
||
139 | * @throws \PakkelabelsException |
||
140 | */ |
||
141 | public function pdf($id) |
||
142 | { |
||
143 | $result = $this->makeApiCall('shipments/pdf', false, array('id' => $id)); |
||
144 | return $result['base64']; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get pickup points. |
||
149 | * |
||
150 | * @param array $params |
||
151 | * |
||
152 | * @return mixed |
||
153 | * @throws \PakkelabelsException |
||
154 | */ |
||
155 | public function getPickupPoints($params = array()) |
||
159 | |||
160 | /** |
||
161 | * Search shipments |
||
162 | * |
||
163 | * @param array $params |
||
164 | * |
||
165 | * @return mixed |
||
166 | * @throws \PakkelabelsException |
||
167 | */ |
||
168 | public function shipments($params = array()) |
||
169 | { |
||
170 | $result = $this->makeApiCall('shipments/shipments', false, $params); |
||
171 | return $result; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get imported shipments |
||
176 | * |
||
177 | * @param array $params |
||
178 | * |
||
179 | * @return mixed |
||
180 | * @throws \PakkelabelsException |
||
181 | */ |
||
182 | public function importedShipments($params = array()) |
||
183 | { |
||
184 | $result = $this->makeApiCall('shipments/imported_shipments', false, $params); |
||
185 | return $result; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Create imported shipment |
||
190 | * |
||
191 | * @param array $params |
||
192 | * |
||
193 | * @return mixed |
||
194 | * @throws \PakkelabelsException |
||
195 | */ |
||
196 | public function createImportedShipment($params) |
||
197 | { |
||
198 | $result = $this->makeApiCall('shipments/imported_shipment', true, $params); |
||
199 | return $result; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Create shipment |
||
204 | * |
||
205 | * @param array $params |
||
206 | * |
||
207 | * @return mixed |
||
208 | * @throws \PakkelabelsException |
||
209 | */ |
||
210 | public function createShipment($params) |
||
211 | { |
||
212 | $result = $this->makeApiCall('shipments/shipment', true, $params); |
||
213 | return $result; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get freight rates |
||
218 | * |
||
219 | * @param array $params |
||
220 | * |
||
221 | * @return mixed |
||
222 | * @throws \PakkelabelsException |
||
223 | */ |
||
224 | public function freightRates($params = array()) |
||
225 | { |
||
226 | $result = $this->makeApiCall('shipments/freight_rates', false, $params); |
||
227 | return $result; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get payment requests |
||
232 | * |
||
233 | * @return mixed |
||
234 | * @throws \PakkelabelsException |
||
235 | */ |
||
236 | public function paymentRequests() |
||
237 | { |
||
238 | $result = $this->makeApiCall('users/payment_requests'); |
||
239 | return $result; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get GLS Droppoints |
||
244 | * |
||
245 | * @param array $params |
||
246 | * |
||
247 | * @return mixed |
||
248 | * @throws \PakkelabelsException |
||
249 | */ |
||
250 | public function glsDroppoints($params) |
||
251 | { |
||
252 | $result = $this->makeApiCall('shipments/gls_droppoints', false, $params); |
||
253 | return $result; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get PostDK Droppoints |
||
258 | * |
||
259 | * @param array $params |
||
260 | * |
||
261 | * @return mixed |
||
262 | * @throws \PakkelabelsException |
||
263 | */ |
||
264 | public function pdkDroppoints($params) |
||
268 | |||
269 | /** |
||
270 | * Get token |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | public function getToken() |
||
275 | { |
||
276 | return $this->token; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Make API Call |
||
281 | * |
||
282 | * @param string $method |
||
283 | * @param boolean $doPost |
||
284 | * @param array $params |
||
285 | * |
||
286 | * @return mixed |
||
287 | * @throws \PakkelabelsException |
||
288 | */ |
||
289 | 1 | protected function makeApiCall($method, $doPost = false, $params = array()) |
|
315 | } |
||
316 |