-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
55 lines (44 loc) · 1.19 KB
/
api.php
File metadata and controls
55 lines (44 loc) · 1.19 KB
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
<?php
class Belco_API {
public static function post($path, $data, $options = array()) {
if (!empty($options['secret'])) {
$secret = $options['secret'];
} else {
$secret = get_option('belco_secret');
}
if (!empty($options['shopId'])) {
$shopId = $options['shopId'];
} else {
$shopId = get_option('belco_shop_id');
}
$protocol = BELCO_USE_SSL ? 'https://' : 'http://';
$json = json_encode($data);
$signature = hash_hmac('sha256', $json, $secret);
$blocking = $options['blocking'];
$response = wp_remote_post($protocol . BELCO_API_HOST . $path, array(
'method' => 'POST',
'body' => $json,
'headers' => array(
'Content-Type' => 'application/json',
'X-Signature' => $signature,
'X-Shop-Id' => $shopId
),
'blocking' => $blocking ? true : !WP_DEBUG,
'timeout' => 5
));
if (!$blocking && !WP_DEBUG) {
return true;
}
if ( is_wp_error( $response ) ) {
error_log('Belco Error: ' + $response->get_error_message());
return $response->get_error_message();
}
$body = json_decode($response['body']);
if ($body->success === false) {
error_log('Belco Error: ' + $body->message);
return $body->message;
}
return true;
}
}
?>