PluginProbe
PayPlug for WooCommerce (Official) / 3.0.0
PayPlug for WooCommerce (Official) v3.0.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Service / Api.php

Api.php in PayPlug for WooCommerce (Official) 3.0.0, at src/Service/Api.php

337 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Service;
4
5 use Payplug\Core\HttpClient;
6 use Payplug\Exception\HttpException;
7 use Payplug\Payplug;
8 use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
9
10 class Api
11 {
12 use ServiceGetter;
13
14 protected $api_payplug;
15
16 /**
17 * @description get merchant account permission
18 *
19 * @param $mode
20 *
21 * @throws \Payplug\Exception\ConfigurationException
22 *
23 * @return array
24 */
25 public function get_account($mode = '')
26 {
27 if (!$this->api_payplug) {
28 $this->initialize($mode);
29 }
30 $account = $this->do_request_with_fallback('\Payplug\Authentication::getAccount', [$this->api_payplug]);
31
32 return [
33 'result' => $account['result'],
34 'response' => isset($account['response']['httpResponse']) && !empty($account['response']['httpResponse'])
35 ? $account['response']['httpResponse']
36 : null,
37 ];
38 }
39
40 /**
41 * @description get the api key for a given email and password
42 *
43 * @param $email
44 * @param $password
45 *
46 * @return array
47 */
48 public function get_keys_by_login($email = '', $password = '')
49 {
50 try {
51 $response = $this->do_request('\Payplug\Authentication::getKeysByLogin', [$email, $password]);
52 $http_response = isset($response['httpResponse']) && !empty($response['httpResponse'])
53 ? $response['httpResponse']
54 : null;
55
56 if (is_null($http_response)) {
57 return ['result' => false, 'response' => null];
58 }
59
60 return [
61 'result' => true,
62 'response' => $http_response,
63 ];
64 } catch (HttpException $e) {
65 $error = $e->getErrorObject();
66
67 return [
68 'result' => false,
69 'response' => null,
70 'multiple_users' => isset($error['message']) && $error['message'] === 'Multiple users found.',
71 ];
72 } catch (\Exception $e) {
73 return [
74 'result' => false,
75 'response' => null,
76 ];
77 }
78 }
79
80 /**
81 * @description Initialize the api with current bearer token
82 *
83 * @param $mode
84 *
85 * @throws \Payplug\Exception\ConfigurationException
86 *
87 * @return void
88 */
89 protected function initialize($mode = ''): void
90 {
91 $bearer_token = $this->get_bearer_token($mode);
92 $this->api_payplug = new Payplug($bearer_token, '2019-08-06');
93 HttpClient::setDefaultUserAgentProduct(
94 'PayPlug-WooCommerce',
95 PAYPLUG_GATEWAY_VERSION,
96 sprintf('WooCommerce/%s', WC()->version)
97 );
98 }
99
100 /**
101 * @description get current mode configured (live|test)
102 *
103 * @return string
104 */
105 protected function get_mode()
106 {
107 return $this->get_configuration()->get_option('mode') ? 'live' : 'test';
108 }
109
110 /**
111 * @description get current bearer token for api
112 *
113 * @param $mode
114 *
115 * @return string
116 */
117 public function get_bearer_token($mode = true)
118 {
119 $options = $this->get_configuration()->get_options();
120 $api_keys = isset($options['api_key'])
121 ? json_decode($options['api_key'], true)
122 : [];
123 $mode = is_string($mode) && !empty($mode)
124 ? $mode
125 : $this->get_mode();
126 $bearer_token = isset($api_keys[$mode]) ? $api_keys[$mode] : '';
127
128 $jwt = isset($options['jwt']) ? json_decode($options['jwt'], true) : [];
129 $oauth_client_data = isset($options['oauth_client_data']) ? json_decode($options['oauth_client_data'], true) : [];
130
131 if (!empty($jwt) && !empty($jwt[$mode]) && !empty($oauth_client_data) && !empty($oauth_client_data[$mode])) {
132 $jwt_data = $jwt[$mode];
133 $now = time();
134 $expires_date = isset($jwt_data['expires_date']) ? (int) $jwt_data['expires_date'] : 0;
135 if ($expires_date > 0 && ($expires_date - $now) < 30) {
136 $new_jwt = $this->generate_jwt(
137 isset($oauth_client_data[$mode]['client_id']) ? $oauth_client_data[$mode]['client_id'] : '',
138 isset($oauth_client_data[$mode]['client_secret']) ? $oauth_client_data[$mode]['client_secret'] : ''
139 );
140 if (!empty($new_jwt) && isset($new_jwt['access_token'])) {
141 $jwt[$mode] = $new_jwt;
142 $api_keys[$mode] = $new_jwt['access_token'];
143 $this->get_configuration()->update_option('jwt', json_encode($jwt));
144 $this->get_configuration()->update_option('api_key', json_encode($api_keys));
145 $bearer_token = $new_jwt['access_token'];
146 }
147 } else {
148 $validate_jwt = $this->validate_jwt($oauth_client_data[$mode], $jwt[$mode]);
149 if (!$validate_jwt['result'] || empty($validate_jwt['token'])) {
150 return '';
151 }
152 $token_validated = $validate_jwt['token'];
153 $token_validated['expires_date'] -= 30;
154 $jwt[$mode] = $token_validated;
155 if ($validate_jwt['need_update']) {
156 $this->get_configuration()->update_option('jwt', json_encode($jwt));
157 }
158 $bearer_token = $jwt[$mode]['access_token'];
159 }
160 }
161
162 return (string) $bearer_token;
163 }
164
165 /**
166 * @description validate usage for a given jwt
167 *
168 * @param $oauth_client_data
169 * @param $jwt
170 *
171 * @return array
172 */
173 protected function validate_jwt($oauth_client_data = [], $jwt = [])
174 {
175 $request = $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$oauth_client_data, $jwt]);
176 if (!$request['result']) {
177 return [];
178 }
179
180 return isset($request['response']) ?
181 $request['response']
182 : [];
183 }
184
185 /**
186 * @description Send request
187 *
188 * @param $callback
189 * @param $params
190 *
191 * @return array
192 */
193 protected function do_request_with_fallback($callback, $params = [])
194 {
195 try {
196 $response = [
197 'result' => true,
198 'response' => $this->do_request($callback, $params),
199 ];
200 } catch (\Exception $e) {
201 $response = [
202 'result' => false,
203 'response' => null,
204 'code' => $e->getCode(),
205 ];
206 }
207
208 return $response;
209 }
210
211 /**
212 * @description Send request to the api without fallback
213 *
214 * @param $callback
215 * @param $params
216 *
217 * @return mixed
218 */
219 protected function do_request($callback, $params = [])
220 {
221 if (!is_array($params)) {
222 $params = [$params];
223 }
224
225 return call_user_func_array($callback, $params);
226 }
227
228 /**
229 * @description create client id and secret
230 *
231 * @param $access_token
232 * @param $company_id
233 * @param $mode
234 *
235 * @throws \Payplug\Exception\ConfigurationException
236 *
237 * @return array
238 */
239 public function create_client_id_and_secret($access_token = '', $company_id = '', $mode = 'live')
240 {
241 try {
242 Payplug::init([
243 'secretKey' => $access_token,
244 ]);
245 } catch (\Exception $e) {
246 if (method_exists($e, 'getCode') && $e->getCode() == 401) {
247 \Payplug\PayplugWoocommerce\PayplugWoocommerceHelper::payplug_logout();
248
249 return [];
250 } elseif (strpos($e->getMessage(), '401') !== false) {
251 \Payplug\PayplugWoocommerce\PayplugWoocommerceHelper::payplug_logout();
252
253 return [];
254 }
255 throw $e;
256 }
257 $request = $this->do_request_with_fallback('\Payplug\Authentication::createClientIdAndSecret', [$company_id, 'WooCommerce', $mode]);
258 if (!$request['result']) {
259 return [];
260 }
261
262 return isset($request['response']['httpResponse']) ?
263 $request['response']['httpResponse']
264 : [];
265 }
266
267 /**
268 * @description generate jwt
269 *
270 * @param $client_id
271 * @param $client_secret
272 *
273 * @return array
274 */
275 public function generate_jwt($client_id = '', $client_secret = '')
276 {
277 $request = $this->do_request_with_fallback('\Payplug\Authentication::generateJWT', [$client_id, $client_secret]);
278 if (!$request['result']) {
279 return [];
280 }
281
282 return isset($request['response']['httpResponse']) ?
283 $request['response']['httpResponse']
284 : [];
285 }
286
287 /**
288 * @description initiate oauth
289 *
290 * @param $client_id
291 * @param $oauth_callback_uri
292 * @param $code_verifier
293 *
294 * @return void
295 */
296 public function initiate_oauth($client_id, $oauth_callback_uri, $code_verifier)
297 {
298 return $this->do_request('\Payplug\Authentication::initiateOAuth', [$client_id, $oauth_callback_uri, $code_verifier]);
299 }
300
301 /**
302 * @description generate jwt one shot
303 *
304 * @param $authorization_code
305 * @param $callback_uri
306 * @param $client_id
307 * @param $code_verifier
308 *
309 * @return array
310 */
311 public function generate_jwt_one_shot($authorization_code, $callback_uri, $client_id, $code_verifier)
312 {
313 $request = $this->do_request_with_fallback('\Payplug\Authentication::generateJWTOneShot', [
314 $authorization_code,
315 $callback_uri,
316 $client_id,
317 $code_verifier,
318 ]);
319 if (!$request['result']) {
320 return [];
321 }
322
323 return isset($request['response']['httpResponse']) ?
324 $request['response']['httpResponse']
325 : [];
326 }
327
328 // todo: this method bellow should be implemented, do no removed it for now
329 public function get_permissions(): void
330 {
331 }
332
333 public function get_register_url(): void
334 {
335 }
336 }
337