-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSellix.php
105 lines (86 loc) · 3.12 KB
/
Sellix.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
<?php
namespace Sellix\PhpSdk;
define("BASE_DIR", dirname(__FILE__));
require_once BASE_DIR . "/Blacklists/index.php";
require_once BASE_DIR . "/Whitelists/index.php";
require_once BASE_DIR . "/Categories/index.php";
require_once BASE_DIR . "/Coupons/index.php";
require_once BASE_DIR . "/Feedback/index.php";
require_once BASE_DIR . "/Orders/index.php";
require_once BASE_DIR . "/Groups/index.php";
require_once BASE_DIR . "/Products/index.php";
require_once BASE_DIR . "/Queries/index.php";
require_once BASE_DIR . "/Payments/index.php";
require_once BASE_DIR . "/Customers/index.php";
require_once BASE_DIR . "/Subscriptions/index.php";
require_once BASE_DIR . "/Exceptions/index.php";
require_once BASE_DIR . "/Tests/index.php";
class Sellix {
protected string $api_key;
protected ?string $merchant;
use Blacklists;
use Whitelists;
use Categories;
use Groups;
use Coupons;
use Feedback;
use Orders;
use Products;
use Queries;
use Payments;
use Customers;
use Subscriptions;
static $API_ENDPOINT = 'https://dev.sellix.io/v1';
public function __construct(string $api_key, ?string $merchant = NULL) {
$this->api_key = $api_key;
$this->merchant = $merchant;
}
private function handle_response($response, $key = NULL) {
if ($response->status != 200) {
throw new SellixException($response->error, $response->status);
} else {
if ($key === NULL) {
return NULL;
}
if (is_array($key) && array_key_exists("oneOf", $key)) {
$updated_response = new \stdClass();
foreach ($key["oneOf"] as $option) {
if (strpos($option, ",") !== false) {
foreach (explode(",", $option) as $key_option) {
if (property_exists($response->data, $key_option)) {
$updated_response->$key_option = $response->data->$key_option;
}
}
} else if (property_exists($response->data, $option)) {
return $response->data->$option;
}
}
return $updated_response;
}
return $response->data->$key;
}
}
private function request($component, $action = "GET", $payload = NULL) {
$curl = curl_init();
$headers = [
"Content-type: application/json",
"Authorization: Bearer " . $this->api_key
];
if ($this->merchant) {
$headers[] = "X-Sellix-Merchant: " . $this->merchant;
}
$curl_array = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $action,
CURLOPT_URL => self::$API_ENDPOINT . $component,
CURLOPT_HTTPHEADER => $headers
];
if ($payload) {
$curl_array[CURLOPT_POSTFIELDS] = json_encode($payload);
}
curl_setopt_array($curl, $curl_array);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
}