This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
310 lines (282 loc) · 14.3 KB
/
test.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
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
<?php
//Variables
$sendy_api_url = "https://api.sendyit.com/v1/";
//$sendy_api_url = "https://apitest.sendyit.com/v1/";
$google_maps_api_key = "AIzaSyBAgQ-vCy1106_l1iZFudZeYQGx2ghCS3g";
//Form data
$billing_first_name = isset($_POST['billing_first_name'])?$_POST['billing_first_name']:"";
$billing_last_name = isset($_POST['billing_last_name'])?$_POST['billing_last_name']:"";
$billing_company = isset($_POST['billing_company'])?$_POST['billing_company']:"";
$billing_email = isset($_POST['billing_email'])?$_POST['billing_email']:"";
$billing_phone = isset($_POST['billing_phone'])?$_POST['billing_phone']:"";
$billing_country = isset($_POST['billing_country'])?$_POST['billing_country']:"";
$billing_address_1 = isset($_POST['billing_address_1'])?$_POST['billing_address_1']:"";
$billing_address_2 = isset($_POST['billing_address_2'])?$_POST['billing_address_2']:"";
$billing_city = isset($_POST['billing_city'])?$_POST['billing_city']:"";
$billing_state = isset($_POST['billing_state'])?$_POST['billing_state']:"";
$billing_postcode = isset($_POST['billing_postcode'])?$_POST['billing_postcode']:"";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SENDY TEST</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row">
<form class="" method="post" action="">
<div class="col-md-6">
<div class="woocommerce-billing-fields">
<h3>Billing Details</h3>
<div class="row">
<p class="form-group col-sm-6 validate-required woocommerce-validated" id="billing_first_name_field">
<label for="billing_first_name" class="">First Name <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text form-control" name="billing_first_name" id="billing_first_name" placeholder="" autocomplete="given-name" value="<?php echo $billing_first_name; ?>" required>
</p>
<p class="form-group col-sm-6 validate-required" id="billing_last_name_field">
<label for="billing_last_name" class="">Last Name <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text form-control" name="billing_last_name" id="billing_last_name" placeholder="" autocomplete="family-name" value="<?php echo $billing_last_name; ?>" required>
</p>
</div>
<div class="clear"></div>
<p class="form-group form-row-wide" id="billing_company_field">
<label for="billing_company" class="">Company Name</label>
<input type="text" class="input-text form-control" name="billing_company" id="billing_company" placeholder="" autocomplete="organization" value="<?php echo $billing_company; ?>">
</p>
<div class="row">
<p class="form-group col-sm-6 validate-required validate-email" id="billing_email_field">
<label for="billing_email" class="">Email Address <abbr class="required" title="required">*</abbr></label>
<input type="email" class="input-text form-control" name="billing_email" id="billing_email" placeholder="" autocomplete="email" value="<?php echo $billing_email; ?>" required>
</p>
<p class="form-group col-sm-6 validate-required validate-phone" id="billing_phone_field">
<label for="billing_phone" class="">Phone <abbr class="required" title="required">*</abbr></label>
<input type="tel" class="input-text form-control" name="billing_phone" id="billing_phone" placeholder="" autocomplete="tel" value="<?php echo $billing_phone; ?>" required>
</p>
</div>
<div class="clear"></div>
<p class="form-group form-row-wide address-field update_totals_on_change validate-required" id="billing_country_field">
<label for="billing_country" class="">Country <abbr class="required" title="required">*</abbr></label>
<select name="billing_country" id="billing_country" autocomplete="country" class="country_to_state country_select form-control" required>
<option value="">Select a country…</option>
<option value="KE" selected="selected">Kenya</option>
<option value="TZ">Tanzania</option>
<option value="UG">Uganda</option>
</select>
</p>
<p class="form-group form-row-wide address-field validate-required woocommerce-validated" id="billing_address_1_field">
<label for="billing_address_1" class="">Address 1 <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text form-control" name="billing_address_1" id="billing_address_1" placeholder="Street address" autocomplete="address-line1" value="<?php echo $billing_address_1; ?>" data-geo="formatted_address" required>
</p>
<p class="form-group form-row-wide address-field" id="billing_address_2_field">
<label for="billing_address_2" class="">Address 2</label>
<input type="text" class="input-text form-control" name="billing_address_2" id="billing_address_2" placeholder="Apartment, suite, unit etc. (optional)" autocomplete="address-line2" value="<?php echo $billing_address_2; ?>" data-geo="name">
</p>
<p class="form-group form-row-wide address-field validate-required" id="billing_city_field">
<label for="billing_city" class="">Town / City <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text form-control" name="billing_city" id="billing_city" placeholder="" autocomplete="address-level2" value="<?php echo $billing_city; ?>" data-geo="locality" required>
</p>
<div class="row">
<p class="form-group col-sm-6 address-field validate-required validate-state" id="billing_state_field">
<label for="billing_state" class="">State / County <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text form-control" placeholder="" autocomplete="address-level1" name="billing_state" id="billing_state" value="<?php echo $billing_state; ?>" data-geo="administrative_area_level_1" required>
</p>
<p class="form-group col-sm-6 address-field validate-postcode" id="billing_postcode_field">
<label for="billing_postcode" class="">Postcode / Zip</label>
<input type="text" class="input-text form-control" name="billing_postcode" id="billing_postcode" placeholder="" autocomplete="postal-code" value="<?php echo $billing_postcode; ?>" data-geo="postal_code">
</p>
</div>
<div class="clear"></div>
<p class="form-group">
<input type="submit" class="btn btn-default" name="Submit" value="Submit" id="find">
</p>
</div>
</div>
<div class="col-md-6">
<h3>Map View</h3>
<div class="map_canvas" id="map_canvas" style="width: 555px;height: 400px;margin: 20px 0 10px 0;"></div>
<div class="clear"></div>
<pre id="logger">Log:</pre>
</div>
</form>
</div>
</div>
</body>
<script src="https://maps.googleapis.com/maps/api/js?key=<?php echo $google_maps_api_key; ?>&libraries=places"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="partials/jquery.geocomplete.js"></script>
<script src="partials/logger.js"></script>
<script>
jQuery(document).ready(function ($) {
var billing_country = $("#billing_country").val();
var country = (typeof billing_country === 'undefined') ? toLowerCase(billing_country) : 'ke';
$.log(country);
var center = new google.maps.LatLng(-1.2920659,36.82194619999996);
$("#billing_address_1").geocomplete({
country: country,
map: ".map_canvas",
details: "form",
detailsAttribute: "data-geo",
types: ["geocode", "establishment"],
mapOptions: {
zoom: 14,
center: center,
scrollwheel: false,
mapTypeId: "roadmap"
},
}).bind("geocode:error", function(event, error){
$.log(error);
});
$("#billing_address_1").on('change', function(e){
//$(this).trigger("geocode");
});
});
</script>
</html>
<?php
function sendy_wc_curl_exec( $url, $command, $data, $request_token_id ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
# Setup request to send json via POST.
$payload = json_encode(array('command' => $command, 'data' => $data, 'request_token_id' => $request_token_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Send request.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function google_maps_api_curl_exec( $url ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Send request.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
if( isset($_POST['Submit']) ){
//$fullAddress = $billing_address_1.",".$billing_city.",".$billing_state;
$fullAddress = $billing_address_1;
$prepAddr = str_replace(" ", "+", $fullAddress);
$prepURL = "https://maps.google.com/maps/api/geocode/json?address=$prepAddr&components=country:$billing_country&key=$google_maps_api_key";
// Convert an address to geocode Latitude/Longitude positioning with Google Maps.
// Example http://maps.google.com/maps/api/geocode/json?address=Kindaruma+Rd+Nairobi+Kenya&sensor=false
//$geocode = file_get_contents($prepURL);
$geocode = google_maps_api_curl_exec( $prepURL );
$georesults = json_decode($geocode, true);
echo "<pre>";
echo "<h1>GOOGLE MAP API RESPONSE</h1>";
//echo "URL:".$prepURL."<br/>";
print_r($georesults);
$geo_lat = $georesults["results"][0]["geometry"]["location"]["lat"];
$geo_long = $georesults["results"][0]["geometry"]["location"]["lng"];
$geo_addr = $georesults["results"][0]["formatted_address"];
if( !empty($sendy_api_url) && !empty($geo_lat) && !empty($geo_long) ){
$command = 'request';//Command of the request e.g. request
$request_token_id = md5(time());//Optional: Token ID of the request
//data
$api_key = "cZGFbGShHTHYnYJTN7FC";
$api_username = "witstechnologiesltd";
$vendor_type = 1;//1 for bike, 2 for Pick up and 3 for Van
//data: from
$from_name = "Test Shop";//Name of the pick up location e.g. Shop
$from_lat = -1.3370364;//Lat of the pick up location
$from_long = 36.7081472;//Long of the pick up location
$from_description = "To be picked from ($from_lat,$from_long)";//Desc of the pick up location e.g. office
//data: to
$to_name = 'Customer';//Name of the destination e.g. Lavington
$to_lat = floatval($geo_lat);//Lat of the destination
$to_long = floatval($geo_long);//Long of the destination
$to_description = "To be delivered to $geo_addr ($to_lat,$to_long).";//Desc of the destination e.g. home
//data: recepient
$recepient_name = "Test User";//Name of the recepient
$recepient_phone = "0711222333";//Phone of the recepient
$recepient_email = "[email protected]";//Email of the recepient
//data: delivery_details
$pick_up_date = date('Y-m-d', strtotime("+1 week"));
//data: delivery_details: collect_payment
$status = false;
$pay_method = 0;//Payment method of collecting the cash. Default to 0.
$amount = 0;//Amount of cash being collected
//data: delivery_details
$return = false; //If order is one way; false. If order is two way; true.
$note = ""; //If there is a note you need the rider to receive
$note_status = false; //True if you need the note sent and false if not.
$request_type = "quote";//Request type of the delivery. Default is 'quote', this gives you a price estimate.
$order_type = "batch_later_order";//ondemand_order for immediately or batch_later_order for later
$ecommerce_order = "";//Optional: ecommerce order of the delivery.
$skew = 1;//(int) skew of the package of the delivery.
//data: delivery_details: package_size
$weight = 0;
$height = 0;
$width = 0;
$item_name = "Test Product";
$data = array(
"api_key" => "".$api_key."",
"api_username" => "".$api_username."",
"vendor_type" => $vendor_type,
"from" => array(
'from_name' => "".$from_name."",
'from_lat' => $from_lat,
'from_long' => $from_long,
'from_description' => "".$from_description."",
),
"to" => array(
'to_name' => "".$to_name."",
'to_lat' => $to_lat,
'to_long' => $to_long,
'to_description' => "".$to_description."",
),
"recepient" => array(
'recepient_name' => "".$recepient_name."",
'recepient_phone' => "".$recepient_phone."",
'recepient_email' => "".$recepient_email."",
),
"delivery_details" => array(
"pick_up_date" => "".$pick_up_date."",
"collect_payment" => array(
"status" => $status,
"pay_method" => $pay_method,
"amount" => $amount,
),
"return" => $return,
"note" => "".$note."",
"note_status" => $note_status,
"request_type" => "".$request_type."",
"order_type" => "".$order_type."",
"ecommerce_order" => "".$ecommerce_order."",
"skew" => $skew,
"package_size" => array(
"weight" => $weight,
"height" => $height,
"width" => $width,
"item_name" => "".$item_name."",
)
),
);
echo "<h1>DATA SENT TO SENDY API</h1>";
echo "URL:".$sendy_api_url."<br/>";
print_r($data);
# Execute the curl command
$result = sendy_wc_curl_exec( $sendy_api_url, $command, $data, $request_token_id );
# Print response
$result = json_decode($result, true);
echo "<br/><h1>DATA RECEIVED FROM SENDY API</h1>";
print_r($result);
}else{
echo "<h1>ERROR</h1>";
echo "Check your API keys and ensure all mandatory fields are not empty before submission";
}
echo "</pre>";
}
?>