@cdek-it/widget 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ <?php
2
+
3
+ $service = new service(/**
4
+ * Вставьте свой аккаунт\идентификатор для интеграции
5
+ * Put your account for integration here
6
+ */ 'cdek-login',
7
+
8
+ /**
9
+ * Вставьте свой пароль для интеграции
10
+ * Put your password for integration here
11
+ */ 'cdek-pass');
12
+ $service->process($_GET, file_get_contents('php://input'));
13
+
14
+ class service
15
+ {
16
+ /**
17
+ * @var string Auth login
18
+ */
19
+ private $login;
20
+ /**
21
+ * @var string Auth pwd
22
+ */
23
+ private $secret;
24
+ /**
25
+ * @var string Base Url for API 2.0 Production
26
+ */
27
+ private $baseUrl;
28
+ /**
29
+ * @var string Auth Token
30
+ */
31
+ private $authToken;
32
+ /**
33
+ * @var array Data From Request
34
+ */
35
+ private $requestData;
36
+
37
+ public function __construct($login, $secret, $baseUrl = 'https://api.cdek.ru/v2')
38
+ {
39
+ $this->login = $login;
40
+ $this->secret = $secret;
41
+ $this->baseUrl = $baseUrl;
42
+ }
43
+
44
+ public function process($requestData, $body)
45
+ {
46
+ $this->requestData = array_merge($requestData, json_decode($body, true) ?: array());
47
+
48
+ if (!isset($this->requestData['action'])) {
49
+ $this->sendValidationError('Action is required');
50
+ }
51
+
52
+ $this->getAuthToken();
53
+
54
+ switch ($this->requestData['action']) {
55
+ case 'offices':
56
+ $this->sendResponse($this->getOffices());
57
+ case 'calculate':
58
+ $this->sendResponse($this->calculate());
59
+ default:
60
+ $this->sendValidationError('Unknown action');
61
+ }
62
+ }
63
+
64
+ private function sendValidationError($message)
65
+ {
66
+ $this->http_response_code(400);
67
+ header('Content-Type: application/json');
68
+ echo json_encode(array('message' => $message));
69
+ exit();
70
+ }
71
+
72
+ private function getAuthToken()
73
+ {
74
+ $result = json_decode($this->httpRequest('oauth/token', array(
75
+ 'grant_type' => 'client_credentials',
76
+ 'client_id' => $this->login,
77
+ 'client_secret' => $this->secret,
78
+ ), true), true);
79
+ if (!isset($result['access_token'])) {
80
+ throw new RuntimeException('Server not authorized to CDEK API');
81
+ }
82
+
83
+ $this->authToken = $result['access_token'];
84
+ }
85
+
86
+ private function httpRequest($method, $data, $useFormData = false, $useJson = false)
87
+ {
88
+ $ch = curl_init("$this->baseUrl/$method");
89
+
90
+ $headers = array (
91
+ 'Accept: application/json',
92
+ );
93
+
94
+ if ($this->authToken) {
95
+ $headers[] = "Authorization: Bearer $this->authToken";
96
+ }
97
+
98
+ if ($useFormData) {
99
+ curl_setopt_array($ch, array(
100
+ CURLOPT_POST => true,
101
+ CURLOPT_POSTFIELDS => $data,
102
+ ));
103
+ } elseif ($useJson) {
104
+ $headers[] = 'Content-Type: application/json';
105
+ curl_setopt_array($ch, array(
106
+ CURLOPT_POST => true,
107
+ CURLOPT_POSTFIELDS => json_encode($data),
108
+ ));
109
+ } else {
110
+ curl_setopt($ch, CURLOPT_URL, "$this->baseUrl/$method?" . http_build_query($data));
111
+ }
112
+
113
+ curl_setopt_array($ch, array(
114
+ CURLOPT_USERAGENT => 'widget/2.0',
115
+ CURLOPT_HTTPHEADER => $headers,
116
+ CURLOPT_RETURNTRANSFER => true,
117
+ ));
118
+
119
+ $result = curl_exec($ch);
120
+
121
+ if ($result === false) {
122
+ throw new RuntimeException(curl_error($ch), curl_errno($ch));
123
+ }
124
+
125
+ return $result;
126
+ }
127
+
128
+ private function sendResponse($data)
129
+ {
130
+ $this->http_response_code(200);
131
+ header('Content-Type: application/json');
132
+ echo $data;
133
+ exit();
134
+ }
135
+
136
+ protected function getOffices()
137
+ {
138
+ return $this->httpRequest('deliverypoints', $this->requestData);
139
+ }
140
+
141
+ protected function calculate()
142
+ {
143
+ return $this->httpRequest('calculator/tarifflist', $this->requestData, false, true);
144
+ }
145
+
146
+ private function http_response_code($code)
147
+ {
148
+ switch ($code) {
149
+ case 100: $text = 'Continue'; break;
150
+ case 101: $text = 'Switching Protocols'; break;
151
+ case 200: $text = 'OK'; break;
152
+ case 201: $text = 'Created'; break;
153
+ case 202: $text = 'Accepted'; break;
154
+ case 203: $text = 'Non-Authoritative Information'; break;
155
+ case 204: $text = 'No Content'; break;
156
+ case 205: $text = 'Reset Content'; break;
157
+ case 206: $text = 'Partial Content'; break;
158
+ case 300: $text = 'Multiple Choices'; break;
159
+ case 301: $text = 'Moved Permanently'; break;
160
+ case 302: $text = 'Moved Temporarily'; break;
161
+ case 303: $text = 'See Other'; break;
162
+ case 304: $text = 'Not Modified'; break;
163
+ case 305: $text = 'Use Proxy'; break;
164
+ case 400: $text = 'Bad Request'; break;
165
+ case 401: $text = 'Unauthorized'; break;
166
+ case 402: $text = 'Payment Required'; break;
167
+ case 403: $text = 'Forbidden'; break;
168
+ case 404: $text = 'Not Found'; break;
169
+ case 405: $text = 'Method Not Allowed'; break;
170
+ case 406: $text = 'Not Acceptable'; break;
171
+ case 407: $text = 'Proxy Authentication Required'; break;
172
+ case 408: $text = 'Request Time-out'; break;
173
+ case 409: $text = 'Conflict'; break;
174
+ case 410: $text = 'Gone'; break;
175
+ case 411: $text = 'Length Required'; break;
176
+ case 412: $text = 'Precondition Failed'; break;
177
+ case 413: $text = 'Request Entity Too Large'; break;
178
+ case 414: $text = 'Request-URI Too Large'; break;
179
+ case 415: $text = 'Unsupported Media Type'; break;
180
+ case 500: $text = 'Internal Server Error'; break;
181
+ case 501: $text = 'Not Implemented'; break;
182
+ case 502: $text = 'Bad Gateway'; break;
183
+ case 503: $text = 'Service Unavailable'; break;
184
+ case 504: $text = 'Gateway Time-out'; break;
185
+ case 505: $text = 'HTTP Version not supported'; break;
186
+ default:
187
+ exit('Unknown http status code "' . htmlentities($code) . '"');
188
+ break;
189
+ }
190
+
191
+ $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
192
+ header($protocol . ' ' . $code . ' ' . $text);
193
+ $GLOBALS['http_response_code'] = $code;
194
+ }
195
+ }
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@cdek-it/widget",
3
+ "version": "3.8.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/cdek-widget.umd.js",
9
+ "module": "./dist/cdek-widget.es.js",
10
+ "types": "./dist/cdek-widget.es.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/cdek-widget.es.js",
14
+ "require": "./dist/cdek-widget.umd.js"
15
+ }
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "scripts": {
21
+ "dev": "vite",
22
+ "build": "vue-tsc && vite build",
23
+ "preview": "vite preview",
24
+ "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix"
25
+ },
26
+ "dependencies": {
27
+ "@vueuse/core": "^10.7.0",
28
+ "axios": "^1.6.2",
29
+ "lodash-es": "^4.17.21",
30
+ "perfect-scrollbar": "^1.5.5",
31
+ "pinia": "^2.1.7",
32
+ "vue": "^3.3.10",
33
+ "vue-i18n": "^9.8.0",
34
+ "vue-yandex-maps": "2.0.0-rc.5",
35
+ "yup": "^1.3.2"
36
+ },
37
+ "devDependencies": {
38
+ "@iconify-json/tabler": "^1.1.100",
39
+ "@intlify/unplugin-vue-i18n": "^1.5.0",
40
+ "@types/lodash-es": "^4.17.12",
41
+ "@types/node": "^20.10.3",
42
+ "@typescript-eslint/eslint-plugin": "^6.13.2",
43
+ "@typescript-eslint/parser": "^6.13.2",
44
+ "@vitejs/plugin-vue": "^4.5.1",
45
+ "@vitejs/plugin-vue-jsx": "^3.1.0",
46
+ "@vue/eslint-config-typescript": "^12.0.0",
47
+ "@yandex/ymaps3-types": "^0.0.19",
48
+ "autoprefixer": "^10.4.16",
49
+ "cssnano": "^6.0.1",
50
+ "eslint": "^8.55.0",
51
+ "eslint-config-prettier": "^9.1.0",
52
+ "eslint-plugin-import": "^2.29.0",
53
+ "eslint-plugin-prettier": "^5.0.1",
54
+ "eslint-plugin-vue": "^9.19.2",
55
+ "postcss": "^8.4.32",
56
+ "prettier": "^3.1.0",
57
+ "sass": "^1.69.5",
58
+ "terser": "^5.25.0",
59
+ "typescript": "^5.3.2",
60
+ "unocss": "^0.58.0",
61
+ "vite": "^5.0.6",
62
+ "vite-plugin-css-injected-by-js": "^3.3.0",
63
+ "vite-plugin-dts": "^3.6.4",
64
+ "vite-plugin-static-copy": "^1.0.0",
65
+ "vite-plugin-transform": "^2.0.1",
66
+ "vite-svg-loader": "^5.1.0",
67
+ "vue-tsc": "^1.8.25"
68
+ },
69
+ "packageManager": "yarn@4.0.2"
70
+ }