Test Failed
Pull Request — master (#3)
by Lars
04:32
created

Pakkelabels::paymentRequests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * PakkelabelsDK
5
 *
6
 * PHP version 5
7
 *
8
 * @category  Pakkelabels
9
 * @package   Pakkelabels
10
 * @author    Lars Olesen <[email protected]>
11
 *            Hasse Ramlev Hansen <[email protected]>
12
 * @copyright 2015 Lars Olesen
13
 * @license   MIT Open Source License https://opensource.org/licenses/MIT
14
 * @version   GIT: <git_id>
15
 * @link      http://github.com/discimport/pakkelabels-dk
16
 */
17
namespace PakkelabelsDK;
18
19
use PakkelabelsDK\Exception\PakkelabelsDKException;
20
21
/**
22
 * Class PakkelabelsDK
23
 *
24
 * Usage:
25
 * ----------------
26
 * The first thing required is to login
27
 * $label = new Pakkelabels('api_user', 'api_key');
28
 *
29
 * This will login and fetch the required token.
30
 * The token is then automatically added to any subsequent calls.
31
 *
32
 * To see the generated token you can use:
33
 * echo $label->getToken();
34
 *
35
 * Examples:
36
 * ----------------
37
 * // Get all Post Danmark labels shipped to Denmark
38
 * $labels = $label->shipments(array('shipping_agent' => 'pdk',
39
 * 'receiver_country' => 'DK'));
40
 *
41
 * // Display the PDF for a specific label
42
 * $base64 = $label->pdf(31629);
43
 * $pdf = base64_decode($base64);
44
 * header('Content-type: application/pdf');
45
 * header('Content-Disposition: inline; filename="label.pdf"');
46
 * echo $pdf;
47
 *
48
 * @category  Pakkelabels
49
 * @package   Pakkelabels
50
 * @author    Lars Olesen <[email protected]>
51
 * @copyright 2015 Lars Olesen
52
 * @license   http://opensource.org/licenses/bsd-license.php New BSD License
53
 * @link      http://github.com/discimport/pakkelabels-dk
54
 */
55
class Pakkelabels {
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 \PakkelabelsDKException
99 1
   */
100
  public function __construct($api_user, $api_key) {
101 1
    $this->api_user = $api_user;
102 1
    $this->api_key  = $api_key;
103 1
    $this->login();
104
  }
105
106
  /**
107
   * Login
108
   *
109
   * @return void
110
   * @throws \PakkelabelsDKException
111
   */
112 1
  protected function login() {
113
    $result = $this->makeApiCall(
114 1
      'users/login',
115 1
      TRUE,
116 1
      array('api_user' => $this->api_user, 'api_key' => $this->api_key)
117 1
    );
118 1
119
    $this->token = $result['token'];
120
  }
121
122
  /**
123
   * Get balance
124
   *
125
   * @return void
126
   * @throws \PakkelabelsDKException
127
   */
128
  public function balance() {
129
    $result = $this->makeApiCall('users/balance');
130
131
    return $result['balance'];
132
  }
133
134
  /**
135
   * Get PDF
136
   *
137
   * @return base64 encoded string
138
   * @throws \PakkelabelsDKException
139
   */
140
  public function pdf($id) {
141
    $result = $this->makeApiCall('shipments/pdf', FALSE, array('id' => $id));
142
143
    return $result['base64'];
144
  }
145
146
  /**
147
   * PDF Multiple.
148
   *
149
   * @param string $ids
150
   *
151
   * @return base64 encoded string.
152
   */
153
  public function pdfMulti(string $ids) {
154
    $result = $this->makeApiCall(
155
      'shipments/pdf_multi',
156
      FALSE,
157
      array('ids' => $ids)
158
    );
159
160
    return $result['base64'];
161
  }
162
163
  /**
164
   * Get ZPL
165
   *
166
   * @return base64 encoded string
167
   * @throws \PakkelabelsDKException
168
   */
169
  public function zpl($id) {
170
    $result = $this->makeApiCall('shipments/zpl', FALSE, array('id' => $id));
171
172
    return $result['base64'];
173
  }
174
175
  /**
176
   * Search shipments
177
   *
178
   * @param array $params
179
   *
180
   * @return mixed
181
   * @throws \PakkelabelsDKException
182
   */
183
  public function shipments($params = array()) {
184
    $result = $this->makeApiCall('shipments/shipments', FALSE, $params);
185
186
    return $result;
187
  }
188
189
  /**
190
   * Get imported shipments
191
   *
192
   * @param array $params
193
   *
194
   * @return mixed
195
   * @throws \PakkelabelsDKException
196
   */
197
  public function importedShipments($params = array()) {
198
    $result = $this->makeApiCall(
199
      'shipments/imported_shipments',
200
      FALSE,
201
      $params
202
    );
203
204
    return $result;
205
  }
206
207
  /**
208
   * Create imported shipment
209
   *
210
   * @param array $params
211
   *
212
   * @return mixed
213
   * @throws \PakkelabelsDKException
214
   */
215
  public function createImportedShipment($params) {
216
    $result = $this->makeApiCall('shipments/imported_shipment', TRUE, $params);
217
218
    return $result;
219
  }
220
221
  /**
222
   * Create shipment
223
   *
224
   * @param array $params
225
   *
226
   * @return mixed
227
   * @throws \PakkelabelsDKException
228
   */
229
  public function createShipment($params) {
230
    $result = $this->makeApiCall('shipments/shipment', TRUE, $params);
231
232
    return $result;
233
  }
234
235
  /**
236
   * Create shipment own customernumber.
237
   *
238
   * @param array $params
239
   *
240
   * @return mixed
241
   * @throws \PakkelabelsDKException
242
   */
243
  public function createShipmentOwnCustomerNumber($params) {
244
    $result = $this->makeApiCall(
245
      'shipments/shipment_own_customer_number',
246
      TRUE,
247
      $params
248
    );
249
250
    return $result;
251
  }
252
253
  /**
254
   * Get freight rates
255
   *
256
   * @return mixed
257
   * @throws \PakkelabelsDKException
258
   */
259
  public function freightRates() {
260 1
    $result = $this->makeApiCall('shipments/freight_rates');
261
262 1
    return $result;
263 1
  }
264 1
265
  /**
266 1
   * Add to print queue.
267 1
   *
268 1
   * @param $params
269 1
   *
270 1
   * @return mixed
271 1
   */
272
  public function addToPrintQueue($params) {
273
    return $this->makeApiCall('shipments/add_to_print_queue', TRUE, $params);
274 1
  }
275
276 1
  /**
277 1
   * Get payment requests
278 1
   *
279
   * @return mixed
280 1
   * @throws \PakkelabelsDKException
281 1
   */
282 1
  public function paymentRequests() {
283
    $result = $this->makeApiCall('users/payment_requests');
284
285
    return $result;
286
  }
287
288
  /**
289
   * Get GLS Droppoints
290
   *
291
   * @param array $params
292
   *
293
   * @return mixed
294
   * @throws \PakkelabelsDKException
295
   */
296
  public function glsDroppoints($params) {
297
    $result = $this->makeApiCall('shipments/gls_droppoints', FALSE, $params);
298
299
    return $result;
300
  }
301
302
  /**
303
   * Get PDK Droppoints
304
   *
305
   * @param array $params
306
   *
307
   * @return mixed
308
   * @throws \PakkelabelsDKException
309
   */
310
  public function pdkDroppoints($params) {
311
    $result = $this->makeApiCall('shipments/pdk_droppoints', FALSE, $params);
312
313
    return $result;
314
  }
315
316
  /**
317
   * Get token
318
   *
319
   * @return string
320
   */
321
  public function getToken() {
322
    return $this->token;
323
  }
324
325
  /**
326
   * Make API Call
327
   *
328
   * @param string  $method
329
   * @param boolean $doPost
330
   * @param array   $params
331
   *
332
   * @return mixed
333
   * @throws \PakkelabelsDK\Exception\PakkelabelsDKException
334
   */
335
  protected function makeApiCall($method, $doPost = FALSE, $params = array()) {
336
    $ch                   = curl_init();
337
    $params['token']      = $this->token;
338
    $params['user_agent'] = 'pdk_php_library v' . self::VERSION;
339
340
    $query = http_build_query($params);
341
    if ($doPost === TRUE) {
342
      curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method);
343
      curl_setopt($ch, CURLOPT_POST, 1);
344
      curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
345
    }
346
    else {
347
      curl_setopt(
348
        $ch,
349
        CURLOPT_URL,
350
        self::API_ENDPOINT . '/' . $method . '?' . $query
351
      );
352
    }
353
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
354
355
    $output    = curl_exec($ch);
356
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
357
358
    curl_close($ch);
359
360
    $output = json_decode($output, TRUE);
361
362
    if ($http_code != 200) {
363
      throw new PakkelabelsDKException($output['message']);
364
    }
365
366
    return $output;
367
  }
368
}
369