-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
294 lines (201 loc) · 7.28 KB
/
index.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
<?php
require_once 'settings.php';
//Check, if csv file already exists; If yes exit the script
if(file_exists($csvPath)){
echo $execTime . " - CSV file was not processed yet!";
exit();
}
$baseUrl = 'https://sellerapi.kaufland.com/v2';
// We are going to get information about the product with ID 20574181
// (at the URL https://www.kaufland.de/product/20574181/)
$lastExec = file_get_contents('last.txt');
$uri = 'https://sellerapi.kaufland.com/v2/orders?storefront=de&ts_created_from_iso='.$lastExec.'&fulfillment_type=fulfilled_by_merchant&limit=100&offset=0';
//The query parameters must be URL-encoded, but the "=" and "&" signs should not be encoded
// Current Unix timestamp in seconds
$timestamp = time();
// Name of your partner solution
$userAgent = 'Löchel-Industriebedarf-Order-Receiver';
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Shop-Client-Key: ' . $clientKey;
$headers[] = 'Shop-Signature: ' . signRequest('GET', $uri, '', $timestamp, $secretKey);
$headers[] = 'Shop-Timestamp: ' . $timestamp;
$headers[] = 'User-Agent: ' . $userAgent;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
//API call failed
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
//API call was successfull
else{
$resultArr = json_decode($result);
$csv = array();
array_push($csv, generateCSVHeadline());
foreach(array_reverse($resultArr->data) as $order){
$orderInfo = getOrderInfo($order->id_order, $clientKey, $secretKey);
$shippingCosts = "-1";
//Cycle throught order units
foreach($orderInfo->order_units as $orderUnit){
if($orderUnit->status !== "sent" && $orderUnit->status !== "received"){
$billing = $orderInfo->billing_address;
$shipping = $orderInfo->shipping_address;
if($billing->first_name != " " && $billing->first_name != "" && $billing->first_name != null){
$csvLine = array();
//E-Mail
array_push($csvLine, $orderInfo->buyer->email);
//Order id
array_push($csvLine, $orderInfo->id_order);
//Invoice Firm1
array_push($csvLine, $billing->first_name . " " . $billing->last_name);
//Invoice Firm2
array_push($csvLine, $billing->company_name);
//Invoice Street
array_push($csvLine, $billing->street . " " . $billing->house_number);
//Invoice ZipCode
array_push($csvLine, $billing->postcode);
//Invoice City
array_push($csvLine, $billing->city);
//Invoice Country
array_push($csvLine, $billing->country);
//Invoice Phone
array_push($csvLine, $billing->phone);
//Shipping Firm1
array_push($csvLine, $shipping->first_name . " " . $shipping->last_name);
//Shipping Firm2
array_push($csvLine, $shipping->company_name);
//Shipping Street
array_push($csvLine, $shipping->street . " " . $shipping->house_number);
//Shipping ZipCode
array_push($csvLine, $shipping->postcode);
//Shipping City
array_push($csvLine, $shipping->city);
//Shipping Country
array_push($csvLine, $shipping->country);
//Shipping Phone
array_push($csvLine, $shipping->phone);
//Article Id
array_push($csvLine, $orderUnit->id_offer);
//Article Price
array_push($csvLine, $orderUnit->price / 100);
//Shipping Costs
if($shippingCosts == "-1"){ $shippingCosts = $orderUnit->shipping_rate / 100; }
array_push($csvLine, $shippingCosts);
//Fees
array_push($csvLine, $orderUnit->price / 100 * 0.12);
//Memo
array_push($csvLine, "");
//Offer ID
array_push($csvLine, "");
//Order Unit ID
array_push($csvLine, sprintf('%0.0F', $orderUnit->id_order_unit));
//Timestamp created
array_push($csvLine, $orderUnit->ts_created_iso);
$lastCreated = $orderUnit->ts_created_iso;
//Timestamp updated
array_push($csvLine, $orderUnit->ts_updated_iso);
//Last execution
array_push($csvLine, $lastExec);
//Add order line to csv
array_push($csv, $csvLine);
}
else{
echo $execTime . " - New order without information.";
}
}
}
}
if(sizeof($csv) !== 1){
writeCsv($csvPath, $csv);
writeLastToFile($lastCreated);
echo $execTime . " - New order(s) got parsed. | Last order: " . $lastCreated . " | Last execution: " . $lastExec;
}
else{
echo $execTime . " - No new orders. | Last execution: " . $lastExec;
}
}
curl_close($ch);
function getOrderInfo($orderId, $clientKey, $secretKey){
$timestamp = time();
$userAgent = 'Löchel-Industriebedarf-GetOrderUnits';
$uri = 'https://sellerapi.kaufland.com/v2/orders/'.$orderId.'?embedded=order_invoices';
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Shop-Client-Key: ' . $clientKey;
$headers[] = 'Shop-Signature: ' . signRequest('GET', $uri, '', $timestamp, $secretKey);
$headers[] = 'Shop-Timestamp: ' . $timestamp;
$headers[] = 'User-Agent: ' . $userAgent;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
else{
$resultArr = json_decode($result);
}
curl_close($ch);
return $resultArr->data;
}
function generateCSVHeadline(){
$headline = array();
array_push($headline, "Mail");
array_push($headline, "Bestellungs-ID");
array_push($headline, "Rechnungsfirma 1");
array_push($headline, "Rechnungsfirma 2");
array_push($headline, "Rechnungsstrasse");
array_push($headline, "Rechnungs PLZ");
array_push($headline, "Rechnungsort");
array_push($headline, "Rechnungs LKZ");
array_push($headline, "Rechnungs Telefon");
array_push($headline, "Lieferfirma 1");
array_push($headline, "Lieferfirma 2");
array_push($headline, "Lieferstrasse");
array_push($headline, "Liefer PLZ");
array_push($headline, "Lieferort");
array_push($headline, "Liefer LKZ");
array_push($headline, "Liefer Telefon");
array_push($headline, "Artikelnr");
array_push($headline, "Preis");
array_push($headline, "Porto");
array_push($headline, "Nebenkosten");
array_push($headline, "Memo");
array_push($headline, "Offer ID");
array_push($headline, "Offer Unit ID");
array_push($headline, "TSCreated");
array_push($headline, "TSUpdated");
array_push($headline, "LastExecution");
return $headline;
}
function writeLastToFile($timeStr){
$lastWriter = fopen('last.txt', 'w+');
fwrite($lastWriter, $timeStr);
fclose($lastWriter);
}
function writeCsv($csvPath, $csv){
$fp = fopen($csvPath, 'w');
foreach ($csv as $fields) {
//Encode csv as ANSI (Windows-1252)
fputcsv($fp, array_map("utf8_decode", $fields), ";");
}
fclose($fp);
}
function signRequest($method, $uri, $body, $timestamp, $secretKey)
{
$string = implode("\n", [
$method,
$uri,
$body,
$timestamp,
]);
return hash_hmac('sha256', $string, $secretKey);
}