Skip to content

Commit 2646834

Browse files
authored
Merge pull request add-servicepoint-support
feat: Add servicepoint picker
2 parents 240187e + 317de68 commit 2646834

9 files changed

Lines changed: 152 additions & 49 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="col-md-3" style="margin-bottom:10px">
2+
<div class="card">
3+
<img src="<?php echo $carrier->logo_url ?>" width="40" height="40" style="margin-top:5px;"
4+
class="card-img-top" alt="<?php echo $carrier->name ?>">
5+
<div class="card-body">
6+
<h5 class="card-title">Name: <?php echo $method->title ?></h5>
7+
<p class="card-text">Description: <?php echo $method->description ?></p>
8+
<p class="card-text">Order by: <?php echo $method_option['parcel_handover_date'] ?></p>
9+
<p class="card-text">Delivered on: <?php echo $method_option['delivery_date'] ?></p>
10+
<p class="card-text">Cost:
11+
<?php echo $method->checkoutMethodPrice->price." ".$method->checkoutMethodPrice->currency; ?>
12+
</p>
13+
<button onclick="setOption(<?php echo $method->id ?>)" class="btn btn-success"
14+
style="float:right">Select Method</button>
15+
</div>
16+
</div>
17+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="col-md-3" style="margin-bottom:10px">
2+
<div class="card">
3+
<img src="<?php echo $carrier->logo_url ?>" width="40" height="40" style="margin-top:5px;"
4+
class="card-img-top" alt="<?php echo $carrier->name ?>">
5+
<div class="card-body">
6+
<h5 class="card-title">Name: <?php echo $method->title ?></h5>
7+
<p class="card-text">Description: <?php echo $method->description ?></p>
8+
<p class="card-text">Order by: <?php echo $method_option['parcel_handover_date'] ?></p>
9+
<p class="card-text">Delivered Today!</p>
10+
<p class="card-text">Cost:
11+
<?php echo $method->checkoutMethodPrice->price." ".$method->checkoutMethodPrice->currency; ?>
12+
</p>
13+
<button onclick="setOption(<?php echo $method->id ?>)" class="btn btn-success"
14+
style="float:right">Select Method</button>
15+
</div>
16+
</div>
17+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="col-md-3" style="margin-bottom:10px">
2+
<div class="card">
3+
<img src="<?php echo $carrier->logo_url ?>" width="40" height="40" style="margin-top:5px;"
4+
class="card-img-top" alt="<?php echo $carrier->name ?>">
5+
<div class="card-body">
6+
<h5 class="card-title">Name: <?php echo $method->title ?></h5>
7+
<p class="card-text">Description: <?php echo $method->description ?></p>
8+
<p class="card-text">Cost:
9+
<?php echo $method->checkoutMethodPrice->price." ".$method->checkoutMethodPrice->currency; ?>
10+
</p>
11+
<button onclick="window.dc.carrier='<?php echo $carrier->code ?>';openServicePointPicker()" class="btn btn-success"
12+
style="float:right">Open Service Point Picker </button>
13+
</div>
14+
</div>
15+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="col-md-3" style="margin-bottom:10px">
2+
<div class="card">
3+
<img src="<?php echo $carrier->logo_url ?>" width="40" height="40" style="margin-top:5px;"
4+
class="card-img-top" alt="<?php echo $carrier->name ?>">
5+
<div class="card-body">
6+
<h5 class="card-title">Name: <?php echo $method->title ?></h5>
7+
<p class="card-text">Description: <?php echo $method->description ?></p>
8+
<p class="card-text">Cost:
9+
<?php echo $method->checkoutMethodPrice->price." ".$method->checkoutMethodPrice->currency; ?>
10+
</p>
11+
<button onclick="setOption(<?php echo $method->id ?>)" class="btn btn-success"
12+
style="float:right">Select Method</button>
13+
</div>
14+
</div>
15+
</div>

app/private/GetCheckoutConfig.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ private function generateApiKey(): string
5151
{
5252
return $this->key.":".$this->secret;
5353
}
54+
55+
public function returnApiKey(): string
56+
{
57+
return $this->key;
58+
}
59+
5460
private function checkResponse($response){
5561
if(!isset($response['delivery_options']))
5662
{

app/private/Method.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public $title;
1010
public $description;
1111
public $cut_off_time;
12+
public $delivery_method_type;
1213
public $checkoutMethodPrice;
1314
public array $checkoutDates;
1415
/**
@@ -21,6 +22,7 @@ function __construct(array $method)
2122
$this->title = $method['title'];
2223
$this->description = $method['description'];
2324
$this->cut_off_time = $method['cut_off_time'];
25+
$this->delivery_method_type = $method['delivery_method_type'];
2426
$this->checkoutMethodPrice = new CheckoutMethodPrice($method['shipping_rate']);
2527
$this->checkoutDates = isset($method['delivery_dates']) ? $method['delivery_dates'] : [["delivery_date"=> "Anytime", "parcel_handover_date" => "Anytime"]];
2628
}

app/public/checkout/index.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
require_once(__DIR__.'/../../private/autoload.php');
3+
set_include_path(__DIR__."/../../");
4+
5+
require_once(get_include_path().'private/autoload.php');
46

57
Functions::setEnvVars();
68

@@ -18,6 +20,19 @@
1820
integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous">
1921
</script>
2022
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
23+
<script src="https://embed.sendcloud.sc/spp/1.0.0/api.min.js"></script>
24+
<script>
25+
this.dc = [];
26+
27+
this.dc.apikey = "<?php echo $dc->returnApiKey(); ?>"
28+
this.dc.country = "<?php echo $_POST['country']; ?>"
29+
this.dc.postal_code = "<?php echo $_POST['postal']; ?>"
30+
this.dc.city = "<?php echo $_POST['city']; ?>";
31+
32+
</script>
33+
34+
<script src="/checkout/service_points.js"></script>
35+
2136
</head>
2237

2338
<body>
@@ -34,35 +49,26 @@
3449
$carrier = new Carrier((array)$delivery_option['carrier']);
3550
$method = new CheckoutMethod((array)$delivery_option);
3651

37-
echo "<h5>Method: ".$method->id."</h5><hr>";
52+
echo "<h5>Method: ".$method->id."</h5>";
53+
echo "<h6>Type: ".$method->delivery_method_type."</h6><hr>";
3854

3955
foreach($method->checkoutDates as $method_option)
4056
{
41-
if($method_option['delivery_date'] != "Anytime")
57+
switch($method->delivery_method_type)
4258
{
43-
$method_option['parcel_handover_date'] = date("Y-m-d H:i", strtotime($method_option['parcel_handover_date']));
44-
$method_option['delivery_date'] = date("Y-m-d", strtotime($method_option['delivery_date']));
59+
case 'same_day_delivery':
60+
include(get_include_path()."private/DeliveryOptions/SameDay.php");
61+
break;
62+
case 'nominated_day_delivery':
63+
include(get_include_path()."private/DeliveryOptions/NominatedDay.php");
64+
break;
65+
case 'standard_delivery':
66+
include(get_include_path()."private/DeliveryOptions/Standard.php");
67+
break;
68+
case 'service_point_delivery':
69+
include(get_include_path()."private/DeliveryOptions/ServicePoint.php");
70+
break;
4571
}
46-
?>
47-
<div class="col-md-3" style="margin-bottom:10px">
48-
<div class="card">
49-
<img src="<?php echo $carrier->logo_url ?>" width="40" height="40" style="margin-top:5px;"
50-
class="card-img-top" alt="<?php echo $carrier->name ?>">
51-
<div class="card-body">
52-
<h5 class="card-title">Name: <?php echo $method->title ?></h5>
53-
<p class="card-text">Description: <?php echo $method->description ?></p>
54-
<p class="card-text">Order by: <?php echo $method_option['parcel_handover_date'] ?></p>
55-
<p class="card-text">Delivered on: <?php echo $method_option['delivery_date'] ?></p>
56-
<p class="card-text">Cost:
57-
<?php echo $method->checkoutMethodPrice->price." ".$method->checkoutMethodPrice->currency; ?>
58-
</p>
59-
<button onclick="setOption(<?php echo $method->id ?>)" class="btn btn-success"
60-
style="float:right">Select Method</button>
61-
</div>
62-
</div>
63-
</div>
64-
65-
<?php
6672
}
6773
?>
6874

app/public/checkout/scripts.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function openServicePointPicker() {
2+
3+
/**
4+
* @typedef {object} ServicePointPickerOptions
5+
* @property {string} apiKey Required. Set it to your API key.
6+
* @property {string} country Required. ISO-2 code of the country you want to display the map in.
7+
* @property {string} language Optional. Defaults to 'en-us'.
8+
* @property {string} postalCode Optional. Defaults to '' (empty string).
9+
* @property {string} city Optional. Defaults to '' (empty string).
10+
* @property {string} carriers Optional. Comma-separated list of carriers for which to filter service points. If not provided, the service points aren’t filtered.
11+
* @property {string} shopType Optional. Defaults to '' (empty string).
12+
* @property {number} servicePointId Optional. Set a pre-selected service point to be shown upon displaying the map.
13+
* @property {string} postNumber Optional. Set a pre-defined post number to fill its corresponding field upon displaying the map.
14+
*/
15+
16+
/** @type {ServicePointPickerOptions} */ const options = {
17+
apiKey: this.dc.apikey,
18+
country: this.dc.country,
19+
postalCode: this.dc.postal_code,
20+
city: this.dc.city,
21+
carriers: this.dc.carrier,
22+
language: "en-gb",
23+
}
24+
25+
sendcloud.servicePoints.open(options, successCallback, failureCallback)
26+
}
27+
28+
/**
29+
* Handles the selection of a service point.
30+
*
31+
* @param {ServicePoint} servicePoint
32+
* @param {string} postNumber Used as `to_post_number` field in the Parcel creation API
33+
*/
34+
function successCallback(servicePoint, postNumber) {
35+
servicePointResultElement.innerHTML = JSON.stringify(servicePoint, null, 2)
36+
resultPostNumberField.innerText = postNumber || '—'
37+
hljs.highlightElement(servicePointResultElement);
38+
document.body.classList.add('is-showing-api-result')
39+
}
40+
41+
/**
42+
* Handles error events and closing the service point picker.
43+
*
44+
* @param {string[]} errors
45+
*/
46+
function failureCallback(errors) {
47+
document.body.classList.remove('is-showing-api-result')
48+
console.error('[Failure callback]', errors.join(', '))
49+
}

0 commit comments

Comments
 (0)