PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.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) 2.18.0, at src/Service/Api.php

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