forked from elementary/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolocate.php
57 lines (50 loc) · 1.67 KB
/
geolocate.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
<?php
require_once __DIR__.'/../_backend/classify.functions.php';
require_once __DIR__.'/../_backend/classify.get_ip.php';
require_once __DIR__.'/../_backend/store/address.php';
require_once __DIR__.'/../_backend/store/api.php';
//// API for Geolocating Shipping
// Parameters:
// - shipping [no value]
// - item (printful variant id of 1 product)
// Outputs:
// {
// "ip": "92.26.51.149",
// "shipping": {
// "address": ...,
// "estimates": ...
// }
// }
if ( isset($_GET['shipping']) && !empty($_GET['item']) ) {
$estimatedAddress = getCurrentLocation($ip);
$result = array(
'ip' => $ip,
'shipping' => array(
'address' => $estimatedAddress,
),
);
if (
!empty($estimatedAddress['countryCode']) &&
!empty($estimatedAddress['stateCode']) &&
!empty($estimatedAddress['city']) &&
!empty($estimatedAddress['postcode'])
) {
$address = new \Store\Address\Address();
$address->set_line1('');
$address->set_country($estimatedAddress['countryCode']);
$address->set_state($estimatedAddress['stateCode']);
$address->set_city($estimatedAddress['city']);
$address->set_postal($estimatedAddress['postcode']);
$items = array(
array (
'quantity' => 1,
'variant_id' => $_GET['item'],
),
);
$result['shipping']['estimates'] = Store\Api\get_shipping($address, $items);
}
echo json_encode($result, JSON_PRETTY_PRINT);
} else {
$result = array ('error' => 'No parameters were supplied, but some were expected.');
echo json_encode($result, JSON_PRETTY_PRINT);
}