This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
598 lines (495 loc) · 25.3 KB
/
index.html
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sendcloud service point picker: API integration demo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<style>
body:not(.is-showing-api-result) #postResult {
display: none;
}
.form-label {
font-weight: bold;
}
.form-label,
.form-check-label {
display: block;
}
.form-label__required {
font-weight: normal;
}
pre:last-child {
margin-bottom: 0;
}
.hljs {
background-color: #f8f8f8;
}
</style>
</head>
<body>
<main class="container-fluid p-4">
<div class="page-header">
<h1>Sendcloud service point picker: API integration demo</h1>
</div>
<div class="row mt-4">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<h2 id="basic-example" class="card-title">
Options
</h2>
</div>
<div class="card-body">
<form action="" class="form-horizontal">
<div class="row">
<div class="col-md-2">
<label for="apiKey" class="form-label">
API key <span class="form-label__required">(required)</span>
</label>
<code>apiKey</code>
</div>
<div class="col-md-10">
<input type="text" id="apiKey" class="form-control" />
<div class="form-text">
You can find your API key on the <a href="https://panel.sendcloud.sc/v2/settings/integrations/manage">integration settings page</a>. It is needed to test the examples below.
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="country" class="form-label">
Country <span class="form-label__required">(required)</span>
</label>
<code>country</code>
</div>
<div class="col-md-10">
<select class="form-select" id="country">
<option value="ad">Andorra</option>
<option value="at">Austria</option>
<option value="be">Belgium</option>
<option value="bg">Bulgaria</option>
<option value="ch">Switzerland</option>
<option value="cz">Czechia</option>
<option value="de">Germany</option>
<option value="dk">Denmark</option>
<option value="ee">Estonia</option>
<option value="es">Spain</option>
<option value="fi">Finland</option>
<option value="fr">France</option>
<option value="gb">United Kingdom</option>
<option value="gr">Greece</option>
<option value="hr">Croatia</option>
<option value="hu">Hungary</option>
<option value="ie">Ireland</option>
<option value="it">Italy</option>
<option value="lt">Lithuania</option>
<option value="lu">Luxembourg</option>
<option value="lv">Latvia</option>
<option value="nl" selected>The Netherlands</option>
<option value="no">Norway</option>
<option value="pl">Poland</option>
<option value="pt">Portugal</option>
<option value="ro">Romania</option>
<option value="se">Sweden</option>
<option value="si">Slovenia</option>
<option value="sk">Slovakia</option>
<option value="ua">Ukraine</option>
</select>
<div class="form-text">
Examples:
<code>de</code>,
<code>es</code>,
<code>fr</code>,
<code>gb</code>,
<code>it</code>,
<code>nl</code>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="language" class="form-label">
UI Language <span class="form-label__required">(required)</span>
</label>
<code>language</code>
</div>
<div class="col-md-10">
<select class="form-select" id="language">
<option value="de-de">Deutsch</option>
<option value="en-gb">English (UK)</option>
<option value="en-us" selected>English (US)</option>
<option value="es-es">Español</option>
<option value="fr-fr">Français</option>
<option value="it-it">Italiano</option>
<option value="nl-nl">Nederlands</option>
</select>
<div class="form-text">
Available options:
<code>de-de</code>,
<code>en-gb</code>,
<code>en-us</code>,
<code>es-es</code>,
<code>fr-fr</code>,
<code>it-it</code>,
<code>nl-nl</code>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="postalCode" class="form-label">
Postal Code
</label>
<code>postalCode</code>
</div>
<div class="col-md-10">
<input class="form-control" type="text" id="postalCode" placeholder="1018 AM" />
<div class="form-text">
<strong>Note</strong>: if a service point ID is provided, the postal code parameter will be ignored and the service point location will be displayed instead.
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="city" class="form-label">
City
</label>
<code>city</code>
</div>
<div class="col-md-10">
<input class="form-control" type="text" id="city" placeholder="Amsterdam" />
<div class="form-text">
<strong>Note</strong>: if a service point ID is provided, the city parameter will be ignored and the service point location will be displayed instead.
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="servicePointId" class="form-label">
Service point ID
</label>
<code>servicePointId</code>
</div>
<div class="col-md-10">
<input type="text" id="servicePointId" class="form-control" />
<small class="form-text">You can open the service point picker with a service point ID and its location will be shown instead of the country’s default location.</small>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="postNumber" class="form-label">
Post number
</label>
<code>postNumber</code>
</div>
<div class="col-md-10">
<input type="text" id="postNumber" placeholder="17455093" class="form-control" />
<small class="form-text">Pre-fill the service point picker’s post number field.</small>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<label for="shopType" class="form-label">
Shop type
</label>
<code>shopType</code>
</div>
<div class="col-md-10">
<select class="form-select" id="shopType">
<option value="" selected>Any</option>
<option value="packStation">Pack station (DHL)</option>
<option value="parcelShop">Parcel shop (DHL)</option>
<option value="postOffice">Post office (DHL)</option>
<option value="punto_poste">Punto poste (Poste Italiane)</option>
<option value="service_point">Service point (Poste Italiane)</option>
<option value="SHOP">Shop (Hermes UK)</option>
</select>
<small class="form-text">You can use any value for the <code>shopType</code> option, not just the ones listed here.</small>
<small class="form-text">
Available options:
<code>packStation</code>,
<code>parcelShop</code>,
<code>postOffice</code>,
<code>punto_poste</code>,
<code>service_point</code>,
<code>SHOP</code> (yes, our shop types are wildly inconsistent)
</small>
</div>
</div>
<div class="row mt-3">
<div class="col-md-2">
<span class="form-label">
Carriers
</span>
<code>carriers</code>
</div>
<div class="col-md-10">
<div class="alert alert-info">
<strong>Heads up</strong>: filtering carriers is limited to the carriers selected in your integration settings in the Sendcloud platform.
</div>
<p>
By default, service points from all selected carriers in your integration settings will be available. You can filter the results further by passing a <code>carriers</code> parameter when opening the service point picker.
</p>
<p>
Example values:
<code>bpost</code>,
<code>chronopost</code>,
<code>colissimo</code>,
<code>correos</code>,
<code>dhl</code>,
<code>mondial_relay</code>,
<code>poste_italiane</code>,
<code>postnl</code>,
<code>ups</code>
</p>
<p>
For a full list of supported carrier codes, please check the <a href="https://docs.sendcloud.sc/api/v2/service-points/#list-active-carriers">carrier list endpoint documentation</a> of the service points API.
</p>
<div>
<div class="form-check">
<input id="carrier_bpost" class="form-check-input" type="checkbox" name="carriers" value="bpost">
<label for="carrier_bpost" class="form-check-label">Bpost</label>
</div>
<div class="form-check">
<input id="carrier_chronopost" class="form-check-input" type="checkbox" name="carriers" value="chronopost">
<label for="carrier_chronopost" class="form-check-label">Chronopost</label>
</div>
<div class="form-check">
<input id="carrier_colissimo" class="form-check-input" type="checkbox" name="carriers" value="colissimo">
<label for="carrier_colissimo" class="form-check-label">Colissimo</label>
</div>
<div class="form-check">
<input id="carrier_correos" class="form-check-input" type="checkbox" name="carriers" value="correos">
<label for="carrier_correos" class="form-check-label">Correos</label>
</div>
<div class="form-check">
<input id="carrier_correos_express" class="form-check-input" type="checkbox" name="carriers" value="correos_express">
<label for="carrier_correos_express" class="form-check-label">Correos Express</label>
</div>
<div class="form-check">
<input id="carrier_dhl" class="form-check-input" type="checkbox" name="carriers" value="dhl">
<label for="carrier_dhl" class="form-check-label">DHL</label>
</div>
<div class="form-check">
<input id="carrier_dhl_de" class="form-check-input" type="checkbox" name="carriers" value="dhl_de">
<label for="carrier_dhl_de" class="form-check-label">DHL Germany</label>
</div>
<div class="form-check">
<input id="carrier_dpd" class="form-check-input" type="checkbox" name="carriers" value="dpd">
<label for="carrier_dpd" class="form-check-label">DPD</label>
</div>
<div class="form-check">
<input id="carrier_dpd_fr" class="form-check-input" type="checkbox" name="carriers" value="dpd_fr">
<label for="carrier_dpd_fr" class="form-check-label">DPD France</label>
</div>
<div class="form-check">
<input id="carrier_dpd_gb" class="form-check-input" type="checkbox" name="carriers" value="dpd_gb">
<label for="carrier_dpd_gb" class="form-check-label">DPD UK</label>
</div>
<div class="form-check">
<input id="carrier_dpd_local" class="form-check-input" type="checkbox" name="carriers" value="dpd_local">
<label for="carrier_dpd_local" class="form-check-label">DPD (Local)</label>
</div>
<div class="form-check">
<input id="carrier_gls_de" class="form-check-input" type="checkbox" name="carriers" value="gls_de">
<label for="carrier_gls_de" class="form-check-label">GLS Germany</label>
</div>
<div class="form-check">
<input id="carrier_gls_es" class="form-check-input" type="checkbox" name="carriers" value="gls_es">
<label for="carrier_gls_es" class="form-check-label">GLS Spain</label>
</div>
<div class="form-check">
<input id="carrier_gls_nl" class="form-check-input" type="checkbox" name="carriers" value="gls_nl">
<label for="carrier_gls_nl" class="form-check-label">GLS Netherlands</label>
</div>
<div class="form-check">
<input id="carrier_hermes_gb" class="form-check-input" type="checkbox" name="carriers" value="hermes_gb">
<label for="carrier_hermes_gb" class="form-check-label">Hermes UK</label>
</div>
<div class="form-check">
<input id="carrier_hermes_c2c_gb" class="form-check-input" type="checkbox" name="carriers" value="hermes_c2c_gb">
<label for="carrier_hermes_c2c_gb" class="form-check-label">Hermes UK (C2C)</label>
</div>
<div class="form-check">
<input id="carrier_homerr" class="form-check-input" type="checkbox" name="carriers" value="homerr">
<label for="carrier_homerr" class="form-check-label">Homerr</label>
</div>
<div class="form-check">
<input id="carrier_inpost_gb" class="form-check-input" type="checkbox" name="carriers" value="inpost_gb">
<label for="carrier_inpost_gb" class="form-check-label">Inpost UK</label>
</div>
<div class="form-check">
<input id="carrier_mondial_relay" class="form-check-input" type="checkbox" name="carriers" value="mondial_relay">
<label for="carrier_mondial_relay" class="form-check-label">Mondial Relay</label>
</div>
<div class="form-check">
<input id="carrier_postat" class="form-check-input" type="checkbox" name="carriers" value="postat">
<label for="carrier_postat" class="form-check-label">Post AT</label>
</div>
<div class="form-check">
<input id="carrier_poste_italiane" class="form-check-input" type="checkbox" name="carriers" value="poste_italiane">
<label for="carrier_poste_italiane" class="form-check-label">Poste Italiane</label>
</div>
<div class="form-check">
<input id="carrier_postnl" class="form-check-input" type="checkbox" name="carriers" value="postnl">
<label for="carrier_postnl" class="form-check-label">PostNL</label>
</div>
<div class="form-check">
<input id="carrier_royal_mail" class="form-check-input" type="checkbox" name="carriers" value="royal_mail">
<label for="carrier_royal_mail" class="form-check-label">Royal Mail</label>
</div>
<div class="form-check">
<input id="carrier_seur" class="form-check-input" type="checkbox" name="carriers" value="seur">
<label for="carrier_seur" class="form-check-label">SEUR</label>
</div>
<div class="form-check">
<input id="carrier_ups" class="form-check-input" type="checkbox" name="carriers" value="ups">
<label for="carrier_ups" class="form-check-label">UPS</label>
</div>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-12">
<button type="button" class="btn btn-primary" id="openServicePointPicker">
Open service point picker
</button>
</div>
</div>
</form>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h2 id="usage" class="card-title">
Usage
</h2>
</div>
<div class="card-body">
<p>
Begin by adding our script to your HTML page:
</p>
<pre><code><script src="https://embed.sendcloud.sc/spp/1.0.0/api.min.js"></script></code></pre>
<p>
You need to provide <code>sendcloud.servicePoints.open</code> a config object, a success callback function and an error callback function.
</p>
<p>The example on this page uses the following code:</p>
<pre><code id="exampleCode" class="language-javascript"></code></pre>
</div>
</div>
</div>
<div class="col-lg-4">
<div id="postResult" class="card">
<div class="card-header">
<h2 id="selected-service-point" class="card-title">
Selected service point
</h2>
</div>
<div class="card-body">
<p>
You now need to send the retrieved data to Sendcloud’s Parcel API (described in <a href="https://docs.sendcloud.sc/api/v2/index.html#parcel" target="_blank" rel="noopener noreferrer nofollow">https://docs.sendcloud.sc/api/v2/index.html#parcel</a>). When creating a new parcel you’ll need to forward the delivery address as stated in the result below among with the service point ID in the <code>to_service_point</code> field. <strong>Be aware that the service point IDs have limited lifetime and change constantly. They weren’t designed for persistance.</strong>
</p>
<p>
When creating shipments with service points <strong>always</strong> send the <code>to_post_number</code> field with the value returned by the service point selection.
</p>
<p>
<strong>Post number:</strong> <span id="postNumberResultElement"></span><br>
<strong>Service point:</strong>
</p>
<pre><code id="servicePointResult" class="language-json"></code></pre>
</div>
</div>
</div>
</div>
</main>
<script src="https://embed.sendcloud.sc/spp/1.0.0/api.min.js"></script>
<!-- Usage -->
<script id="actualExampleCode">
const apiKeyField = document.getElementById('apiKey')
const countryField = document.getElementById('country')
const languageField = document.getElementById('language')
const postalCodeField = document.getElementById('postalCode')
const cityField = document.getElementById('city')
const servicePointIdField = document.getElementById('servicePointId')
const postNumberField = document.getElementById('postNumber')
const shopTypeField = document.getElementById('shopType')
const openServicePointPickerButton = document.getElementById('openServicePointPicker')
const servicePointResultElement = document.getElementById('servicePointResult')
const resultPostNumberField = document.getElementById('postNumberResultElement')
openServicePointPickerButton.addEventListener('click', openServicePointPicker)
function openServicePointPicker() {
if (!apiKeyField.value) {
alert('Missing API key')
}
/**
* @typedef {object} ServicePointPickerOptions
* @property {string} apiKey Required. Set it to your API key.
* @property {string} country Required. ISO-2 code of the country you want to display the map in.
* @property {string} language Optional. Defaults to 'en-us'.
* @property {string} postalCode Optional. Defaults to '' (empty string).
* @property {string} city Optional. Defaults to '' (empty string).
* @property {string} carriers Optional. Comma-separated list of carriers for which to filter service points. If not provided, the service points aren’t filtered.
* @property {string} shopType Optional. Defaults to '' (empty string).
* @property {number} servicePointId Optional. Set a pre-selected service point to be shown upon displaying the map.
* @property {string} postNumber Optional. Set a pre-defined post number to fill its corresponding field upon displaying the map.
*/
/** @type {ServicePointPickerOptions} */ const options = {
apiKey: apiKeyField.value,
country: countryField.value,
postalCode: postalCodeField.value,
city: cityField.value,
carriers: getCarrierList(),
servicePointId: parseInt(servicePointIdField.value),
postNumber: postNumberField.value,
language: languageField.value,
shopType: shopTypeField.value,
}
sendcloud.servicePoints.open(options, successCallback, failureCallback)
}
function getCarrierList() {
const carrierInputs = Array.from(document.querySelectorAll('[name="carriers"]'))
return carrierInputs
.filter((carrierInput) => carrierInput.checked)
.map((carrierInput) => carrierInput.value)
.join()
}
/**
* Handles the selection of a service point.
*
* @param {ServicePoint} servicePoint
* @param {string} postNumber Used as `to_post_number` field in the Parcel creation API
*/
function successCallback(servicePoint, postNumber) {
servicePointResultElement.innerHTML = JSON.stringify(servicePoint, null, 2)
resultPostNumberField.innerText = postNumber || '—'
hljs.highlightElement(servicePointResultElement);
document.body.classList.add('is-showing-api-result')
}
/**
* Handles error events and closing the service point picker.
*
* @param {string[]} errors
*/
function failureCallback(errors) {
document.body.classList.remove('is-showing-api-result')
console.error('[Failure callback]', errors.join(', '))
}
</script>
<script>
const code = document.getElementById('actualExampleCode').innerText.replace(/^\n/, '')
const indentation = code.match(/^(\s+)/)[0]
const deIndentedCode = code.split('\n')
.map((line) => line.startsWith(indentation) ? line.replace(indentation, '') : line)
.join('\n')
.trim()
document.getElementById('exampleCode').innerHTML = deIndentedCode
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<script>
hljs.highlightAll()
</script>
</body>
</html>