forked from kloon/WooCommerce-REST-API-Client-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wc-api-client.php
444 lines (385 loc) · 11.4 KB
/
class-wc-api-client.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
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
<?php
/**
* WooCommerce API Client Class
*
* @author Gerhard Potgieter
* @since 2013.12.05
* @copyright Gerhard Potgieter
* @version 0.3.1
* @license GPL 3 or later http://www.gnu.org/licenses/gpl.html
*/
class WC_API_Client {
/**
* API base endpoint
*/
const API_ENDPOINT = 'wc-api/v1/';
/**
* The HASH alorithm to use for oAuth signature, SHA256 or SHA1
*/
const HASH_ALGORITHM = 'SHA256';
/**
* The API URL
* @var string
*/
private $_api_url;
/**
* The WooCommerce Consumer Key
* @var string
*/
private $_consumer_key;
/**
* The WooCommerce Consumer Secret
* @var string
*/
private $_consumer_secret;
/**
* If the URL is secure, used to decide if oAuth or Basic Auth must be used
* @var boolean
*/
private $_is_ssl;
/**
* Return the API data as an Object, set to false to keep it in JSON string format
* @var boolean
*/
private $_return_as_object = true;
/**
* Default contructor
* @param string $consumer_key The consumer key
* @param string $consumer_secret The consumer secret
* @param string $store_url The URL to the WooCommerce store
* @param boolean $is_ssl If the URL is secure or not, optional
*/
public function __construct( $consumer_key, $consumer_secret, $store_url, $is_ssl = false ) {
if ( ! empty( $consumer_key ) && ! empty( $consumer_secret ) && ! empty( $store_url ) ) {
$this->_api_url = ( rtrim($store_url,'/' ) . '/' ) . self::API_ENDPOINT;
$this->set_consumer_key( $consumer_key );
$this->set_consumer_secret( $consumer_secret );
$this->set_is_ssl( $is_ssl );
} else if ( ! isset( $consumer_key ) && ! isset( $consumer_secret ) ) {
throw new Exception( 'Error: __construct() - Consumer Key / Consumer Secret missing.' );
} else {
throw new Exception( 'Error: __construct() - Store URL missing.' );
}
}
/**
* Get API Index
* @return mixed|json string
*/
public function get_index() {
return $this->_make_api_call( '' );
}
/**
* Get all orders
* @param array $params
* @return mixed|jason string
*/
public function get_orders( $params = array() ) {
return $this->_make_api_call( 'orders', $params );
}
/**
* Get a single order
* @param integer $order_id
* @return mixed|json string
*/
public function get_order( $order_id ) {
return $this->_make_api_call( 'orders/' . $order_id );
}
/**
* Get the total order count
* @return mixed|json string
*/
public function get_orders_count() {
return $this->_make_api_call( 'orders/count' );
}
/**
* Get orders notes for an order
* @param integer $order_id
* @return mixed|json string
*/
public function get_order_notes( $order_id ) {
return $this->_make_api_call( 'orders/' . $order_id . '/notes' );
}
/**
* Update the order, currently only status update suported by API
* @param integer $order_id
* @param array $data
* @return mixed|json string
*/
public function update_order( $order_id, $data = array() ) {
return $this->_make_api_call( 'orders/' . $order_id, $data, 'POST' );
}
/**
* Delete the order, not suported in WC 2.1, scheduled for 2.2
* @param integer $order_id
* @return mixed|json string
*/
public function delete_order( $order_id ) {
return $this->_make_api_call( 'orders/' . $order_id, $data = array(), 'DELETE' );
}
/**
* Get all coupons
* @param array $params
* @return mixed|json string
*/
public function get_coupons( $params = array() ) {
return $this->_make_api_call( 'coupons', $params );
}
/**
* Get a single coupon
* @param integer $coupon_id
* @return mixed|json string
*/
public function get_coupon( $coupon_id ) {
return $this->_make_api_call( 'coupons/' . $coupon_id );
}
/**
* Get the total coupon count
* @return mixed|json string
*/
public function get_coupons_count() {
return $this->_make_api_call( 'coupons/count' );
}
/**
* Get a coupon by the coupon code
* @param string $coupon_code
* @return mixed|json string
*/
public function get_coupon_by_code( $coupon_code ) {
return $this->_make_api_call( 'coupons/code/' . rawurlencode( rawurldecode( $coupon_code ) ) );
}
/**
* Get all customers
* @param array $params
* @return mixed|json string
*/
public function get_customers( $params = array() ) {
return $this->_make_api_call( 'customers', $params );
}
/**
* Get a single customer
* @param integer $customer_id
* @return mixed|json string
*/
public function get_customer( $customer_id ) {
return $this->_make_api_call( 'customers/' . $customer_id );
}
/**
* Get a single customer by email
* @param string $email
* @return mixed|json string
*/
public function get_customer_by_email( $email ) {
return $this->_make_api_call( 'customers/email/' . $email );
}
/**
* Get the total customer count
* @return mixed|json string
*/
public function get_customers_count() {
return $this->_make_api_call( 'customers/count' );
}
/**
* Get the customer's orders
* @param integer $customer_id
* @return mixed|json string
*/
public function get_customer_orders( $customer_id ) {
return $this->_make_api_call( 'customers/' . $customer_id . '/orders' );
}
/**
* Get all the products
* @param array $params
* @return mixed|json string
*/
public function get_products( $params = array() ) {
return $this->_make_api_call( 'products', $params );
}
/**
* Get a single product
* @param integer $product_id
* @return mixed|json string
*/
public function get_product( $product_id ) {
return $this->_make_api_call( 'products/' . $product_id );
}
/**
* Get the total product count
* @return mixed|json string
*/
public function get_products_count() {
return $this->_make_api_call( 'products/count' );
}
/**
* Get reviews for a product
* @param integer $product_id
* @return mixed|json string
*/
public function get_product_reviews( $product_id ) {
return $this->_make_api_call( 'products/' . $product_id . '/reviews' );
}
/**
* Get reports
* @param array $params
* @return mixed|json string
*/
public function get_reports( $params = array() ) {
return $this->_make_api_call( 'reports', $params );
}
/**
* Get the sales report
* @param array $params
* @return mixed|json string
*/
public function get_sales_report( $params = array() ) {
return $this->_make_api_call( 'reports/sales', $params );
}
/**
* Get the top sellers report
* @param array $params
* @return mixed|json string
*/
public function get_top_sellers_report( $params = array() ) {
return $this->_make_api_call( 'reports/sales/top_sellers', $params );
}
/**
* Run a custom endpoint call, for when you extended the API with your own endpoints
* @param string $endpoint
* @param array $params
* @param string $method
* @return mixed|json string
*/
public function make_custom_endpoint_call( $endpoint, $params = array(), $method = 'GET' ) {
return $this->_make_api_call( $endpoint, $params, $method );
}
/**
* Set the consumer key
* @param string $consumer_key
*/
public function set_consumer_key( $consumer_key ) {
$this->_consumer_key = $consumer_key;
}
/**
* Set the consumer secret
* @param string $consumer_secret
*/
public function set_consumer_secret( $consumer_secret ) {
$this->_consumer_secret = $consumer_secret;
}
/**
* Set SSL variable
* @param boolean $is_ssl
*/
public function set_is_ssl( $is_ssl ) {
if ( $is_ssl == '' ) {
if ( strtolower( substr( $this->_api_url, 0, 5 ) ) == 'https' ) {
$this->_is_ssl = true;
} else $this->_is_ssl = false;
} else $this->_is_ssl = $is_ssl;
}
/**
* Set the return data as object
* @param boolean $is_object
*/
public function set_return_as_object( $is_object = true ) {
$this->_return_as_object = $is_object;
}
/**
* Make the call to the API
* @param string $endpoint
* @param array $params
* @param string $method
* @return mixed|json string
*/
private function _make_api_call( $endpoint, $params = array(), $method = 'GET' ) {
$ch = curl_init();
// Check if we must use Basic Auth or 1 legged oAuth, if SSL we use basic, if not we use OAuth 1.0a one-legged
if ( $this->_is_ssl ) {
curl_setopt( $ch, CURLOPT_USERPWD, $this->_consumer_key . ":" . $this->_consumer_secret );
} else {
$params['oauth_consumer_key'] = $this->_consumer_key;
$params['oauth_timestamp'] = time();
$params['oauth_nonce'] = sha1( microtime() );
$params['oauth_signature_method'] = 'HMAC-' . self::HASH_ALGORITHM;
$params['oauth_signature'] = $this->generate_oauth_signature( $params, $method, $endpoint );
}
if ( isset( $params ) && is_array( $params ) ) {
$paramString = '?' . http_build_query( $params );
} else {
$paramString = null;
}
// Set up the enpoint URL
curl_setopt( $ch, CURLOPT_URL, $this->_api_url . $endpoint . $paramString );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
if ( 'POST' === $method ) {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $params ) );
} else if ( 'DELETE' === $method ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
}
$return = curl_exec( $ch );
$code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( $this->_return_as_object ) {
$return = json_decode( $return );
}
if ( empty( $return ) ) {
$return = '{"errors":[{"code":"' . $code . '","message":"cURL HTTP error ' . $code . '"}]}';
$return = json_decode( $return );
}
return $return;
}
/**
* Generate oAuth signature
* @param array $params
* @param string $http_method
* @param string $endpoint
* @return string
*/
public function generate_oauth_signature( $params, $http_method, $endpoint ) {
$base_request_uri = rawurlencode( $this->_api_url . $endpoint );
// normalize parameter key/values and sort them
$params = $this->normalize_parameters( $params );
uksort( $params, 'strcmp' );
// form query string
$query_params = array();
foreach ( $params as $param_key => $param_value ) {
$query_params[] = $param_key . '%3D' . $param_value; // join with equals sign
}
$query_string = implode( '%26', $query_params ); // join with ampersand
// form string to sign (first key)
$string_to_sign = $http_method . '&' . $base_request_uri . '&' . $query_string;
return base64_encode( hash_hmac( self::HASH_ALGORITHM, $string_to_sign, $this->_consumer_secret, true ) );
}
/**
* Normalize each parameter by assuming each parameter may have already been
* encoded, so attempt to decode, and then re-encode according to RFC 3986
*
* Note both the key and value is normalized so a filter param like:
*
* 'filter[period]' => 'week'
*
* is encoded to:
*
* 'filter%5Bperiod%5D' => 'week'
*
* This conforms to the OAuth 1.0a spec which indicates the entire query string
* should be URL encoded
*
* @since 0.3.1
* @see rawurlencode()
* @param array $parameters un-normalized pararmeters
* @return array normalized parameters
*/
private function normalize_parameters( $parameters ) {
$normalized_parameters = array();
foreach ( $parameters as $key => $value ) {
// percent symbols (%) must be double-encoded
$key = str_replace( '%', '%25', rawurlencode( rawurldecode( $key ) ) );
$value = str_replace( '%', '%25', rawurlencode( rawurldecode( $value ) ) );
$normalized_parameters[ $key ] = $value;
}
return $normalized_parameters;
}
}