PluginProbe
Route ‑ Shipping Protection / 2.2.6
Route ‑ Shipping Protection v2.2.6
2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 151 releases
routeapp / includes / class-routeapp-setup.php

class-routeapp-setup.php in Route ‑ Shipping Protection 2.2.6, at includes/class-routeapp-setup.php

644 lines 21.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A Route Magento Extension that adds secure shipping
4 * insurance to your orders
5 *
6 * Php version 7.0^
7 *
8 * @category
9 * @package Route_Route
10 * @author Route Development Team <dev@routeapp.io>
11 * @copyright 2019 Route App Inc. Copyright (c) https://www.routeapp.io/
12 * @license https://www.routeapp.io/merchant-terms-of-use Proprietary License
13 * @link https://magento.routeapp.io/magento2/index.html
14 */
15
16
17 class Route_Setup
18 {
19
20 const WOOCOMMERCE = "woocommerce";
21
22 const REGISTRATION_FIXED = '0';
23 const REGISTRATION_STEP_USER_LOGIN_SUCCESS = '1-200';
24 const FAILED_REGISTRATION_STEP_USER = "1";
25 const FAILED_REGISTRATION_STEP_USER_DUPLICATED = '1-409';
26 const FAILED_REGISTRATION_STEP_USER_LOGIN_FAILED = '1-401';
27 const FAILED_REGISTRATION_STEP_USER_ACTIVATION = '2';
28 const FAILED_REGISTRATION_STEP_MERCHANT = '3';
29 const FAILED_REGISTRATION_STEP_MERCHANT_DUPLICATED = '3-409';
30
31 const FAILED_REGISTRATION = 'routeapp_failed_registration';
32 const USER_TOKEN_OPTION = 'routeapp_user_token';
33 const USER_ID_OPTION = 'routeapp_user_id';
34 const SECRET_TOKEN_OPTION = 'routeapp_secret_token';
35 const PUBLIC_TOKEN_OPTION = 'routeapp_public_token';
36 const USER_ID = 'routeapp_user_id';
37 const REGISTRATION_STEP = 'routeapp_route_registration_step';
38 const DASHBOARD = 'https://dashboard.route.com/login?redirect=onboarding';
39 const DASHBOARD_STAGE = 'https://dashboard-stage.route.com/login?redirect=onboarding';
40 const REDIRECTED = '2';
41 const MODULE_INSTALLED = '1';
42 const ACTIVATION_LINK = 'activation_link';
43 const SETUP_CHECK_WIDGET_INSTALLATION_DATE = 'route_setup_check_widget_installation_date';
44 const SETUP_CHECK_WIDGET_API_CALL = 'route_setup_check_widget_api_call';
45 const SETUP_CHECK_WIDGET_PHP = 'route_setup_check_widget_php';
46 const SETUP_CHECK_WIDGET_JS = 'route_setup_check_widget_js';
47 const ACTIVE = 'Active';
48
49 public static function init()
50 {
51 if (!self::is_fresh_new_installation()) {
52 self::update_merchant_status();
53 return;
54 }
55
56 self::create_user();
57 }
58
59 public static function is_fresh_new_installation()
60 {
61 return !self::has_valid_merchant_tokens() &&
62 !self::registration_step();
63 }
64
65 /**
66 * Prepare user data, call user create request
67 * and handle response
68 *
69 * @return bool
70 */
71 public static function create_user()
72 {
73 $routeapp_public = self::get_route_public_instance();
74 $response = $routeapp_public->routeapp_create_user(self::get_user_data());
75
76 try{
77 if ($routeapp_public->routeapp_last_request_has_failed()) {
78 self::set_registration_failed_as(
79 $routeapp_public->routeapp_last_request_has_conflicted() ?
80 self::FAILED_REGISTRATION_STEP_USER_DUPLICATED:
81 self::FAILED_REGISTRATION_STEP_USER
82 );
83 throw new Exception($routeapp_public->routeapp_last_request_has_conflicted() ?
84 'FAILED_REGISTRATION_STEP_USER_DUPLICATED':
85 'FAILED_REGISTRATION_STEP_USER');
86 }
87 }catch (Exception $exception) {
88 $extraData = array(
89 'params' => self::get_user_data(),
90 'method' => 'POST',
91 'endpoint' => 'users'
92 );
93 $routeapp_public->routeapp_log($exception, $extraData);
94 return false;
95 }
96
97 if (self::register_user($response)) {
98 self::set_registration_failed_as(self::REGISTRATION_FIXED);
99 self::set_as_installed();
100
101 return true;
102 }
103 }
104
105 /**
106 * Prepare user data, call user create request
107 * and handle response
108 *
109 * @return bool
110 */
111 public static function register_user_login($username, $password)
112 {
113 $routeapp_public = self::get_route_public_instance();
114 $response = $routeapp_public->routeapp_user_login($username, $password);
115
116 try{
117 if ($routeapp_public->routeapp_last_request_has_failed()) {
118 self::set_registration_failed_as(self::FAILED_REGISTRATION_STEP_USER_LOGIN_FAILED);
119 throw new Exception('FAILED_REGISTRATION_STEP_USER_LOGIN_FAILED');
120 }
121 }catch (Exception $exception) {
122 $extraData = array(
123 'params' => array('username' => $username, 'password' => 'XXXXX'),
124 'method' => 'POST',
125 'endpoint' => 'login'
126 );
127 $routeapp_public->routeapp_log($exception, $extraData);
128 return false;
129 }
130
131 if (self::register_user($response, true)) {
132 self::set_registration_failed_as(self::REGISTRATION_STEP_USER_LOGIN_SUCCESS);
133
134 self::set_as_installed();
135
136 return true;
137 }
138 }
139
140 /**
141 * @param $response
142 * @param $is_active
143 *
144 * @return bool
145 */
146 public static function register_user($response, $is_active = false)
147 {
148
149 self::set_user_key($response->token);
150 self::set_user_id($response->id);
151
152 if (!$is_active) {
153 self::active_account();
154 }
155
156 return self::create_merchant();
157 }
158
159 /**
160 * Activate User Account
161 *
162 * @return bool
163 */
164 public static function active_account()
165 {
166 $routeapp_public = self::get_route_public_instance();
167 try{
168 $response = $routeapp_public->routeapp_activate_account(self::get_current_email());
169
170 try{
171 if(!$response){
172 self::set_registration_failed_as(self::FAILED_REGISTRATION_STEP_USER_ACTIVATION);
173 throw new Exception('FAILED_REGISTRATION_STEP_USER_ACTIVATION');
174 }
175 } catch (Exception $exception) {
176 $extraData = array(
177 'params' => self::get_current_email(),
178 'method' => 'POST',
179 'endpoint' => 'activate_account'
180 );
181 $routeapp_public->routeapp_log($exception, $extraData);
182 return false;
183 }
184 self::set_activation_link($response->set_password_url);
185 }catch (Exception $e){
186 self::set_registration_failed_as(self::FAILED_REGISTRATION_STEP_USER_ACTIVATION);
187 $extraData = array(
188 'params' => self::get_current_email(),
189 'method' => 'POST',
190 'endpoint' => 'activate_account'
191 );
192 $routeapp_public->routeapp_log($e, $extraData);
193 return false;
194 }
195
196 self::set_registration_failed_as(self::REGISTRATION_FIXED);
197
198 return true;
199
200 }
201
202 public static function has_valid_merchant_tokens(){
203 $routeapp_public = self::get_route_public_instance();
204 return self::has_merchant_tokens() &&
205 !empty($routeapp_public->routeapp_api_client->get_merchant());
206 }
207
208 public static function has_merchant_tokens(){
209 $routeapp_public = self::get_route_public_instance();
210 return
211 !empty($routeapp_public->routeapp_get_public_token()) &&
212 !empty($routeapp_public->routeapp_get_secret_token());
213 }
214
215 public static function get_route_public_instance(){
216 global $routeapp_public;
217 return $routeapp_public;
218 }
219
220 /**
221 * @param $store_domain
222 * @param $merchant_store_domain
223 * @return bool
224 */
225 public static function is_same_domain($store_domain, $merchant_store_domain)
226 {
227 if(strpos($store_domain,'www.') === 0 || strpos($merchant_store_domain,'www.') === 0){
228 return strpos($store_domain, $merchant_store_domain) !== false || strpos($merchant_store_domain, $store_domain) !== false;
229 }
230
231 return $merchant_store_domain === $store_domain;
232 }
233
234 /**
235 * Prepare merchant data, call merchant create request
236 * and handle response
237 *
238 * @return bool
239 */
240 public static function create_merchant()
241 {
242 $created_domains = [];
243 $responses = [];
244
245 foreach (self::get_all_sites() as $site){
246
247 //Avoid duplicated merchant to the same user account
248 if (in_array($site->domain, $created_domains)) {
249 continue;
250 }
251
252 $associated = false;
253 $responses = [];
254
255 $created_domains[$site->blog_id] = $site->domain;
256 $merchant_data = self::get_merchant_data($site);
257 $routeapp_public = self::get_route_public_instance();
258 $merchant_creation_response = $routeapp_public->routeapp_create_merchant($merchant_data);
259
260 if($merchant_creation_response){
261 $responses[$site->blog_id] = $merchant_creation_response;
262 }
263
264 try{
265 if ($routeapp_public->routeapp_last_request_has_failed()) {
266
267
268 $merchants_by_user = $routeapp_public->routeapp_get_merchants();
269
270 if(!empty($merchants_by_user) && is_array($merchants_by_user)){
271 foreach ($merchants_by_user as $merchant) {
272 if(self::is_same_domain($merchant->store_domain, $site->domain)){
273 $responses[$site->blog_id] = $merchant;
274 $associated = true;
275 }
276 }
277 }
278
279 if (empty($responses)) {
280 self::set_registration_failed_as(
281 $routeapp_public->routeapp_last_request_has_conflicted() ?
282 self::FAILED_REGISTRATION_STEP_MERCHANT_DUPLICATED:
283 self::FAILED_REGISTRATION_STEP_MERCHANT
284 );
285 throw new Exception($routeapp_public->routeapp_last_request_has_conflicted() ?
286 'FAILED_REGISTRATION_STEP_MERCHANT_DUPLICATED':
287 'FAILED_REGISTRATION_STEP_MERCHANT');
288 }
289 }
290 } catch (Exception $exception) {
291 $routeapp_public->routeapp_log($exception);
292 return false;
293 }
294
295
296
297 //TODO Currently when we try to create merchant that already exists it's returning 200 http code success
298 //TODO So we need try to update it with current platform
299 if (!$associated && $routeapp_public->routeapp_last_request_has_success()) {
300
301 try{
302 if (!self::merchant_can_be_updated($responses[$site->blog_id])) {
303 self::set_registration_failed_as(self::FAILED_REGISTRATION_STEP_MERCHANT);
304 throw new Exception('FAILED_REGISTRATION_STEP_MERCHANT');
305 }
306 } catch (Exception $exception) {
307 $routeapp_public->routeapp_log($exception);
308 return false;
309 }
310
311 //TODO This is happening when we try to update merchant that user doesn't own
312 //TODO So we need try to recreate it with additional slash at the store's domain end
313 $merchant_data['store_domain'] = $merchant_data['store_domain'] . '/';
314 $responses[$site->blog_id] = $routeapp_public->routeapp_create_merchant($merchant_data);
315
316 try{
317 if ($routeapp_public->routeapp_last_request_has_failed()) {
318 self::set_registration_failed_as(
319 $routeapp_public->routeapp_last_request_has_conflicted() ?
320 self::FAILED_REGISTRATION_STEP_MERCHANT_DUPLICATED :
321 self::FAILED_REGISTRATION_STEP_MERCHANT);
322 throw new Exception($routeapp_public->routeapp_last_request_has_conflicted() ?
323 'FAILED_REGISTRATION_STEP_MERCHANT_DUPLICATED' :
324 'FAILED_REGISTRATION_STEP_MERCHANT');
325 }
326 } catch (Exception $exception) {
327 $routeapp_public->routeapp_log($exception);
328 return false;
329 }
330 }
331
332 }
333
334 foreach ($responses as $blog_id => $response){
335 self::set_public_key($response->public_api_key, $blog_id);
336 self::set_secret_key($response->prod_api_secret, $blog_id);
337 }
338
339 return true;
340 }
341
342 /**
343 * @param $response
344 * @return bool
345 */
346 public static function merchant_can_be_updated($response)
347 {
348 return (isset($response->platform_id) && strtolower($response->platform_id) == 'email') ||
349 (isset($response->status) && strtolower($response->status) != 'active');
350 }
351
352 /**
353 * @param $data
354 * @return int Total of orders in last month
355 */
356 public static function get_calculate_dealsize()
357 {
358 $created_at_min = date( 'Y-m-d', strtotime( '-1 month' ) );
359
360 $ordersCollection = wc_get_orders(
361 array(
362 'limit' => -1,
363 'date_after' => $created_at_min,
364 'return' => 'ids',
365 )
366 );
367
368 return strval( count( $ordersCollection ) );
369 }
370
371 /**
372 * @param $data
373 * @return array
374 */
375 private static function get_user_data()
376 {
377 $userData = [];
378 $user = self::get_current_user();
379 $userData['name'] = $user->user_firstname;
380 $userData['password'] = self::generate_temp_pass();
381 $userData['platform_id'] = self::WOOCOMMERCE;
382 $userData['phone'] = '';
383 $userData['primary_email'] = self::get_current_email();
384 return $userData;
385 }
386
387 /**
388 * @return string
389 */
390 private static function get_current_email()
391 {
392 return get_bloginfo('admin_email');
393 }
394
395 /**
396 * @param $data
397 * @return array
398 */
399 private static function get_merchant_data($site)
400 {
401 $merchantData = [];
402 $merchantData['platform_id'] = self::WOOCOMMERCE;
403 $merchantData['store_domain'] = $site->domain;
404 $merchantData['store_name'] = self::get_blog_name($site);
405 $merchantData['deal_size_order_count'] = self::get_calculate_dealsize();
406 $merchantData['country'] = self::get_store_country($site);
407 $merchantData['currency'] = self::get_currency($site);
408 $merchantData['source'] = self::WOOCOMMERCE;
409 $merchantData['status'] = self::ACTIVE;
410 return $merchantData;
411 }
412
413 /**
414 * Prepare Merchant Data
415 * @param $store
416 * @return mixed
417 */
418 public function prepare_compatibility_data()
419 {
420 $widgetPhpFlag = $this->get_setup_check_widget(self::SETUP_CHECK_WIDGET_PHP);
421 $widgetJsFlag = $this->get_setup_check_widget(self::SETUP_CHECK_WIDGET_JS);
422 $creationDate = $this->get_setup_check_widget(self::SETUP_CHECK_WIDGET_INSTALLATION_DATE);
423
424 $successInstallation = $widgetPhpFlag && $widgetJsFlag;
425 $data = [];
426 $data['php_flag'] = $widgetPhpFlag ? $widgetPhpFlag : 0;
427 $data['js_flag'] = $widgetJsFlag ? $widgetJsFlag : 0;
428 $data['success_installation'] = $successInstallation;
429 $data['install_date'] = $creationDate;
430 $data['store_domain'] = parse_url(get_site_url(get_current_blog_id()), PHP_URL_HOST);
431 $data['platform_id'] = self::WOOCOMMERCE;
432 // If get_plugins() isn't available, require it
433 if ( ! function_exists( 'get_plugins' ) )
434 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
435 $data['modules'] = get_plugins();
436 $data['subject'] = $successInstallation ? 'Success Installation' : 'Installation Issues';
437 return $data;
438 }
439
440 public function can_send_compatibility_report(){
441 $apiCalled = $this->get_setup_check_widget(self::SETUP_CHECK_WIDGET_API_CALL);
442 $creationDate = $this->get_setup_check_widget(self::SETUP_CHECK_WIDGET_INSTALLATION_DATE);
443
444 return (time() > $creationDate && !$apiCalled);
445 }
446
447
448 /**
449 * @return array|
450 */
451 public static function get_all_sites()
452 {
453 if(is_multisite()) {
454 return get_sites();
455 }
456 $site = new stdClass();
457 $site->domain = parse_url(get_site_url(), PHP_URL_HOST);
458 $site->blog_id = 1;
459 return [$site];
460 }
461
462 /**
463 * @param $site
464 * @return mixed
465 */
466 private static function get_blog_name($site)
467 {
468 if (is_multisite())
469 return get_blog_option($site->blog_id, 'blogname');
470 return get_option('blogname');
471 }
472
473 /**
474 * @param $site
475 * @return mixed
476 */
477 private static function get_currency($site)
478 {
479 if(is_multisite())
480 return get_blog_option($site->blog_id, 'woocommerce_currency');
481 return get_woocommerce_currency();
482 }
483
484 /**
485 * @param $site
486 * @return mixed|string
487 */
488 private static function get_store_country($site){
489 if(is_multisite()) {
490 $country = get_blog_option($site->blog_id, 'woocommerce_default_country');
491 if (strpos($country, ':') > 0) {
492 $countryState = explode(':', $country);
493 return isset($countryState[0]) ? $countryState[0] : $country;
494 }
495 return $country;
496 }
497 return get_woocommerce_currency();
498 }
499
500 private static function generate_temp_pass()
501 {
502 return "pass" . substr(hash('sha256', rand()), 0, 10);
503 }
504
505 private static function set_registration_failed_as($step){
506 update_option(self::FAILED_REGISTRATION , $step);
507 }
508
509 private static function get_registration_failed_as(){
510 return get_option(self::FAILED_REGISTRATION);
511 }
512
513 private static function set_secret_key($token, $blog_id = null){
514 if (isset($blog_id) && is_multisite()) {
515 return update_blog_option($blog_id, self::SECRET_TOKEN_OPTION, $token);
516 }
517 return update_option(self::SECRET_TOKEN_OPTION , $token);
518 }
519
520 private static function set_user_key($token, $blog_id = null){
521 if (isset($blog_id) && is_multisite()) {
522 return update_blog_option($blog_id, self::USER_TOKEN_OPTION, $token);
523 }
524 return update_option(self::USER_TOKEN_OPTION , $token);
525 }
526
527 private static function set_user_id($userId, $blog_id = null){
528 if (isset($blog_id) && is_multisite()) {
529 return update_blog_option($blog_id, self::USER_ID_OPTION, $userId);
530 }
531 return update_option(self::USER_ID_OPTION , $userId);
532 }
533
534 private static function set_public_key($token, $blog_id = null){
535 if (isset($blog_id) && is_multisite()) {
536 return update_blog_option($blog_id, self::PUBLIC_TOKEN_OPTION, $token);
537 }
538 return update_option(self::PUBLIC_TOKEN_OPTION , $token);
539 }
540
541 private static function get_activation_link(){
542 return get_option(self::ACTIVATION_LINK);
543 }
544
545 private static function set_as_installed(){
546 update_option(self::REGISTRATION_STEP, self::MODULE_INSTALLED);
547 self::set_setup_check_widget(self::SETUP_CHECK_WIDGET_INSTALLATION_DATE, time());
548 }
549
550 private static function set_activation_link($activation_link){
551 update_option(self::ACTIVATION_LINK, $activation_link);
552 }
553
554 private static function registration_step(){
555 return get_option(self::REGISTRATION_STEP);
556 }
557
558 public static function is_installed(){
559 return self::registration_step() == self::MODULE_INSTALLED;
560 }
561
562 public static function has_user_login_succeed(){
563 return self::get_registration_failed_as() == self::REGISTRATION_STEP_USER_LOGIN_SUCCESS;
564 }
565
566 /**
567 * Set setup check widget
568 *
569 * @param $config
570 * @param bool $value
571 */
572 public static function set_setup_check_widget($config, $value = false)
573 {
574 if (!get_option($config)) {
575 $value = $value ? $value : 1;
576 update_option($config, $value);
577 }
578 }
579
580 /**
581 * Get setup check widget config
582 *
583 * @param $config
584 * @return mixed
585 */
586 public function get_setup_check_widget($config)
587 {
588 return get_option($config);
589 }
590
591 /**
592 * @return WP_User
593 */
594 private static function get_current_user()
595 {
596 return wp_get_current_user();
597 }
598
599
600 public static function retry_user(){
601 self::create_user();
602 wp_redirect( self_admin_url( "plugins.php" ) );
603 die();
604 }
605
606 public static function user_login() {
607 self::register_user_login($_POST['username'],$_POST['password']);
608 wp_redirect( self_admin_url( "plugins.php" ) );
609 die();
610 }
611
612 public static function retry_merchant(){
613 self::create_merchant();
614 wp_redirect( self_admin_url( "plugins.php" ) );
615 die();
616 }
617
618 public static function get_route_redirect(){
619 if(self::has_user_login_succeed()){
620 $custom_env = getenv('ROUTEAPP_ENVIRONMENT_ENDPOINT');
621 if (is_null($custom_env) || !$custom_env) {
622 $custom_env = isset($_SERVER['ROUTEAPP_ENVIRONMENT_ENDPOINT']) ? $_SERVER['ROUTEAPP_ENVIRONMENT_ENDPOINT'] : '';
623 }
624 if ($custom_env == 'stage') {
625 return self::DASHBOARD_STAGE;
626 }
627 return self::DASHBOARD ;
628 }
629 return self::get_activation_link();
630 }
631
632 private static function update_merchant_status(){
633 $routeapp_public = self::get_route_public_instance();
634 $merchant = $routeapp_public->routeapp_api_client->get_merchant();
635 if (empty($merchant)) {
636 return;
637 }
638
639 if (property_exists($merchant, 'status')) {
640 $routeapp_public->routeapp_api_client->update_merchant_status('Active');
641 }
642 }
643 }
644