· 6 years ago · Jul 18, 2019, 04:36 AM
1<?php
2/**
3 * The core plugin class.
4 *
5 * This is used to define internationalization, admin-specific hooks, and
6 * public-facing site hooks.
7 *
8 * Also maintains the unique identifier of this plugin as well as the current
9 * version of the plugin.
10 *
11 * @since 1.0.0
12 * @package Jl_Token_Helper_Api
13 * @subpackage Jl_Token_Helper_Api/includes
14 * @author Jouleslabs <hello@jouleslabs.com>
15 */
16class Jl_Token_Helper_Api extends WP_REST_Controller {
17
18 public $my_namespace = 'jlt/v';
19 public $my_version = '1';
20 private $base_url;
21 public $token_list = array();
22
23 /*****
24 * Affiliate Property
25 */
26 private $plugin_name;
27 private $version;
28 private $current_rank = 0;
29 private static $rule;
30 protected static $user_id;
31 protected static $affiliate_id;
32 protected static $special_payment_type = '';
33 protected static $source;
34 protected static $campaign;
35 protected static $currency;
36 protected static $visit_id = 0;
37 private $source_type = 'WiToken Purchase';
38 private static $checkout_referrals_select_settings = array();
39 private static $mlm_amout_type_per_level;
40 private static $mlm_amount_value_per_level;
41 private static $offer_array;
42 private static $rank_settings;
43 private $input_amount;
44 private $rank_output_amount = FALSE;//0;
45 private $offer_output_amount = FALSE;//0;
46
47 /**
48 * Define the core functionality of the plugin.
49 *
50 * @since 1.0.0
51 */
52 public function __construct() {
53
54 /*****
55 *
56 * Affiliate Methods
57 *
58 */
59 /// SET CURRENT RANK
60 $this->current_rank = get_option('jas_register_new_user_rank');
61
62 self::$campaign = '';
63 self::$currency = get_option('jas_currency');
64 if (!self::$currency){
65 self::$currency = 'USD';
66 }
67 self::$source = (empty($source)) ? '' : $source;
68 //self::$offer_array = $this->set_offers();
69 //self::$rank_settings = $this->set_rank_settings();
70 self::$rule = get_option('jas_referral_offer_type');
71 if (empty(self::$rule)){
72 self::$rule = 'lowest';
73 }
74
75 }
76
77 public function get_amount_price($taxonomy_term_id, $bonus_type, $phase_or_stage){
78
79 $data = array();
80 if( $phase_or_stage == 'stage' ){
81
82 $counter = get_term_meta( $taxonomy_term_id, 'witoken_custom_pricing', true);
83 for( $i=0; $i<$counter; $i++ ){
84 if( $bonus_type =="custom_price" ){
85 $field = 'witoken_custom_pricing_'.$i.'_amount';
86 $_custom_pricing = 'witoken_custom_pricing_'.$i.'_custom_token_price';
87 $_amount = get_term_meta( $taxonomy_term_id, $field, true);
88 $custom_pricing = get_term_meta( $taxonomy_term_id, $_custom_pricing, true);
89 $data[$_amount] = $custom_pricing;
90 } else {
91 $field = 'witoken_custom_pricing_'.$i.'_amount';
92 $_bonus_amount = 'witoken_custom_pricing_'.$i.'_bonus_amount';
93 $_amount = get_term_meta( $taxonomy_term_id, $field, true);
94 $percentage = get_term_meta( $taxonomy_term_id, $_bonus_amount, true);
95 $data[$_amount] = $percentage;
96 }
97
98 }
99
100 } else {
101 $counter = get_post_meta( $taxonomy_term_id, 'witoken_custom_pricing', true);
102 for( $j=0; $j<$counter; $j++ ){
103 if( $bonus_type =="custom_price" ){
104 $field = 'witoken_custom_pricing_'.$j.'_amount';
105 $_custom_pricing = 'witoken_custom_pricing_'.$j.'_custom_price_amount';
106 $_amount = get_post_meta( $taxonomy_term_id, $field, true);
107 $custom_pricing = get_post_meta( $taxonomy_term_id, $_custom_pricing, true);
108 $data[$_amount] = $custom_pricing;
109 } else {
110 $field = 'witoken_custom_pricing_'.$j.'_amount';
111 $_bonus_amount = 'witoken_custom_pricing_'.$j.'_bonus_amount';
112 $_amount = get_post_meta( $taxonomy_term_id, $field, true);
113 $percentage = get_post_meta( $taxonomy_term_id, $_bonus_amount, true);
114 $data[$_amount] = $percentage;
115 }
116
117 }
118 }
119
120 return $data;
121
122 }
123
124
125 /**
126 *
127 * @since 1.0.0
128 */
129 public function token_sale_bonus_heading_info(){
130 //$amount = '';
131 $countDate='';
132 $amount_info = array();
133 $bonus_type = '';
134 $title = '';
135 $phase_or_stage = '';
136 $is_running = false;
137 $upcomming = false;
138 $bonus_percentage = '';
139 $taxonomies = array( 'taxonomy' =>'tss_category','hide_empty'=> false,'meta_key' =>'start_date','orderby' => 'meta_value','order' => 'ASC');
140 $i=1;
141 $current_date = date('Y-m-d h:i:s A');
142 $current_date = date("Y-m-d h:i:s A", strtotime($current_date));
143
144 $taxonomy_terms = get_terms( $taxonomies );
145 foreach ( $taxonomy_terms as $taxonomy_term ) :
146 $taxonomy_term_name = $taxonomy_term->name;
147 $taxonomy_term_id = $taxonomy_term->term_id;
148 $start_date = get_term_meta( $taxonomy_term_id, 'start_date', true);
149 $end_date = get_term_meta( $taxonomy_term_id, 'end_date', true);
150
151 $start_date = date("Y-m-d h:i:s A", strtotime($start_date . '00:00:00'));
152 $end_date = date("Y-m-d h:i:s A", strtotime($end_date . ' + 23 hours +59 minutes'));
153
154 if( $current_date < $start_date ){
155 $title = $taxonomy_term_name;
156 $phase_or_stage = 'st-'.$taxonomy_term_id;
157 $term_slug = $taxonomy_term->slug;
158 $countDate = $start_date;
159 $bonus_percentage = get_term_meta($taxonomy_term_id, 'percentage_', true);
160 $upcomming = true;
161 $is_running = false;
162 break;
163
164 } else if( $current_date >=$start_date && $current_date <=$end_date ){
165 $title = $taxonomy_term_name;
166 $phase_or_stage = 'st-'.$taxonomy_term_id;
167 $term_slug = $taxonomy_term->slug;
168 $countDate = $end_date;
169 $bonus_type = get_term_meta( $taxonomy_term_id, 'bonus_type', true);
170 $bonus_percentage = get_term_meta($taxonomy_term_id, 'percentage_', true);
171 $upcomming = false;
172 $is_running = true;
173 $amount_info = $this->get_amount_price($taxonomy_term_id, $bonus_type, 'stage');
174 break;
175 }
176 endforeach;
177
178 $general_settings = get_option( 'general_info' );
179 $disable_token_sale = $general_settings['gn_disable_token_sale'];
180 if( $disable_token_sale ){
181 $is_running = false;
182 }
183 else if( empty($disable_token_sale) && $upcomming == true ){
184 $is_running = false;
185 }
186 else {
187 $is_running = true;
188 }
189
190 return array(
191 'bonus_percentage' => $bonus_percentage,
192 'phase_or_stage' => $phase_or_stage,
193 'term_slug' => $term_slug,
194 'bonus_type' => $bonus_type,
195 'stage' => $title,
196 'amount_info' => $amount_info,
197 'date' => $countDate,
198 'isAvailable' => $is_running,
199 'upcomming' => $upcomming
200 );
201 }
202
203 public function get_available_packages(){
204
205 $arr = array();
206 $taxonomies = array( 'taxonomy' =>'tss_category','hide_empty'=> false,'meta_key' =>'start_date','orderby' => 'meta_value','order' => 'ASC');
207 $current_date = date('Y-m-d');
208 $taxonomy_terms = get_terms( $taxonomies );
209
210 foreach ( $taxonomy_terms as $taxonomy_term ) :
211 $taxonomy_term_id = $taxonomy_term->term_id;
212 $query = new WP_Query(array(
213 'post_type' => 'toke_sale_stage',
214 'tax_query' => array(
215 array(
216 'taxonomy' => 'tss_category',
217 'terms' => $taxonomy_term_id,
218 'field' => 'term_id',
219 )
220 ),
221 'orderby' => 'title',
222 'order' => 'ASC' )
223 );
224
225 $i = 0;
226 $j=0;
227 if ( $query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
228
229 $phase_start_date = get_post_meta( get_the_ID(), 'start_date', true);
230 $phase_end_date = get_post_meta( get_the_ID(), 'end_date', true);
231
232 $phase_start_date = date("Y-m-d", strtotime($phase_start_date));
233 $phase_end_date = date("Y-m-d", strtotime($phase_end_date));
234
235 if( $current_date >=$phase_start_date && $current_date <=$phase_end_date ){
236 $slug = get_post_field( 'post_name', get_the_ID() );
237
238 $query = new WP_Query(array(
239 'post_type' => 'witoken-packages',
240 'post_status' => 'publish',
241 'orderby' => 'id',
242 'order' => 'ASC',
243 'meta_query' => array(
244 array(
245 'key' => 'token_sale_stages',
246 'value' => sprintf(':"%s";', $slug),
247 'compare' => 'LIKE'
248 )
249 )
250 )
251 );
252
253 if ( $query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
254 $cost = get_post_meta(get_the_ID(), 'package_cost', true);
255 $cost = str_replace( ',', '', $cost );
256 $rate = get_post_meta(get_the_ID(), 'package_witoken_rate', true);
257 $qty = get_post_meta(get_the_ID(), 'package_witoken_qty', true);
258 $bonus = get_post_meta(get_the_ID(), 'package_bonus', true );
259 $round = get_post_meta(get_the_ID(), 'package_bonus_round', true );
260 $image = get_the_post_thumbnail_url(get_the_ID(), 'post-thumbnail');
261 $stage = get_post_meta(get_the_ID(), 'token_sale_stages', true );
262 $bounty_affiliate = get_post_meta(get_the_ID(), 'bounty_affiliate', true );
263
264 $rank = '';
265 if( !empty($bounty_affiliate) ){
266 $rank_data = $this->get_rank($bounty_affiliate);
267 $rank = $rank_data['label'];
268 }
269
270 $arr[$i]['id'] = get_the_ID();
271 $arr[$i]['title'] = get_the_title();
272 $arr[$i]['cost'] = $cost;
273 $arr[$i]['rate'] = $rate;
274 $arr[$i]['qty'] = $qty;
275 $arr[$i]['bonus'] = $bonus;
276 $arr[$i]['round'] = $round;
277 $arr[$i]['image'] = $image;
278 $arr[$i]['rank'] = $rank;
279 $i++;
280 endwhile;
281 endif;
282 }
283
284 endwhile;
285 else:
286 $start_date = get_term_meta( $taxonomy_term_id, 'start_date', true);
287 $end_date = get_term_meta( $taxonomy_term_id, 'end_date', true);
288
289 $start_date = date("Y-m-d", strtotime($start_date));
290 $end_date = date("Y-m-d", strtotime($end_date));
291
292 if( $current_date >=$start_date && $current_date <=$end_date ){
293 $slug = $taxonomy_term->slug;
294
295 $query = new WP_Query(array(
296 'post_type' => 'witoken-packages',
297 'post_status' => 'publish',
298 'orderby' => 'id',
299 'order' => 'ASC',
300 'meta_query' => array(
301 array(
302 'key' => 'token_sale_stages',
303 'value' => sprintf(':"%s";', $slug),
304 'compare' => 'LIKE'
305 )
306 )
307 )
308 );
309
310 if ( $query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
311 $cost = get_post_meta(get_the_ID(), 'package_cost', true);
312 $cost = str_replace( ',', '', $cost );
313 $rate = get_post_meta(get_the_ID(), 'package_witoken_rate', true);
314 $qty = get_post_meta(get_the_ID(), 'package_witoken_qty', true);
315 $bonus = get_post_meta(get_the_ID(), 'package_bonus', true );
316 $round = get_post_meta(get_the_ID(), 'package_bonus_round', true );
317 $image = get_the_post_thumbnail_url(get_the_ID(), 'post-thumbnail');
318 $stage = get_post_meta(get_the_ID(), 'token_sale_stages', true );
319 $bounty_affiliate = get_post_meta(get_the_ID(), 'bounty_affiliate', true );
320
321 $rank = '';
322 if( !empty($bounty_affiliate) ){
323 $rank_data = $this->get_rank($bounty_affiliate);
324 $rank = $rank_data['label'];
325 }
326
327 $arr[$j]['id'] = get_the_ID();
328 $arr[$j]['title'] = get_the_title();
329 $arr[$j]['cost'] = $cost;
330 $arr[$j]['rate'] = $rate;
331 $arr[$j]['qty'] = $qty;
332 $arr[$j]['bonus'] = $bonus;
333 $arr[$j]['round'] = $round;
334 $arr[$j]['image'] = $image;
335 $arr[$j]['rank'] = $rank;
336 $j++;
337 endwhile;
338 endif;
339 }
340 endif;
341 endforeach;
342
343 return $arr;
344 }
345
346 public function get_package_availability(){
347
348 // CHECK PACKAGE IS AVAILABLE
349 $assigned_package = '';
350 $arr = array();
351 $taxonomies = array( 'taxonomy' =>'tss_category','hide_empty'=> false,'meta_key' =>'start_date','orderby' => 'meta_value','order' => 'ASC');
352 $current_date = date('Y-m-d');
353 $taxonomy_terms = get_terms( $taxonomies );
354 foreach ( $taxonomy_terms as $taxonomy_term ) :
355 $taxonomy_term_id = $taxonomy_term->term_id;
356 $query = new WP_Query(array(
357 'post_type' => 'toke_sale_stage',
358 'tax_query' => array(
359 array(
360 'taxonomy' => 'tss_category',
361 'terms' => $taxonomy_term_id,
362 'field' => 'term_id',
363 )
364 ),
365 'orderby' => 'title',
366 'order' => 'ASC' )
367 );
368
369 if ( $query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
370
371 $phase_start_date = get_post_meta( get_the_ID(), 'start_date', true);
372 $phase_end_date = get_post_meta( get_the_ID(), 'end_date', true);
373
374 $phase_start_date = date("Y-m-d", strtotime($phase_start_date));
375 $phase_end_date = date("Y-m-d", strtotime($phase_end_date));
376
377 if( $current_date < $phase_start_date){
378
379 $slug = get_post_field( 'post_name', get_the_ID() );
380 $query = new WP_Query(array(
381 'post_type' => 'witoken-packages',
382 'post_status' => 'publish',
383 'orderby' => 'id',
384 'order' => 'ASC',
385 'meta_query' => array(
386 array(
387 'key' => 'token_sale_stages',
388 'value' => sprintf(':"%s";', $slug),
389 'compare' => 'LIKE'
390 )
391 )
392 )
393 );
394
395 $assigned_package = $query->found_posts;
396 if( $assigned_package > 0){
397 break;
398 }
399 } else if( $current_date >=$phase_start_date && $current_date <=$phase_end_date ){
400 $slug = get_post_field( 'post_name', get_the_ID() );
401
402 $query = new WP_Query(array(
403 'post_type' => 'witoken-packages',
404 'post_status' => 'publish',
405 'orderby' => 'id',
406 'order' => 'ASC',
407 'meta_query' => array(
408 array(
409 'key' => 'token_sale_stages',
410 'value' => sprintf(':"%s";', $slug),
411 'compare' => 'LIKE'
412 )
413 )
414 )
415 );
416
417 $assigned_package = $query->found_posts;
418 if( $assigned_package > 0){
419 break;
420 }
421 }
422 endwhile;
423 else:
424 $start_date = get_term_meta( $taxonomy_term_id, 'start_date', true);
425 $end_date = get_term_meta( $taxonomy_term_id, 'end_date', true);
426
427 $start_date = date("Y-m-d", strtotime($start_date));
428 $end_date = date("Y-m-d", strtotime($end_date));
429 if( $current_date < $start_date ){
430
431 $slug = $taxonomy_term->slug;
432
433 $query = new WP_Query(array(
434 'post_type' => 'witoken-packages',
435 'post_status' => 'publish',
436 'orderby' => 'id',
437 'order' => 'ASC',
438 'meta_query' => array(
439 array(
440 'key' => 'token_sale_stages',
441 'value' => sprintf(':"%s";', $slug),
442 'compare' => 'LIKE'
443 )
444 )
445 )
446 );
447 $assigned_package = $query->found_posts;
448 if( $assigned_package > 0){
449 break;
450 }
451
452 } else if( $current_date >=$start_date && $current_date <=$end_date ){
453 $slug = $taxonomy_term->slug;
454
455 $query = new WP_Query(array(
456 'post_type' => 'witoken-packages',
457 'post_status' => 'publish',
458 'orderby' => 'id',
459 'order' => 'ASC',
460 'meta_query' => array(
461 array(
462 'key' => 'token_sale_stages',
463 'value' => sprintf(':"%s";', $slug),
464 'compare' => 'LIKE'
465 )
466 )
467 )
468 );
469 $assigned_package = $query->found_posts;
470 if( $assigned_package > 0){
471 break;
472 }
473 }
474 endif;
475 endforeach;
476 // CHECK PACKAGE IS AVAILABLE
477
478 return $assigned_package;
479 }
480
481 /**
482 *
483 * @since 1.0.0
484 */
485 public function register_routes(){
486
487 $namespace = $this->my_namespace . $this->my_version;
488
489 register_rest_route( $namespace, '/change_langauge', array(
490 array(
491 'methods' => WP_REST_Server::READABLE,
492 'callback' => array( $this, 'read_langauge_file' ),
493 ),
494 ));
495
496
497 register_rest_route( $namespace, '/user_login', array(
498 array(
499 'methods' => WP_REST_Server::READABLE,
500 'callback' => array( $this, 'generate_auth_cookie' ),
501 ),
502 ) );
503
504 register_rest_route( $namespace, '/user_logout', array(
505 array(
506 'methods' => WP_REST_Server::READABLE,
507 'callback' => array( $this, 'run_logout' ),
508 ),
509 ) );
510
511 register_rest_route( $namespace, '/check_auth_cookie', array(
512 array(
513 'methods' => WP_REST_Server::READABLE,
514 'callback' => array( $this, 'validate_auth_cookie' ),
515 ),
516 ) );
517
518 register_rest_route( $namespace, '/single_details', array(
519 array(
520 'methods' => WP_REST_Server::READABLE,
521 'callback' => array( $this, 'get_single_details' ),
522 ),
523 ));
524
525
526 register_rest_route( $namespace, '/faq', array(
527 array(
528 'methods' => WP_REST_Server::READABLE,
529 'callback' => array( $this, 'get_faq' ),
530 ),
531 ));
532
533
534 register_rest_route( $namespace, '/create_user', array(
535 array(
536 'methods' => WP_REST_Server::CREATABLE,
537 'callback' => array( $this, 'create_new_user_from_app' ),
538 ),
539 ));
540
541 register_rest_route( $namespace, '/create_user_fomopay', array(
542 array(
543 'methods' => WP_REST_Server::CREATABLE,
544 'callback' => array( $this, 'create_user_using_fomopay' ),
545 ),
546 ));
547
548 register_rest_route( $namespace, '/witoken_status_byfomo', array(
549 array(
550 'methods' => WP_REST_Server::CREATABLE,
551 'callback' => array( $this, 'update_witoken_status_byfomo' ),
552 ),
553 ));
554
555 // Register user when WiToken stage is not available
556 register_rest_route( $namespace, '/register_user', array(
557 array(
558 'methods' => WP_REST_Server::CREATABLE,
559 'callback' => array( $this, 'register_user_from_app' ),
560 ),
561 ));
562
563
564 register_rest_route( $namespace, '/paywithstripe', array(
565 array(
566 'methods' => WP_REST_Server::CREATABLE,
567 'callback' => array( $this, 'run_pay_with_stripe' ),
568 ),
569 ));
570
571
572 register_rest_route( $namespace, '/contactus', array(
573 array(
574 'methods' => WP_REST_Server::CREATABLE,
575 'callback' => array( $this, 'contact_us' ),
576 ),
577 ));
578
579
580 register_rest_route( $namespace, '/token_sale', array(
581 array(
582 'methods' => WP_REST_Server::READABLE,
583 'callback' => array( $this, 'get_token_sale' ),
584 ),
585 ));
586
587 register_rest_route( $namespace, '/token_sale_bonus_heading', array(
588 array(
589 'methods' => WP_REST_Server::READABLE,
590 'callback' => array( $this, 'get_token_sale_bonus' ),
591 ),
592 ));
593
594
595 register_rest_route( $namespace, '/staff', array(
596 array(
597 'methods' => WP_REST_Server::READABLE,
598 'callback' => array( $this, 'get_staff' ),
599 ),
600 ));
601
602
603 register_rest_route( $namespace, '/staff_details', array(
604 array(
605 'methods' => WP_REST_Server::READABLE,
606 'callback' => array( $this, 'get_staff_details' ),
607 ),
608 ));
609
610 register_rest_route( $namespace, '/token_statement', array(
611 array(
612 'methods' => WP_REST_Server::READABLE,
613 'callback' => array( $this, 'get_token_statement' ),
614 ),
615 ));
616
617 register_rest_route( $namespace, '/add_token_statement', array(
618 array(
619 'methods' => WP_REST_Server::CREATABLE,
620 'callback' => array( $this, 'set_token_statement' ),
621 ),
622 ));
623
624
625 register_rest_route( $namespace, '/token_statement_details', array(
626 array(
627 'methods' => WP_REST_Server::READABLE,
628 'callback' => array( $this, 'get_token_statement_details' ),
629 ),
630 ));
631
632 register_rest_route( $namespace, '/token_statement_balance', array(
633 array(
634 'methods' => WP_REST_Server::READABLE,
635 'callback' => array( $this, 'get_token_statement_balance' ),
636 ),
637 ));
638
639 register_rest_route( $namespace, '/token_transactions', array(
640 array(
641 'methods' => WP_REST_Server::READABLE,
642 'callback' => array( $this, 'get_token_transactions' ),
643 ),
644 ));
645
646 register_rest_route( $namespace, '/token_transactions_details', array(
647 array(
648 'methods' => WP_REST_Server::READABLE,
649 'callback' => array( $this, 'get_token_transaction_details' ),
650 ),
651 ));
652
653 register_rest_route( $namespace, '/method_meta', array(
654 array(
655 'methods' => WP_REST_Server::READABLE,
656 'callback' => array( $this, 'get_method_meta' ),
657 ),
658 ));
659
660
661 register_rest_route( $namespace, '/token_transaction_verify_details', array(
662 array(
663 'methods' => WP_REST_Server::READABLE,
664 'callback' => array( $this, 'get_token_transaction_verify_details' ),
665 ),
666 ));
667
668 register_rest_route( $namespace, '/notifications', array(
669 array(
670 'methods' => WP_REST_Server::READABLE,
671 'callback' => array( $this, 'get_notification' ),
672 ),
673 ));
674
675 register_rest_route( $namespace, '/single_notification', array(
676 array(
677 'methods' => WP_REST_Server::READABLE,
678 'callback' => array( $this, 'get_single_notification' ),
679 ),
680 ));
681
682 register_rest_route( $namespace, '/app_home', array(
683 array(
684 'methods' => WP_REST_Server::READABLE,
685 'callback' => array( $this, 'get_app_home_page' ),
686 ),
687 ));
688
689
690 register_rest_route( $namespace, '/settings', array(
691 array(
692 'methods' => WP_REST_Server::READABLE,
693 'callback' => array( $this, 'get_settings_info' ),
694 ),
695 ));
696
697 register_rest_route( $namespace, '/user_meta', array(
698 array(
699 'methods' => WP_REST_Server::READABLE,
700 'callback' => array( $this, 'get_user_metas' ),
701 ),
702 ));
703
704 register_rest_route( $namespace, '/update_avatar', array(
705 array(
706 'methods' => WP_REST_Server::CREATABLE,
707 'callback' => array( $this, 'update_avatar_from_app' ),
708 ),
709 ));
710
711 register_rest_route( $namespace, '/update_nid', array(
712 array(
713 'methods' => WP_REST_Server::CREATABLE,
714 'callback' => array( $this, 'update_nid_from_app' ),
715 ),
716 ));
717
718 register_rest_route( $namespace, '/menu', array(
719 array(
720 'methods' => WP_REST_Server::READABLE,
721 'callback' => array( $this, 'get_menu' ),
722 ),
723 ));
724
725 register_rest_route( $namespace, '/btc_current_rate', array(
726 array(
727 'methods' => WP_REST_Server::READABLE,
728 'callback' => array( $this, 'get_btc_rate' ),
729 ),
730 ));
731
732 register_rest_route( $namespace, '/ethereum_current_rate', array(
733 array(
734 'methods' => WP_REST_Server::READABLE,
735 'callback' => array( $this, 'get_eth_rate' ),
736 ),
737 ));
738
739 register_rest_route( $namespace, '/verify_bank_transaction', array(
740 array(
741 'methods' => WP_REST_Server::CREATABLE,
742 'callback' => array( $this, 'update_verify_bank_transaction' ),
743 ),
744 ));
745
746 register_rest_route( $namespace, '/fomo_response_status', array(
747 array(
748 'methods' => WP_REST_Server::READABLE,
749 'callback' => array( $this, 'retrieve_fomo_response_status' ),
750 ),
751 ));
752
753 register_rest_route( $namespace, '/refferal_list', array(
754 array(
755 'methods' => WP_REST_Server::READABLE,
756 'callback' => array( $this, 'get_refferal' ),
757 ),
758 ));
759
760 register_rest_route( $namespace, '/witoken_stage_info', array(
761 array(
762 'methods' => WP_REST_Server::READABLE,
763 'callback' => array( $this, 'get_witoken_stage_info' ),
764 ),
765 ));
766
767 register_rest_route( $namespace, '/verify_btc_transaction', array(
768 array(
769 'methods' => WP_REST_Server::CREATABLE,
770 'callback' => array( $this, 'update_verify_btc_transaction' ),
771 ),
772 ));
773
774 register_rest_route( $namespace, '/verify_eth_transaction', array(
775 array(
776 'methods' => WP_REST_Server::CREATABLE,
777 'callback' => array( $this, 'update_verify_eth_transaction' ),
778 ),
779 ));
780
781 register_rest_route( $namespace, '/gift_Witoken_by_qrcode', array(
782 array(
783 'methods' => WP_REST_Server::READABLE,
784 'callback' => array( $this, 'get_gift_Witoken_by_qrcode' ),
785 ),
786 ));
787
788 register_rest_route( $namespace, '/redeem_gift_Witoken', array(
789 array(
790 'methods' => WP_REST_Server::CREATABLE,
791 'callback' => array( $this, 'run_redeem_gift_Witoken' ),
792 ),
793 ));
794
795 register_rest_route( $namespace, '/gift_Witoken_settings', array(
796 array(
797 'methods' => WP_REST_Server::READABLE,
798 'callback' => array( $this, 'run_gift_Witoken_settings' ),
799 ),
800 ));
801
802 register_rest_route( $namespace, '/token_transfer_wiwallet', array(
803 array(
804 'methods' => WP_REST_Server::READABLE,
805 'callback' => array( $this, 'run_token_transfer_wiwallet' ),
806 ),
807 ));
808
809 register_rest_route( $namespace, '/event_list', array(
810 array(
811 'methods' => WP_REST_Server::READABLE,
812 'callback' => array( $this, 'run_event_list' ),
813 ),
814 ));
815
816 register_rest_route( $namespace, '/event_details', array(
817 array(
818 'methods' => WP_REST_Server::READABLE,
819 'callback' => array( $this, 'run_event_details' ),
820 ),
821 ));
822
823 register_rest_route( $namespace, '/book_now', array(
824 array(
825 'methods' => WP_REST_Server::CREATABLE,
826 'callback' => array( $this, 'run_book_now' ),
827 ),
828 ));
829
830
831 register_rest_route( $namespace, '/token_transfer', array(
832 array(
833 'methods' => WP_REST_Server::CREATABLE,
834 'callback' => array( $this, 'run_token_transfer' ),
835 ),
836 ));
837
838 register_rest_route( $namespace, '/balance_transactions', array(
839 array(
840 'methods' => WP_REST_Server::READABLE,
841 'callback' => array( $this, 'run_balance_transactions' ),
842 ),
843 ));
844
845 register_rest_route( $namespace, '/update_profile', array(
846 array(
847 'methods' => WP_REST_Server::CREATABLE,
848 'callback' => array( $this, 'run_update_profile' ),
849 ),
850 ));
851
852
853 register_rest_route( $namespace, '/password_reset', array(
854 array(
855 'methods' => WP_REST_Server::CREATABLE,
856 'callback' => array( $this, 'run_password_reset' ),
857 ),
858 ));
859
860 register_rest_route( $namespace, '/witoken_request', array(
861 array(
862 'methods' => WP_REST_Server::CREATABLE,
863 'callback' => array( $this, 'run_witoken_request' ),
864 ),
865 ));
866
867 register_rest_route( $namespace, '/witoken_request_list', array(
868 array(
869 'methods' => WP_REST_Server::READABLE,
870 'callback' => array( $this, 'run_witoken_request_list' ),
871 ),
872 ));
873
874
875 register_rest_route( $namespace, '/country_wise_payment_methods', array(
876 array(
877 'methods' => WP_REST_Server::READABLE,
878 'callback' => array( $this, 'run_country_wise_payment_methods' ),
879 ),
880 ));
881
882
883 /**************
884 *
885 * Affiliate Endpoint
886 *
887 **************/
888
889 register_rest_route( $namespace, '/header', array(
890 array(
891 'methods' => WP_REST_Server::READABLE,
892 'callback' => array( $this, 'get_header' ),
893 ),
894 ));
895
896 register_rest_route( $namespace, '/affiliate_info', array(
897 array(
898 'methods' => WP_REST_Server::READABLE,
899 'callback' => array( $this, 'get_affiliate_info' ),
900 ),
901 ));
902
903 register_rest_route( $namespace, '/search_referrals', array(
904 array(
905 'methods' => WP_REST_Server::READABLE,
906 'callback' => array( $this, 'get_search_referrals' ),
907 ),
908 ));
909
910 register_rest_route( $namespace, '/search_referrals_history', array(
911 array(
912 'methods' => WP_REST_Server::READABLE,
913 'callback' => array( $this, 'get_search_referrals_history' ),
914 ),
915 ));
916
917 register_rest_route( $namespace, '/search_visits', array(
918 array(
919 'methods' => WP_REST_Server::READABLE,
920 'callback' => array( $this, 'get_search_visits' ),
921 ),
922 ));
923
924
925 register_rest_route( $namespace, '/save_payment_settings', array(
926 array(
927 'methods' => WP_REST_Server::CREATABLE,
928 'callback' => array( $this, 'run_save_payment_settings' ),
929 ),
930 ));
931
932 register_rest_route( $namespace, '/save_campaign', array(
933 array(
934 'methods' => WP_REST_Server::CREATABLE,
935 'callback' => array( $this, 'run_save_campaign' ),
936 ),
937 ));
938
939 register_rest_route( $namespace, '/delete_campaign', array(
940 array(
941 'methods' => WP_REST_Server::CREATABLE,
942 'callback' => array( $this, 'run_delete_campaign' ),
943 ),
944 ));
945
946 register_rest_route( $namespace, '/generate_affiliate_link', array(
947 array(
948 'methods' => WP_REST_Server::CREATABLE,
949 'callback' => array( $this, 'run_generate_affiliate_link' ),
950 ),
951 ));
952
953 register_rest_route( $namespace, '/update_rank', array(
954 array(
955 'methods' => WP_REST_Server::CREATABLE,
956 'callback' => array( $this, 'run_update_affiliate_rank_by_package' ),
957 ),
958 ));
959
960 }
961
962 /**
963 *
964 * @since 1.0.0
965 */
966 public function run_rest_action(){
967 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
968 }
969
970
971 /***************************************
972 *
973 * ALL METHODS FOR AFFILIATE
974 *
975 ***************************************/
976
977 public function affiliate_get_id_by_uid($uid=0){
978 /*
979 * @param int
980 * @return int
981 */
982 if ($uid){
983 global $wpdb;
984 $table_name = $wpdb->prefix . 'jas_affiliates';
985 $q = $wpdb->prepare("SELECT id FROM $table_name WHERE uid=%d ;", $uid);
986 $data = $wpdb->get_row($q);
987 if (!empty($data) && !empty($data->id)){
988 return $data->id;
989 }
990 }
991 return 0;
992 }
993
994 public function add_new_mlm_relation($aff_id=0, $parent_aff_id=0){
995 /*
996 * @param int, int
997 * @return none
998 */
999 if ($aff_id && $parent_aff_id){
1000 global $wpdb;
1001 $table = $wpdb->prefix . 'jas_mlm_relations';
1002 $q = $wpdb->prepare("INSERT INTO $table VALUES(NULL, %d, %d);", $aff_id, $parent_aff_id);
1003 $wpdb->query($q);
1004 }
1005 }
1006
1007 public function get_affiliate_rank($id=0, $uid=0){
1008 /*
1009 * @param int, int
1010 * @return int
1011 */
1012 global $wpdb;
1013 $table = $wpdb->prefix . 'jas_affiliates';
1014 $q = "SELECT rank_id FROM $table WHERE ";
1015 if ($id){
1016 $id = esc_sql($id);
1017 $q .= "id='$id' ";
1018 } else {
1019 $uid = esc_sql($uid);
1020 $q .= "uid='$uid' ";
1021 }
1022 $data = $wpdb->get_row($q);
1023 if (!empty($data->rank_id)){
1024 return $data->rank_id;
1025 } return 0;
1026 }
1027
1028 public function mlm_affiliate_parent_can_get_new_child($affiliate_id=0, $limit=0){
1029 /*
1030 * @param int
1031 * @return boolean
1032 */
1033 if ($affiliate_id){
1034 $rank = $this->get_affiliate_rank($affiliate_id);
1035 global $wpdb;
1036 $table = $wpdb->prefix . 'jas_mlm_relations';
1037 $q = $wpdb->prepare("SELECT count(*) as result FROM $table WHERE parent_affiliate_id=%d;", $affiliate_id);
1038 $data = $wpdb->get_row($q);
1039 $current_num = (empty($data->result)) ? 0 : (int)$data->result;
1040 return ($limit>$current_num) ? TRUE : FALSE;
1041 }
1042 return FALSE;
1043 }
1044
1045 public function set_mlm_relation_on_new_affiliate($affiliate_id=0, $parent_affiliate_id=0){
1046 /*
1047 * USE THIS WHEN AFFILIATE REGISTER FROM ANOTHER SYSTEM OR BECOME AFFILIATE BY SHORTCODE BUTTON
1048 * @param int, int
1049 * @return none
1050 */
1051 if (get_option('jas_mlm_enable') && $affiliate_id){
1052 if (isset($_COOKIE) && !empty($_COOKIE['jas_referral']) && empty($parent_affiliate_id)){
1053 /// SERACH INTO COOKIE
1054 $cookie_data = unserialize(stripslashes($_COOKIE['jas_referral']));
1055 $parent_affiliate_id = (empty($cookie_data['affiliate_id'])) ? 0 : $cookie_data['affiliate_id'];
1056 }
1057
1058 if (!empty($parent_affiliate_id)){
1059
1060 $matrix_type = get_option('jas_mlm_matrix_type');
1061 $limit = get_option('jas_mlm_child_limit');
1062 $depth = get_option('jas_mlm_matrix_depth');
1063 switch ($matrix_type){
1064 case 'unilevel':
1065 $this->add_new_mlm_relation($affiliate_id, $parent_affiliate_id);
1066 break;
1067 case 'binary':
1068 if ($this->mlm_affiliate_parent_can_get_new_child($parent_affiliate_id, $limit)){
1069 $this->add_new_mlm_relation($affiliate_id, $parent_affiliate_id);
1070 } else {
1071 require_once WP_PLUGIN_DIR . '/jouleslabs-affliliate-sytem/classes/MLM_Force_Matrix_Parent_Test.class.php';
1072 $object = new MLM_Force_Matrix_Parent_Test($parent_affiliate_id, 2, $depth);
1073 $new_parent = $object->get_result();
1074 if ($new_parent){
1075 $this->add_new_mlm_relation($affiliate_id, $new_parent);
1076 }
1077 }
1078 break;
1079 case 'force':
1080 require_once WP_PLUGIN_DIR . '/jouleslabs-affliliate-sytem/classes/MLM_Force_Matrix_Parent_Test.class.php';
1081 $object = new MLM_Force_Matrix_Parent_Test($parent_affiliate_id, $limit, $depth);
1082 $new_parent = $object->get_result();
1083 if ($new_parent){
1084 $jas_db->add_new_mlm_relation($affiliate_id, $new_parent);
1085 }
1086 break;
1087 }
1088 }
1089 }
1090 }
1091
1092
1093
1094 private function set_mlm_parent($user_id, $referred_affid){
1095 /*
1096 * @param none
1097 * @return none
1098 */
1099 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
1100 if ($this->is_magic_feat_enable('mlm') ){
1101 // SET MLM PARENT IN PUBLIC SECTION
1102 $this->set_mlm_relation_on_new_affiliate($affiliate_id, $referred_affid);
1103 }
1104 }
1105
1106
1107 public function add_new_rank_to_history($uid=0, $prev_rid=0, $rid=0){
1108 /*
1109 * @param int, int, int
1110 * @return none
1111 */
1112 if ($uid && isset($rid)){
1113 global $wpdb;
1114 $table = $wpdb->prefix . 'jas_ranks_history';
1115 $q = $wpdb->prepare("INSERT INTO $table VALUES(null, %d, %d, %d, NOW());", $uid, $prev_rid, $rid);
1116 $wpdb->query($q);
1117 }
1118 }
1119
1120 public function get_affiliate_id_by_wpuid($uid=0){
1121 /*
1122 * @param int
1123 * @return int
1124 */
1125 if ($uid){
1126 global $wpdb;
1127 $table = $wpdb->prefix . 'jas_affiliates';
1128 $q = $wpdb->prepare("SELECT id FROM $table WHERE uid=%d", $uid);
1129 $data = $wpdb->get_row($q);
1130 if ($data && !empty($data->id)){
1131 return $data->id;
1132 }
1133 }
1134 return 0;
1135 }
1136
1137 public function get_rank($id=0){
1138 /*
1139 * @param int
1140 * @return array
1141 */
1142 global $wpdb;
1143 $table = $wpdb->prefix . 'jas_ranks';
1144 if ($id){
1145 $q = $wpdb->prepare("SELECT * FROM $table WHERE id=%d ;", $id);
1146 $data = $wpdb->get_row($q);
1147 if ($data){
1148 $data = (array)$data;
1149 $settings = unserialize($data['settings']);
1150 $data['color'] = (empty($settings['color'])) ? '' : $settings['color'];
1151 $data['description'] = (empty($settings['description'])) ? '' : $settings['description'];
1152 @$data['mlm_amount_value'] = (empty($data['mlm_amount_value'])) ? array() : unserialize($data['mlm_amount_value']);
1153 @$data['mlm_amount_type'] = (empty($data['mlm_amount_type'])) ? array() : unserialize($data['mlm_amount_type']);
1154 return $data;
1155 }
1156 } else {
1157 /// DEFAULTS
1158 $data = $wpdb->get_row("SELECT id FROM $table ORDER BY id DESC LIMIT 1");
1159 $id = (empty($data->id)) ? 1 : $data->id + 1;
1160 $data = $wpdb->get_row("SELECT rank_order FROM $table ORDER BY rank_order DESC LIMIT 1");
1161 $rank_order = (empty($data->rank_order)) ? 1 : $data->rank_order + 1;
1162 return array(
1163 'id' => 0,
1164 'slug' => 'rank_' . $id,
1165 'label' => 'Untitled Rank',
1166 'amount_type' => '%',
1167 'amount_value' => 1,
1168 'achieve' => '',
1169 'rank_order' => $rank_order,
1170 'color' => '',//from settings
1171 'description' => '',//from settings
1172 'bonus' => '',
1173 'sign_up_amount_value' => '',
1174 'lifetime_amount_type' => '',
1175 'lifetime_amount_value' => '',
1176 'reccuring_amount_type' => '',
1177 'reccuring_amount_value' => '',
1178 'mlm_amount_type' => '',
1179 'mlm_amount_value' => '',
1180 'status' => 1,
1181 );
1182 }
1183 }
1184
1185 public function save_referral($post_data=array()){
1186 /*
1187 * @param array
1188 * @return int
1189 */
1190 if (!empty($post_data)){
1191 global $wpdb;
1192 $table = $wpdb->prefix . "jas_referrals";
1193 if (empty($post_data['date'])){
1194 $post_data['date'] = date('Y-m-d H:i:s', time());
1195 }
1196
1197 ////// MAX AMOUNT
1198 if ($this->is_magic_feat_enable('max_amount')){
1199 $rank_id = $this->get_affiliate_rank($post_data['affiliate_id']);
1200 $max_amount_per_rank = get_option('jas_maximum_amount_value_per_rank');
1201 if ($max_amount_per_rank && isset($max_amount_per_rank[$rank_id]) && $max_amount_per_rank[$rank_id]!=''){
1202 if ($max_amount_per_rank[$rank_id]<$post_data['amount']){
1203 $post_data['amount'] = $max_amount_per_rank[$rank_id];
1204 }
1205 } else {
1206 $max_amount_value = get_option('jas_maximum_amount_value');
1207 if (!empty($max_amount_value) && $post_data['amount']>$max_amount_value){ ///
1208 $post_data['amount'] = $max_amount_value;
1209 }
1210 }
1211 }
1212 ////// MAX AMOUNT
1213
1214 if (!empty($post_data['id'])){
1215 $q = $wpdb->prepare("SELECT * FROM $table WHERE id=%d ", $post_data['id']);
1216 $data = $wpdb->get_row($q);
1217 if (!empty($data)){
1218 $post_data = apply_filters('jas_update_referral_filter', $post_data);
1219 /// UPDATE
1220 $q = $wpdb->prepare("UPDATE $table SET
1221 refferal_wp_uid=%d,
1222 campaign=%s,
1223 affiliate_id=%d,
1224 visit_id=%d,
1225 description=%s,
1226 source=%s,
1227 reference=%s,
1228 reference_details=%s,
1229 parent_referral_id=%d,
1230 child_referral_id=%d,
1231 bonus_type=%s,
1232 amount=%s,
1233 bonus_status=%s,
1234 currency=%s,
1235 date=%s,
1236 status=%s,
1237 payment=%s
1238 WHERE id=%d
1239 ;", $post_data['refferal_wp_uid'], $post_data['campaign'], $post_data['affiliate_id'],
1240 $post_data['visit_id'], $post_data['description'], $post_data['source'], $post_data['reference'],
1241 $post_data['reference_details'], $post_data['parent_referral_id'], $post_data['child_referral_id'],
1242 $post_data['bonus_type'], $post_data['amount'], $post_data['bonus_status'], $post_data['currency'], $post_data['date'], $post_data['status'], $post_data['payment'],
1243 $post_data['id']
1244 );
1245 $wpdb->query($q);
1246 update_post_meta( $post_data['transaction_id'], 'witoken_ref', $post_data['id']); // update referrals id under witoken statement transaction id
1247 return $post_data['id'];
1248 }
1249 }
1250
1251 $post_data = apply_filters('jas_save_referral_filter', $post_data);
1252 // SAVE
1253
1254 $q = $wpdb->prepare("INSERT INTO $table
1255 VALUES( NULL,
1256 %d,
1257 %s,
1258 %d,
1259 %d,
1260 %s,
1261 %s,
1262 %s,
1263 %s,
1264 %d,
1265 %d,
1266 %s,
1267 %s,
1268 %s,
1269 %s,
1270 %s,
1271 %s,
1272 %s );
1273 ", $post_data['refferal_wp_uid'], $post_data['campaign'], $post_data['affiliate_id'],
1274 $post_data['visit_id'], $post_data['description'], $post_data['source'], $post_data['reference'],
1275 $post_data['reference_details'], $post_data['parent_referral_id'], $post_data['child_referral_id'],
1276 $post_data['bonus_type'], $post_data['amount'], $post_data['bonus_status'], $post_data['currency'], $post_data['date'], $post_data['status'], $post_data['payment']
1277 );
1278 $wpdb->query($q);
1279 add_post_meta( $post_data['transaction_id'], 'witoken_ref', $wpdb->insert_id); // store referrals id under witoken statement transaction id
1280
1281 if ($post_data['campaign'] && isset($wpdb->insert_id)){
1282 /// save into campaign if is insert and the campaign is set
1283 $this->increment_campaign_referrals($post_data['campaign'], $post_data['affiliate_id']);
1284 }
1285
1286 /// AFFILIATE NOTIFICATION ON EVERY REFERRAL
1287 // if (!class_exists('Jas_Affiliate_Notification_Reports')){
1288 // require_once WP_PLUGIN_DIR . '/jouleslabs-affliliate-sytem/classes/Jas_Affiliate_Notification_Reports.class.php';
1289 // }
1290 // $object = new Jas_Affiliate_Notification_Reports();
1291 // $object->send_single_referral_notification($post_data['affiliate_id'], $wpdb->insert_id, $post_data['source']);
1292
1293 /// INCREMENT DASHBOARD NOTIFICATION COUNT
1294 $this->increment_dashboard_notification('referrals');
1295
1296 return $wpdb->insert_id;
1297 }
1298 }
1299
1300 public function increment_campaign_referrals($campaign='', $affiliate_id=0){
1301 /*
1302 * @param string, int
1303 * @return none
1304 */
1305 /// CAMPAIGN
1306 if (!empty($campaign) && !empty($affiliate_id)){
1307 /// check if campaign-affiliate exists
1308 global $wpdb;
1309 $table = $wpdb->prefix . 'jas_campaigns';
1310 $q = $wpdb->prepare("SELECT id, referrals
1311 FROM $table
1312 WHERE name=%s
1313 AND affiliate_id=%d;", $campaign, $affiliate_id);
1314 $temp_data = $wpdb->get_row($q);
1315 if (!empty($temp_data->id) && isset($temp_data->referrals)){
1316 /// UPDATE
1317 $referrals = (int)$temp_data->referrals;
1318 $referrals++;
1319 $q = $wpdb->prepare("UPDATE $table
1320 SET referrals=%s
1321 WHERE id=%d
1322 ", $referrals, $temp_data->id);
1323 $wpdb->query($q);
1324 }
1325 }
1326 }
1327
1328
1329 public function pay_bonus_for_rank($uid=0, $rank_id=0){
1330 /*
1331 * @param int, int
1332 * @return none
1333 */
1334 if ($uid && $rank_id && $this->is_magic_feat_enable('bonus_on_rank')){
1335 $affiliate_id = $this->get_affiliate_id_by_wpuid($uid);
1336 $rank_data = $this->get_rank($rank_id);
1337 $amount_value = $rank_data['bonus'];
1338 if ($amount_value===FALSE || $amount_value==''){
1339 $amount_value = 0;
1340 }
1341 $rank_name = $rank_data['label'];
1342 $currency = get_option('jas_currency');
1343 $status = get_option('jas_bonus_on_rank_default_referral_sts');
1344 if ($status===FALSE){
1345 $status = 2; /// verified
1346 }
1347
1348 /// EMPTY REFERRALS
1349 $general_settings_data = $this->return_settings_from_wp_option('general-settings');
1350 if (empty($general_settings_data['jas_empty_referrals_enable'])){
1351 ///don't insert referrals with 0$
1352 $min = 0.01;
1353 if ($amount_value<$min){
1354 return;
1355 }
1356 }
1357 /// EMPTY REFERRALS
1358
1359 $args = array(
1360 'refferal_wp_uid' => 0,
1361 'campaign' => '',
1362 'affiliate_id' => $affiliate_id,
1363 'visit_id' => '',
1364 'description' => __('Bonus for reaching rank: ', 'jas') . $rank_name,
1365 'source' => 'bonus',
1366 'reference' => 0,
1367 'reference_details' => 'Bonus',
1368 'amount' => $amount_value,
1369 'currency' => $currency,
1370 'date' => date('Y-m-d H:i:s', time()),
1371 'status' => $status,
1372 'payment' => 0,
1373 'parent_referral_id' => '',
1374 'child_referral_id' => '',
1375 );
1376 $this->save_referral($args);
1377 }
1378 }
1379
1380
1381 public function update_affiliate_rank_by_uid($uid, $rank_id){
1382 /*
1383 * This will change the affiliate rank and pay the bonus if it's case.
1384 * @param int, int
1385 * @return none
1386 */
1387 global $wpdb;
1388 $table = $wpdb->prefix . 'jas_affiliates';
1389 $q = $wpdb->prepare("SELECT id, rank_id FROM $table WHERE uid=%d ;", $uid);
1390 $exists = $wpdb->get_row($q);
1391 if (!empty($exists->id) && $exists->rank_id!=$rank_id){
1392 $q = $wpdb->prepare("UPDATE $table SET rank_id=%d WHERE uid=%d ;", $rank_id, $uid);
1393 $wpdb->query($q);
1394
1395 ///rank history
1396 $this->add_new_rank_to_history($exists->id, $exists->rank_id, $rank_id);
1397
1398 /// PAY BONUS
1399 $this->pay_bonus_for_rank($uid, $rank_id);
1400 }
1401 }
1402
1403
1404 public function return_settings_from_wp_option($type, $only_name=false, $return_default=false){
1405 /*
1406 * @param string, bool, bool
1407 * @return array
1408 */
1409 //all metas
1410 switch ($type){
1411
1412 case 'general-public_workflow':
1413 $arr = array(
1414 'jas_hide_payments_warnings' => 0,
1415 'jas_default_payment_system' => 'bt',
1416 'jas_disable_bt_payment_system' => 0,
1417 'jas_custom_source_name_woo' => 'WooCommerce',
1418 'jas_custom_source_name_ump' => 'Ultimate Membership Pro',
1419 'jas_custom_source_name_edd' => 'Easy Download Digital',
1420 'jas_custom_source_name_bonus' => 'Bonus',
1421 'jas_custom_source_name_mlm' => 'MLM',
1422 'jas_custom_source_name_landing_commissions' => 'Landing commissions',
1423 'jas_custom_source_name_user_signup' => 'User SignUp',
1424 );
1425 break;
1426 case 'general-settings':
1427 $arr = array(
1428 'jas_redirect_without_param' => 1,
1429 'jas_referral_variable' => 'ref',
1430 'jas_referral_custom_base_link' => '',
1431 'jas_campaign_variable' => 'campaign',
1432 'jas_default_ref_format' => 'username',
1433 'jas_cookie_expire' => 360,//value in days
1434 'jas_currency' => 'USD',
1435 'jas_currency_position' => 'right',
1436 'jas_referral_offer_type' => 'biggest',
1437 'jas_all_new_users_become_affiliates' => 0,
1438 'jas_exclude_shipping' => 1,
1439 'jas_exclude_tax' => 1,
1440 'jas_empty_referrals_enable' => 1,
1441 );
1442 break;
1443 case 'paypal':
1444 $arr = array(
1445 'jas_paypal_enable' => 0,
1446 'jas_paypal_sandbox' => 0,
1447 'jas_paypal_sandbox_client_id' => '',
1448 'jas_paypal_sandbox_client_secret' => '',
1449 'jas_paypal_client_id' => '',
1450 'jas_paypal_client_secret' => '',
1451 );
1452 break;
1453 case 'stripe':
1454 $arr = array(
1455 'jas_stripe_enable' => 0,
1456 'jas_stripe_sandbox' => 0,
1457 'jas_stripe_sandbox_secret_key' => '',
1458 'jas_stripe_sandbox_publishable_key' => '',
1459 'jas_stripe_secret_key' => '',
1460 'jas_stripe_publishable_key' => '',
1461 );
1462 break;
1463
1464 case 'stripe_v2' :
1465 $arr = array(
1466 'jas_stripe_v2_enable' => 0,
1467 'jas_stripe_v2_sandbox' => 0,
1468 'jas_stripe_v2_sandbox_secret_key' => '',
1469 'jas_stripe_v2_sandbox_publishable_key' => '',
1470 'jas_stripe_v2_secret_key' => '',
1471 'jas_stripe_v2_publishable_key' => '',
1472 );
1473 break;
1474 case 'bonus_on_rank':
1475 $arr = array(
1476 'jas_bonus_on_rank_enable' => 0,
1477 'jas_bonus_on_rank_default_referral_sts' => 2,
1478 );
1479 break;
1480 case 'mlm':
1481 $arr = array(
1482 'jas_mlm_enable' => 0,
1483 'jas_mlm_matrix_type' => 'binary',
1484 'jas_mlm_child_limit' => 2,
1485 'jas_mlm_matrix_depth' => 3,
1486 'jas_mlm_default_amount_value' => '',
1487 'jas_mlm_default_amount_type' => '',
1488 'mlm_amount_value_per_level' => '',
1489 'mlm_amount_type_per_level' => '',
1490 'jas_mlm_use_amount_from' => 'child_referral',
1491 );
1492 break;
1493 case 'checkout_select_referral':
1494 $arr = array(
1495 'jas_checkout_select_referral_enable' => 0,
1496 'jas_checkout_select_referral_s_type' => 1,
1497 'jas_checkout_select_referral_label' => '',
1498 'jas_checkout_select_affiliate_list' => '',
1499 'jas_checkout_select_referral_rewrite' => 0,
1500 'jas_checkout_select_referral_require' => 0,
1501 'jas_checkout_select_referral_name' => 'user_login',
1502 );
1503 break;
1504
1505 }
1506
1507 if ($return_default){
1508 //return default values
1509 return $arr;
1510 }
1511
1512 if (isset($arr)){
1513 if ($only_name){
1514 return $arr;
1515 }
1516 foreach ($arr as $k=>$v){
1517 $data = get_option($k);
1518 if ($data!==FALSE){
1519 $arr[$k] = $data;
1520 } else {
1521 add_option($k, $v);
1522 }
1523 }
1524 return $arr;
1525 }
1526 return FALSE;
1527
1528 }
1529
1530
1531 public function is_magic_feat_enable($type=''){
1532 /*
1533 * @param string
1534 * @return boolean
1535 */
1536 $temp_data = $this->return_settings_from_wp_option($type);
1537 switch ($type){
1538 case 'bonus_on_rank':
1539 return ($temp_data['jas_bonus_on_rank_enable']) ? TRUE : FALSE;
1540 break;
1541 case 'paypal':
1542 return ($temp_data['jas_paypal_enable']) ? TRUE : FALSE;
1543 break;
1544 case 'stripe':
1545 return ($temp_data['jas_stripe_enable']) ? TRUE : FALSE;
1546 break;
1547 case 'stripe_v2':
1548 return $temp_data['jas_stripe_v2_enable'];
1549 break;
1550 case 'mlm':
1551 return ($temp_data['jas_mlm_enable']) ? TRUE : FALSE;
1552 break;
1553 case 'max_amount':
1554 return $temp_data['jas_maximum_amount_enabled'];
1555 break;
1556 }
1557 return FALSE;
1558 }
1559
1560
1561 public function get_payment_types_available(){
1562 /*
1563 * @param none
1564 * @return array
1565 */
1566 $payments = array(
1567 'bt' => __('Bank Transfer', 'jas'),
1568 'paypal' => __('PayPal', 'jas'),
1569 'stripe' => __('Stripe', 'jas'),
1570 'stripe_v2' => __('Stripe Managed Accounts', 'jas'),
1571 );
1572 if (!$this->is_magic_feat_enable('paypal')){
1573 unset($payments['paypal']);
1574 }
1575 if (!$this->is_magic_feat_enable('stripe')){
1576 unset($payments['stripe']);
1577 }
1578 if (!$this->is_magic_feat_enable('stripe_v2')){
1579 unset($payments['stripe_v2']);
1580 }
1581 $disable_bt = get_option('jas_disable_bt_payment_system');
1582 if (!empty($disable_bt)){
1583 unset($payments['bt']);
1584 }
1585 return $payments;
1586 }
1587
1588 public function get_uid_by_affiliate_id($aff_id=0){
1589 /*
1590 * @param int
1591 * @return int
1592 */
1593 if ($aff_id){
1594 global $wpdb;
1595 $table_name = $wpdb->prefix . 'jas_affiliates';
1596 $q = $wpdb->prepare("SELECT uid FROM $table_name WHERE id=%d ;", $aff_id);
1597 $data = $wpdb->get_row($q);
1598 if (!empty($data) && !empty($data->uid)){
1599 return $data->uid;
1600 }
1601 }
1602 return 0;
1603 }
1604
1605
1606 public function get_affiliate_payment_settings($uid=0, $affiliate_id=0, $only_keys=FALSE){
1607 /*
1608 * @param int, int
1609 * @return array
1610 */
1611 $array = array(
1612 'jas_affiliate_payment_type' => 'bt',
1613 ///BT
1614 'jas_affiliate_bank_transfer_data' => '',
1615 /// PAYPAL
1616 'jas_affiliate_paypal_email' => '',
1617 /// STRIPE
1618 'jas_affiliate_stripe_name' => '',
1619 'jas_affiliate_stripe_card_number' => '',
1620 //'jas_affiliate_stripe_cvc' => '',
1621 'jas_affiliate_stripe_expiration_month' => '',
1622 'jas_affiliate_stripe_expiration_year' => '',
1623 'jas_affiliate_stripe_card_type' => 'individual',
1624 //'jas_affiliate_stripe_tax_id' => '',
1625
1626 );
1627 if ($only_keys){
1628 return $array;
1629 }
1630 if (!$uid && $affiliate_id){
1631 $uid = $this->get_uid_by_affiliate_id($affiliate_id);
1632 }
1633 if ($uid){
1634 foreach ($array as $meta_key=>$meta_value){
1635 $temp_data = get_user_meta($uid, $meta_key, TRUE);
1636 if (isset($temp_data)){
1637 $array[$meta_key] = $temp_data;
1638 }
1639 }
1640 }
1641 return $array;
1642 }
1643
1644
1645 public function save_affiliate_payment_settings($uid=0, $post_data=array()){
1646 /*
1647 * @param int, array
1648 * @return none
1649 */
1650 $keys = $this->get_affiliate_payment_settings($uid, 0, TRUE);
1651 if ($keys){
1652 foreach ($keys as $meta_key=>$meta_value){
1653 if (isset($post_data[$meta_key])){
1654 update_user_meta($uid, $meta_key, $post_data[$meta_key]);
1655 }
1656 }
1657 }
1658 }
1659
1660
1661 public function set_default_payment_on_register_affiliate($uid=0){
1662 /*
1663 * @param int
1664 * @return none
1665 */
1666 if ($uid){
1667 $value = '';
1668 $available_systems = $this->get_payment_types_available();
1669 if ($available_systems){
1670 $temp = $this->return_settings_from_wp_option('general-public_workflow');
1671 $default_value = $temp['jas_default_payment_system'];
1672 if ($default_value && !empty($available_systems[$default_value])){
1673 $value = $default_value;
1674 }
1675 $this->save_affiliate_payment_settings($uid, array('jas_affiliate_payment_type' => $value));
1676 }
1677 }
1678
1679 }
1680
1681
1682 public function increment_dashboard_notification($type=''){
1683 /*
1684 * @param string ( affiliates || referrals )
1685 * @return none
1686 */
1687 global $wpdb;
1688 $table = $wpdb->prefix . 'jas_dashboard_notifications';
1689 $q = $wpdb->prepare("UPDATE $table SET value=value+1 WHERE type=%s ", $type);
1690 $wpdb->query($q);
1691 }
1692
1693
1694 public function save_affiliate($uid=0){
1695 /*
1696 * @param int
1697 * @return int
1698 */
1699
1700 $affiliate_id = 0;
1701 if ($uid){
1702 global $wpdb;
1703 $table = $wpdb->prefix . 'jas_affiliates';
1704 $q = $wpdb->prepare("SELECT * FROM $table WHERE uid=%d ;", $uid);
1705 $exists = $wpdb->get_row($q);
1706 if (!$exists){
1707 $rank_id = get_option('jas_register_new_user_rank');
1708 $q = $wpdb->prepare("INSERT INTO $table VALUES(NULL, %d, 0, NOW(), 1);", $uid);
1709 $wpdb->query($q);
1710 $affiliate_id = $wpdb->insert_id;
1711
1712 // SET DEFAULT PAYMENT SYSTEM
1713 $this->set_default_payment_on_register_affiliate($uid);
1714
1715 // INCREMENT DASHBOARD NOTIFICATION COUNT
1716 $this->increment_dashboard_notification('affiliates');
1717
1718 do_action('jas_save_affiliate_action', $uid, $affiliate_id);
1719 }
1720 }
1721 return $affiliate_id;
1722 }
1723
1724 public function set_rank_settings($affiliate_id){
1725 /*
1726 * @param none
1727 * @return array
1728 */
1729 $rank = $this->get_affiliate_rank($affiliate_id);
1730 if ($rank){
1731 $data = $this->get_rank($rank);
1732 return $data;
1733 }
1734 return array();
1735 }
1736
1737
1738 public function set_offers($affiliate_id){
1739 /*
1740 * @param none
1741 * @return array
1742 */
1743 global $jas_db;
1744 $data = $this->get_offer_id_by_affiliate_id_and_source(self::$affiliate_id, self::$source);
1745 return $data;
1746 }
1747
1748
1749 public function get_offer_id_by_affiliate_id_and_source($affiliate_id=0, $source=''){
1750 /*
1751 *
1752 * @param string
1753 * @return array
1754 */
1755 $arr = array();
1756 if ($affiliate_id){
1757 $affiliate_id = esc_sql($affiliate_id);
1758 $source = esc_sql($source);
1759 global $wpdb;
1760 $table = $wpdb->prefix . 'jas_offers_affiliates_reference';
1761 $table_offers = $wpdb->prefix . 'jas_offers';
1762 $data = $wpdb->get_results("SELECT * FROM $table d
1763 INNER JOIN $table_offers b
1764 ON d.offer_id = b.id
1765 WHERE
1766 1=1
1767 AND (d.affiliate_id='$affiliate_id' OR d.affiliate_id='-1')
1768 AND d.source='$source'
1769 AND UNIX_TIMESTAMP(b.start_date)<UNIX_TIMESTAMP(NOW())
1770 AND UNIX_TIMESTAMP(b.end_date)>UNIX_TIMESTAMP(NOW())
1771 AND b.status=1
1772 ORDER BY d.offer_id DESC;");
1773 if ($data){
1774 foreach ($data as $object){
1775 if (isset($object->offer_id)){
1776 $arr[$object->offer_id] = (array)$object;
1777 }
1778 }
1779 }
1780 }
1781 return $arr;
1782 }
1783
1784 protected function set_affiliate_id($user_id, $referred_affid){
1785 /*
1786 * @param none
1787 * @return none
1788 */
1789 self::$affiliate_id = $referred_affid;
1790 $lifetime = get_option('jas_lifetime_commissions_enable');
1791 if (empty(self::$affiliate_id) && empty($_COOKIE['jas_referral'])){ /// SEARCH INTO DB
1792 if ($lifetime){
1793 /// LIFETIME
1794 self::$affiliate_id = $this->search_affiliate_id_for_current_user($user_id);
1795 if (self::$affiliate_id){
1796 self::$special_payment_type = 'lifetime';
1797 }
1798 } else if (self::$special_payment_type=='reccuring'){
1799 /// RECCURING
1800 self::$affiliate_id = $this->search_affiliate_id_for_current_user($user_id);
1801 }
1802
1803 } else if (empty(self::$affiliate_id) && !empty($_COOKIE['jas_referral'])){ /// SEARCH INTO COOKIE
1804 /// get affiliate id from cookie
1805 $cookie_data = unserialize(stripslashes($_COOKIE['jas_referral']));
1806 if (!empty($cookie_data['affiliate_id'])){
1807 self::$affiliate_id = $cookie_data['affiliate_id'];
1808 self::$campaign = (empty($cookie_data['campaign'])) ? '' : $cookie_data['campaign'];
1809 self::$visit_id = (empty($cookie_data['visit_id'])) ? 0 : $cookie_data['visit_id'];
1810 }
1811 }
1812
1813 if (self::$affiliate_id){
1814 $old_affiliate = $this->search_affiliate_id_for_current_user($user_id);;
1815 if ($old_affiliate){
1816 $rewrite_referrals = get_option('jas_rewrite_referrals_enable');
1817 if ($rewrite_referrals){
1818 /// update user - affiliate relation, use new affiliate
1819 $this->update_affiliate_referral_user_relation_by_ids($old_affiliate, self::$affiliate_id, $user_id);
1820 } else {
1821 /// use old affiliate
1822 self::$affiliate_id = $old_affiliate;
1823 }
1824 } else {
1825 /// insert user - affiliate relation
1826 $this->insert_affiliate_referral_user_new_relation(self::$affiliate_id, $user_id);
1827 }
1828 }
1829
1830 }
1831
1832
1833
1834 public function create_referral($user_id, $referred_affid, $amount, $status, $transaction_id){
1835 /*
1836 * @param int (order id)
1837 * @return none
1838 */
1839
1840 $this->set_affiliate_id($user_id, $referred_affid);
1841
1842 //CHECKOUT REFERRAL SELECT
1843 $this->check_for_selected_affiliate($user_id);
1844 //CHECKOUT REFERRAL SELECT
1845
1846 if ($this->valid_referral($user_id)){
1847
1848 $temp_data = $this->return_settings_from_wp_option('general-settings');
1849 $sum = 0;
1850 // $affiliate_id = self::$affiliate_id;
1851 $affiliate_id = $referred_affid;
1852 $temp_amount = $this->get_result($amount, $affiliate_id);// input price
1853 $sum += $temp_amount;
1854
1855 // Bonus Type: USD/WiToken
1856 $affi_bonus_settings = get_option( 'aff_bonus_info' );
1857 $affi_bonus_type = $affi_bonus_settings['aff_bonus_type'];
1858 $aff_bonus_witoken = $affi_bonus_settings['aff_bonus_witoken']; // if checked exclude bonus witoken
1859 if( empty($affi_bonus_type) ){
1860 $affi_bonus_type = 'usd';
1861 }
1862
1863 $get_from_referral = $this->get_wp_username_by_user_id($user_id);
1864
1865 $args = array(
1866 'refferal_wp_uid' => $user_id,
1867 'campaign' => self::$campaign,
1868 // 'affiliate_id' => self::$affiliate_id,
1869 'affiliate_id' => $referred_affid,
1870 'visit_id' => self::$visit_id,
1871 'description' => 'From '.$get_from_referral,
1872 'source' => $this->source_type,
1873 'reference' => $user_id,
1874 'reference_details' => '',
1875 'bonus_type' => $affi_bonus_type,
1876 'amount' => $sum,
1877 'bonus_status' => $status,
1878 'currency' => self::$currency,
1879 'transaction_id' => $transaction_id
1880 );
1881 $this->save_referral_unverified($args);
1882
1883 // WiToken transfer to WiWallet
1884 if( $status == "approved" ){
1885
1886 if( $affi_bonus_type == "usd" ){
1887 $currency_type = 1;
1888 $transaction_details = "Affiliate bonus transfer to WiWallet";
1889 } else {
1890 $currency_type = 94;
1891 $transaction_details = "Affiliate WiToken bonus transfer to WiWallet";
1892 }
1893
1894 $setting_options = get_option( 'general_info' );
1895 $parent_affiliate_user_id = $this->get_uid_by_affiliate_id($referred_affid);
1896 $access_token = get_user_meta( $parent_affiliate_user_id, 'access_token', true );
1897 $user_info = get_userdata($parent_affiliate_user_id);
1898 $user_email = $user_info->user_email;
1899
1900 $ch = curl_init();
1901 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
1902 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
1903 'Content-Type' => 'application/json',
1904 'Authorization: Bearer '.$access_token
1905 ));
1906 curl_setopt($ch, CURLOPT_POST, 1);
1907 curl_setopt($ch, CURLOPT_POSTFIELDS,
1908 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".number_format($sum, $setting_options['gn_witoken_decimal'])."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
1909 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1910 $server_output = curl_exec($ch);
1911 curl_close ($ch);
1912 $json_data = json_decode($server_output);
1913
1914 if( $json_data->status == "success" ){
1915 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
1916 } else {
1917 $errors = $json_data->errors;
1918 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
1919 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
1920 }
1921
1922 } // End WiToken transfer to WiWallet
1923
1924 }
1925
1926 }
1927
1928
1929 public function update_affiliate_referral_user_relation_by_ids($old_affiliate=0, $new_affiliate=0, $referral_wp_id=0){
1930 /*
1931 * @param int, int, int
1932 * @return none
1933 */
1934 if ($old_affiliate && $new_affiliate && $referral_wp_id){
1935 global $wpdb;
1936 $table = $wpdb->prefix . 'jas_affiliate_referral_users_relations';
1937 $q = $wpdb->prepare("SELECT * FROM $table WHERE affiliate_id=%d AND referral_wp_uid=%d", $old_affiliate, $referral_wp_id);
1938 $data = $wpdb->get_row($q);
1939 if (!empty($data->id)){
1940 $q = $wpdb->prepare("UPDATE $table SET affiliate_id=%d WHERE id=%d ", $new_affiliate, $data->id);
1941 $wpdb->query($q);
1942 }
1943 }
1944 }
1945
1946 public function insert_affiliate_referral_user_new_relation($affiliate_id=0, $referral_wp_id=0){
1947 /*
1948 * @param int, int
1949 * @return none
1950 */
1951 if ($affiliate_id && $referral_wp_id){
1952 global $wpdb;
1953 $table = $wpdb->prefix . 'jas_affiliate_referral_users_relations';
1954 $q = $wpdb->prepare("SELECT * FROM $table WHERE affiliate_id=%d AND referral_wp_uid=%d", $affiliate_id, $referral_wp_id);
1955 $data = $wpdb->get_row($q);
1956 if (!$data){
1957 $q = $wpdb->prepare("INSERT INTO $table VALUES(NULL, %d, %d, NOW());", $affiliate_id, $referral_wp_id);
1958 $wpdb->query($q);
1959 }
1960 }
1961 }
1962
1963 public function search_affiliate_id_for_current_user($referral_wp_id=0){
1964 /*
1965 * @param int
1966 * @return int
1967 */
1968 if ($referral_wp_id){
1969 global $wpdb;
1970 $table = $wpdb->prefix . 'jas_affiliate_referral_users_relations';
1971 $q = $wpdb->prepare("SELECT affiliate_id FROM $table WHERE referral_wp_uid=%d;", $referral_wp_id);
1972 $data = $wpdb->get_row($q);
1973 if (!empty($data->affiliate_id)){
1974 return (int)$data->affiliate_id;
1975 }
1976 }
1977 return 0;
1978 }
1979
1980 public function check_for_selected_affiliate(){
1981 /*
1982 * @param none
1983 * @return none
1984 */
1985 if (empty(self::$checkout_referrals_select_settings)){
1986 self::$checkout_referrals_select_settings = $this->return_settings_from_wp_option('checkout_select_referral');
1987 }
1988 if (self::$checkout_referrals_select_settings['jas_checkout_select_referral_enable']){
1989 if (!empty($_POST['jas_affiliate_username'])){
1990 self::$affiliate_id = $_POST['jas_affiliate_username'];
1991 } else if (!empty($_POST['jas_affiliate_username_text'])){
1992 $temp = $this->get_affiliate_id_by_username($_POST['jas_affiliate_username_text']);
1993 if ($temp){
1994 self::$affiliate_id = $temp;
1995 }
1996 }
1997 }
1998
1999 }
2000
2001 public function get_affiliate_id_by_username($user_name=''){
2002 /*
2003 * @param string
2004 * @return int
2005 */
2006 if ($user_name){
2007 global $wpdb;
2008 $q = $wpdb->prepare("SELECT jas.id FROM " . $wpdb->prefix . "jas_affiliates jas
2009 INNER JOIN " . $wpdb->base_prefix . "users u
2010 ON jas.uid=u.ID
2011 WHERE u.user_login=%s ;", $user_name);
2012 $data = $wpdb->get_row($q);
2013 if (!empty($data->id)){
2014 return $data->id;
2015 }
2016 }
2017 return 0;
2018 }
2019
2020 public function valid_referral($user_id){
2021 /*
2022 * @param none
2023 * @return boolean
2024 */
2025 /// CHECK FOR OWN REFERRENCE
2026 if (self::$affiliate_id && $user_id && $this->affiliate_get_id_by_uid($user_id)==self::$affiliate_id){
2027 if (!get_option('jas_allow_own_referrence_enable')){
2028 return FALSE;//own referrence not allowed
2029 }
2030 }
2031 if (self::$affiliate_id && $this->is_affiliate_active(self::$affiliate_id)){
2032 return TRUE;
2033 }
2034 return FALSE;
2035 }
2036
2037 public function is_affiliate_active($id=0){
2038 /*
2039 * @param int
2040 * @return boolean
2041 */
2042 if ($id){
2043 global $wpdb;
2044 $table = $wpdb->prefix . 'jas_affiliates';
2045 $q = $wpdb->prepare("SELECT status FROM $table WHERE id=%d ;", $id);
2046 $data = $wpdb->get_row($q);
2047 if (!empty($data->status)){
2048 return TRUE;
2049 }
2050 }
2051 return FALSE;
2052 }
2053
2054
2055 protected function save_referral_unverified($args=array()){
2056 /*
2057 * UNVERIFIED STATUS
2058 * @param array
2059 * @return boolean
2060 */
2061 $keys = array(
2062 'refferal_wp_uid',
2063 'campaign',
2064 'affiliate_id',
2065 'visit_id',
2066 'description',
2067 'source',
2068 'reference',
2069 'reference_details',
2070 'bonus_type',
2071 'amount',
2072 'bonus_status',
2073 'currency',
2074 'transaction_id',
2075 );
2076
2077 foreach ($keys as $key){
2078 if (!isset($args[$key])){
2079 return FALSE;
2080 }
2081 }
2082
2083 /// NEGATIVE REFERRALS?
2084 if ($args['amount']<0){
2085 $args['amount'] = 0;
2086 }
2087
2088 // EMPTY REFERRALS
2089 $general_settings_data = $this->return_settings_from_wp_option('general-settings');
2090 if (empty($general_settings_data['jas_empty_referrals_enable'])){
2091 ///don't insert referrals with 0$
2092 $min = 0.01;
2093 if ($args['amount']<$min){
2094 return;
2095 }
2096 }
2097 /// EMPTY REFERRALS
2098
2099 $args['date'] = date('Y-m-d H:i:s', time());
2100 $args['status'] = 1;//unverified
2101 $args['payment'] = 0;//unpaid
2102 $args['parent_referral_id'] = '';// empty for moment, will be updated if it's case
2103 $args['child_referral_id'] = '';//always will be empty
2104 $referral_id = $this->save_referral($args);
2105 if ($referral_id){
2106 $this->update_visit_referral_id($args['visit_id'], $referral_id);
2107
2108 if (get_option('jas_mlm_enable')){
2109 $limit = get_option('jas_mlm_matrix_depth');
2110 $first_child_username = $this->get_wp_username_by_affiliate_id($args['affiliate_id']);
2111
2112 $theAmount = $args['amount'];
2113 $jas_mlm_use_amount_from = get_option('jas_mlm_use_amount_from');
2114 if ($jas_mlm_use_amount_from && $jas_mlm_use_amount_from=='product_price' && isset($args['product_price'])){
2115 $theAmount = $args['product_price'];
2116 }
2117
2118 $this->mlm_do_save_referral_unverified($args['affiliate_id'], $referral_id, 1, $limit, $args['bonus_type'], $theAmount, $args['bonus_status'], $first_child_username, $referral_id, $args['transaction_id']);
2119
2120 }
2121 }
2122 return TRUE;
2123 }
2124
2125 public function update_visit_referral_id($visit_id=0, $referral_id=0){
2126 /*
2127 * @param int
2128 * @return none
2129 */
2130 if ($visit_id && $referral_id){
2131 global $wpdb;
2132 $table = $wpdb->prefix . 'jas_visits';
2133 $q = $wpdb->prepare("SELECT * FROM $table WHERE id=%d ", $visit_id);
2134 $exists = $wpdb->get_row($q);
2135 if ($exists){
2136 $q = $wpdb->prepare("UPDATE $table
2137 SET referral_id=%d
2138 WHERE id=%d ", $referral_id, $visit_id);
2139 $wpdb->query($q);
2140 }
2141 }
2142 }
2143
2144 public function get_wp_username_by_affiliate_id($affiliate_id=0){
2145 /*
2146 * @param string
2147 * @return int
2148 */
2149 if ($affiliate_id){
2150 global $wpdb;
2151 $q = $wpdb->prepare("SELECT u.user_login
2152 FROM " . $wpdb->prefix . "jas_affiliates jas
2153 INNER JOIN " . $wpdb->base_prefix . "users u
2154 ON jas.uid=u.ID
2155 WHERE jas.id=%d;
2156 ", $affiliate_id);
2157 $data = $wpdb->get_row($q);
2158 if (!empty($data->user_login)){
2159 return $data->user_login;
2160 }
2161 }
2162 return '';
2163 }
2164
2165 public function get_wp_username_by_user_id($user_id){
2166 /*
2167 * @param string
2168 * @return int
2169 */
2170 if ($user_id){
2171 global $wpdb;
2172 $q = $wpdb->prepare("SELECT u.user_login
2173 FROM " . $wpdb->prefix . "jas_affiliates jas
2174 INNER JOIN " . $wpdb->base_prefix . "users u
2175 ON jas.uid=u.ID
2176 WHERE jas.uid=%d;
2177 ", $user_id);
2178 $data = $wpdb->get_row($q);
2179 if (!empty($data->user_login)){
2180 return $data->user_login;
2181 }
2182 }
2183 return '';
2184 }
2185
2186
2187 protected function mlm_do_save_referral_unverified($child_affiliate_id=0, $child_referral_id=0, $count=1, $limit=0, $bonus_type, $amount=0, $bonus_status, $first_child_username='', $first_child_referrence='', $transaction_id){
2188 /*
2189 * @param int, int, int, int, int, string, string
2190 * @return none
2191 */
2192 /// CHECK LIMIT DEPTH
2193 if ($limit<$count){
2194 return;
2195 }
2196 if ($child_affiliate_id && $child_referral_id){
2197 global $jas_db;
2198 $parent_id = $this->mlm_get_parent($child_affiliate_id);
2199 $description = 'From MLM';
2200 if (!empty($first_child_username)){
2201 $description = 'From ' . $first_child_username;
2202 }
2203 $reference = '-';
2204 if (!empty($first_child_referrence)){
2205 $reference = 'mlm_' . $first_child_referrence;
2206 }
2207
2208 if ($parent_id){
2209 $args = array(
2210 'refferal_wp_uid' => '-',
2211 'campaign' => '-',
2212 'affiliate_id' => $parent_id,
2213 'visit_id' => '-',
2214 'description' => $description,
2215 'source' => 'mlm',
2216 'reference' => $reference,
2217 'reference_details' => '-',
2218 'parent_referral_id' => '',//will be updated if it;s case
2219 'child_referral_id' => $child_referral_id,
2220 );
2221 $args['date'] = date('Y-m-d H:i:s', time());
2222 $args['status'] = 1;//unverified
2223 $args['payment'] = 0;//unpaid
2224
2225 $affi_bonus_settings = get_option( 'aff_bonus_info' );
2226 $affi_bonus_type = $affi_bonus_settings['aff_bonus_type'];
2227 $aff_bonus_witoken = $affi_bonus_settings['aff_bonus_witoken']; // if checked exclude bonus witoken
2228 if( empty($bonus_type) ){
2229
2230 if( empty($affi_bonus_type) ){
2231 $bonus_type = 'usd';
2232 } else {
2233 $bonus_type = $affi_bonus_type;
2234 }
2235 }
2236 $args['bonus_type'] = $bonus_type;
2237
2238 /// SET AMOUNT
2239 $args['amount'] = $this->mlm_get_amount($parent_id, $amount, $count);
2240 $args['bonus_status'] = $bonus_status;
2241 $args['currency'] = self::$currency;
2242
2243 $args['transaction_id'] = $transaction_id;
2244
2245 /// save referral
2246 $inserted_referral_id = $this->save_referral($args);
2247
2248 // //update the child referral
2249 $this->referral_update_child($child_referral_id, $inserted_referral_id);
2250
2251
2252 // WiToken transfer to WiWallet
2253 if( $bonus_status == "approved" ){
2254
2255 if( $bonus_type == "usd" ){
2256 $currency_type = 1;
2257 $transaction_details = "Affiliate mlm bonus transfer to WiWallet";
2258 } else {
2259 $currency_type = 94;
2260 $transaction_details = "Affiliate mlm WiToken bonus transfer to WiWallet";
2261 }
2262
2263 $setting_options = get_option( 'general_info' );
2264 $parent_affiliate_user_id = $this->get_uid_by_affiliate_id($parent_id);
2265 $access_token = get_user_meta( $parent_affiliate_user_id, 'access_token', true );
2266 $user_info = get_userdata($parent_affiliate_user_id);
2267 $user_email = $user_info->user_email;
2268
2269 $ch = curl_init();
2270 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
2271 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
2272 'Content-Type' => 'application/json',
2273 'Authorization: Bearer '.$access_token
2274 ));
2275 curl_setopt($ch, CURLOPT_POST, 1);
2276 curl_setopt($ch, CURLOPT_POSTFIELDS,
2277 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".number_format($args['amount'], $setting_options['gn_witoken_decimal'])."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
2278 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2279 $server_output = curl_exec($ch);
2280 curl_close ($ch);
2281 $json_data = json_decode($server_output);
2282
2283 if( $json_data->status == "success" ){
2284 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
2285 } else {
2286 $errors = $json_data->errors;
2287 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
2288 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
2289 }
2290
2291 } // End WiToken transfer to WiWallet
2292
2293 // /// search for parent
2294 $count++;
2295 $this->mlm_do_save_referral_unverified($parent_id, $inserted_referral_id, $count, $limit, $bonus_type, $amount, $bonus_status, $first_child_username, $first_child_referrence, $transaction_id);
2296 }
2297 }
2298 }
2299
2300 public function referral_update_child($child_referral_id=0, $parent_referral_id=0){
2301 /*
2302 * @param int, int
2303 * @return none
2304 */
2305 if ($child_referral_id && $parent_referral_id){
2306 global $wpdb;
2307 $table = $wpdb->prefix . "jas_referrals";
2308 $q = $wpdb->prepare("UPDATE $table SET parent_referral_id=%d WHERE id=%d;", $parent_referral_id, $child_referral_id);
2309 $wpdb->query($q);
2310 }
2311 }
2312
2313 public function mlm_get_parent($child=0){
2314 /*
2315 * @param int
2316 * @return int
2317 */
2318 if ($child){
2319 global $wpdb;
2320 $table = $wpdb->prefix . 'jas_mlm_relations';
2321 $q = $wpdb->prepare("SELECT parent_affiliate_id FROM $table WHERE affiliate_id=%d", $child);
2322 $data = $wpdb->get_row($q);
2323 if (!empty($data->parent_affiliate_id)){
2324 /// check if affiliate user exists
2325 $uid = $this->get_uid_by_affiliate_id($data->parent_affiliate_id);
2326 if ($uid){
2327 $table = $wpdb->base_prefix . 'users';
2328 $q = $wpdb->prepare("SELECT * FROM $table WHERE ID=%d ", $uid);
2329 $inside_data = $wpdb->get_row($q);
2330 if (!empty($inside_data->ID)){
2331 return $data->parent_affiliate_id;
2332 }
2333 }
2334 }
2335 }
2336 return 0;
2337 }
2338
2339
2340 public function mlm_get_amount($affiliate_id=0, $child_amount=0, $level=1){
2341 /*
2342 * @param int, int
2343 * @return int
2344 */
2345 /// DEFAULT AMOUNT
2346 $amount = get_option('jas_mlm_default_amount_value');
2347 $amount_type = get_option('jas_mlm_default_amount_type');
2348
2349 /// AMOUNT TYPE & VALUE BY MLM LEVEL
2350 if (empty(self::$mlm_amount_value_per_level)){
2351 self::$mlm_amount_value_per_level = get_option('mlm_amount_value_per_level');
2352 }
2353 if (empty(self::$mlm_amout_type_per_level)){
2354 self::$mlm_amout_type_per_level = get_option('mlm_amount_type_per_level');
2355 }
2356 /// AMOUNT BY MLM LEVEL
2357 if (!empty(self::$mlm_amount_value_per_level) && !empty(self::$mlm_amount_value_per_level[$level])){
2358 $amount = self::$mlm_amount_value_per_level[$level];
2359 $amount_type = self::$mlm_amout_type_per_level[$level];
2360 }
2361
2362 /// AMOUNT TYPE & VALUE BY AFFILIATE RANK
2363 if ($affiliate_id){
2364 global $wpdb;
2365 $rank = $this->get_affiliate_rank($affiliate_id);
2366 $table = $wpdb->prefix . 'jas_ranks';
2367 $q = $wpdb->prepare("SELECT mlm_amount_type, mlm_amount_value FROM $table WHERE id=%d", $rank);
2368 $data = $wpdb->get_row($q);
2369 if (!empty($data->mlm_amount_type) && !empty($data->mlm_amount_value)){
2370 @$data->mlm_amount_type = unserialize($data->mlm_amount_type);
2371 @$data->mlm_amount_value = unserialize($data->mlm_amount_value);
2372 if (isset($data->mlm_amount_value) && !empty($data->mlm_amount_value[$level]) && !empty($data->mlm_amount_type) && !empty($data->mlm_amount_type[$level])){
2373 $amount = $data->mlm_amount_value[$level];
2374 $amount_type = $data->mlm_amount_type[$level];
2375 }
2376 }
2377 }
2378
2379 if ($amount_type=='flat'){
2380 return $amount;
2381 } else {
2382 return $amount * $child_amount / 100;
2383 }
2384 }
2385
2386 public function set_output_amount_by_offer($affiliate_id){
2387 /*
2388 * @param none
2389 * @return none
2390 */
2391 self::$offer_array = $this->set_offers($affiliate_id);
2392 if (self::$offer_array && is_array(self::$offer_array) && count(self::$offer_array)>0){
2393 foreach (self::$offer_array as $offer_array){
2394 if (!empty($offer_array['products'])){
2395 $only_products = explode(',', $offer_array['products']);
2396 } else {
2397 continue;
2398 }
2399
2400 if ( !in_array($this->product_id, $only_products)){
2401 continue;
2402 }
2403 if ($offer_array['amount_type']=='percentage'){
2404 //rank percentage
2405 $value = $offer_array['amount_value'] * $this->input_amount / 100;
2406 } else {
2407 //offer flat
2408 $value = $offer_array['amount_value'];
2409 }
2410 if (isset($output_value)){
2411 if (self::$rule=='biggest'){
2412 $output_value = max($value, $output_value);
2413 } else {
2414 $output_value = min($value, $output_value);
2415 }
2416 } else {
2417 $output_value = $value;
2418 }
2419 }//end foreach
2420 if (isset($output_value)){
2421 $this->offer_output_amount = $output_value;
2422 }
2423 //$this->offer_output_amount = (isset($output_value)) ? $output_value : 0;
2424 }
2425 }
2426
2427 public function set_output_amount_by_rank($affiliate_id){
2428 /*
2429 * @param none
2430 * @return none
2431 */
2432
2433 self::$rank_settings = $this->set_rank_settings($affiliate_id);
2434
2435 if (self::$rank_settings){
2436 if (self::$special_payment_type=='lifetime'){
2437 /// LIFETIME
2438 if (isset(self::$rank_settings['lifetime_amount_value']) && self::$rank_settings['lifetime_amount_value']>=0){
2439 $amount_value = self::$rank_settings['lifetime_amount_value'];
2440 $amount_type = self::$rank_settings['lifetime_amount_type'];
2441 }
2442 } else if (self::$special_payment_type=='reccuring'){
2443 /// RECCURING
2444 if (isset(self::$rank_settings['reccuring_amount_value']) && self::$rank_settings['reccuring_amount_value']>=0){
2445 $amount_value = self::$rank_settings['reccuring_amount_value'];
2446 $amount_type = self::$rank_settings['reccuring_amount_type'];
2447 }
2448 }
2449
2450 /// DEFAULT RANK VALUE
2451 if (empty($amount_value)){
2452 $amount_value = self::$rank_settings['amount_value'];
2453 $amount_type = self::$rank_settings['amount_type'];
2454 }
2455
2456 if ($amount_type=='percentage'){
2457 //rank percentage
2458 $this->rank_output_amount = $amount_value * $this->input_amount / 100;
2459 } else {
2460 //rank flat
2461 $this->rank_output_amount = $amount_value;
2462 }
2463 }
2464
2465 }
2466
2467
2468 public function get_result($input_amount=0, $affiliate_id){
2469 /*
2470 * @param float, string
2471 * @return float
2472 */
2473 $this->input_amount = $input_amount;
2474
2475 $this->set_output_amount_by_rank($affiliate_id);
2476 $this->set_output_amount_by_offer($affiliate_id);
2477
2478 if ($this->rank_output_amount!==FALSE && $this->offer_output_amount!==FALSE){
2479 /// compare values
2480 if (self::$rule=='biggest'){
2481 $return_value = max($this->rank_output_amount, $this->offer_output_amount);
2482 $return_value = round($return_value, 3);
2483 } else {
2484 $return_value = min($this->rank_output_amount, $this->offer_output_amount);
2485 $return_value = round($return_value, 3);
2486 }
2487 return $return_value;
2488 } else if ($this->rank_output_amount!==FALSE){
2489 /// rank value
2490 return $this->rank_output_amount;
2491 } else if ($this->offer_output_amount!==FALSE){
2492 /// offer value
2493 return $this->offer_output_amount;
2494 }
2495 return 0;
2496 }
2497
2498 public function get_custom_slug_for_uid($uid=0){
2499 /*
2500 * @param int
2501 * @return string
2502 */
2503 if ($uid){
2504 $data = get_user_meta($uid, 'jas_affiliate_custom_slug', TRUE);
2505 if ($data){
2506 return $data;
2507 }
2508 }
2509 return '';
2510 }
2511
2512 public function update_affiliate_rank_for_package($user_id, $package_id){
2513
2514 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
2515 if( $affiliate_id !=0 ){
2516
2517 //Package rank id & rank order
2518 $rank_id = get_post_meta( $package_id, 'bounty_affiliate', true);
2519 $package_rank = $this->get_rank($rank_id);
2520 $package_rank_order = $package_rank['rank_order'];
2521
2522 //Retrive user present rank order and compare with package rank order
2523 $users_rank_id = $this->get_affiliate_rank($affiliate_id);
2524 $users_rank = $this->get_rank($users_rank_id);
2525 $users_rank_order = $users_rank['rank_order'];
2526
2527 //check user's rank order small than package rank order
2528 if( $users_rank_order < $package_rank_order){
2529 $this->update_affiliate_rank_by_uid($user_id, $rank_id);
2530 }
2531
2532 }
2533
2534 }
2535
2536
2537 /*****************************************
2538 *
2539 * END ALL METHODS FOR AFFILIATE
2540 *
2541 ******************************************/
2542
2543
2544 public function read_langauge_file( WP_REST_Request $request ){
2545
2546
2547 $lang = $request->get_params( 'lang' );
2548 $lang = $lang['lang'];
2549 if( $lang == "en" ){
2550 $file = file_get_contents('/home/witoken/public_html/wp-content/plugins/jl-token-helper/admin/partial/i18n/'.$lang.'.json');
2551 } else if( $lang == "cn" ){
2552 $file = file_get_contents('/home/witoken/public_html/wp-content/plugins/jl-token-helper/admin/partials/i18n/'.$lang.'.json');
2553 } else if( $lang == "ar" ){
2554 $file = file_get_contents('/home/witoken/public_html/wp-content/plugins/jl-token-helper/admin/partials/i18n/'.$lang.'.json');
2555 } else if( $lang == "my" ){
2556 $file = file_get_contents('/home/witoken/public_html/wp-content/plugins/jl-token-helper/admin/partials/i18n/'.$lang.'.json');
2557 } else {
2558 $file = file_get_contents('/home/witoken/public_html/wp-content/plugins/jl-token-helper/admin/partials/i18n/'.$lang.'.json');
2559 }
2560
2561 return $file;
2562
2563 }
2564
2565
2566 public function get_menu( WP_REST_Request $request ){
2567
2568 $lang = $request->get_params( 'lang' );
2569 $lang = $lang['lang'];
2570 if ($lang == "en") {
2571 $menuid = 34;
2572 }
2573 else
2574 if ($lang == "cn") {
2575 $menuid = 35;
2576 }
2577 else
2578 if ($lang == "ar") {
2579 $menuid = 36;
2580 }
2581 else
2582 if ($lang == "my") {
2583 $menuid = 37;
2584 }
2585 else {
2586 $menuid = 34;
2587 }
2588
2589 $menuLocations = get_nav_menu_locations();
2590 $items = wp_get_nav_menu_items($menuid);
2591 $tmp = [];
2592 $s = 0;
2593 foreach($items as $key => $item) {
2594 $tmp[$s] = ['serial' => $s, 'id' => $item->ID, 'menu_item_parent' => $item->menu_item_parent, 'name' => strip_tags($item->title) , 'iconClass' => get_post_meta($item->ID, 'menu-item-app-icon', true) , 'type' => ($item->type_label == 'Page') ? 'page' : get_post_meta($item->ID, 'menu-item-app-type', true) , 'url' => ($item->type_label == 'Page') ? $item->object_id : get_post_meta($item->ID, 'menu-item-app-url', true) ];
2595 $s++;
2596 }
2597
2598 $menus = $items ? $this->buildTree($tmp, 0) : null;
2599 return array(
2600 "menu" => $menus
2601 );
2602
2603 }
2604
2605
2606 public function buildTree(array & $elements, $parentId = 0)
2607 {
2608 $branch = array();
2609 foreach($elements as & $element) {
2610 if ($element['menu_item_parent'] == $parentId) {
2611 $children = $this->buildTree($elements, $element['id']);
2612 if ($children) {
2613 $element['items'] = $children;
2614 }
2615 else {
2616 $element['items'] = "";
2617 }
2618
2619 $branch[$element['serial']] = $element;
2620 unset($element);
2621 }
2622 }
2623
2624 return $branch;
2625 }
2626
2627
2628
2629
2630 /**
2631 *
2632 * @since 1.0.0
2633 */
2634 public function update_avatar_from_app( WP_REST_Request $request ){
2635
2636 $post = (array) $request->get_params();
2637 $files = $_FILES['avatar'];
2638 $userid = $post['user_id'];
2639
2640 $old_avatar_id = get_user_meta( $userid, 'avatar_id', true);
2641
2642 if( !empty($old_avatar_id) ){
2643 wp_delete_attachment( $old_avatar_id );
2644 }
2645
2646 require_once( ABSPATH . 'wp-admin/includes/file.php' );
2647 $upload_overrides = array( 'test_form' => false );
2648 $movefile = wp_handle_upload( $files, $upload_overrides);
2649
2650 $upload_dir = wp_upload_dir();
2651 $image_data = file_get_contents($movefile['url']);
2652 $filename = basename($movefile['url']);
2653 if(wp_mkdir_p($upload_dir['path'])){
2654 $file = $upload_dir['path'] . '/' . $filename;
2655 } else {
2656 $file = $upload_dir['basedir'] . '/' . $filename;
2657 }
2658 file_put_contents($file, $image_data);
2659
2660 $wp_filetype = wp_check_filetype($filename, null );
2661 $attachment = array(
2662 'post_mime_type' => $wp_filetype['type'],
2663 'post_title' => sanitize_file_name($filename),
2664 'post_content' => '',
2665 'post_status' => 'inherit'
2666 );
2667 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
2668 require_once(ABSPATH . 'wp-admin/includes/image.php');
2669 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
2670 wp_update_attachment_metadata( $attach_id, $attach_data );
2671
2672 update_user_meta($userid, 'avatar_id', $attach_id);
2673 return $avatar = wp_get_attachment_url( get_user_meta($userid, 'avatar_id', true) );
2674
2675 }
2676
2677 /**
2678 *
2679 * @since 1.0.0
2680 */
2681 public function update_nid_from_app( WP_REST_Request $request ){
2682
2683 $post = (array) $request->get_params();
2684 $files = $_FILES['nid'];
2685 $userid = $post['user_id'];
2686
2687 $old_avatar_id = get_user_meta( $userid, 'nid', true);
2688
2689 if( !empty($old_avatar_id) ){
2690 wp_delete_attachment( $old_avatar_id );
2691 }
2692
2693 require_once( ABSPATH . 'wp-admin/includes/file.php' );
2694 $upload_overrides = array( 'test_form' => false );
2695 $movefile = wp_handle_upload( $files, $upload_overrides);
2696
2697 $upload_dir = wp_upload_dir();
2698 $image_data = file_get_contents($movefile['url']);
2699 $filename = basename($movefile['url']);
2700 if(wp_mkdir_p($upload_dir['path'])){
2701 $file = $upload_dir['path'] . '/' . $filename;
2702 } else {
2703 $file = $upload_dir['basedir'] . '/' . $filename;
2704 }
2705 file_put_contents($file, $image_data);
2706
2707 $wp_filetype = wp_check_filetype($filename, null );
2708 $attachment = array(
2709 'post_mime_type' => $wp_filetype['type'],
2710 'post_title' => sanitize_file_name($filename),
2711 'post_content' => '',
2712 'post_status' => 'inherit'
2713 );
2714 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
2715 require_once(ABSPATH . 'wp-admin/includes/image.php');
2716 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
2717 wp_update_attachment_metadata( $attach_id, $attach_data );
2718
2719 update_user_meta($userid, 'nid', $attach_id);
2720 return $avatar = wp_get_attachment_url( get_user_meta($userid, 'nid', true) );
2721
2722 }
2723
2724
2725
2726 /**
2727 *
2728 * @since 1.0.0
2729 */
2730 public function get_settings_info( WP_REST_Request $request ){
2731
2732 $general_info = get_option( 'general_info' );
2733 $paypal_info = get_option( 'paypal_info' );
2734 $stripe_info = get_option( 'stripe_info' );
2735 $bitcoin_info = get_option( 'bitcoin_info' );
2736 $eth_info = get_option( 'ethereum_info' );
2737 $bank_info = get_option( 'bank_info' );
2738 $fomo_info = get_option( 'fomo_info' );
2739 $wiwallet_info = get_option( 'wiwallet_info' );
2740 $smart2pay_info = get_option( 'smart2pay_info' );
2741
2742 $params = $request->get_params();
2743 $lang = $params['lang'];
2744 $access_token = $params['access_token'];
2745 $device_id = $params['device_id'];
2746
2747 $data = array();
2748 if( $lang == "ar" ){
2749 $data['video'] = $general_info['gn_intro_video_ar']; //intro video
2750 $data['gn_btc_eth_notice'] = $general_info['gn_btc_eth_notice_ar']; // btc_eth_notice
2751 $data['gn_btc_eth_bank_notice'] = $general_info['gn_btc_eth_bank_notice_ar']; // btc_eth_notice
2752 $data['gn_disable_token_sale_notice'] = $general_info['gn_disable_token_sale_ar'];
2753
2754 $data['paypal_title'] = $paypal_info['paypl_title_ar'];
2755 $data['stripe_title'] = $stripe_info['strp_title_ar'];
2756 $data['btc_title'] = $bitcoin_info['btc_title_ar'];
2757 $data['eth_title'] = $eth_info['eth_title_ar'];
2758 $data['bt_title'] = $bank_info['bt_title_ar'];
2759 $data['fomo_title'] = $fomo_info['fomo_method_title_ar'];
2760 $data['wiwallet_title'] = $wiwallet_info['wiwallet_title_ar'];
2761 $data['smart2pay_title'] = $smart2pay_info['smart2pay_title_ar'];
2762 $data['gn_witoken_offer_notice']= $general_info['gn_witoken_offer_notice_ar'];
2763
2764 } else if( $lang == "cn" ){
2765 $data['video'] = $general_info['gn_intro_video_cn']; //intro video
2766 $data['gn_btc_eth_notice'] = $general_info['gn_btc_eth_notice_cn']; // btc_eth_notice
2767 $data['gn_btc_eth_bank_notice'] = $general_info['gn_btc_eth_bank_notice_cn']; // btc_eth_notice
2768 $data['gn_disable_token_sale_notice'] = $general_info['gn_disable_token_sale_cn'];
2769
2770 $data['paypal_title'] = $paypal_info['paypl_title_cn'];
2771 $data['stripe_title'] = $stripe_info['strp_title_cn'];
2772 $data['btc_title'] = $bitcoin_info['btc_title_cn'];
2773 $data['eth_title'] = $eth_info['eth_title_cn'];
2774 $data['bt_title'] = $bank_info['bt_title_cn'];
2775 $data['fomo_title'] = $fomo_info['fomo_method_title_cn'];
2776 $data['wiwallet_title'] = $wiwallet_info['wiwallet_title_cn'];
2777 $data['smart2pay_title'] = $smart2pay_info['smart2pay_title_cn'];
2778 $data['gn_witoken_offer_notice']= $general_info['gn_witoken_offer_notice_cn'];
2779
2780 } else if($lang == "en" ){
2781 $data['video'] = $general_info['gn_intro_video']; //intro video
2782 $data['gn_btc_eth_notice'] = $general_info['gn_btc_eth_notice']; // btc_eth_notice
2783 $data['gn_btc_eth_bank_notice'] = $general_info['gn_btc_eth_bank_notice']; // btc_eth_notice
2784 $data['gn_disable_token_sale_notice'] = $general_info['gn_disable_token_sale_en'];
2785 $data['gn_witoken_offer_notice']= $general_info['gn_witoken_offer_notice'];
2786
2787 $data['paypal_title'] = $paypal_info['paypl_title'];
2788 $data['stripe_title'] = $stripe_info['strp_title'];
2789 $data['btc_title'] = $bitcoin_info['btc_title'];
2790 $data['eth_title'] = $eth_info['eth_title'];
2791 $data['bt_title'] = $bank_info['bt_title'];
2792 $data['fomo_title'] = $fomo_info['fomo_method_title'];
2793 $data['wiwallet_title'] = $wiwallet_info['wiwallet_title'];
2794 $data['smart2pay_title'] = $smart2pay_info['smart2pay_title'];
2795
2796 } else if( $lang == "my" ) {
2797 $data['video'] = $general_info['gn_intro_video_my']; //intro video
2798 $data['gn_btc_eth_notice'] = $general_info['gn_btc_eth_notice_my']; // btc_eth_notice
2799 $data['gn_btc_eth_bank_notice'] = $general_info['gn_btc_eth_bank_notice_my']; // btc_eth_notice
2800 $data['gn_disable_token_sale_notice'] = $general_info['gn_disable_token_sale_my'];
2801 $data['gn_witoken_offer_notice']= $general_info['gn_witoken_offer_notice_my'];
2802
2803 $data['paypal_title'] = $paypal_info['paypl_title_my'];
2804 $data['stripe_title'] = $stripe_info['strp_title_my'];
2805 $data['btc_title'] = $bitcoin_info['btc_title_my'];
2806 $data['eth_title'] = $eth_info['eth_title_my'];
2807 $data['bt_title'] = $bank_info['bt_title_my'];
2808 $data['fomo_title'] = $fomo_info['fomo_method_title_my'];
2809 $data['wiwallet_title'] = $wiwallet_info['wiwallet_title_my'];
2810 $data['smart2pay_title'] = $smart2pay_info['smart2pay_title_my'];
2811
2812 } else {
2813 $data['video'] = $general_info['gn_intro_video']; //intro video
2814 $data['gn_btc_eth_notice'] = $general_info['gn_btc_eth_notice']; // btc_eth_notice
2815 $data['gn_btc_eth_bank_notice'] = $general_info['gn_btc_eth_bank_notice']; // btc_eth_notice
2816 $data['gn_disable_token_sale_notice'] = $general_info['gn_disable_token_sale_en'];
2817 }
2818
2819 $data['payment_purpose'] = $general_info['gn_payment_purpose']; // pricing rate
2820 $data['pricing_rate'] = $general_info['gn_pricing_rate']; // pricing rate
2821 $data['witoken_after_decimal'] = $general_info['gn_witoken_decimal']; // pricing rate
2822 $data['btc_allow_difference'] = $general_info['gn_btc_allow_difference']; // pricing rate
2823 $data['eth_allow_difference'] = $general_info['gn_eth_allow_difference']; // pricing rate
2824
2825 $data['paypal_enable'] = $paypal_info['paypl_enable'];
2826 $data['paypal_email'] = $paypal_info['paypl_email']; // paypal email
2827 $data['paypal_sandbox'] = $paypal_info['paypl_sandbox_key']; // paypal sandbox
2828 $data['paypal_live'] = $paypal_info['paypl_production_key']; // paypal live
2829
2830 $data['stripe_enable'] = $stripe_info['strp_enable'];
2831 $data['stripe_key'] = $stripe_info['strp_key']; // stripe key
2832 $data['lang'] = $lang; // lang
2833
2834 $data['btc_enable'] = $bitcoin_info['btc_enable'];
2835 $data['btc_address'] = $bitcoin_info['btc_address']; // Bitcoin Address
2836 $data['btc_desc'] = $bitcoin_info['btc_description']; // Bitcoin Description
2837 $data['btc_percentage'] = $bitcoin_info['btc_percentage']; // Bitcoin Percentage
2838
2839 $data['eth_enable'] = $eth_info['eth_enable'];
2840 $data['eth_address'] = $eth_info['eth_address']; // Ethereum Address
2841 $data['eth_desc'] = $eth_info['eth_description']; // Ethereum Description
2842 $data['eth_percentage'] = $eth_info['eth_percentage']; // Ethereum Percentage
2843
2844 $data['bt_enable'] = $bank_info['bt_enable'];
2845 $data['bt_acc_no'] = $bank_info['bt_acc_no']; // Bank Account No
2846 $data['bt_name'] = $bank_info['bt_name']; // Bank Name
2847 $data['bt_swift_code'] = $bank_info['bt_swift_code']; // Bank Swift Code
2848 $data['bt_benefic_name'] = $bank_info['bt_beneficiary_name']; // Bank Beneficiary Name
2849 $data['bt_address'] = $bank_info['bt_address']; // Bank Beneficiary Address
2850
2851 $data['fomo_enable_fomo'] = $fomo_info['fomo_enable_fomo'];
2852 $data['fomo_enable_sandbox'] = $fomo_info['fomo_enable_sandbox'];
2853 $data['fomo_sand_api_username'] = $fomo_info['fomo_sand_api_username'];
2854 $data['fomo_sand_api_key'] = $fomo_info['fomo_sand_api_key'];
2855 $data['fomo_sandbox_url'] = $fomo_info['fomo_sandbox_url'];
2856 $data['fomo_live_url'] = $fomo_info['fomo_live_url'];
2857 $data['fomo_api_key'] = $fomo_info['fomo_api_key'];
2858 $data['fomo_merchant'] = $fomo_info['fomo_merchant'];
2859 $data['fomo_timeout'] = $fomo_info['fomo_timeout'];
2860 $data['wiwallet_enable'] = $wiwallet_info['wiwallet_enable'];
2861 $data['smart2pay_enable'] = $smart2pay_info['smart2pay_enable'];
2862 $data['smart2pay_enable_sandbox']= $smart2pay_info['smart2pay_enable_sandbox'];
2863 $data['smart2pay_api'] = $smart2pay_info['smart2pay_api'];
2864 $data['smart2pay_site_id'] = $smart2pay_info['smart2pay_site_id'];
2865
2866 $get_witoken_stage_info = $this->token_sale_bonus_heading_info();
2867 $data['amount_info'] = $get_witoken_stage_info['amount_info'];
2868 $data['bonus_percentage'] = $get_witoken_stage_info['bonus_percentage'];
2869 $data['bonus_type'] = $get_witoken_stage_info['bonus_type'];
2870 $data['start_date'] = $get_witoken_stage_info['date'];
2871 $data['isAvailable'] = $get_witoken_stage_info['isAvailable'];
2872 $data['stage'] = $get_witoken_stage_info['stage'];
2873 $data['phase_or_stage'] = $get_witoken_stage_info['phase_or_stage'];
2874 $data['term_slug'] = $get_witoken_stage_info['term_slug'];
2875 $data['upcomming'] = $get_witoken_stage_info['upcomming'];
2876 $data['disable_token_sale'] = $general_info['gn_disable_token_sale'];
2877 $witoken_package_settings = get_option( 'witoken_package_settings' );
2878 $data['disable_witoken_package']= $witoken_package_settings['gn_disable_witoken_package'];
2879 $data['package_availability'] = $this->get_package_availability();
2880 $data['available_packages'] = $this->get_available_packages();
2881
2882 $ch = curl_init();
2883 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/auth/wallets/usd?client_id=3&device_id=".$device_id."");
2884 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
2885 'Content-Type' => 'application/json',
2886 'Authorization: Bearer '.$access_token
2887 ));
2888 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2889 $server_output = curl_exec($ch);
2890 curl_close ($ch);
2891 $json_data = json_decode($server_output);
2892 $data['wiwallet_balance_usd'] = $json_data->data->available_balance;
2893
2894
2895 global $wpdb;
2896 $table = $wpdb->prefix.'witoken_statement';
2897 $sql = "SELECT Id FROM $table ORDER BY Id DESC LIMIT 1";
2898 $last_id = $wpdb->get_results($sql);
2899 $transaction = date('dmyhis') . '_' .$last_id[0]->Id;
2900 $data['transaction_id'] = $transaction;
2901
2902 return $data;
2903
2904 }
2905
2906
2907 /**
2908 *
2909 * @since 1.0.0
2910 */
2911 public function get_token_statement_details( WP_REST_Request $request ){
2912
2913 $data = array();
2914 $get_id = $request->get_params( 'token_id' );
2915 $token_id = $get_id['token_id'];
2916 global $wpdb;
2917 $table = $wpdb->prefix.'witoken_statement';
2918 $sql = "SELECT * FROM $table WHERE id =".$token_id."";
2919 $token_data = $wpdb->get_results($sql, ARRAY_A);
2920 return $token_data;
2921
2922 }
2923
2924
2925 /**
2926 *
2927 * @since 1.0.0
2928 */
2929 function validate_auth_cookie( WP_REST_Request $request )
2930 {
2931 $cookie = $request->get_param( 'cookie' );
2932 $valid = wp_validate_auth_cookie($cookie, 'logged_in') ? true : false;
2933 return array(
2934 "valid" => $valid,
2935 );
2936 }
2937
2938 /**
2939 *
2940 * @since 1.0.0
2941 */
2942 public function generate_auth_cookie( WP_REST_Request $request )
2943 {
2944
2945 $seconds = 1209600;
2946 $email = $request->get_param( 'email' );
2947 $password = $request->get_param( 'password' );
2948 $device_id = $request->get_param( 'device_id' );
2949
2950 if (is_email($email )) {
2951
2952 $ch = curl_init();
2953 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/oauth/token");
2954 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
2955 'Content-Type' => 'application/json'
2956 ));
2957 curl_setopt($ch, CURLOPT_POST, 1);
2958 curl_setopt($ch, CURLOPT_POSTFIELDS,
2959 "grant_type=password&client_id=22&client_secret=iT8sHZdMYj20zdCAyg5XTykf6ri1eXVV3N27M79c&device_id=".$device_id."&username=".$email."&password=".$password."&scope=");
2960 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2961 $server_output = curl_exec($ch);
2962 curl_close ($ch);
2963
2964 $json_data = json_decode($server_output);
2965 if( $json_data->token_type == "Bearer" ){ // check user is in wiaccount
2966
2967 if (!email_exists($email)) { // if user does not exists in WiAccount server than user register and login
2968
2969 $displayname = strstr($email, '@', true); // As of PHP 5.3.0
2970
2971 if ( username_exists( $displayname ) ){
2972 $rand_digits = date('dmyhis');
2973 $last4chars = substr($rand_digits, -4);
2974 $displayname = $displayname.'_'.$last4chars;
2975 }
2976 $new_vendor_data = array(
2977 'user_login' => $displayname,
2978 'user_email' => $email,
2979 'user_pass' => $password,
2980 'user_nicename' => $displayname,
2981 'display_name' => $displayname,
2982 'first_name' => $first_name,
2983 'last_name' => $last_name,
2984 'role' => 'subscriber'
2985 );
2986
2987 $id = wp_insert_user( $new_vendor_data ); // if user is not exists in witoken.global than forcely registered user
2988 wp_update_user( array ('ID' => $id, 'role' => 'subscriber') ) ;
2989
2990 $user = wp_authenticate($email, $password);
2991 $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true);
2992 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
2993
2994 // Add access_token
2995 $get_access = get_user_meta($user->ID, 'access_token', true);
2996 if( !empty($get_access) ){
2997 update_user_meta($user->ID, 'access_token', $json_data->access_token);
2998 update_user_meta($user->ID, 'refresh_token', $json_data->refresh_token);
2999 } else {
3000 add_user_meta($user->ID, 'access_token', $json_data->access_token);
3001 add_user_meta($user->ID, 'refresh_token', $json_data->refresh_token);
3002 }
3003
3004 // retrive access_token to get user details from wiaccount and save witoken.global server
3005 $access_token = get_user_meta($user->ID, 'access_token', true);
3006 $ch = curl_init();
3007 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/api/details");
3008 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3009 'Accept: application/json',
3010 'Authorization: Bearer '.$access_token
3011 ));
3012 curl_setopt($ch, CURLOPT_POST, 1);
3013 curl_setopt($ch, CURLOPT_POSTFIELDS,
3014 "client_id=22&device_id=afsfd234432423423423");
3015 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3016 $server_output = curl_exec($ch);
3017 curl_close ($ch);
3018 $data = json_decode($server_output); // Get user data
3019
3020
3021 wp_set_current_user($user_signon->ID);
3022 add_user_meta( $user->ID, 'ico_user_type', 'WiTokenIcoUser');
3023 update_user_meta( $user->ID, 'first_name', $data->first_name );
3024 update_user_meta( $user->ID, 'last_name', $data->last_name );
3025 add_user_meta( $user->ID, 'gender', $data->gender );
3026 add_user_meta( $user->ID, 'birth_date', $data->date_of_birth );
3027 add_user_meta( $user->ID, 'phone_number', $data->mobile_no );
3028 add_user_meta( $user->ID, 'address', $data->address );
3029 add_user_meta( $user->ID, 'zip_code', $data->zip_code );
3030 add_user_meta( $user->ID, 'country_code', $data->country_code );
3031 add_user_meta( $user->ID, 'country_id', $data->country->country_id );
3032 add_user_meta( $user->ID, 'country_name', $data->country->country_name );
3033
3034 // Get all user meta data for $user_id
3035 $meta = get_user_meta($user->ID);
3036 // Filter out empty meta data
3037 $data = array_filter(array_map(
3038 function ($a)
3039 {
3040 return $a[0];
3041 }
3042
3043 , $meta));
3044
3045 $lang = trim(get_user_meta($user->ID, "lang", true));
3046 if ($lang) {
3047 $userlan = $lang;
3048 }
3049 else {
3050 $userlan = "en";
3051 }
3052
3053 $avatar = wp_get_attachment_url( get_user_meta($user->ID, 'avatar_id', true) );
3054 $nid = wp_get_attachment_url( get_user_meta($user->ID, 'nid', true) );
3055
3056 // get meta
3057 return array(
3058 "cookie" => $cookie,
3059 "lang" => $userlan,
3060 "meta" => $data,
3061 'avatar' =>$avatar,
3062 'nid' => $nid,
3063 'access_token' => $access_token,
3064 "cookie_name" => LOGGED_IN_COOKIE,
3065 "user" => array(
3066 "id" => $user->ID,
3067 "username" => $user->user_login,
3068 "nicename" => $user->user_nicename,
3069 "email" => $user->user_email,
3070 "url" => $user->user_url,
3071 "registered" => $user->user_registered,
3072 "displayname" => $user->display_name,
3073 "firstname" => $user->user_firstname,
3074 "lastname" => $user->last_name,
3075 "nickname" => $user->nickname,
3076 "description" => $user->user_description,
3077 "capabilities" => $user->wp_capabilities,
3078 "password" => $password,
3079
3080 ) ,
3081 );
3082
3083
3084 } else{
3085
3086 // if user exists in witoken.global server than login
3087
3088 $userdata = get_user_by('email', $email);
3089 $result = wp_check_password($password, $userdata->user_pass);
3090 if ($result) {
3091 $user = wp_authenticate($email, $password);
3092 }
3093 else {
3094 return array('status'=> 'error', 'msg' => 'Email/Password does not match' );
3095 }
3096
3097 if (is_wp_error($user)) {
3098 return array('status'=> 'error', 'msg' => 'Invalid email or password' );
3099 remove_action('wp_login_failed', $email);
3100 }
3101
3102 $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true);
3103 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
3104
3105 $access_token = get_user_meta($user->ID, 'access_token', true);
3106 $refresh_token = get_user_meta($user->ID, 'refresh_token', true);
3107
3108 if( !empty($access_token) && !empty($refresh_token) ){
3109 update_user_meta($user->ID, 'access_token', $json_data->access_token);
3110 update_user_meta($user->ID, 'refresh_token', $json_data->refresh_token);
3111 } else {
3112 add_user_meta($user->ID, 'access_token', $json_data->access_token);
3113 add_user_meta($user->ID, 'refresh_token', $json_data->refresh_token);
3114 }
3115
3116 // Get all user meta data for $user_id
3117 $meta = get_user_meta($user->ID);
3118 // Filter out empty meta data
3119 $data = array_filter(array_map(
3120 function ($a)
3121 {
3122 return $a[0];
3123 }
3124
3125 , $meta));
3126
3127 $lang = trim(get_user_meta($user->ID, "lang", true));
3128 if ($lang) {
3129 $userlan = $lang;
3130 }
3131 else {
3132 $userlan = "en";
3133 }
3134
3135 $avatar = wp_get_attachment_url( get_user_meta($user->ID, 'avatar_id', true) );
3136 $nid = wp_get_attachment_url( get_user_meta($user->ID, 'nid', true) );
3137
3138
3139 // get meta
3140 return array(
3141 "cookie" => $cookie,
3142 "lang" => $userlan,
3143 "meta" => $data,
3144 'avatar' =>$avatar,
3145 'nid' => $nid,
3146 'access_token' => $access_token,
3147 "cookie_name" => LOGGED_IN_COOKIE,
3148 "user" => array(
3149 "id" => $user->ID,
3150 "username" => $user->user_login,
3151 "nicename" => $user->user_nicename,
3152 "email" => $user->user_email,
3153 "url" => $user->user_url,
3154 "registered" => $user->user_registered,
3155 "displayname" => $user->display_name,
3156 "firstname" => $user->user_firstname,
3157 "lastname" => $user->last_name,
3158 "nickname" => $user->nickname,
3159 "description" => $user->user_description,
3160 "capabilities" => $user->wp_capabilities,
3161 "password" => $password
3162 ) ,
3163 );
3164
3165 }
3166
3167 } else { // User and password does not match in Wiaccount
3168 return array('status'=> 'error', 'msg' => 'Invalid email or password' );
3169 }
3170
3171 }
3172 else {
3173 return array('status'=> 'error', 'msg' => 'Invalid email' );
3174 }
3175
3176 }
3177
3178
3179 public function run_logout(WP_REST_Request $request ){
3180
3181 $email = $request->get_param( 'email' );
3182 $password = $request->get_param( 'password' );
3183 $user_id = $request->get_param( 'user_id' );
3184
3185 $user = wp_authenticate($email, $password);
3186 $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true);
3187 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
3188
3189 // Add access_token
3190 $get_access = get_user_meta($user_id, 'access_token', true);
3191
3192 $ch = curl_init();
3193 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/api/logout");
3194 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3195 'Content-Type' => 'application/json'
3196 ));
3197 curl_setopt($ch, CURLOPT_POST, 1);
3198 curl_setopt($ch, CURLOPT_POSTFIELDS,
3199 "grant_type=password&client_id=22&client_secret=iT8sHZdMYj20zdCAyg5XTykf6ri1eXVV3N27M79c&username=".$email."&password=".$password."&scope=");
3200 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3201 $server_output = curl_exec($ch);
3202 curl_close ($ch);
3203 }
3204
3205
3206 /**
3207 *
3208 * @since 1.0.0
3209 */
3210 public function uploadImgWhenRegisteredUserFromWiaccount( $userid, $type, $file_url ){
3211
3212 require_once( ABSPATH . 'wp-admin/includes/file.php' );
3213 $upload_overrides = array( 'test_form' => false );
3214 $movefile = wp_handle_upload( $file_url, $upload_overrides);
3215
3216 $upload_dir = wp_upload_dir();
3217 $image_data = file_get_contents($movefile['url']);
3218 $filename = basename($movefile['url']);
3219 if(wp_mkdir_p($upload_dir['path'])){
3220 $file = $upload_dir['path'] . '/' . $filename;
3221 } else {
3222 $file = $upload_dir['basedir'] . '/' . $filename;
3223 }
3224 file_put_contents($file, $image_data);
3225
3226 $wp_filetype = wp_check_filetype($filename, null );
3227 $attachment = array(
3228 'post_mime_type' => $wp_filetype['type'],
3229 'post_title' => sanitize_file_name($filename),
3230 'post_content' => '',
3231 'post_status' => 'inherit'
3232 );
3233 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
3234 require_once(ABSPATH . 'wp-admin/includes/image.php');
3235 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
3236 wp_update_attachment_metadata( $attach_id, $attach_data );
3237
3238 update_user_meta($userid, $type, $attach_id);
3239
3240
3241 }
3242
3243
3244 /**
3245 *
3246 * @since 1.0.0
3247 */
3248 public function get_user_metas( WP_REST_Request $request ){
3249
3250 global $wpdb;
3251 $user_id = $request->get_params( 'user_id' );
3252 $user_id = $user_id['user_id'];
3253 $meta = get_user_meta($user_id);
3254 // Filter out empty meta data
3255 $data = array_filter(array_map(
3256 function ($a)
3257 {
3258 return $a[0];
3259 }
3260
3261 , $meta));
3262
3263 return $data;
3264
3265 }
3266
3267
3268 /**
3269 *
3270 * @since 1.0.0
3271 */
3272 public function get_token_statement( WP_REST_Request $request ){
3273 $data = array();
3274 global $wpdb;
3275 $params = $request->get_params();
3276 $user_id = $params['user_id'];
3277 $current_page = $params['page'];
3278 $limit = 10;
3279
3280 $table = $wpdb->prefix.'witoken_statement';
3281 $total_row_sql = "SELECT COUNT(id) as total_row FROM $table WHERE user_id =".$user_id. " ORDER BY Id DESC";
3282 $total_row = $wpdb->get_results($total_row_sql);
3283 $total_row = $total_row[0]->total_row;
3284 $total_page = $total_row / $limit;
3285
3286 if( $total_page <= $limit ){
3287 $total_page = 1;
3288 }
3289 if( ($total_row % $limit) != 0 ) {
3290 $total_page = ceil($total_row / $limit);
3291 }
3292
3293 $start_from = ($current_page - 1) * $limit;
3294 $sql = "SELECT * FROM $table WHERE user_id =".$user_id." ORDER BY Id DESC LIMIT $start_from, $limit";
3295 $result = $wpdb->get_results($sql, ARRAY_A);
3296
3297 $data['limit'] = $limit;
3298 $data['row'] = $total_row;
3299 $data['total_page'] = $total_page;
3300 $data['current_page'] = $current_page;
3301 $data['result'] = $result;
3302
3303 return $data;
3304 }
3305
3306
3307 /**
3308 *
3309 * @since 1.0.0
3310 */
3311 public function get_token_statement_balance( WP_REST_Request $request ){
3312
3313 $token_balance = array();
3314
3315 global $wpdb;
3316 $params = $request->get_params();
3317 $user_id = $params['user_id'];
3318 $access_token = $params['access_token'];
3319 $table = $wpdb->prefix.'witoken_statement';
3320
3321 $ch = curl_init();
3322 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/auth/wallets/wit?client_id=3&device_id=ADSFADFADFA525ADSF");
3323 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3324 'Content-Type' => 'application/json',
3325 'Authorization: Bearer '.$access_token
3326 ));
3327 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3328 $server_output = curl_exec($ch);
3329 curl_close ($ch);
3330 $json_data = json_decode($server_output);
3331 $total_available = $json_data->data->available_balance;
3332
3333 $pending_query = "SELECT SUM(total_token) as total_witoken FROM $table WHERE user_id =".$user_id." AND (status='pending' OR status='waiting') ORDER BY Id DESC";
3334 $pending_witoken = $wpdb->get_results($pending_query);
3335
3336 if( empty($pending_witoken[0]->total_witoken) ){
3337 $total_pending = 0.00;
3338 } else{
3339 $total_pending = $pending_witoken[0]->total_witoken;
3340 }
3341
3342 $token_balance['available_bal'] = $total_available;
3343 $token_balance['pending_bal'] = $total_pending;
3344
3345 return $token_balance;
3346
3347 }
3348
3349
3350 /**
3351 *
3352 * @since 1.0.0
3353 */
3354 public function get_token_transactions( WP_REST_Request $request ){
3355 $data = array();
3356 global $wpdb;
3357 $user_id = $request->get_params( 'user_id' );
3358 $user_id = $user_id['user_id'];
3359 $current_page = $request->get_params( 'page' );
3360 $current_page = $current_page['page'];
3361 $limit = 10;
3362
3363 $table = $wpdb->prefix.'witoken_statement';
3364
3365 $total_row_sql = "SELECT COUNT(id) as total_row FROM $table WHERE user_id =".$user_id." AND (status='pending' OR status='waiting') ORDER BY Id DESC";
3366
3367 $total_row = $wpdb->get_results($total_row_sql);
3368 $total_row = $total_row[0]->total_row;
3369
3370 $total_page = $total_row / $limit;
3371
3372 if( $total_page <= $limit ){
3373 $total_page = 1;
3374 }
3375 if( ($total_row % $limit) != 0 ) {
3376 $total_page = ceil($total_row / $limit);
3377 // $total_page = $total_page + 1;
3378 }
3379
3380 $start_from = ($current_page - 1) * $limit;
3381
3382 $sql = "SELECT * FROM $table WHERE user_id =".$user_id." AND (status='pending' OR status='waiting') ORDER BY Id DESC LIMIT $start_from, $limit";
3383
3384 $result = $wpdb->get_results($sql, ARRAY_A);
3385
3386 $data['limit'] = $limit;
3387 $data['row'] = $total_row;
3388 $data['total_page'] = $total_page;
3389 $data['total_page'] = $total_page;
3390 $data['current_page'] = $current_page;
3391 $data['result'] = $result;
3392
3393 return $data;
3394 }
3395
3396
3397 public function get_package_details_by_id($package_id){
3398
3399 $data = array();
3400 $data['package'] = get_the_title( $package_id );
3401 $data['package_cost'] = get_post_meta( $package_id, 'package_cost', true );
3402 $data['package_witoken_rate'] = get_post_meta( $package_id, 'package_witoken_rate', true );
3403 $data['package_bonus'] = get_post_meta( $package_id, 'package_bonus', true );
3404 $data['package_bonus_round'] = get_post_meta( $package_id, 'package_bonus_round', true );
3405 $rank_id = get_post_meta( $package_id, 'bounty_affiliate', true );
3406 $rank_data = $this->get_rank($rank_id);
3407 $data['bounty_affiliate'] = $rank_data['label'];
3408 return $data;
3409 }
3410
3411
3412 /**
3413 *
3414 * @since 1.0.0
3415 */
3416 public function get_token_transaction_details( WP_REST_Request $request ){
3417
3418 $data = array();
3419 $get_id = $request->get_params( 'token_id' );
3420 $token_id = $get_id['token_id'];
3421 global $wpdb;
3422 $table = $wpdb->prefix.'witoken_statement';
3423 $sql = "SELECT * FROM $table WHERE id =".$token_id."";
3424 $token_data = $wpdb->get_results($sql, ARRAY_A);
3425
3426 if( $token_data[0]['purchase_type'] == "package" ){
3427 $subs = explode("-",$token_data[0]['package_stage_phase_id']);
3428 $package_id = $subs[1];
3429 $package_details = $this->get_package_details_by_id($package_id);
3430 $token_data = array_merge($token_data[0], $package_details);
3431 $token_data = array( '0'=> $token_data);
3432 }
3433 return $token_data;
3434
3435 }
3436
3437 /**
3438 *
3439 * @since 1.0.0
3440 */
3441 public function get_method_meta( WP_REST_Request $request ){
3442
3443 $data = array();
3444 $get_id = $request->get_params( 'token_id' );
3445 $token_id = $get_id['token_id'];
3446 global $wpdb;
3447 $table = $wpdb->prefix.'witoken_statement';
3448 $sql = "SELECT * FROM $table WHERE id =".$token_id."";
3449 $token_data = $wpdb->get_results($sql);
3450
3451 if( $token_data[0]->pmethod == "ETH" ){
3452 $data['eth_trans_date'] = get_post_meta( $token_data[0]->id, 'eth_trans_date', true );
3453 $data['eth_txhash'] = get_post_meta( $token_data[0]->id, 'eth_txhash', true );
3454 $data['eth_wallet_add'] = get_post_meta( $token_data[0]->id, 'eth_wallet_add', true );
3455 $data['eth_quantity'] = get_post_meta( $token_data[0]->id, 'eth_quantity', true );
3456
3457 } else if( $token_data[0]->pmethod == "BTC" ){
3458 $data['btc_trans_date'] = get_post_meta( $token_data[0]->id, 'btc_trans_date', true );
3459 $data['btc_txhash'] = get_post_meta( $token_data[0]->id, 'btc_txhash', true );
3460 $data['btc_wallet_add'] = get_post_meta( $token_data[0]->id, 'btc_wallet_add', true );
3461 $data['btc_quantity'] = get_post_meta( $token_data[0]->id, 'btc_quantity', true );
3462 }
3463 $data['pmethod'] = $token_data[0]->pmethod;
3464 return $data;
3465
3466 }
3467
3468
3469
3470
3471 /**
3472 *
3473 * @since 1.0.0
3474 */
3475 public function get_token_transaction_verify_details( WP_REST_Request $request ){
3476
3477 $data = array();
3478 $get_id = $request->get_params( 'token_id' );
3479 $token_id = $get_id['token_id'];
3480
3481 global $wpdb;
3482 $table = $wpdb->prefix.'witoken_statement';
3483 $sql = "SELECT * FROM $table WHERE id =".$token_id;
3484 $result = $wpdb->get_results($sql);
3485
3486 $pmethod = $result[0]->pmethod;
3487
3488 if( $pmethod == 'ETH' ){
3489 $data['eth_trans_date'] = get_post_meta( $token_id , 'eth_trans_date', true );
3490 $data['eth_txhash'] = get_post_meta( $token_id , 'eth_txhash', true );
3491 $data['eth_wallet_add'] = get_post_meta( $token_id , 'eth_wallet_add', true );
3492 $data['eth_quantity'] = get_post_meta( $token_id , 'eth_quantity', true );
3493 }
3494
3495 if( $pmethod == 'BTC' ){
3496 $data['btc_trans_date'] = get_post_meta( $token_id , 'btc_trans_date', true );
3497 $data['btc_txhash'] = get_post_meta( $token_id , 'btc_txhash', true );
3498 $data['btc_wallet_add'] = get_post_meta( $token_id , 'btc_wallet_add', true );
3499 $data['btc_quantity'] = get_post_meta( $token_id , 'btc_quantity', true );
3500 }
3501
3502 if( $pmethod == 'Bank' ){
3503 $data['bank_trans_slip'] = wp_get_attachment_url( get_post_meta( $token_id , 'bank_trans_slip', true ) );
3504 $data['bank_ref_id'] = get_post_meta( $token_id , 'bank_ref_id', true );
3505 $data['bank_trans_date'] = get_post_meta( $token_id , 'bank_trans_date', true );
3506 $data['bank_name'] = get_post_meta( $token_id , 'bank_name', true );
3507 $data['sender_name'] = get_post_meta( $token_id , 'sender_name', true );
3508 $data['sender_address'] = get_post_meta( $token_id , 'sender_address', true );
3509 $data['account_no'] = get_post_meta( $token_id , 'account_no', true );
3510 }
3511 $data['status'] = $result[0]->status;
3512 return $data;
3513
3514 }
3515
3516
3517 /**
3518 *
3519 * @since 1.0.0
3520 */
3521 public function run_pay_with_stripe( WP_REST_Request $request ){
3522
3523 $post = (array) $request->get_params();
3524 $month = $post['month'];
3525 $year = $post['year'];
3526 $card_no = $post['card_no'];
3527 $name_on_card = $post['name_on_card'];
3528 $cvc = $post['cvc'];
3529 $amount = $post['amount'];
3530 $stripe_key = $post['stripe_key'];
3531 $user_id = $post['user_id'];
3532 $purpose = $post['payment_purpose'];
3533 $email = $post['user_email'];
3534
3535 $response = wp_remote_post(
3536 'https://witoken.global/payments/public/api/payWithStripe',
3537 array(
3538 'timeout' => 100000,
3539 'body' => array(
3540 'ccExpiryMonth' => (int)$month,
3541 'ccExpiryYear' => $year,
3542 'card_no' => $card_no,
3543 'cvcNumber' => $cvc,
3544 'amount' => $amount,
3545 'stripe_key' => $stripe_key, // stripe sandbox/live key,
3546 'description' => $purpose,
3547 'email' => $email
3548
3549 )
3550 )
3551 );
3552
3553 return $response;
3554
3555 }
3556
3557
3558 /**
3559 *
3560 * @since 1.0.0
3561 */
3562 public function contact_us( WP_REST_Request $request ){
3563
3564 $post = (array) $request->get_params();
3565 $name = $post['full_name'];
3566 $phone = $post['phone'];
3567 $email = $post['email'];
3568 $subject = $post['subject'];
3569 $message = $post['message'];
3570 $contact_to = get_option('admin_email');
3571
3572 $headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
3573 $mail = wp_mail($contact_to, $subject, $message, $headers);
3574 if( $mail ){
3575 return $message = 'Your email was successfully sent and we will be in touch with you soon.';
3576 } else {
3577 return $messages = 'Something went wrong';
3578 }
3579
3580
3581 }
3582
3583
3584
3585 /**
3586 *
3587 * @since 1.0.0
3588 */
3589 public function get_app_home_page( WP_REST_Request $request ){
3590
3591 $lang = $request->get_params( 'lang' );
3592 $lang = $lang['lang'];
3593 if( empty($lang) ){
3594 $lang = "en";
3595 }
3596 $get_lang = get_option('lang_switch');
3597 if( empty($get_lang) ){
3598 add_option( 'lang_switch', $lang );
3599 } else {
3600 update_option( 'lang_switch', $lang );
3601 }
3602
3603 if( $lang == "ar" ){
3604 $app_ar_ids = get_option('app_ar_ids');
3605 $postId = $app_ar_ids['ar_home_id']; // AR Home page id
3606 } else if( $lang == "cn" ){
3607 $app_cn_ids = get_option('app_cn_ids');
3608 $postId = $app_cn_ids['cn_home_id']; // CN Home page id
3609 } else if($lang == "en" ){
3610 $app_en_ids = get_option('app_en_ids');
3611 $postId = $app_en_ids['en_home_id']; // EN Home page id
3612 } else if( $lang == "my" ) {
3613 $app_my_ids = get_option('app_my_ids');
3614 $postId = $app_my_ids['my_home_id']; // Malay Home page id
3615 } else {
3616 $app_en_ids = get_option('app_en_ids');
3617 $postId = $app_en_ids['en_home_id']; // EN Home page id
3618 }
3619
3620 $posts = get_post($postId);
3621 WPBMap::addAllMappedShortcodes();
3622 return apply_filters('the_content', $posts->post_content);
3623
3624
3625 }
3626
3627
3628 /**
3629 *
3630 * @since 1.0.0
3631 */
3632 public function get_single_details( WP_REST_Request $request ){
3633
3634 $pageId = $request->get_params( 'pageId' );
3635 $pageId = $pageId['pageId'];
3636 $data = array();
3637 $posts = get_post($pageId);
3638 $data = array();
3639 $data['title'] = $posts->post_title;
3640 WPBMap::addAllMappedShortcodes();
3641 $data['content'] = apply_filters('the_content', $posts->post_content);
3642 return $data;
3643
3644 }
3645
3646
3647 /**
3648 *
3649 * @since 1.0.0
3650 */
3651 public function get_token_sale( WP_REST_Request $request ){
3652
3653 $params = $request->get_params();
3654 $lang = $params['lang'];
3655 include 'token-sale-template.php';
3656 return $output;
3657
3658 }
3659
3660 /**
3661 *
3662 * @since 1.0.0
3663 */
3664 public function get_token_sale_bonus(){
3665
3666 $taxonomies = array('taxonomy'=> 'tss_category', 'hide_empty'=> false,'meta_key' => 'start_date','orderby' => 'meta_value', 'order' => 'ASC');
3667 $current_date = date('Y-m-d');
3668 $bonus_percentage = '';
3669 $stage_heading = '';
3670 $phase_heading = '';
3671 $min_amount = '';
3672 $data = array();
3673 $taxonomy_terms = get_terms( $taxonomies );
3674 foreach ( $taxonomy_terms as $taxonomy_term ) :
3675
3676 $taxonomy_term_name = $taxonomy_term->name;
3677 $taxonomy_term_id = $taxonomy_term->term_id;
3678
3679 $start_date = get_term_meta( $taxonomy_term_id, 'start_date', true);
3680 $end_date = get_term_meta( $taxonomy_term_id, 'end_date', true);
3681 $start_date = date("Y-m-d", strtotime($start_date)); //formatted
3682 $end_date = date("Y-m-d", strtotime($end_date)); //formatted
3683
3684 if($current_date >=$start_date && $current_date <=$end_date ){
3685 $get_term_id = $taxonomy_term->term_id;
3686
3687 $check_phase_exists = count(get_posts(array( 'post_type' => 'toke_sale_stage',
3688 'tax_query' => array(
3689 array(
3690 'taxonomy' => 'tss_category',
3691 'terms' => $get_term_id,
3692 'field' => 'term_id',
3693 )
3694 ),
3695 'orderby' => 'title',
3696 'order' => 'ASC' )
3697 ));
3698
3699
3700 if( $check_phase_exists !='' || $check_phase_exists > 0 ){
3701 $query = new WP_Query(array(
3702 'post_type' => 'toke_sale_stage',
3703 'tax_query' => array(
3704 array(
3705 'taxonomy' => 'tss_category',
3706 'terms' => $get_term_id,
3707 'field' => 'term_id',
3708 )
3709 ),
3710 'orderby' => 'title',
3711 'order' => 'ASC' )
3712 );
3713
3714 if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
3715 $phase_start_date = get_post_meta( get_the_ID(), 'start_date', true);
3716 $phase_end_date = get_post_meta( get_the_ID(), 'end_date', true);
3717
3718 $phase_start_date = date("Y-m-d", strtotime($phase_start_date));
3719 $phase_end_date = date("Y-m-d", strtotime($phase_end_date));
3720
3721 if($current_date >=$phase_start_date && $current_date <=$phase_end_date ){
3722 $bonus_percentage = get_post_meta( get_the_ID(), 'bonus_percentage', true);
3723 $phase_heading = get_the_title();
3724 $stage_heading = $taxonomy_term_name;
3725 $min_amount = get_post_meta( get_the_ID(), 'min_amount', true);
3726
3727
3728 }
3729
3730 endwhile;endif;
3731 } else {
3732 $bonus_percentage = get_term_meta( $taxonomy_term_id, 'percentage_', true);
3733 $phase_heading = '';
3734 $stage_heading = $taxonomy_term_name;
3735 $min_amount = get_term_meta( $taxonomy_term_id, 'min_amount', true);
3736 }
3737
3738 }
3739 endforeach;
3740
3741 return array(
3742 'bonus_percentage' => $bonus_percentage,
3743 'min_amount' => $min_amount,
3744 'stage_heading' => $stage_heading,
3745 'phase_heading' => $phase_heading
3746 );
3747
3748 }
3749
3750
3751 /**
3752 *
3753 * @since 1.0.0
3754 */
3755 public function create_new_user_from_app( WP_REST_Request $request ){
3756
3757 $post = (array) $request->get_params();
3758 $first_name = $post['first_name'];
3759 $last_name = $post['last_name'];
3760 $birth_date = $post['birth_date'];
3761 $gender = $post['gender'];
3762 $nationality = $post['nationality'];
3763 $email = $post['email'];
3764 $password = $post['password'];
3765 $phone_number = $post['phone_number'];
3766 $address = $post['address'];
3767 $zip_code = $post['zip_code'];
3768 $country_code = $post['country_code'];
3769 $referred_affid = $post['referred_affid'];
3770 if( !empty($referred_affid) ){
3771 self::$affiliate_id = $post['referred_affid'];
3772 }
3773 $country_id = $post['country_id'];
3774 $country_name = $post['country_name'];
3775 $pmethod = $post['pmethod'];
3776
3777 if( $pmethod == "PayPal" ){
3778 $status = "approved";
3779 $paypal_email = $post['paypal_email'];
3780
3781 } else if( $pmethod == "Stripe" ){
3782 $month = $post['month'];
3783 $year = $post['year'];
3784 $card_no = $post['card_no'];
3785 $name_on_card = $post['name_on_card'];
3786 $cvc = $post['cvc'];
3787 $status = "approved";
3788
3789 } else if( $pmethod == "BTC" ){
3790 $btc_quantity = $post['btc_quantity'];
3791 $btc_wallet_add = $post['btc_wallet_add'];
3792 $status = "pending";
3793 } else if( $pmethod == "ETH" ){
3794 $eth_quantity = $post['eth_quantity'];
3795 $eth_wallet_add = $post['eth_wallet_add'];
3796 $status = "pending";
3797 } else if( $pmethod == "Bank" ){
3798 $status = "pending";
3799 $account_no = $post['account_no'];
3800 $bank_name = $post['bank_name'];
3801 $sender_name = $post['sender_name'];
3802 $sender_address = $post['sender_address'];
3803 }
3804
3805 $amount = $post['amount'];
3806 $bonus_amount = $post['bonus_amount'];
3807 $bonus = $post['bonus'];
3808 $bonus_type = $post['bonus_type'];
3809 $total_token = $post['total_token'];
3810
3811 $token_sale_info = $this->token_sale_bonus_heading_info();
3812 if( $bonus_type == "bonus_percentage" ){
3813 $stage_contact = $token_sale_info['stage'] ." (".$bonus."%) ";
3814 } else {
3815 $stage_contact = $token_sale_info['stage'] ." (".$bonus."USD) ";
3816 }
3817
3818 $files = $_FILES['file'];
3819 $nfiles = $_FILES['nfile'];
3820
3821 $avatar_id = $this->uploadImgforProfile($files);
3822 $nid = $this->uploadImgforNid($nfiles);
3823
3824 $displayname = strstr($email, '@', true); // As of PHP 5.3.0
3825 if ( username_exists( $displayname ) ){
3826 $rand_digits = date('dmyhis');
3827 $last4chars = substr($rand_digits, -4);
3828 $displayname = $displayname.'_'.$last4chars;
3829 }
3830 $user_data = array(
3831 'user_login' => $displayname,
3832 'user_email' => $email,
3833 'user_pass' => $password,
3834 'user_nicename' => $displayname,
3835 'display_name' => $displayname,
3836 'first_name' => $first_name,
3837 'last_name' => $last_name,
3838 'role' => 'subscriber'
3839 );
3840
3841 $user_id = wp_insert_user( $user_data );
3842
3843 if (!is_wp_error($user_id)) {
3844 do_action( 'user_register', $user_id );
3845 add_user_meta( $user_id, 'ico_user_type', 'WiTokenIcoUser');
3846 add_user_meta( $user_id, 'gender', $gender);
3847 add_user_meta( $user_id, 'nationality', $nationality);
3848 add_user_meta( $user_id, 'birth_date', $birth_date);
3849 add_user_meta( $user_id, 'phone_number', $phone_number);
3850 add_user_meta( $user_id, 'address', $address);
3851 add_user_meta( $user_id, 'zip_code', $zip_code);
3852 add_user_meta( $user_id, 'country_code', $country_code);
3853 add_user_meta( $user_id, 'country_id', $country_id);
3854 add_user_meta( $user_id, 'country_name', $country_name);
3855 add_user_meta( $user_id, 'avatar_id', $avatar_id);
3856 add_user_meta( $user_id, 'jas_avatar', $avatar_id);
3857 add_user_meta( $user_id, 'nid', $nid);
3858
3859 global $wpdb;
3860 $table = $wpdb->prefix.'witoken_statement';
3861 $wpdb->insert($table,
3862 array(
3863 'user_id' => $user_id,
3864 'purchase_date' => date('Y-m-d'),
3865 'amount' => $amount,
3866 'bonus_type' => $bonus_type,
3867 'bonus_per' => $bonus,
3868 'bonus_amount' => $bonus_amount,
3869 'pmethod' => $pmethod,
3870 'total_token' => $total_token,
3871 'sale_stage' => $stage_contact,
3872 'note' => 'Purchase and Signup',
3873 'status' => $status
3874 )
3875 );
3876
3877 $transaction_id = $wpdb->insert_id;
3878 if( $pmethod == "BTC" ){
3879 add_post_meta( $transaction_id, 'btc_quantity', $btc_quantity);
3880 add_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
3881 } else if( $pmethod == "ETH" ){
3882 add_post_meta( $transaction_id, 'eth_quantity', $eth_quantity);
3883 add_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
3884 } else if( $pmethod == "Bank" ){
3885 add_post_meta( $transaction_id, 'account_no', $account_no);
3886 add_post_meta( $transaction_id, 'bank_name', $bank_name);
3887 add_post_meta( $transaction_id, 'sender_name', $sender_name);
3888 add_post_meta( $transaction_id, 'sender_address', $sender_address);
3889 } else if( $pmethod == "Stripe" ){
3890 add_post_meta( $transaction_id, 'card_no', $card_no);
3891
3892 } else if( $pmethod == "PayPal" ){
3893 add_post_meta( $transaction_id, 'paypal_email', $paypal_email);
3894 }
3895
3896 $info = array();
3897 $info['user_login'] = $email;
3898 $info['user_password'] = $password;
3899 $info['remember'] = true;
3900 $user_signon = wp_signon( $info, false );
3901
3902 // Login in WiAccount
3903 $ch = curl_init();
3904 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/oauth/token");
3905 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3906 'Content-Type' => 'application/json'
3907 ));
3908 curl_setopt($ch, CURLOPT_POST, 1);
3909 curl_setopt($ch, CURLOPT_POSTFIELDS,
3910 "grant_type=password&client_id=22&client_secret=iT8sHZdMYj20zdCAyg5XTykf6ri1eXVV3N27M79c&username=".$email."&password=".$password."&scope=");
3911 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3912 $server_output = curl_exec($ch);
3913 curl_close ($ch);
3914
3915 $json_data = json_decode($server_output);
3916 if( $json_data->token_type == "Bearer" ){ // check user is in wiaccount
3917 // Add access_token
3918 add_user_meta($user_id, 'access_token', $json_data->access_token);
3919 add_user_meta($user_id, 'refresh_token', $json_data->refresh_token);
3920 $access_token = $json_data->access_token;
3921
3922 //WiToken transfer to WiWallet
3923 if( $status == "approved" ){ //Only for PayPal & Stripe
3924 $transaction_details = "WiToken transfer to WiWallet";
3925 $user_info = get_userdata($user_id);
3926 $user_email = $user_info->user_email;
3927
3928 $ch = curl_init();
3929 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
3930 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3931 'Content-Type' => 'application/json',
3932 'Authorization: Bearer '.$access_token
3933 ));
3934 curl_setopt($ch, CURLOPT_POST, 1);
3935 curl_setopt($ch, CURLOPT_POSTFIELDS,
3936 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
3937 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3938 $server_output = curl_exec($ch);
3939 curl_close ($ch);
3940 $json_data = json_decode($server_output);
3941
3942 if( $json_data->status == "success" ){
3943 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
3944 } else {
3945 $errors = $json_data->errors;
3946 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
3947 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
3948 }
3949 }
3950 }
3951
3952
3953 /***** Call affiliate methods to create referral */
3954 $this->save_affiliate($user_id);
3955 $this->update_affiliate_rank_by_uid($user_id, $this->current_rank);
3956 if( !empty($referred_affid) ){
3957 $this->set_mlm_parent($user_id, $referred_affid);
3958 }
3959
3960
3961 $affi_bonus_settings = get_option( 'aff_bonus_info' );
3962 $affi_bonus_type = $affi_bonus_settings['aff_bonus_type'];
3963 $aff_bonus_witoken = $affi_bonus_settings['aff_bonus_witoken']; // if checked exclude bonus witoken
3964 if( empty($affi_bonus_type) ){
3965 $affi_bonus_type = 'usd';
3966 }
3967
3968 if( $affi_bonus_type == 'usd' ){
3969 if( !empty($referred_affid) ){
3970 $this->create_referral($user_id, $referred_affid, $amount, $status, $transaction_id);
3971 }
3972 } else {
3973
3974 if( $aff_bonus_witoken == "1" ){
3975 $amount = $total_token - $bonus_amount; //exclude bonus witoken
3976 } else {
3977 $amount = $total_token;
3978 }
3979 if( !empty($referred_affid) ){
3980 $this->create_referral($user_id, $referred_affid, $amount, $status, $transaction_id);
3981 }
3982 }
3983
3984 if( $status == "approved" ){
3985 $to = $email;
3986 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
3987 $amount = $amount;
3988 $purchase_date = date('Y-m-d');
3989 $payment_method = $pmethod;
3990 $bonus = $bonus;
3991 $bonus_token = $bonus_amount;
3992 $total_token = $total_token;
3993 $token_stage = $stage_contact;
3994 $email_info = get_option( 'email_settings' );
3995 $subject = $email_info['settings_email_subject2'];
3996 $message = $email_info['settings_email_message2'];
3997 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
3998 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
3999 $body = str_replace($searchVal, $replaceVal, $message);
4000 wp_mail($to, $subject, $body);
4001 } else {
4002 $to = $email;
4003 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
4004 $amount = $amount;
4005 $purchase_date = date('Y-m-d');
4006 $payment_method = $pmethod;
4007 $bonus = $bonus;
4008 $bonus_token = $bonus_amount;
4009 $total_token = $total_token;
4010 $token_stage = $stage_contact;
4011 $email_info = get_option( 'email_settings' );
4012 $subject = $email_info['settings_email_subject'];
4013 $message = $email_info['settings_email_message'];
4014 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4015 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4016 $body = str_replace($searchVal, $replaceVal, $message);
4017 wp_mail($to, $subject, $body);
4018 }
4019
4020 return true;
4021 } else {
4022 $error = $user_id->get_error_message();
4023 return false;
4024 }
4025
4026 }
4027
4028 public function create_user_using_fomopay( WP_REST_Request $request ){
4029
4030 $data = array();
4031 $post = (array) $request->get_params();
4032 $first_name = $post['first_name'];
4033 $last_name = $post['last_name'];
4034 $birth_date = $post['birth_date'];
4035 $gender = $post['gender'];
4036 $nationality = $post['nationality'];
4037 $email = $post['email'];
4038 $password = $post['password'];
4039 $phone_number = $post['phone_number'];
4040 $address = $post['address'];
4041 $zip_code = $post['zip_code'];
4042 $country_code = $post['country_code'];
4043 $country_id = $post['country_id'];
4044 $country_name = $post['country_name'];
4045 $referred_affid = $post['referred_affid'];
4046 if( !empty($referred_affid) ){
4047 self::$affiliate_id = $post['referred_affid'];
4048 }
4049 $pmethod = $post['pmethod'];
4050
4051 $amount = $post['amount'];
4052 $bonus_amount = $post['bonus_amount'];
4053 $bonus = $post['bonus'];
4054 $bonus_type = $post['bonus_type'];
4055 $total_token = $post['total_token'];
4056 $token_sale_info = $this->token_sale_bonus_heading_info();
4057 if( $bonus_type == "bonus_percentage" ){
4058 $stage_contact = $token_sale_info['stage'] ." (".$bonus."%) ";
4059 } else {
4060 $stage_contact = $token_sale_info['stage'] ." (".$bonus."USD) ";
4061 }
4062
4063 $files = $_FILES['file'];
4064 $nfiles = $_FILES['nfile'];
4065
4066 $avatar_id = $this->uploadImgforProfile($files);
4067 $nid = $this->uploadImgforNid($nfiles);
4068
4069 $displayname = strstr($email, '@', true); // As of PHP 5.3.0
4070 if ( username_exists( $displayname ) ){
4071 $rand_digits = date('dmyhis');
4072 $last4chars = substr($rand_digits, -4);
4073 $displayname = $displayname.'_'.$last4chars;
4074 }
4075 $user_data = array(
4076 'user_login' => $displayname,
4077 'user_email' => $email,
4078 'user_pass' => $password,
4079 'user_nicename' => $displayname,
4080 'display_name' => $displayname,
4081 'first_name' => $first_name,
4082 'last_name' => $last_name,
4083 'role' => 'subscriber'
4084 );
4085
4086 $user_id = wp_insert_user( $user_data );
4087
4088 if (!is_wp_error($user_id)) {
4089 do_action( 'user_register', $user_id );
4090 add_user_meta( $user_id, 'ico_user_type', 'WiTokenIcoUser');
4091 add_user_meta( $user_id, 'gender', $gender);
4092 add_user_meta( $user_id, 'nationality', $nationality);
4093 add_user_meta( $user_id, 'birth_date', $birth_date);
4094 add_user_meta( $user_id, 'phone_number', $phone_number);
4095 add_user_meta( $user_id, 'address', $address);
4096 add_user_meta( $user_id, 'zip_code', $zip_code);
4097 add_user_meta( $user_id, 'country_code', $country_code);
4098 add_user_meta( $user_id, 'country_id', $country_id);
4099 add_user_meta( $user_id, 'country_name', $country_name);
4100 add_user_meta( $user_id, 'avatar_id', $avatar_id);
4101 add_user_meta( $user_id, 'jas_avatar', $avatar_id);
4102 add_user_meta( $user_id, 'nid', $nid);
4103
4104 global $wpdb;
4105 $table = $wpdb->prefix.'witoken_statement';
4106 $wpdb->insert($table,
4107 array(
4108 'user_id' => $user_id,
4109 'purchase_date' => date('Y-m-d'),
4110 'amount' => $amount,
4111 'bonus_type' => $bonus_type,
4112 'bonus_per' => $bonus,
4113 'bonus_amount' => $bonus_amount,
4114 'pmethod' => $pmethod,
4115 'total_token' => $total_token,
4116 'sale_stage' => $stage_contact,
4117 'note' => 'Purchase and Signup',
4118 'status' => 'pending'
4119 )
4120 );
4121
4122 $info = array();
4123 $info['user_login'] = $email;
4124 $info['user_password'] = $password;
4125 $info['remember'] = true;
4126 $user_signon = wp_signon( $info, false );
4127
4128 // Login in WiAccount
4129 $ch = curl_init();
4130 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/oauth/token");
4131 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4132 'Content-Type' => 'application/json'
4133 ));
4134 curl_setopt($ch, CURLOPT_POST, 1);
4135 curl_setopt($ch, CURLOPT_POSTFIELDS,
4136 "grant_type=password&client_id=22&client_secret=iT8sHZdMYj20zdCAyg5XTykf6ri1eXVV3N27M79c&username=".$email."&password=".$password."&scope=");
4137 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4138 $server_output = curl_exec($ch);
4139 curl_close ($ch);
4140
4141 $json_data = json_decode($server_output);
4142 if( $json_data->token_type == "Bearer" ){ // check user is in wiaccount
4143 // Add access_token
4144 add_user_meta($user_id, 'access_token', $json_data->access_token);
4145 add_user_meta($user_id, 'refresh_token', $json_data->refresh_token);
4146
4147 }
4148
4149
4150 /***** Call affiliate methods to create referral */
4151 $this->save_affiliate($user_id);
4152 $this->update_affiliate_rank_by_uid($user_id, $this->current_rank);
4153 if( !empty($referred_affid) ){
4154 $this->set_mlm_parent($user_id, $referred_affid);
4155 }
4156
4157 // Purchase confirmation email
4158 $to = $email;
4159 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
4160 $amount = $amount;
4161 $purchase_date = date('Y-m-d');
4162 $payment_method = $pmethod;
4163 $bonus = $bonus;
4164 $bonus_token = $bonus_amount;
4165 $total_token = $total_token;
4166 $token_stage = $stage_contact;
4167 $email_info = get_option( 'email_settings' );
4168 $subject = $email_info['settings_email_subject'];
4169 $message = $email_info['settings_email_message'];
4170 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4171 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4172 $body = str_replace($searchVal, $replaceVal, $message);
4173 wp_mail($to, $subject, $body);
4174
4175 $data['status'] = true;
4176 $data['user_id'] = $user_id;
4177 } else {
4178 $error = $user_id->get_error_message();
4179 $data['status'] = false;
4180 }
4181 return $data;
4182
4183 }
4184
4185 /**
4186 *
4187 * @since 1.0.0
4188 */
4189 public function update_witoken_status_byfomo( WP_REST_Request $request ){
4190
4191 global $wpdb;
4192 $data = array();
4193 $post = (array) $request->get_params();
4194 $user_id = $post['user_id'];
4195 $amount = $post['amount'];
4196 $referred_affid = $post['referred_affid'];
4197 $table = $wpdb->prefix.'witoken_statement';
4198 $sql = "SELECT * FROM $table WHERE user_id = ".$user_id."";
4199 $witoken_info = $wpdb->get_results($sql);
4200 $fomo_response = get_user_meta( $user_id, 'fomo_response', true);
4201 add_post_meta( $witoken_info[0]->id, 'transaction', $fomo_response['transaction']);
4202 add_post_meta( $witoken_info[0]->id, 'payment_id', $fomo_response['payment_id']);
4203 add_post_meta( $witoken_info[0]->id, 'upstream', $fomo_response['upstream']);
4204 add_post_meta( $witoken_info[0]->id, 'signature', $fomo_response['signature']);
4205 add_post_meta( $witoken_info[0]->id, 'result', $fomo_response['result']);
4206
4207 $order_explosive = explode('_', $fomo_response['transaction']);
4208 $order_id = absint($order_explosive[1]);
4209 delete_post_meta($order_id, 'user_id' );
4210 delete_user_meta( $user_id, 'fomo_response' );
4211
4212 $wpdb->update( $table, array( 'status' => 'approved' ), array( 'id' => $witoken_info[0]->id ));
4213
4214
4215 // WiToken transfer to WiWallet
4216 $access_token = get_user_meta( $user_id, 'access_token', true );
4217 $transaction_details = "WiToken transfer to WiWallet";
4218 $user_info = get_userdata($user_id);
4219 $user_email = $user_info->user_email;
4220 $total_token = $witoken_info[0]->total_token;
4221
4222 $ch = curl_init();
4223 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
4224 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4225 'Content-Type' => 'application/json',
4226 'Authorization: Bearer '.$access_token
4227 ));
4228 curl_setopt($ch, CURLOPT_POST, 1);
4229 curl_setopt($ch, CURLOPT_POSTFIELDS,
4230 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
4231 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4232 $server_output = curl_exec($ch);
4233 curl_close ($ch);
4234 $json_data = json_decode($server_output);
4235
4236 if( $json_data->status == "success" ){
4237 add_post_meta( $witoken_info[0]->id, 'wiwallet_transfer', 'success' );
4238 } else {
4239 $errors = $json_data->errors;
4240 add_post_meta( $witoken_info[0]->id, 'wiwallet_transfer', 'failed' );
4241 add_post_meta( $witoken_info[0]->id, 'wiwallet_transfer_errors', $errors );
4242 }
4243
4244 // Purchase confirmation email
4245 $to = $user_email;
4246 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
4247 $amount = $witoken_info[0]->amount;
4248 $purchase_date = $witoken_info[0]->purchase_date;
4249 $payment_method = $witoken_info[0]->pmethod;
4250 $bonus = $witoken_info[0]->bonus_per;
4251 $bonus_token = $witoken_info[0]->bonus_amount;
4252 $total_token = $witoken_info[0]->total_token;
4253 $token_stage = $witoken_info[0]->sale_stage;
4254 $email_info = get_option( 'email_settings' );
4255 $subject = $email_info['settings_email_subject2'];
4256 $message = $email_info['settings_email_message2'];
4257 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4258 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4259 $body = str_replace($searchVal, $replaceVal, $message);
4260 wp_mail($to, $subject, $body);
4261
4262 $affi_bonus_settings = get_option( 'aff_bonus_info' );
4263 $affi_bonus_type = $affi_bonus_settings['aff_bonus_type'];
4264 $aff_bonus_witoken = $affi_bonus_settings['aff_bonus_witoken']; // if checked exclude bonus witoken
4265 if( empty($affi_bonus_type) ){
4266 $affi_bonus_type = 'usd';
4267 }
4268
4269 if( $affi_bonus_type == 'usd' ){
4270 if( !empty($referred_affid) ){
4271 $this->create_referral($user_id, $referred_affid, $amount, $status="approved", $witoken_info[0]->id);
4272 }
4273 } else {
4274
4275 if( $aff_bonus_witoken == "1" ){
4276 $amount = $witoken_info[0]->total_token - $witoken_info[0]->bonus_amount; //exclude bonus witoken
4277 } else {
4278 $amount = $witoken_info[0]->total_token;
4279 }
4280 if( !empty($referred_affid) ){
4281 $this->create_referral($user_id, $referred_affid, $amount, $status="approved", $witoken_info[0]->id);
4282 }
4283 }
4284
4285 return 'success';
4286
4287 }
4288
4289
4290 /**
4291 *
4292 * @since 1.0.0
4293 */
4294 public function register_user_from_app( WP_REST_Request $request ){
4295
4296 $post = (array) $request->get_params();
4297 $first_name = $post['first_name'];
4298 $last_name = $post['last_name'];
4299 $birth_date = $post['birth_date'];
4300 $gender = $post['gender'];
4301 $nationality = $post['nationality'];
4302 $email = $post['email'];
4303 $password = $post['password'];
4304 $phone_number = $post['phone_number'];
4305 $address = $post['address'];
4306 $zip_code = $post['zip_code'];
4307 $country_code = $post['country_code'];
4308 $country_id = $post['country_id'];
4309 $country_name = $post['country_name'];
4310
4311 $files = $post['file'];
4312 $nfiles = $post['nfile'];
4313 $avatar_id = $this->uploadImgforProfile($files);
4314 $nid = $this->uploadImgforNid($nfiles);
4315
4316 $displayname = strstr($email, '@', true); // As of PHP 5.3.0
4317
4318 if ( username_exists( $displayname ) ){
4319 $rand_digits = date('dmyhis');
4320 $last4chars = substr($rand_digits, -4);
4321 $displayname = $displayname.'_'.$last4chars;
4322 }
4323
4324 $user_data = array(
4325 'user_login' => $displayname, // it is username and must be unique
4326 'user_email' => $email,
4327 'user_pass' => $password,
4328 'user_nicename' => $displayname,
4329 'display_name' => $displayname,
4330 'first_name' => $first_name,
4331 'last_name' => $last_name,
4332 'role' => 'subscriber'
4333 );
4334
4335 $user_id = wp_insert_user( $user_data );
4336
4337 $subject = "WiToken Registration";
4338
4339 $body = '<p style="font-size: 14px; line-height: 18px; text-align: left; margin: 0;">
4340 Congratulation. Your account has been created successfully
4341 </p>
4342 <br>
4343 <p><strong>Your email: </strong>'.$email.'</p>
4344 <p><strong>Your password: </strong>'.$password.'</p>
4345 <br>
4346 <p style="font-size: 14px; line-height: 18px; text-align: left; margin: 0;">Welcome to WiFamily. Wi Ecosystem is the World’s first unified block-chain enabled e-commerce, gaming, freelancing, stock music, game </p>
4347 <p style="font-size: 14px; line-height: 18px; text-align: left; margin: 0;">Our goal is to make the entire crypto-currency usage process more approachable to non-geek users. </p>
4348 <p style="font-size: 14px; line-height: 18px; text-align: left; margin: 0;"> Our plan is to create a unified ecosystem catering to users from shoppers, merchants, gamers, freelancers, employers, music artists, advertisers to bloggers and social influencers, allowing them to move frictionlessly throughout our ecosystem.</p>';
4349
4350 $headers = array('Content-Type: text/html; charset=UTF-8');
4351 wp_mail($email, $subject, $body);
4352
4353 if (!is_wp_error($user_id)) {
4354 do_action( 'user_register', $user_id );
4355 add_user_meta( $user_id, 'ico_user_type', 'WiTokenIcoUser');
4356 add_user_meta( $user_id, 'gender', $gender);
4357 add_user_meta( $user_id, 'nationality', $nationality);
4358 add_user_meta( $user_id, 'birth_date', $birth_date);
4359 add_user_meta( $user_id, 'phone_number', $phone_number);
4360 add_user_meta( $user_id, 'address', $address);
4361 add_user_meta( $user_id, 'zip_code', $zip_code);
4362 add_user_meta( $user_id, 'country_code', $country_code);
4363 add_user_meta( $user_id, 'country_id', $country_id);
4364 add_user_meta( $user_id, 'country_name', $country_name);
4365 add_user_meta( $user_id, 'avatar_id', $avatar_id);
4366 add_user_meta( $user_id, 'jas_avatar', $avatar_id);
4367 add_user_meta( $user_id, 'nid', $nid);
4368
4369 $info = array();
4370 $info['user_login'] = $email;
4371 $info['user_password'] = $password;
4372 $info['remember'] = true;
4373 $user_signon = wp_signon( $info, false );
4374
4375 // Login in WiAccount
4376 $ch = curl_init();
4377 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/oauth/token");
4378 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4379 'Content-Type' => 'application/json'
4380 ));
4381 curl_setopt($ch, CURLOPT_POST, 1);
4382 curl_setopt($ch, CURLOPT_POSTFIELDS,
4383 "grant_type=password&client_id=22&client_secret=iT8sHZdMYj20zdCAyg5XTykf6ri1eXVV3N27M79c&username=".$email."&password=".$password."&scope=");
4384 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4385 $server_output = curl_exec($ch);
4386 curl_close ($ch);
4387
4388 $json_data = json_decode($server_output);
4389 if( $json_data->token_type == "Bearer" ){ // check user is in wiaccount
4390 // Add access_token
4391 add_user_meta($user_id, 'access_token', $json_data->access_token);
4392 add_user_meta($user_id, 'refresh_token', $json_data->refresh_token);
4393
4394 }
4395
4396 /***** Call affiliate methods to create referral */
4397 $referred_affid = $post['referred_affid'];
4398 if( !empty($referred_affid) ){
4399 self::$affiliate_id = $post['referred_affid'];
4400 }
4401 $this->save_affiliate($user_id);
4402 $this->update_affiliate_rank_by_uid($user_id, $this->current_rank);
4403 if( !empty($referred_affid) ){
4404 $this->set_mlm_parent($user_id, $referred_affid);
4405 }
4406
4407 return true;
4408
4409 } else {
4410 return $error = $user_id->get_error_message();
4411
4412 }
4413
4414 }
4415
4416
4417 /**
4418 *
4419 * @since 1.0.0
4420 */
4421 public function uploadImgforProfile ($files){
4422
4423
4424 $img = $files ;// your base64 encoded
4425 if ( is_string($img)){
4426 $upload_dir = wp_upload_dir();
4427
4428 // @new
4429 $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
4430 //$img = $_POST['imageData'];
4431 $img = str_replace('data:image/png;base64,', '', $img);
4432 $img = str_replace(' ', '+', $img);
4433
4434 $decoded = base64_decode($img) ;
4435
4436 $filename = 'profile.png';
4437
4438 $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
4439
4440 // @new
4441 $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
4442
4443 // @new
4444 $file = array();
4445 $file['error'] = '';
4446 $file['tmp_name'] = $upload_path . $hashed_filename;
4447 $file['name'] = $hashed_filename;
4448 $file['type'] = 'image/png';
4449 $file['size'] = filesize( $upload_path . $hashed_filename );
4450
4451 // upload file to server
4452 // @new use $file instead of $image_upload
4453 $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
4454
4455 $filename = $file_return['file'];
4456 $attachment = array(
4457 'post_mime_type' => $file_return['type'],
4458 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
4459 'post_content' => '',
4460 'post_status' => 'inherit',
4461 'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
4462 );
4463 $attach_id = wp_insert_attachment( $attachment, $filename, 289 );
4464 require_once(ABSPATH . 'wp-admin/includes/image.php');
4465 $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
4466 wp_update_attachment_metadata( $attach_id, $attach_data );
4467
4468 } else {
4469
4470 require_once( ABSPATH . 'wp-admin/includes/file.php' );
4471 $upload_overrides = array( 'test_form' => false );
4472 $movefile = wp_handle_upload( $files, $upload_overrides);
4473
4474 $upload_dir = wp_upload_dir();
4475 $image_data = file_get_contents($movefile['url']);
4476 $filename = basename($movefile['url']);
4477 if(wp_mkdir_p($upload_dir['path'])){
4478 $file = $upload_dir['path'] . '/' . $filename;
4479 } else {
4480 $file = $upload_dir['basedir'] . '/' . $filename;
4481 }
4482 file_put_contents($file, $image_data);
4483
4484 $wp_filetype = wp_check_filetype($filename, null );
4485 $attachment = array(
4486 'post_mime_type' => $wp_filetype['type'],
4487 'post_title' => sanitize_file_name($filename),
4488 'post_content' => '',
4489 'post_status' => 'inherit'
4490 );
4491 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
4492 require_once(ABSPATH . 'wp-admin/includes/image.php');
4493 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
4494 wp_update_attachment_metadata( $attach_id, $attach_data );
4495 }
4496
4497 return $attach_id;
4498 }
4499
4500
4501 /**
4502 *
4503 * @since 1.0.0
4504 */
4505 public function uploadImgforNid ($nfiles){
4506
4507 $img = $nfiles ;// your base64 encoded
4508 if ( is_string($img)){
4509 $upload_dir = wp_upload_dir();
4510
4511 // @new
4512 $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
4513 //$img = $_POST['imageData'];
4514 $img = str_replace('data:image/png;base64,', '', $img);
4515 $img = str_replace(' ', '+', $img);
4516
4517 $decoded = base64_decode($img) ;
4518
4519 $filename = 'NID.png';
4520
4521 $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
4522
4523 // @new
4524 $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
4525
4526 // @new
4527 $file = array();
4528 $file['error'] = '';
4529 $file['tmp_name'] = $upload_path . $hashed_filename;
4530 $file['name'] = $hashed_filename;
4531 $file['type'] = 'image/png';
4532 $file['size'] = filesize( $upload_path . $hashed_filename );
4533
4534 // upload file to server
4535 // @new use $file instead of $image_upload
4536 $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
4537
4538 $filename = $file_return['file'];
4539 $attachment = array(
4540 'post_mime_type' => $file_return['type'],
4541 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
4542 'post_content' => '',
4543 'post_status' => 'inherit',
4544 'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
4545 );
4546 $attach_id = wp_insert_attachment( $attachment, $filename, 289 );
4547 require_once(ABSPATH . 'wp-admin/includes/image.php');
4548 $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
4549 wp_update_attachment_metadata( $attach_id, $attach_data );
4550
4551
4552
4553 } else {
4554
4555 require_once( ABSPATH . 'wp-admin/includes/file.php' );
4556 $upload_overrides = array( 'test_form' => false );
4557 $movefile = wp_handle_upload( $nfiles, $upload_overrides);
4558
4559 $upload_dir = wp_upload_dir();
4560 $image_data = file_get_contents($movefile['url']);
4561 $filename = basename($movefile['url']);
4562 if(wp_mkdir_p($upload_dir['path'])){
4563 $file = $upload_dir['path'] . '/' . $filename;
4564 } else {
4565 $file = $upload_dir['basedir'] . '/' . $filename;
4566 }
4567 file_put_contents($file, $image_data);
4568
4569 $wp_filetype = wp_check_filetype($filename, null );
4570 $attachment = array(
4571 'post_mime_type' => $wp_filetype['type'],
4572 'post_title' => sanitize_file_name($filename),
4573 'post_content' => '',
4574 'post_status' => 'inherit'
4575 );
4576 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
4577 require_once(ABSPATH . 'wp-admin/includes/image.php');
4578 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
4579 wp_update_attachment_metadata( $attach_id, $attach_data );
4580 }
4581
4582 return $attach_id;
4583 }
4584
4585 public function array_push_assoc($array, $key1, $value1, $key2, $value2, $key3, $value3, $key4, $value4, $key5, $value5){
4586 $array[$key1] = $value1;
4587 $array[$key2] = $value2;
4588 $array[$key3] = $value3;
4589 $array[$key4] = $value4;
4590 $array[$key5] = $value5;
4591 return $array;
4592 }
4593
4594 /**
4595 *
4596 * @since 1.0.0
4597 */
4598 public function get_data($id, $lang){
4599
4600 $data = array();
4601 $final = array();
4602 $args=array(
4603 'posts_per_page' => 50,
4604 'post_type' => 'stm_staff',
4605 'tax_query' => array(
4606 array(
4607 'taxonomy' => 'stm_staff_category',
4608 'terms' => $id
4609 ),
4610 ),
4611
4612 );
4613 $query = new WP_Query( $args );
4614 while ( $query->have_posts() ) : $query->the_post();
4615 $id = get_the_ID();
4616 $title = get_the_title();
4617 $img = wp_get_attachment_url( get_post_thumbnail_id($id) );
4618
4619 if( $lang == "ar" ){
4620 $content = get_post_meta( get_the_ID(), 'excerpt_arabic', true );
4621 } else if( $lang == "cn" ){
4622 $content = get_post_meta( get_the_ID(), 'excerpt_chinese', true );
4623 } else if( $lang == "my" ){
4624 $content = get_post_meta( get_the_ID(), 'excerpt_malay', true );
4625 } else {
4626 $content = get_the_excerpt();
4627 }
4628 array_push($final, $this->array_push_assoc($data, 'id', $id, 'title',$title, 'img', $img, 'content', $content, 'lang', $lang));
4629 endwhile;
4630 $data = array();
4631 return $final;
4632
4633 }
4634
4635
4636 /**
4637 *
4638 * @since 1.0.0
4639 */
4640 public function get_staff( WP_REST_Request $request ){
4641
4642 $team = array();
4643 $cat_des = array();
4644 $staff_cat = get_option( 'jl_pr_options' );
4645
4646 $params = $request->get_params();
4647 $lang = $params['lang']; // Language
4648
4649 foreach ( $staff_cat as $sc ){
4650 $temp = $this->get_data($sc, $lang);
4651 array_push($team, $temp);
4652
4653 if( $lang == "ar" ){
4654 $text = get_term_meta( $sc, 'description_arabic', true);
4655 } else if( $lang == "cn" ){
4656 $text = get_term_meta( $sc, 'description_chinese', true);
4657 } else if( $lang == "my" ){
4658 $text = get_term_meta( $sc, 'description_malay', true);
4659 } else {
4660 $text = category_description($sc);
4661 $text=str_ireplace('<p>','',$text);
4662 $text=str_ireplace('</p>','',$text);
4663 }
4664
4665 array_push($cat_des, $text);
4666 }
4667
4668 return array(
4669 'staff' => $team,
4670 'category' => $cat_des
4671 );
4672 }
4673
4674
4675 /**
4676 *
4677 * @since 1.0.0
4678 */
4679 public function get_staff_details( WP_REST_Request $request ){
4680
4681 $data = array();
4682 $params = $request->get_params();
4683 $postId = $params['postID']; // Staff Id
4684 $lang = $params['lang']; // Language
4685 $posts = get_post($postId);
4686 if( $lang == "en" ){
4687
4688 WPBMap::addAllMappedShortcodes();
4689 $data['content'] = apply_filters('the_content', $posts->post_content);
4690
4691 } else if( $lang == "ar" ){
4692 $data['content'] = get_post_meta( $postId, 'details-arabic', true );
4693 $data['excerpt'] = get_post_meta( $postId, 'excerpt_arabic', true );
4694
4695 } else if( $lang == "cn" ){
4696 $data['content'] = get_post_meta( $postId, 'details_chinese', true );
4697 $data['excerpt'] = get_post_meta( $postId, 'excerpt_chinese', true );
4698 } else if( $lang == "my" ){
4699 $data['content'] = get_post_meta( $postId, 'details_malay', true );
4700 $data['excerpt'] = get_post_meta( $postId, 'excerpt_malay', true );
4701 } else {
4702
4703 WPBMap::addAllMappedShortcodes();
4704 $data['content'] = apply_filters('the_content', $posts->post_content);
4705
4706 }
4707 $data['title'] = $posts->post_title;
4708 $data['img'] = wp_get_attachment_url( get_post_thumbnail_id($postId) );
4709 return $data;
4710
4711 }
4712
4713
4714 /**
4715 *
4716 * @since 1.0.0
4717 */
4718 public function set_token_statement( WP_REST_Request $request ){
4719
4720 $data = array();
4721 $post = (array) $request->get_params();
4722 $user_id = $post['user_id'];
4723 $package_stage_phase_id = $post['package_stage_phase_id'];
4724 $purchase_type = $post['purchase_type'];
4725 $amount = $post['amount'];
4726 $amount = str_replace( ',', '', $amount );
4727 $bonus_type = $post['bonus_type'];
4728 if( $bonus_type == "bonus_percentage" ){
4729 $setting_options = get_option( 'general_info' );
4730 $bonus = $setting_options['gn_pricing_rate'];
4731 $bonus_per = $post['bonus'];
4732 } else {
4733 $bonus = $post['bonus'];
4734 }
4735 //$bonus = $post['bonus'];
4736 $bonus_amount = $post['bonus_amount'];
4737 $pmethod = $post['pmethod'];
4738 $total_token = $post['total_token'];
4739 $total_token = str_replace( ',', '', $total_token );
4740 $token_sale_info = $this->token_sale_bonus_heading_info();
4741 if( $bonus_type == "bonus_percentage" ){
4742 $stage_contact = $token_sale_info['stage'] ." (".$bonus_per."%) ";
4743 } else {
4744 $stage_contact = $token_sale_info['stage'] ." (".$bonus."USD) ";
4745 }
4746 $note = $post['note'];
4747 $access_token = $post['access_token'];
4748 $device_id = $post['device_id'];
4749
4750 if( $pmethod == "PayPal" ){
4751 $status = "approved";
4752 $paypal_email = $post['paypal_email'];
4753 $paypal_transaction_id = $post['paypal_transaction_id'];
4754
4755 } else if( $pmethod == "Stripe" ){
4756 $stripe_transaction_id = $post['transaction_id'];
4757 $balance_transaction = $post['balance_transaction'];
4758 $card_no = $post['card_no'];
4759 $status = "approved";
4760 } else if( $pmethod == "BTC" ){
4761 $btc_quantity = $post['btc_quantity'];
4762 $btc_wallet_add = $post['btc_wallet_add'];
4763 $status = "pending";
4764 } else if( $pmethod == "ETH" ){
4765 $eth_quantity = $post['eth_quantity'];
4766 $eth_wallet_add = $post['eth_wallet_add'];
4767 $status = "pending";
4768 } else if( $pmethod == "Bank" ){
4769 $account_no = $post['account_no'];
4770 $bank_name = $post['bank_name'];
4771 $sender_name = $post['sender_name'];
4772 $sender_address = $post['sender_address'];
4773 $status = "pending";
4774 } else if( $pmethod == "Fomopay" ){
4775 $status = "approved";
4776 } else if( $pmethod == "WiWallet" ){
4777
4778 $general_info = get_option('general_info');
4779 $payment_purpose = $general_info['gn_payment_purpose'];
4780 $wiwallet_info = get_option('wiwallet_info');
4781 $wiwallet_receiver = $wiwallet_info['wiwallet_email'];
4782 $user_info = get_userdata($user_id);
4783 $user_email = $user_info->user_email;
4784
4785 try {
4786 $ch = curl_init();
4787 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
4788 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4789 'Content-Type' => 'application/json',
4790 'Authorization: Bearer '.$access_token
4791 ));
4792 curl_setopt($ch, CURLOPT_POST, 1);
4793 curl_setopt($ch, CURLOPT_POSTFIELDS,
4794 "app_id=67&transaction_type_id=16&client_id=3&device_id=".$device_id."&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=1&transactionDetails[0][transaction_details]=".$payment_purpose."&user_email=".$user_email."&end_user_email=".$wiwallet_receiver."");
4795 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4796 $server_output = curl_exec($ch);
4797 curl_close ($ch);
4798 $json_data = json_decode($server_output);
4799 if( $json_data->status == "success" ){
4800 $status = "approved";
4801 } else {
4802 $status = "pending";
4803 }
4804
4805 } catch (\Exception $e) {
4806 $data['success'] = false;
4807 //$e = $e->getMessage();
4808 $status = "pending";
4809 }
4810 }
4811
4812 global $wpdb;
4813 $table = $wpdb->prefix.'witoken_statement';
4814 $check = $wpdb->insert($table,
4815 array(
4816 'user_id' => $user_id,
4817 'purchase_date' => date('Y-m-d'),
4818 'purchase_type' => $purchase_type,
4819 'package_stage_phase_id' => $package_stage_phase_id,
4820 'amount' => $amount,
4821 'bonus_type' => $bonus_type,
4822 'bonus_per' => $bonus,
4823 'bonus_amount' => $bonus_amount,
4824 'pmethod' => $pmethod,
4825 'total_token' => $total_token,
4826 'sale_stage' => $stage_contact,
4827 'note' => $note,
4828 'status' => $status
4829 )
4830 );
4831 $transaction_id = $wpdb->insert_id;
4832
4833 $affiliate_id = $this->search_affiliate_id_for_current_user($user_id);
4834 $affi_bonus_settings = get_option( 'aff_bonus_info' );
4835 $affi_bonus_type = $affi_bonus_settings['aff_bonus_type'];
4836 $aff_bonus_witoken = $affi_bonus_settings['aff_bonus_witoken']; // if checked exclude bonus witoken
4837 if( empty($affi_bonus_type) ){
4838 $affi_bonus_type = 'usd';
4839 }
4840
4841 if( $affi_bonus_type == 'usd' ){
4842 $this->create_referral($user_id, $affiliate_id, $amount, $status, $transaction_id);
4843 } else {
4844
4845 if( $aff_bonus_witoken == "1" ){
4846 $amount = $total_token - $bonus_amount; //exclude bonus witoken
4847 } else {
4848 $amount = $total_token;
4849 }
4850 $this->create_referral($user_id, $affiliate_id, $amount, $status, $transaction_id);
4851 }
4852
4853 if($check){
4854
4855 if( $pmethod == "BTC" ){
4856 add_post_meta( $transaction_id, 'btc_quantity', $btc_quantity);
4857 add_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
4858 } else if( $pmethod == "ETH" ){
4859 add_post_meta( $transaction_id, 'eth_quantity', $eth_quantity);
4860 add_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
4861 } else if( $pmethod == "Bank" ){
4862 add_post_meta( $transaction_id, 'account_no', $account_no);
4863 add_post_meta( $transaction_id, 'bank_name', $bank_name);
4864 add_post_meta( $transaction_id, 'sender_name', $sender_name);
4865 add_post_meta( $transaction_id, 'sender_address', $sender_address);
4866 } else if( $pmethod == "Stripe" ){
4867 add_post_meta( $transaction_id, 'stripe_transaction_id', $stripe_transaction_id);
4868 add_post_meta( $transaction_id, 'balance_transaction', $balance_transaction);
4869 add_post_meta( $transaction_id, 'card_no', $card_no);
4870 // add_post_meta( $transaction_id, 'name_on_card', $name_on_card);
4871 } else if( $pmethod == "PayPal" ){
4872 add_post_meta( $transaction_id, 'paypal_email', $paypal_email);
4873 add_post_meta( $transaction_id, 'paypal_transaction_id', $paypal_transaction_id);
4874
4875 } else if( $pmethod == "Fomopay" ){
4876
4877 $fomo_response = get_user_meta( $user_id, 'fomo_response', true);
4878 add_post_meta( $transaction_id, 'transaction', $fomo_response['transaction']);
4879 add_post_meta( $transaction_id, 'payment_id', $fomo_response['payment_id']);
4880 add_post_meta( $transaction_id, 'upstream', $fomo_response['upstream']);
4881 add_post_meta( $transaction_id, 'signature', $fomo_response['signature']);
4882 add_post_meta( $transaction_id, 'result', $fomo_response['result']);
4883
4884 $order_explosive = explode('_', $fomo_response['transaction']);
4885 $order_id = absint($order_explosive[1]);
4886 delete_post_meta($order_id, 'user_id' );
4887 delete_user_meta( $user_id, 'fomo_response' );
4888
4889 }
4890
4891 if( $status == "approved" ){
4892
4893 // check package type and update user rank id
4894 if( $purchase_type == "package"){
4895
4896 $subs = explode("-",$package_stage_phase_id);
4897 $package_id = $subs[1];
4898
4899 $this->update_affiliate_rank_for_package($user_id, $package_id);
4900
4901 }
4902
4903 $transaction_details = "WiToken transfer to WiWallet";
4904 $user_info = get_userdata($user_id);
4905 $user_email = $user_info->user_email;
4906
4907 $ch = curl_init();
4908 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
4909 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4910 'Content-Type' => 'application/json',
4911 'Authorization: Bearer '.$access_token
4912 ));
4913 curl_setopt($ch, CURLOPT_POST, 1);
4914 curl_setopt($ch, CURLOPT_POSTFIELDS,
4915 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
4916 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4917 $server_output = curl_exec($ch);
4918 curl_close ($ch);
4919 $json_data = json_decode($server_output);
4920
4921 if( $json_data->status == "success" ){
4922 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
4923 } else {
4924 $errors = $json_data->errors;
4925 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
4926 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
4927 }
4928
4929 // Purchase confirmation email
4930 $to = $user_email;
4931 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
4932 $amount = $amount;
4933 $purchase_date = date('Y-m-d');
4934 $payment_method = $pmethod;
4935 $bonus = $bonus;
4936 $bonus_token = $bonus_amount;
4937 $total_token = $total_token;
4938 $token_stage = $stage_contact;
4939 $email_info = get_option( 'email_settings' );
4940 $subject = $email_info['settings_email_subject2'];
4941 $message = $email_info['settings_email_message2'];
4942 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4943 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4944 $body = str_replace($searchVal, $replaceVal, $message);
4945 wp_mail($to, $subject, $body);
4946
4947 } else {
4948
4949 $user_info = get_userdata($user_id);
4950 $user_email = $user_info->user_email;
4951 // Purchase confirmation email
4952 $to = $user_email;
4953 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
4954 $amount = $amount;
4955 $purchase_date = date('Y-m-d');
4956 $payment_method = $pmethod;
4957 $bonus = $bonus;
4958 $bonus_token = $bonus_amount;
4959 $total_token = $total_token;
4960 $token_stage = $stage_contact;
4961 $email_info = get_option( 'email_settings' );
4962 $subject = $email_info['settings_email_subject'];
4963 $message = $email_info['settings_email_message'];
4964 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4965 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4966 $body = str_replace($searchVal, $replaceVal, $message);
4967 wp_mail($to, $subject, $body);
4968
4969 }
4970 //admin email notification
4971 $email_info = get_option( 'admin_email_settings' );
4972 $subject = $email_info['admin_email_subject'];
4973 $message = $email_info['admin_email_message'];
4974 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
4975 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
4976 $body = str_replace($searchVal, $replaceVal, $message);
4977 $contact_to = get_option('admin_email');
4978 wp_mail($contact_to, $subject, $message);
4979
4980 return 'success';
4981
4982 } else {
4983
4984 return 'error';
4985 }
4986
4987 }
4988
4989
4990 /**
4991 *
4992 * @since 1.0.0
4993 */
4994 public function get_notification(){
4995
4996 $data = array();
4997 $args = array(
4998 'post_type' => 'notifications',
4999 'post_status' => 'publish'
5000 );
5001
5002 $posts = get_posts($args);
5003 $i=0;
5004 foreach( $posts as $post ){
5005 $data[$i]['id'] = $post->ID;
5006 $data[$i]['post_date'] = $post->post_date;
5007 $data[$i]['title'] = $post->post_title;
5008 $data[$i]['content'] = $post->post_excerpt;
5009 $i++;
5010 }
5011 return $data;
5012
5013 }
5014
5015
5016 /**
5017 *
5018 * @since 1.0.0
5019 */
5020 public function get_single_notification( WP_REST_Request $request ){
5021
5022 $data = array();
5023 $postId = $request->get_param( 'post_id' ); // post_id
5024 $args = array(
5025 'post_type' => 'notifications',
5026 'post_status' => 'publish',
5027 'p' => $postId
5028 );
5029 $posts = get_posts($args);
5030 $i = 0;
5031 foreach( $posts as $post ){
5032 $data[$i]['id'] = $post->ID;
5033 $data[$i]['post_date'] = $post->post_date;
5034 $data[$i]['title'] = $post->post_title;
5035 $data[$i]['content'] = $post->post_content;
5036 $data[$i]['img'] = wp_get_attachment_url( get_post_thumbnail_id($postId) );
5037 $i++;
5038 }
5039 return $data;
5040
5041 }
5042
5043
5044 /**
5045 *
5046 * @since 1.0.0
5047 */
5048 public function get_btc_rate( WP_REST_Request $request ){
5049
5050 $content = array();
5051 $bitcoin_info = get_option('bitcoin_info');
5052 if( !empty($bitcoin_info['btc_percentage']) && array_key_exists("btc_percentage",$bitcoin_info) )
5053 {
5054 $deducation_percentage = $bitcoin_info['btc_percentage'];
5055 } else {
5056 $deducation_percentage = 0;
5057 }
5058
5059 $json = file_get_contents('https://api.coinbase.com/v2/prices/spot?currency=USD');
5060 $data = json_decode($json, TRUE);
5061 $deduction_rate = ($data['data']['amount'] - ($data['data']['amount']*$deducation_percentage)/100);
5062 $content['btc'] = $deduction_rate;
5063 $content['currect_rate'] = $data['data']['amount'];
5064 $content['apercentage'] = ($data['data']['amount']*$deducation_percentage)/100;
5065 $content['dpercentage'] = $deducation_percentage;
5066 return $content;
5067
5068 }
5069
5070 /**
5071 *
5072 * @since 1.0.0
5073 */
5074 public function get_eth_rate( WP_REST_Request $request ){
5075
5076 $content = array();
5077 $eth_info = get_option('ethereum_info');
5078 if( !empty($eth_info['eth_percentage'] ) && array_key_exists( "eth_percentage",$eth_info) ){
5079 $deducation_percentage = $eth_info['eth_percentage'];
5080 } else {
5081 $deducation_percentage = 0;
5082 }
5083
5084 $json = wp_remote_get('https://api.coinmarketcap.com/v1/ticker/ethereum/');
5085 $data = json_decode($json['body']);
5086 $deduction_rate = ($data[0]->price_usd -($data[0]->price_usd*$deducation_percentage)/100);
5087 $content['eth'] = $deduction_rate;
5088 $content['currect_rate'] = $data[0]->price_usd;
5089 $content['apercentage'] = ($data[0]->price_usd*$deducation_percentage)/100;
5090 $content['dpercentage'] = $deducation_percentage;
5091 return $content;
5092
5093 }
5094
5095
5096 /**
5097 *
5098 * @since 1.0.0
5099 */
5100 public function update_verify_bank_transaction( WP_REST_Request $request ){
5101
5102 $data = array();
5103 $post = (array) $request->get_params();
5104 $transaction_id = $post['transaction_id'];
5105 $bank_ref_id = $post['bank_ref_id'];
5106 $bank_trans_date = $post['bank_trans_date'];
5107
5108 $files = $_FILES['file'];
5109 $old_bank_trans_slip = get_post_meta( $transaction_id, 'bank_trans_slip', true);
5110
5111 if( !empty($old_bank_trans_slip) ){
5112 wp_delete_attachment( $old_bank_trans_slip );
5113 }
5114
5115 require_once( ABSPATH . 'wp-admin/includes/file.php' );
5116 $upload_overrides = array( 'test_form' => false );
5117 $movefile = wp_handle_upload( $files, $upload_overrides);
5118
5119 $upload_dir = wp_upload_dir();
5120 $image_data = file_get_contents($movefile['url']);
5121 $filename = basename($movefile['url']);
5122 if(wp_mkdir_p($upload_dir['path'])){
5123 $file = $upload_dir['path'] . '/' . $filename;
5124 } else {
5125 $file = $upload_dir['basedir'] . '/' . $filename;
5126 }
5127 file_put_contents($file, $image_data);
5128
5129 $wp_filetype = wp_check_filetype($filename, null );
5130 $attachment = array(
5131 'post_mime_type' => $wp_filetype['type'],
5132 'post_title' => sanitize_file_name($filename),
5133 'post_content' => '',
5134 'post_status' => 'inherit'
5135 );
5136 $attach_id = wp_insert_attachment( $attachment, $file, 1 );
5137 require_once(ABSPATH . 'wp-admin/includes/image.php');
5138 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
5139 wp_update_attachment_metadata( $attach_id, $attach_data );
5140
5141
5142 $post_meta_bank_txhash = get_post_meta( $transaction_id, 'bank_ref_id', true );
5143 $post_meta_bank_trans_date = get_post_meta( $transaction_id, 'bank_trans_date', true );
5144 $post_meta_bank_trans_slip = get_post_meta( $transaction_id, 'bank_trans_slip', true );
5145
5146 if( !empty($post_meta_bank_txhash) ){
5147 update_post_meta( $transaction_id, 'bank_ref_id', $bank_ref_id);
5148 } else {
5149 add_post_meta( $transaction_id, 'bank_ref_id', $bank_ref_id);
5150 }
5151
5152 if( !empty($post_meta_bank_trans_date) ){
5153 update_post_meta( $transaction_id, 'bank_trans_date', $bank_trans_date);
5154 } else {
5155 add_post_meta( $transaction_id, 'bank_trans_date', $bank_trans_date);
5156 }
5157
5158 if( !empty($post_meta_bank_trans_slip) ){
5159 update_post_meta( $transaction_id, 'bank_trans_slip', $attach_id);
5160 } else {
5161 add_post_meta( $transaction_id, 'bank_trans_slip', $attach_id);
5162 }
5163
5164 global $wpdb;
5165 $table = $wpdb->prefix.'witoken_statement';
5166 $wpdb->update( $table, array( 'status' => 'waiting' ), array( 'id' => $transaction_id ));
5167
5168 $data["status"] = true;
5169 return $data;
5170 }
5171
5172 public function retrieve_fomo_response_status( WP_REST_Request $request ){
5173
5174 $data = array();
5175 $user_id = $request->get_params();
5176 $fomo = get_user_meta( $user_id['user_id'], 'fomo_response', true );
5177 return $fomo['result'];
5178
5179 }
5180
5181 public function get_refferal()
5182 {
5183 global $wpdb;
5184 $table_name = $wpdb->prefix . "jas_affiliates";
5185 $status = 1;
5186 $datum = $wpdb->get_results("SELECT * FROM " . $table_name . " WHERE status=1");
5187 $min = 0;
5188 foreach($datum as $key => $value) {
5189 $user_info = get_userdata($value->uid);
5190 $authorname = $user_info->first_name." ".$user_info->last_name.", ".$user_info->country_name;
5191 $user[$min]['user_id'] = $value->uid;
5192 $user[$min]['affid'] = $value->id;
5193 $user[$min]['name'] = $authorname;
5194 $min++;
5195 }
5196
5197 return array(
5198 "userlist" => $user
5199 );
5200 }
5201
5202
5203
5204 public function get_header( WP_REST_Request $request ){
5205
5206 require JAS_PATH . 'public/Affiliate_Account_Page.class.api.php';
5207 $user_id = $request->get_params( 'user_id' );
5208 $affiliate_id = $this->affiliate_get_id_by_uid($user_id['user_id']);
5209 $obj = new Affiliate_Account_Page_Api($user_id['user_id'], $affiliate_id);
5210 $output = $obj->head();
5211 return $output;
5212 }
5213
5214 public function get_affiliate_info( WP_REST_Request $request ){
5215
5216 require JAS_PATH . 'public/Affiliate_Account_Page.class.api.php';
5217 $user_id = $request->get_params( 'user_id' );
5218 $affiliate_id = $this->affiliate_get_id_by_uid($user_id['user_id']);
5219 $obj = new Affiliate_Account_Page_Api($user_id['user_id'], $affiliate_id);
5220 $output = $obj->output($user_id['tab']);
5221 return $output;
5222 }
5223
5224 public function get_search_referrals( WP_REST_Request $request ){
5225
5226 require JAS_PATH . 'public/Affiliate_Account_Page.class.api.php';
5227 $params = $request->get_params();
5228 $affiliate_id = $this->affiliate_get_id_by_uid($params['user_id']);
5229 $obj = new Affiliate_Account_Page_Api($params['user_id'], $affiliate_id);
5230 $output = $obj->search_referrals($params['ref_from_date'],$params['ref_to_date'],$params['status'],$params['page'],$affiliate_id);
5231 return $output;
5232 }
5233
5234 public function get_search_referrals_history( WP_REST_Request $request ){
5235
5236 require JAS_PATH . 'public/Affiliate_Account_Page.class.api.php';
5237 $params = $request->get_params();
5238 $affiliate_id = $this->affiliate_get_id_by_uid($params['user_id']);
5239 $obj = new Affiliate_Account_Page_Api($params['user_id'], $affiliate_id);
5240 $output = $obj->search_referrals_history($params['ref_from_date'],$params['ref_to_date'],$params['status'],$params['page'],$affiliate_id);
5241 return $output;
5242 }
5243
5244 public function get_search_visits( WP_REST_Request $request ){
5245
5246 require JAS_PATH . 'public/Affiliate_Account_Page.class.api.php';
5247 $params = $request->get_params();
5248 $affiliate_id = $this->affiliate_get_id_by_uid($params['user_id']);
5249 $obj = new Affiliate_Account_Page_Api($params['user_id'], $affiliate_id);
5250 $output = $obj->search_visits($params['ref_from_date'],$params['ref_to_date'],$params['page'],$affiliate_id);
5251 return $output;
5252 }
5253
5254 public function run_save_payment_settings( WP_REST_Request $request ){
5255
5256 $data = array();
5257 $post = (array) $request->get_params();
5258 $pmethod = $post['pmethod'];
5259 $user_id = $post['user_id'];
5260
5261 update_user_meta($user_id, 'jas_affiliate_payment_type', $pmethod);
5262
5263 if( $pmethod =="bt" ){
5264 $user_id = $post['user_id'];
5265 $bank_ac = $post['bank_ac'];
5266 $bank_ac_holder = $post['bank_ac_holder'];
5267 $bank_name = $post['bank_name'];
5268 $bank_address = $post['bank_address'];
5269 update_user_meta($user_id, 'jas_affiliate_bank_ac', $bank_ac);
5270 update_user_meta($user_id, 'jas_affiliate_bank_ac_holder', $bank_ac_holder);
5271 update_user_meta($user_id, 'jas_affiliate_bank_name', $bank_name);
5272 update_user_meta($user_id, 'jas_affiliate_bank_address', $bank_address);
5273
5274 } else if( $pmethod =="PayPal" ){
5275 $user_id = $post['user_id'];
5276 $paypal_email = $post['paypal_email'];
5277 update_user_meta($user_id, 'jas_affiliate_paypal_email', $paypal_email);
5278
5279 } else if( $pmethod =="stripe" ){
5280 $jas_affiliate_stripe_email = $post['jas_affiliate_stripe_email'];
5281 update_user_meta($user_id, 'jas_affiliate_stripe_email', $jas_affiliate_stripe_email);
5282
5283 } else if( $pmethod =="alipay/wechatpay" ){
5284 $jas_affiliate_alipay_wechatpay = $post['jas_affiliate_alipay_wechatpay'];
5285 update_user_meta($user_id, 'jas_affiliate_alipay_wechatpay', $jas_affiliate_alipay_wechatpay);
5286 }
5287
5288 $data['status'] = true;
5289 return $data;
5290 }
5291
5292 public function run_save_campaign( WP_REST_Request $request ){
5293
5294 $data = array();
5295 $post = (array) $request->get_params();
5296 $name = $post['campaign_name'];
5297 $user_id = $post['user_id'];
5298 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
5299
5300 global $wpdb;
5301 $table = $wpdb->prefix . 'jas_campaigns';
5302 $q = $wpdb->prepare("INSERT INTO $table VALUE(NULL, %s, %d, 0, 0, 0);", $name, $affiliate_id);
5303 $insert = $wpdb->query($q);
5304 if( $insert ){
5305 $data['success'] = true;
5306 } else {
5307 $data['success'] = false;
5308 }
5309 return $data;
5310 }
5311
5312 public function run_delete_campaign( WP_REST_Request $request ){
5313
5314 $data = array();
5315 $post = (array) $request->get_params();
5316 $name = $post['campaign_name'];
5317 $user_id = $post['user_id'];
5318 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
5319
5320 global $wpdb;
5321 $table = $wpdb->prefix . 'jas_campaigns';
5322 $q = $wpdb->prepare("DELETE FROM $table WHERE name=%s AND affiliate_id=%d ", $name, $affiliate_id);
5323 $del = $wpdb->query($q);
5324 if( $del ){
5325 $data['success'] = true;
5326 } else {
5327 $data['success'] = false;
5328 }
5329 return $data;
5330
5331 }
5332
5333 public function run_generate_affiliate_link( WP_REST_Request $request ){
5334
5335 $post = (array) $request->get_params();
5336 $user_id = $post['user_id'];
5337 $campaign = $post['campaign'];
5338 $friendly_links = $post['friendly_links'];
5339 $slug = $post['slug'];
5340 $url = $post['url'];
5341 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
5342
5343 if (!empty($affiliate_id) && !empty($url)){
5344 $param = 'ref';
5345 $value = $affiliate_id;
5346 $campaign_variable = '';
5347 $campaign_value = '';
5348
5349
5350 $settings = $this->return_settings_from_wp_option('general-settings');
5351 if (!empty($settings['jas_referral_variable'])){
5352 $param = $settings['jas_referral_variable'];
5353 }
5354
5355 $uid = $this->get_uid_by_affiliate_id($affiliate_id);
5356 if (!empty($slug)){
5357 $slug = $this->get_custom_slug_for_uid($uid);
5358 if ($slug){
5359 $value = $slug;
5360 }
5361 } else if ($uid && $settings['jas_default_ref_format']=='username'){
5362 $user_info = get_userdata($uid);
5363 if (!empty($user_info->user_login)){
5364 ///$value = $user_info->user_login;
5365 $value = urlencode($user_info->user_login);
5366 }
5367 }
5368
5369 //$url = $_REQUEST['url'];
5370
5371 if (!empty($campaign)){
5372 $campaign_variable = get_option('jas_campaign_variable');
5373 $campaign_value = $campaign;
5374 }
5375
5376 $arr['url'] = jas_create_affiliate_link($url, $param, $value, $campaign_variable, $campaign_value, $friendly_links);
5377 $arr['social'] = '';
5378 $arr['qr'] = '';
5379 /// SOCIAL
5380 if (jas_is_social_share_intalled_and_active() && get_option('jas_social_share_enable')){
5381 $shortcode = get_option('jas_social_share_shortcode');
5382 if ($shortcode){
5383 $shortcode = stripslashes($shortcode);
5384 $shortcode = str_replace(']', '', $shortcode);
5385 $shortcode .= " is_affiliates=1"; ///just for safe
5386 $shortcode .= " custom_description='" . get_option('jas_social_share_message') . "'";
5387 $shortcode .= " jas_no_fb_js=1 ";
5388 $shortcode .= " custom_url='" . $arr['url'] ."']";
5389 $arr['social'] = do_shortcode($shortcode);
5390 }
5391 }
5392 /// QR CODE
5393 if ($this->is_magic_feat_enable('qr_code')){
5394 $img = jas_generate_qr_code($arr['url'], $_REQUEST['aff_id'] . '_custom_url');
5395 $arr['qr_code'] = '<div class="jas-qr-code-wrapper">
5396 <img src="' . $img . '" />
5397 <a href="' . $img . '" download="' . basename($img) . '" class="jas-qr-code-download">' . __('Download', 'jas') . '</a>
5398 </div>';
5399 }
5400
5401 return $arr;
5402 }
5403 }
5404
5405
5406 public function check_input_btc_address($inputs, $btc_wallet_add)
5407 {
5408 $exists = false;
5409 if( is_array($inputs) )
5410 {
5411 for( $i=0; $i<count($inputs); $i++)
5412 {
5413 if($inputs[$i]['addresses']){
5414 $add_arr = $inputs[$i]['addresses'];
5415 for( $j=0; $j<count($add_arr); $j++){
5416 if( $btc_wallet_add == $add_arr[$j] ){
5417 $exists = true;
5418 break;
5419 }
5420 }
5421 }
5422 }
5423 }
5424 return $exists;
5425 }
5426
5427 public function check_output_btc_address($outputs, $witoken_global_btc_add){
5428
5429 $btc_value = 0;
5430 if( is_array($outputs) )
5431 {
5432 for( $i=0; $i<count($outputs); $i++)
5433 {
5434 if($outputs[$i]['addresses']){
5435 $add_arr = $outputs[$i]['addresses'];
5436 for( $j=0; $j<count($add_arr); $j++){
5437 if( $add_arr[$j] == $witoken_global_btc_add){
5438 $btc_value = $outputs[$i]['value'];
5439 }
5440 }
5441 }
5442 }
5443 }
5444 return $btc_value;
5445 }
5446
5447 /**
5448 *
5449 * @since 1.0.0
5450 */
5451 public function update_verify_btc_transaction( WP_REST_Request $request ){
5452
5453 global $wpdb;
5454 $table = $wpdb->prefix.'witoken_statement';
5455 $data = array();
5456 $get_btc_value = 0;
5457 $actual_btc = 0;
5458
5459 $bitcoin_info = get_option('bitcoin_info');
5460 $witoken_global_btc_add = $bitcoin_info['btc_address'];
5461
5462 $post = (array) $request->get_params();
5463 $transaction_id = $post['transaction_id'];
5464 $btc_txhash = $post['btc_txhash'];
5465 $btc_trans_date = $post['btc_trans_date'];
5466 $btc_wallet_add = $post['btc_wallet_add'];
5467
5468 $user_id = $post['user_id'];
5469 $access_token = $post['access_token'];
5470 $total_token = $post['total_token'];
5471
5472 try{
5473
5474 $btc_api = "https://api.blockcypher.com/v1/btc/main/txs/".$btc_txhash;
5475
5476 $ch = curl_init();
5477 curl_setopt($ch, CURLOPT_URL, $btc_api);
5478 curl_setopt($ch, CURLOPT_GET, 1);
5479 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5480 $server_output = curl_exec($ch);
5481 curl_close ($ch);
5482 $data = json_decode($server_output, true);
5483
5484 if(is_array($data)){
5485 if( array_key_exists("inputs", $data) && is_array($data['inputs'])){
5486
5487 $inputs = $data['inputs'];
5488 $this->check_input_btc_address($inputs, $btc_wallet_add);
5489
5490 if( $this->check_input_btc_address($inputs, $btc_wallet_add) == true ){ // check users btc wallet exists in inputs
5491 $get_btc_value = $this->check_output_btc_address($data['outputs'], $witoken_global_btc_add);
5492
5493
5494 if( '0' == $get_btc_value || empty($get_btc_value) ){ // WITOKEN GLOBAL btc wallet address not found
5495
5496 $data['success'] = false;
5497 $data['msg'] = esc_html__( "Receiver wallet address does not match with WiToken wallet address", "jl-token-helper");
5498
5499
5500 } else {
5501
5502 // BTC quantity user sent
5503 $btc_quantity = get_post_meta( $transaction_id, 'btc_quantity', true );
5504 $received_btc = $get_btc_value/100000000; // value/1 tahoshi
5505
5506 $get_sub_value = $received_btc - $btc_quantity;
5507 $difference = ($get_sub_value/$btc_quantity)*100;
5508
5509 $general_info = get_option('general_info');
5510 $btc_allow_upto = $general_info['gn_btc_allow_difference'];
5511
5512 $ttrack_tbl = $wpdb->prefix . 'transaction_track';
5513 $sql = "SELECT * FROM $ttrack_tbl WHERE transaction ='".$btc_txhash."' AND token_id = '".$transaction_id."'";
5514 $get_ttrack = $wpdb->get_results($sql);
5515
5516 // Final check if transaction already exists in DB and will be stop verify processing
5517 if( count($get_ttrack) > 0 ){
5518
5519 $data['success'] = false;
5520 $data['msg'] = esc_html__( "Transaction already exists. Please contact with our support team to get further assist.", 'jl-token-helper' );
5521
5522 } else {
5523
5524 if( $difference < 0 ){
5525
5526 if( abs($difference) <= $btc_allow_upto ){
5527
5528 $post_meta_btc_txhash = get_post_meta( $transaction_id, 'btc_txhash', true );
5529 $post_meta_btc_trans_date = get_post_meta( $transaction_id, 'btc_trans_date', true );
5530 $post_meta_btc_wallet_add = get_post_meta( $transaction_id, 'btc_wallet_add', true );
5531
5532 if( !empty($post_meta_btc_wallet_add) ){
5533 update_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
5534 } else {
5535 add_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
5536 }
5537
5538 if( !empty($post_meta_btc_txhash) ){
5539 update_post_meta( $transaction_id, 'btc_txhash', $btc_txhash);
5540 } else {
5541 add_post_meta( $transaction_id, 'btc_txhash', $btc_txhash);
5542 }
5543
5544 if( !empty($post_meta_btc_trans_date) ){
5545 update_post_meta( $transaction_id, 'btc_trans_date', $btc_trans_date);
5546 } else {
5547 add_post_meta( $transaction_id, 'btc_trans_date', $btc_trans_date);
5548 }
5549
5550 $wpdb->update( $table, array( 'status' => 'approved' ), array( 'id' => $transaction_id ));
5551
5552 //We get payments that's why referrals will be approved
5553 $get_ref_ids = get_post_meta( $transaction_id, 'witoken_ref', false); // retrieve referral ids as array that's here must be put meta unique is false
5554 $tbl_ref = $wpdb->prefix . 'jas_referrals';
5555 if( is_array($get_ref_ids) ){
5556 foreach( $get_ref_ids as $id ){
5557 $wpdb->update( $tbl_ref, array( 'bonus_status' => 'approved' ), array( 'id' => $id ));
5558
5559 /************** Affiliate bonus transfer to WiWallet ****************/
5560 $sql = "SELECT * FROM $tbl_ref WHERE id = ".$id."";
5561 $referral_info = $wpdb->get_results($sql);
5562 $affiliate_parent_user_id = $this->get_uid_by_affiliate_id($referral_info[0]->affiliate_id);
5563
5564 $access_token = get_user_meta( $affiliate_parent_user_id, 'access_token', true );
5565 if( $referral_info[0]->bonus_type == "usd" ){
5566 $currency_type = 1;
5567 $transaction_details = "Affiliate bonus transfer to WiWallet";
5568 } else {
5569 $currency_type = 94;
5570 $transaction_details = "Affiliate WiToken transfer to WiWallet";
5571 }
5572
5573 $user_info = get_userdata($affiliate_parent_user_id);
5574 $user_email = $user_info->user_email;
5575 $amount = $referral_info[0]->amount;
5576
5577 $ch = curl_init();
5578 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
5579 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5580 'Content-Type' => 'application/json',
5581 'Authorization: Bearer '.$access_token
5582 ));
5583 curl_setopt($ch, CURLOPT_POST, 1);
5584 curl_setopt($ch, CURLOPT_POSTFIELDS,
5585 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
5586 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5587 $server_output = curl_exec($ch);
5588 curl_close ($ch);
5589 $json_data = json_decode($server_output);
5590
5591 if( $json_data->status == "success" ){
5592 add_post_meta( $id, 'wiwallet_transfer', 'success' );
5593 } else {
5594 $errors = $json_data->errors;
5595 add_post_meta( $id, 'wiwallet_transfer', 'failed' );
5596 add_post_meta( $id, 'wiwallet_transfer_errors', $errors );
5597 }
5598 // End WiToken transfer to WiWallet
5599
5600 }
5601 }
5602
5603 // Added transaction in transaction_track
5604 $wpdb->insert($ttrack_tbl, array(
5605 'token_id' => $transaction_id,
5606 'transaction' => $btc_txhash,
5607 ));
5608
5609 // WiToken transfer to WiWallet
5610 $sql = "SELECT * FROM $table WHERE id = ".$transaction_id."";
5611 $witoken_info = $wpdb->get_results($sql);
5612 $transaction_details = "WiToken transfer to WiWallet";
5613 $user_info = get_userdata($user_id);
5614 $user_email = $user_info->user_email;
5615
5616 $ch = curl_init();
5617 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
5618 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5619 'Content-Type' => 'application/json',
5620 'Authorization: Bearer '.$access_token
5621 ));
5622 curl_setopt($ch, CURLOPT_POST, 1);
5623 curl_setopt($ch, CURLOPT_POSTFIELDS,
5624 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
5625 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5626 $server_output = curl_exec($ch);
5627 curl_close ($ch);
5628 $json_data = json_decode($server_output);
5629
5630 if( $json_data->status == "success" ){
5631 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
5632 } else {
5633 $errors = $json_data->errors;
5634 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
5635 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
5636 }
5637 // End Transfer WiToken to wallet
5638
5639
5640 // Purchase confirmation email
5641 $to = $user_email;
5642 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
5643 $amount = $witoken_info[0]->amount;
5644 $purchase_date = $witoken_info[0]->purchase_date;
5645 $payment_method = $witoken_info[0]->pmethod;
5646 $bonus = $witoken_info[0]->bonus_per;
5647 $bonus_token = $witoken_info[0]->bonus_amount;
5648 $total_token = $witoken_info[0]->total_token;
5649 $token_stage = $witoken_info[0]->sale_stage;
5650 $email_info = get_option( 'email_settings' );
5651 $subject = $email_info['settings_email_subject2'];
5652 $message = $email_info['settings_email_message2'];
5653 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
5654 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
5655 $body = str_replace($searchVal, $replaceVal, $message);
5656 wp_mail($to, $subject, $body);
5657
5658 $data['success'] = true;
5659 $data['msg'] = esc_html__( "BTC payment is verified.", "jl-token-helper" );
5660
5661 } else {
5662
5663 $msg = "Payment is unverified. You should sent '".$btc_quantity."' BTC, but we'd received '".$received_btc."' BTC, which is more than ".$btc_allow_upto."%. Please contact WiToken Global support center for further assistance";
5664 $data['success'] = false;
5665 $data['msg'] = $msg;
5666
5667 }
5668
5669
5670 } else {
5671
5672 $post_meta_btc_txhash = get_post_meta( $transaction_id, 'btc_txhash', true );
5673 $post_meta_btc_trans_date = get_post_meta( $transaction_id, 'btc_trans_date', true );
5674 $post_meta_btc_wallet_add = get_post_meta( $transaction_id, 'btc_wallet_add', true );
5675
5676 if( !empty($post_meta_btc_wallet_add) ){
5677 update_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
5678 } else {
5679 add_post_meta( $transaction_id, 'btc_wallet_add', $btc_wallet_add);
5680 }
5681
5682 if( !empty($post_meta_btc_txhash) ){
5683 update_post_meta( $transaction_id, 'btc_txhash', $btc_txhash);
5684 } else {
5685 add_post_meta( $transaction_id, 'btc_txhash', $btc_txhash);
5686 }
5687
5688 if( !empty($post_meta_btc_trans_date) ){
5689 update_post_meta( $transaction_id, 'btc_trans_date', $btc_trans_date);
5690 } else {
5691 add_post_meta( $transaction_id, 'btc_trans_date', $btc_trans_date);
5692 }
5693
5694 $wpdb->update( $table, array( 'status' => 'approved' ), array( 'id' => $transaction_id ));
5695
5696 //We get payments that's why referrals will be approved
5697 $get_ref_ids = get_post_meta( $transaction_id, 'witoken_ref', false); // retrieve referral ids as array that's here must be put meta unique is false
5698 $tbl_ref = $wpdb->prefix . 'jas_referrals';
5699 if( is_array($get_ref_ids) ){
5700 foreach( $get_ref_ids as $id ){
5701 $wpdb->update( $tbl_ref, array( 'bonus_status' => 'approved' ), array( 'id' => $id ));
5702
5703 /************** Affiliate bonus transfer to WiWallet ****************/
5704 $sql = "SELECT * FROM $tbl_ref WHERE id = ".$id."";
5705 $referral_info = $wpdb->get_results($sql);
5706 $affiliate_parent_user_id = $this->get_uid_by_affiliate_id($referral_info[0]->affiliate_id);
5707
5708 $access_token = get_user_meta( $affiliate_parent_user_id, 'access_token', true );
5709 if( $referral_info[0]->bonus_type == "usd" ){
5710 $currency_type = 1;
5711 $transaction_details = "Affiliate bonus transfer to WiWallet";
5712 } else {
5713 $currency_type = 94;
5714 $transaction_details = "Affiliate WiToken transfer to WiWallet";
5715 }
5716
5717 $user_info = get_userdata($affiliate_parent_user_id);
5718 $user_email = $user_info->user_email;
5719 $amount = $referral_info[0]->amount;
5720
5721 $ch = curl_init();
5722 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
5723 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5724 'Content-Type' => 'application/json',
5725 'Authorization: Bearer '.$access_token
5726 ));
5727 curl_setopt($ch, CURLOPT_POST, 1);
5728 curl_setopt($ch, CURLOPT_POSTFIELDS,
5729 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
5730 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5731 $server_output = curl_exec($ch);
5732 curl_close ($ch);
5733 $json_data = json_decode($server_output);
5734
5735 if( $json_data->status == "success" ){
5736 add_post_meta( $id, 'wiwallet_transfer', 'success' );
5737 } else {
5738 $errors = $json_data->errors;
5739 add_post_meta( $id, 'wiwallet_transfer', 'failed' );
5740 add_post_meta( $id, 'wiwallet_transfer_errors', $errors );
5741 }
5742 // End WiToken transfer to WiWallet
5743
5744 }
5745 }
5746
5747 // Added transaction in transaction_track
5748 $wpdb->insert($ttrack_tbl, array(
5749 'token_id' => $transaction_id,
5750 'transaction' => $btc_txhash,
5751 ));
5752
5753 $transaction_details = "WiToken transfer to WiWallet";
5754 $user_info = get_userdata($user_id);
5755 $user_email = $user_info->user_email;
5756
5757 $ch = curl_init();
5758 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
5759 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5760 'Content-Type' => 'application/json',
5761 'Authorization: Bearer '.$access_token
5762 ));
5763 curl_setopt($ch, CURLOPT_POST, 1);
5764 curl_setopt($ch, CURLOPT_POSTFIELDS,
5765 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
5766 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5767 $server_output = curl_exec($ch);
5768 curl_close ($ch);
5769 $json_data = json_decode($server_output);
5770
5771 if( $json_data->status == "success" ){
5772 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
5773 } else {
5774 $errors = $json_data->errors;
5775 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
5776 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
5777 }
5778
5779 // WiToken transfer to WiWallet
5780 $sql = "SELECT * FROM $table WHERE id = ".$transaction_id."";
5781 $witoken_info = $wpdb->get_results($sql);
5782 // Purchase confirmation email
5783 $to = $user_email;
5784 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
5785 $amount = $witoken_info[0]->amount;
5786 $purchase_date = $witoken_info[0]->purchase_date;
5787 $payment_method = $witoken_info[0]->pmethod;
5788 $bonus = $witoken_info[0]->bonus_per;
5789 $bonus_token = $witoken_info[0]->bonus_amount;
5790 $total_token = $witoken_info[0]->total_token;
5791 $token_stage = $witoken_info[0]->sale_stage;
5792 $email_info = get_option( 'email_settings' );
5793 $subject = $email_info['settings_email_subject2'];
5794 $message = $email_info['settings_email_message2'];
5795 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
5796 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
5797 $body = str_replace($searchVal, $replaceVal, $message);
5798 wp_mail($to, $subject, $body);
5799
5800 $data['success'] = true;
5801 $data['msg'] = esc_html__( "BTC payment is verified.", "jl-token-helper" );
5802
5803
5804 }
5805
5806 }
5807 }
5808
5809 } else { // User BTC wallet address not found in transaction
5810
5811 $data['success'] = false;
5812 $data['msg'] = esc_html__( "Sender wallet address does not match with the wallet address entered in the system", 'jl-token-helper' );
5813 }
5814
5815 } else {
5816
5817 $data['success'] = false;
5818 $data['msg'] = esc_html__( 'Sender wallet address or transaction not found', 'jl-token-helper' );
5819 }
5820
5821 } else { // Data not found
5822
5823 $data['success'] = false;
5824 $data['msg'] = esc_html__( 'Transaction not found', 'jl-token-helper' );
5825 }
5826
5827
5828 } catch (Exception $e){
5829 $return = array(
5830 'success' => false,
5831 'msg' => $e->getMessage()
5832 );
5833
5834 $data['success'] = false;
5835 $data['msg'] = $e->getMessage() ;
5836
5837 }
5838
5839 return $data;
5840
5841 }
5842
5843
5844
5845 public function check_input_eth_address($inputs, $eth_wallet_add)
5846 {
5847 $exists = false;
5848 if( is_array($inputs) )
5849 {
5850 for( $i=0; $i<count($inputs); $i++)
5851 {
5852 if($inputs[$i]['addresses']){
5853 $add_arr = $inputs[$i]['addresses'];
5854 for( $j=0; $j<count($add_arr); $j++){
5855 if( $eth_wallet_add == $add_arr[$j] ){
5856 $exists = true;
5857 break;
5858 }
5859 }
5860 }
5861 }
5862 }
5863 return $exists;
5864 }
5865
5866 public function check_output_eth_address($outputs, $witoken_global_eth_add){
5867
5868 $eth_value = 0;
5869 if( is_array($outputs) )
5870 {
5871 for( $i=0; $i<count($outputs); $i++)
5872 {
5873 if($outputs[$i]['addresses']){
5874 $add_arr = $outputs[$i]['addresses'];
5875 for( $j=0; $j<count($add_arr); $j++){
5876 if( $add_arr[$j] == $witoken_global_eth_add){
5877 $eth_value = $outputs[$i]['value'];
5878 }
5879 }
5880 }
5881 }
5882 }
5883 return $eth_value;
5884 }
5885
5886
5887 /**
5888 *
5889 * @since 1.0.0
5890 */
5891 public function update_verify_eth_transaction( WP_REST_Request $request ){
5892
5893 global $wpdb;
5894 $table = $wpdb->prefix.'witoken_statement';
5895 $data = array();
5896 $get_eth_value = 0;
5897 $actual_eth = 0;
5898
5899 $ethereum_info = get_option('ethereum_info');
5900 $witoken_global_eth_add = $ethereum_info['eth_address'];
5901
5902 $post = (array) $request->get_params();
5903 $transaction_id = $post['transaction_id'];
5904 $eth_txhash = $post['eth_txhash'];
5905 $eth_trans_date = $post['eth_trans_date'];
5906 $eth_wallet_add = $post['eth_wallet_add'];
5907
5908 $user_id = $post['user_id'];
5909 $access_token = $post['access_token'];
5910 $total_token = $post['total_token'];
5911
5912 $eth_api = "https://api.blockcypher.com/v1/eth/main/txs/".$eth_txhash;
5913
5914 try{
5915
5916 $ch = curl_init();
5917 curl_setopt($ch, CURLOPT_URL, $eth_api);
5918 curl_setopt($ch, CURLOPT_GET, 1);
5919 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5920 $server_output = curl_exec($ch);
5921 curl_close ($ch);
5922 $data = json_decode($server_output, true);
5923
5924 if(is_array($data)){
5925 if( array_key_exists("inputs", $data) && is_array($data['inputs'])){
5926
5927 $inputs = $data['inputs'];
5928 if( $this->check_input_eth_address($inputs, $eth_wallet_add) == true ){ // check users eth wallet exists in inputs
5929
5930 $get_eth_value = $this->check_output_eth_address($data['outputs'], $witoken_global_eth_add);
5931 if( '0' == $get_eth_value || empty($get_eth_value) ){
5932 $data['success'] = false;
5933 $data['msg'] = esc_html__( 'Receiver wallet address does not match with WiToken Globals wallet address', 'jl-token-helper' );
5934
5935 } else {
5936
5937 // ETH quantity user sent
5938
5939 $received_eth = $get_eth_value/1000000000000000000;
5940 $eth_quantity = get_post_meta( $transaction_id, 'eth_quantity', true);
5941
5942
5943 $get_sub_value = $received_eth - $eth_quantity;
5944 $difference = ($get_sub_value/$eth_quantity)*100;
5945
5946 $general_info = get_option('general_info');
5947 $eth_allow_upto = $general_info['gn_eth_allow_difference'];
5948 $ttrack_tbl = $wpdb->prefix . 'transaction_track';
5949 $sql = "SELECT * FROM $ttrack_tbl WHERE transaction='".$eth_txhash."' AND token_id='".$transaction_id."'";
5950 $get_ttrack = $wpdb->get_results($sql);
5951
5952 // Final check if transaction already exists in DB and will be stop verify processing
5953 if( count($get_ttrack) > 0 ){
5954
5955 $data['success'] = false;
5956 $data['msg'] = esc_html__( "Transaction already exists. Please contact with our support team to get further assist.", 'jl-token-helper' );
5957
5958 } else {
5959
5960 if( $difference < 0 ){
5961
5962 if( abs($difference) <= $eth_allow_upto ){
5963
5964 $post_meta_eth_txhash = get_post_meta( $transaction_id, 'eth_txhash', true );
5965 $post_meta_eth_trans_date = get_post_meta( $transaction_id, 'eth_trans_date', true );
5966 $post_meta_eth_wallet_add = get_post_meta( $transaction_id, 'eth_wallet_add', true );
5967
5968 if( !empty($post_meta_eth_wallet_add) ){
5969 update_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
5970 } else {
5971 add_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
5972 }
5973
5974 if( !empty($post_meta_eth_txhash) ){
5975 update_post_meta( $transaction_id, 'eth_txhash', $eth_txhash);
5976 } else {
5977 add_post_meta( $transaction_id, 'eth_txhash', $eth_txhash);
5978 }
5979
5980 if( !empty($post_meta_eth_trans_date) ){
5981 update_post_meta( $transaction_id, 'eth_trans_date', $eth_trans_date);
5982 } else {
5983 add_post_meta( $transaction_id, 'eth_trans_date', $eth_trans_date);
5984 }
5985
5986 $wpdb->update( $table, array( 'status' => 'approved' ), array( 'id' => $transaction_id ));
5987
5988 //We get payments that's why referrals will be approved
5989 $get_ref_ids = get_post_meta( $transaction_id, 'witoken_ref', false); // retrieve referral ids as array that's here must be put meta unique is false
5990 $tbl_ref = $wpdb->prefix . 'jas_referrals';
5991 if( is_array($get_ref_ids) ){
5992 foreach( $get_ref_ids as $id ){
5993 $wpdb->update( $tbl_ref, array( 'bonus_status' => 'approved' ), array( 'id' => $id ));
5994
5995 /************** Affiliate bonus transfer to WiWallet ****************/
5996 $sql = "SELECT * FROM $tbl_ref WHERE id = ".$id."";
5997 $referral_info = $wpdb->get_results($sql);
5998 $affiliate_parent_user_id = $this->get_uid_by_affiliate_id($referral_info[0]->affiliate_id);
5999
6000 $access_token = get_user_meta( $affiliate_parent_user_id, 'access_token', true );
6001 if( $referral_info[0]->bonus_type == "usd" ){
6002 $currency_type = 1;
6003 $transaction_details = "Affiliate bonus transfer to WiWallet";
6004 } else {
6005 $currency_type = 94;
6006 $transaction_details = "Affiliate WiToken transfer to WiWallet";
6007 }
6008
6009 $user_info = get_userdata($affiliate_parent_user_id);
6010 $user_email = $user_info->user_email;
6011 $amount = $referral_info[0]->amount;
6012
6013 $ch = curl_init();
6014 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6015 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6016 'Content-Type' => 'application/json',
6017 'Authorization: Bearer '.$access_token
6018 ));
6019 curl_setopt($ch, CURLOPT_POST, 1);
6020 curl_setopt($ch, CURLOPT_POSTFIELDS,
6021 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6022 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6023 $server_output = curl_exec($ch);
6024 curl_close ($ch);
6025 $json_data = json_decode($server_output);
6026
6027 if( $json_data->status == "success" ){
6028 add_post_meta( $id, 'wiwallet_transfer', 'success' );
6029 } else {
6030 $errors = $json_data->errors;
6031 add_post_meta( $id, 'wiwallet_transfer', 'failed' );
6032 add_post_meta( $id, 'wiwallet_transfer_errors', $errors );
6033 }
6034 // End WiToken transfer to WiWallet
6035
6036 }
6037 }
6038
6039 // Added transaction in transaction_track
6040 $wpdb->insert($ttrack_tbl, array(
6041 'token_id' => $transaction_id,
6042 'transaction' => $eth_txhash,
6043 ));
6044
6045 // WiToken transfer to WiWallet
6046 $transaction_details = "WiToken transfer to WiWallet";
6047 $user_info = get_userdata($user_id);
6048 $user_email = $user_info->user_email;
6049
6050 $ch = curl_init();
6051 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6052 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6053 'Content-Type' => 'application/json',
6054 'Authorization: Bearer '.$access_token
6055 ));
6056 curl_setopt($ch, CURLOPT_POST, 1);
6057 curl_setopt($ch, CURLOPT_POSTFIELDS,
6058 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6059 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6060 $server_output = curl_exec($ch);
6061 curl_close ($ch);
6062 $json_data = json_decode($server_output);
6063
6064 if( $json_data->status == "success" ){
6065 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
6066 } else {
6067 $errors = $json_data->errors;
6068 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
6069 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
6070 }
6071 // End WiToken transfer to WiWallet
6072
6073
6074 $sql = "SELECT * FROM $table WHERE id = ".$transaction_id."";
6075 $witoken_info = $wpdb->get_results($sql);
6076 // Purchase confirmation email
6077 $to = $user_email;
6078 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
6079 $amount = $witoken_info[0]->amount;
6080 $purchase_date = $witoken_info[0]->purchase_date;
6081 $payment_method = $witoken_info[0]->pmethod;
6082 $bonus = $witoken_info[0]->bonus_per;
6083 $bonus_token = $witoken_info[0]->bonus_amount;
6084 $total_token = $witoken_info[0]->total_token;
6085 $token_stage = $witoken_info[0]->sale_stage;
6086 $email_info = get_option( 'email_settings' );
6087 $subject = $email_info['settings_email_subject2'];
6088 $message = $email_info['settings_email_message2'];
6089 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
6090 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
6091 $body = str_replace($searchVal, $replaceVal, $message);
6092 wp_mail($to, $subject, $body);
6093
6094 $data['success'] = true;
6095 $data['msg'] = esc_html__( 'ETH payment is verified.', 'jl-token-helper' );
6096
6097 } else {
6098
6099 $msg = "Payment is unverified. You should sent '".$eth_quantity."' ETH, but we'd received '".$received_eth."' ETH, which is more than ".$eth_allow_upto."%. Please contact WiToken Global support center for further assistance";
6100 $data['success'] = false;
6101 $data['msg'] = $msg;
6102 }
6103
6104
6105 } else {
6106
6107 $post_meta_eth_txhash = get_post_meta( $transaction_id, 'eth_txhash', true );
6108 $post_meta_eth_trans_date = get_post_meta( $transaction_id, 'eth_trans_date', true );
6109 $post_meta_eth_wallet_add = get_post_meta( $transaction_id, 'eth_wallet_add', true );
6110
6111 if( !empty($post_meta_eth_wallet_add) ){
6112 update_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
6113 } else {
6114 add_post_meta( $transaction_id, 'eth_wallet_add', $eth_wallet_add);
6115 }
6116
6117 if( !empty($post_meta_eth_txhash) ){
6118 update_post_meta( $transaction_id, 'eth_txhash', $eth_txhash);
6119 } else {
6120 add_post_meta( $transaction_id, 'eth_txhash', $eth_txhash);
6121 }
6122
6123 if( !empty($post_meta_eth_trans_date) ){
6124 update_post_meta( $transaction_id, 'eth_trans_date', $eth_trans_date);
6125 } else {
6126 add_post_meta( $transaction_id, 'eth_trans_date', $eth_trans_date);
6127 }
6128
6129 $wpdb->update( $table, array( 'status' => 'approved' ), array( 'id' => $transaction_id ));
6130
6131 //We get payments that's why referrals will be approved
6132 $get_ref_ids = get_post_meta( $transaction_id, 'witoken_ref', false); // retrieve referral ids as array that's here must be put meta unique is false
6133 $tbl_ref = $wpdb->prefix . 'jas_referrals';
6134 if( is_array($get_ref_ids) ){
6135 foreach( $get_ref_ids as $id ){
6136 $wpdb->update( $tbl_ref, array( 'bonus_status' => 'approved' ), array( 'id' => $id ));
6137
6138 /************** Affiliate bonus transfer to WiWallet ****************/
6139 $sql = "SELECT * FROM $tbl_ref WHERE id = ".$id."";
6140 $referral_info = $wpdb->get_results($sql);
6141 $affiliate_parent_user_id = $this->get_uid_by_affiliate_id($referral_info[0]->affiliate_id);
6142
6143 $access_token = get_user_meta( $affiliate_parent_user_id, 'access_token', true );
6144 if( $referral_info[0]->bonus_type == "usd" ){
6145 $currency_type = 1;
6146 $transaction_details = "Affiliate bonus transfer to WiWallet";
6147 } else {
6148 $currency_type = 94;
6149 $transaction_details = "Affiliate WiToken transfer to WiWallet";
6150 }
6151
6152 $user_info = get_userdata($affiliate_parent_user_id);
6153 $user_email = $user_info->user_email;
6154 $amount = $referral_info[0]->amount;
6155
6156 $ch = curl_init();
6157 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6158 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6159 'Content-Type' => 'application/json',
6160 'Authorization: Bearer '.$access_token
6161 ));
6162 curl_setopt($ch, CURLOPT_POST, 1);
6163 curl_setopt($ch, CURLOPT_POSTFIELDS,
6164 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=".$currency_type."&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6165 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6166 $server_output = curl_exec($ch);
6167 curl_close ($ch);
6168 $json_data = json_decode($server_output);
6169
6170 if( $json_data->status == "success" ){
6171 add_post_meta( $id, 'wiwallet_transfer', 'success' );
6172 } else {
6173 $errors = $json_data->errors;
6174 add_post_meta( $id, 'wiwallet_transfer', 'failed' );
6175 add_post_meta( $id, 'wiwallet_transfer_errors', $errors );
6176 }
6177 // End WiToken transfer to WiWallet
6178 }
6179 }
6180
6181 // Added transaction in transaction_track
6182 $wpdb->insert($ttrack_tbl, array(
6183 'token_id' => $transaction_id,
6184 'transaction' => $eth_txhash,
6185 ));
6186
6187
6188 // WiToken transfer to WiWallet
6189 $transaction_details = "WiToken transfer to WiWallet";
6190 $user_info = get_userdata($user_id);
6191 $user_email = $user_info->user_email;
6192
6193 $ch = curl_init();
6194 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6195 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6196 'Content-Type' => 'application/json',
6197 'Authorization: Bearer '.$access_token
6198 ));
6199 curl_setopt($ch, CURLOPT_POST, 1);
6200 curl_setopt($ch, CURLOPT_POSTFIELDS,
6201 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6202 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6203 $server_output = curl_exec($ch);
6204 curl_close ($ch);
6205 $json_data = json_decode($server_output);
6206
6207 if( $json_data->status == "success" ){
6208 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
6209 } else {
6210 $errors = $json_data->errors;
6211 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
6212 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
6213 }
6214 // End WiToken transfer to WiWallet
6215
6216 $sql = "SELECT * FROM $table WHERE id = ".$transaction_id."";
6217 $witoken_info = $wpdb->get_results($sql);
6218 // Purchase confirmation email
6219 $to = $user_email;
6220 $user = get_user_meta( $user_id, "first_name", true)." ".get_user_meta( $user_id, "last_name", true);
6221 $amount = $witoken_info[0]->amount;
6222 $purchase_date = $witoken_info[0]->purchase_date;
6223 $payment_method = $witoken_info[0]->pmethod;
6224 $bonus = $witoken_info[0]->bonus_per;
6225 $bonus_token = $witoken_info[0]->bonus_amount;
6226 $total_token = $witoken_info[0]->total_token;
6227 $token_stage = $witoken_info[0]->sale_stage;
6228 $email_info = get_option( 'email_settings' );
6229 $subject = $email_info['settings_email_subject2'];
6230 $message = $email_info['settings_email_message2'];
6231 $searchVal = array("{name}", "{amount}", "{purchase_date}", "{payment_method}", "{bonus}", "{bonus_token}", "{total_token}", "{token_stage}");
6232 $replaceVal = array($user, $amount, $purchase_date, $payment_method, $bonus, $bonus_token, $total_token, $token_stage);
6233 $body = str_replace($searchVal, $replaceVal, $message);
6234 wp_mail($to, $subject, $body);
6235
6236 $data['success'] = true;
6237 $data['msg'] = esc_html__( 'ETH payment is verified.', 'jl-token-helper' );
6238
6239 }
6240 }
6241
6242 }
6243 } else {
6244
6245 $data['success'] = false;
6246 $data['msg'] = esc_html__( 'Sender wallet address does not match with the wallet address entered in the system', 'jl-token-helper' );
6247 }
6248
6249 } else {
6250
6251 $data['success'] = false;
6252 $data['msg'] = esc_html__( 'Sender wallet address or transaction not found', 'jl-token-helper' );
6253 }
6254
6255 } else {
6256
6257 $data['success'] = false;
6258 $data['msg'] = esc_html__( 'Transaction not found', 'jl-token-helper' );
6259
6260 }
6261
6262
6263 } catch (Exception $e){
6264
6265 $data['success'] = false;
6266 $data['msg'] = $e->getMessage();
6267 }
6268
6269 return $data;
6270
6271 }
6272
6273 public function get_gift_Witoken_by_qrcode( WP_REST_Request $request ){
6274
6275 global $wpdb;
6276 $success = true;
6277 $current_date = date('Y-m-d h:i:s A');
6278 $current_date = date("Y-m-d h:i:s A", strtotime($current_date));
6279
6280 $data = array();
6281 $params = $request->get_params();
6282 $card_no = $params['card_no'];
6283 $access_token = $params['access_token'];
6284 $device_id = $params['device_id'];
6285
6286 $ch = curl_init();
6287// curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/witoken-cards/".$card_no."?client_id=18&device_id=".$device_id."");
6288 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/witoken-cards/".$card_no."?client_id=18&device_id=1520");
6289 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6290 'Content-Type' => 'application/json',
6291 'Authorization: Bearer '.$access_token
6292 ));
6293 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6294 $server_output = curl_exec($ch);
6295 curl_close ($ch);
6296 $token_data = json_decode($server_output);
6297 $token_data = $token_data->data;
6298
6299
6300 if( count($token_data) > 0 ){
6301 $expire_date = date("Y-m-d h:i:s A", strtotime($token_data->expired_at . ' + 23 hours +59 minutes'));
6302 $data['id'] = $token_data->id;
6303 $data['amount'] = $token_data->amount;
6304 $data['expire_date'] = $expire_date;
6305 $data['card_no'] = $token_data->card_number;
6306 $data['cvv'] = $token_data->cvv;
6307 $data['status'] = $token_data-> status;
6308 $data['secret_key'] = $token_data-> secret_key;
6309 $data['current_date'] = $current_date;
6310
6311 $expire = new DateTime($expire_date);
6312 $current = new DateTime($current_date);
6313
6314 if( $token_data->is_used != 0 ){
6315 $data['msg'] = "Redemption code was used before";
6316 $data['success'] = false;
6317
6318 } else if( $expire < $current ){
6319 $data['msg'] = "Redemption code expired";
6320 $data['success'] = false;
6321
6322 } else if( $token_data->status != "active"){
6323 $data['msg'] = "Redemption code deactivated";
6324 $data['success'] = false;
6325 } else {
6326 $data['success'] = true;
6327 $data['msg'] = "";
6328 }
6329
6330 } else {
6331 $data['msg'] = "Sorry, this card not found";
6332 $data['success'] = false;
6333 $data['amount'] = 0;
6334 $data['expire_date'] = '';
6335 }
6336
6337 return $data;
6338
6339 }
6340
6341 public function run_redeem_gift_Witoken( WP_REST_Request $request ){
6342
6343 $data = array();
6344 $post = (array) $request->get_params();
6345 $user_id = $post['user_id'];
6346 $card_no = $post['card_no'];
6347 $cvv = $post['cvv'];
6348 $expire_month = $post['expire_month'];
6349 $expire_year = $post['expire_year'];
6350 $access_token = $post['access_token'];
6351 $token_qty = $post['token_qty'];
6352 $note = $post['note'];
6353
6354 $ch = curl_init();
6355 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/witoken-cards/redeem?client_id=18&device_id=1520");
6356 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6357 'Content-Type' => 'application/json',
6358 'Authorization: Bearer '.$access_token
6359 ));
6360 curl_setopt($ch, CURLOPT_POST, 1);
6361 curl_setopt($ch, CURLOPT_POSTFIELDS,
6362 "card_number=".$card_no."&cvv=".$cvv."&expiry_month=".$expire_month."&expiry_year=".$expire_year."");
6363 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6364 $server_output = curl_exec($ch);
6365 curl_close ($ch);
6366 $json_data = json_decode($server_output);
6367
6368 if( $json_data->status == "success" ){
6369
6370 global $wpdb;
6371 $table = $wpdb->prefix . 'witoken_statement';
6372 $check = $wpdb->insert($table,
6373 array(
6374 'user_id' => $user_id,
6375 'purchase_date' => date('Y-m-d'),
6376 'amount' => '0.00',
6377 'bonus_type' => 'gift_witoken',
6378 'bonus_per' => '0',
6379 'bonus_amount' => '0',
6380 'pmethod' => 'Redeem WiToken',
6381 'total_token' => $token_qty,
6382 'sale_stage' => '',
6383 'note' => $note,
6384 'status' => "approved"
6385 )
6386 );
6387
6388 if( $check ){
6389
6390 $tbl = $wpdb->prefix . 'gift_token';
6391 $wpdb->update( $tbl, array( 'user_id' => $user_id ), array( 'id' => $token_id ));
6392
6393 $data['status'] = true;
6394 $data['msg'] = $json_data->message;
6395
6396 // WiToken transfer to WiWallet
6397 $transaction_details = "Redeem WiToken transfer to WiWallet";
6398 $user_info = get_userdata($user_id);
6399 $user_email = $user_info->user_email;
6400
6401 $ch = curl_init();
6402 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6403 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6404 'Content-Type' => 'application/json',
6405 'Authorization: Bearer '.$access_token
6406 ));
6407 curl_setopt($ch, CURLOPT_POST, 1);
6408 curl_setopt($ch, CURLOPT_POSTFIELDS,
6409 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$token_qty."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6410 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6411 $server_output = curl_exec($ch);
6412 curl_close ($ch);
6413 $fetch_data = json_decode($server_output);
6414
6415 if( $fetch_data->status == "success" ){
6416 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
6417 } else {
6418 $errors = $fetch_data->errors;
6419 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
6420 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
6421 }
6422
6423 } else {
6424 $data['status'] = false;
6425 $data['msg'] = 'Something went wrong';
6426 }
6427 } else {
6428 $data['status'] = false;
6429 $data['msg'] = "The given data was invalid";
6430 }
6431 return $data;
6432 }
6433
6434 public function run_gift_Witoken_settings( WP_REST_Request $request ){
6435
6436 $data = array();
6437 $lang = $request->get_params();
6438 $lang = $lang['lang'];
6439
6440 if( $lang == "ar" ){
6441 $gwit_info = get_option('gwit_info_ar');
6442 $data['title'] = $gwit_info['gwit_title_ar'];
6443 $data['desc'] = $gwit_info['gwit_desc_ar'];
6444 } else if( $lang == "cn" ){
6445 $gwit_info = get_option('gwit_info_cn');
6446 $data['title'] = $gwit_info['gwit_title_cn'];
6447 $data['desc'] = $gwit_info['gwit_desc_cn'];
6448 } else if($lang == "en" ){
6449 $gwit_info = get_option('gwit_info');
6450 $data['title'] = $gwit_info['gwit_title'];
6451 $data['desc'] = $gwit_info['gwit_desc'];
6452
6453 } else if( $lang == "my" ) {
6454 $gwit_info = get_option('gwit_info_my');
6455 $data['title'] = $gwit_info['gwit_title_my'];
6456 $data['desc'] = $gwit_info['gwit_desc_my'];
6457 } else {
6458 $gwit_info = get_option('gwit_info');
6459 $data['title'] = $gwit_info['gwit_title'];
6460 $data['desc'] = $gwit_info['gwit_desc'];
6461 }
6462
6463 return $data;
6464 }
6465
6466
6467 public function run_token_transfer_wiwallet(){
6468
6469 global $wpdb;
6470 $params = $request->get_params();
6471 $user_id = $params['user_id'];
6472 $access_token = $params['access_token'];
6473 $total_token = $params['total_token'];
6474
6475 $transaction_details = "WiToken transfer to WiWallet";
6476 $user_info = get_userdata($user_id);
6477 $user_email = $user_info->user_email;
6478
6479 $ch = curl_init();
6480 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6481 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6482 'Content-Type' => 'application/json',
6483 'Authorization: Bearer '.$access_token
6484 ));
6485 curl_setopt($ch, CURLOPT_POST, 1);
6486 curl_setopt($ch, CURLOPT_POSTFIELDS,
6487 "app_id=67&transaction_type_id=10&client_id=3&device_id=ALSDJFLASDFPIOML&transactionDetails[0][amount]=".$total_token."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$user_email."&end_user_email=admin@admin.com");
6488 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6489 $server_output = curl_exec($ch);
6490 curl_close ($ch);
6491 $json_data = json_decode($server_output);
6492
6493 $table = $wpdb->prefix.'witoken_statement';
6494 $sql = "SELECT id FROM $table WHERE user_id =".$user_id. " ORDER BY Id DESC";
6495 $query = $wpdb->get_results($sql);
6496 $transaction_id = $query[0]->id;
6497
6498 if( $json_data->status == "success" ){
6499 add_post_meta( $transaction_id, 'wiwallet_transfer', 'success' );
6500 } else {
6501 $errors = $json_data->errors;
6502 add_post_meta( $transaction_id, 'wiwallet_transfer', 'failed' );
6503 add_post_meta( $transaction_id, 'wiwallet_transfer_errors', $errors );
6504 }
6505
6506
6507 }
6508
6509 public function run_event_list( WP_REST_Request $request ){
6510
6511 $params = $request->get_params();
6512 $lang = $params['lang'];
6513 $data = array();
6514 $allslides = array('post_type' => 'awl_event_monster', 'post_status'=>'publish', 'orderby' => 'ASC', 'order' => 'ASC','posts_per_page' => -1);
6515 $loop = new WP_Query( $allslides );
6516 $i=0;
6517 if ( $loop->have_posts() ) {
6518 while ( $loop->have_posts() ) : $loop->the_post();
6519 $post_id = get_the_ID();
6520 $event_monster_output_settings = unserialize(base64_decode(get_post_meta( get_the_ID(), 'awl_em_settings_'.get_the_ID(), true)));
6521 // get settings from setting page
6522 $data[$i]['em_title'] = get_the_title($post_id);
6523 $data[$i]['em_id'] = $post_id;
6524 $data[$i]['em_upload_image'] = $event_monster_output_settings['em_upload_image'];
6525 $data[$i]['em_upload_video'] = $event_monster_output_settings['em_upload_video'];
6526 $data[$i]['em_upload_icon'] = $event_monster_output_settings['em_upload_icon'];
6527
6528 $data[$i]['em_start_date'] = $event_monster_output_settings['em_start_date'];
6529 $data[$i]['em_end_date'] = $event_monster_output_settings['em_end_date'];
6530 $data[$i]['em_start_time'] = $event_monster_output_settings['em_start_time'];
6531 $data[$i]['em_end_time'] = $event_monster_output_settings['em_end_time'];
6532 $data[$i]['em_address'] = $event_monster_output_settings['em_address'];
6533 $time = strtotime($em_start_date);
6534 $data[$i]['em_day'] = date('d',$time);
6535 $data[$i]['em_month'] = date('M',$time);
6536 $i++;
6537 endwhile;
6538 }
6539
6540 return $data;
6541 }
6542
6543 public function run_event_details( WP_REST_Request $request ){
6544
6545 // Get table name
6546 global $wpdb;
6547 $booking_table_name = $wpdb->prefix."em_bookings";
6548 $payment_table_name = $wpdb->prefix."em_payments";
6549 $attendee_table_name = $wpdb->prefix."em_attendees";
6550 $ticket_table_name = $wpdb->prefix."em_tickets";
6551 $coupon_table_name = $wpdb->prefix . 'em_coupons';
6552
6553
6554 $timezone_setting_string = get_option('timezone_string');
6555 if(!$timezone_setting_string) {
6556 date_default_timezone_set("Asia/Singapore");
6557 } else {
6558 date_default_timezone_set($timezone_setting_string);
6559 }
6560
6561 $params = $request->get_params();
6562 $id = $params['id'];
6563 $data = array();
6564 $sponsors = array();
6565 $gallery = array();
6566 $speakers = array();
6567 $organizers = array();
6568 $tickets = array();
6569 $ticket_remain_max_range = array();
6570 $thumbnail_url = '';
6571 $allslides = array('post_type' => 'awl_event_monster', 'p'=> $id);
6572 $loop = new WP_Query( $allslides );
6573 if ( $loop->have_posts() ) {
6574 while ( $loop->have_posts() ) : $loop->the_post();
6575 $post_id = get_the_ID();
6576 $event_monster_output_settings = unserialize(base64_decode(get_post_meta( get_the_ID(), 'awl_em_settings_'.get_the_ID(), true)));
6577 // get settings from setting page
6578 $data['em_title'] = get_the_title($post_id);
6579 $data['em_upload_image'] = $event_monster_output_settings['em_upload_image'];
6580 $data['em_upload_video'] = $event_monster_output_settings['em_upload_video'];
6581 $data['em_upload_icon'] = $event_monster_output_settings['em_upload_icon'];
6582 $data['logo_upd'] = $event_monster_output_settings['logo_upd'];
6583 $data['em_video_option'] = $event_monster_output_settings['em_video_option'];
6584 $data['em_logo_size'] = $event_monster_output_settings['em_logo_size'];
6585 $data['logo_size'] = $event_monster_output_settings['logo_size'];
6586 $data['em_logo_coustom_width'] = $event_monster_output_settings['em_logo_coustom_width'];
6587 $data['em_logo_coustom_height'] = $event_monster_output_settings['em_logo_coustom_height'];
6588 $data['em_upload_logo_color'] = $event_monster_output_settings['em_upload_logo_color'];
6589 $data['em_upload_logo_size'] = $event_monster_output_settings['em_upload_logo_size'];
6590 $data['em_logo_border'] = $event_monster_output_settings['em_logo_border'];
6591 $data['em_logo_border_color'] = $event_monster_output_settings['em_logo_border_color'];
6592 $data['em_upload_logo_bgcolor'] = $event_monster_output_settings['em_upload_logo_bgcolor'];
6593 $data['em_title_size'] = $event_monster_output_settings['em_title_size'];
6594 $data['em_title_color'] = $event_monster_output_settings['em_title_color'];
6595 $data['em_title_align'] = $event_monster_output_settings['em_title_align'];
6596
6597 //get wordpress date time format
6598 $em_date_format = get_option('date_format');
6599 $em_time_format = get_option('time_format');
6600 $data['em_start_date'] = date($em_date_format, strtotime($event_monster_output_settings['em_start_date']));
6601 $data['em_end_date'] = date($em_date_format, strtotime($event_monster_output_settings['em_end_date']));
6602 $data['em_start_time'] = date($em_time_format, strtotime($event_monster_output_settings['em_start_time']));
6603 $data['em_end_time'] = date($em_time_format, strtotime($event_monster_output_settings['em_end_time']));
6604
6605 $data['em_address'] = $event_monster_output_settings['em_address'];
6606 $data['em_venue_phone'] = $event_monster_output_settings['em_venue_phone'];
6607 $data['em_venue_email'] = $event_monster_output_settings['em_venue_email'];
6608 $data['em_map'] = $event_monster_output_settings['em_map'];
6609 $data['em_map_code'] = $event_monster_output_settings['em_map_code'];
6610 $data['em_map_height'] = $event_monster_output_settings['em_map_height'];
6611 $data['em_map_opacity'] = $event_monster_output_settings['em_map_opacity'];
6612 $data['em_accept_registration'] = $event_monster_output_settings['em_accept_registration'];
6613 $data['em_registration_type'] = $event_monster_output_settings['em_registration_type'];
6614 $data['em_reg_btn_txt'] = $event_monster_output_settings['em_reg_btn_txt'];
6615 $data['em_buttons_type'] = $event_monster_output_settings['em_buttons_type'];
6616 $data['em_reg_btn_align'] = $event_monster_output_settings['em_reg_btn_align'];
6617 $data['em_show_event_sponsor'] = $event_monster_output_settings['em_show_event_sponsor'];
6618 $data['em_custom_css'] = $event_monster_output_settings['em_custom_css'];
6619 $data['em_reg_btn_color'] = $event_monster_output_settings['em_reg_btn_color'];
6620 $data['em_reg_btn_link'] = $event_monster_output_settings['em_reg_btn_link'];
6621 $data['em_reg_btn_link_tab'] = $event_monster_output_settings['em_reg_btn_link_tab'];
6622 $data['em_spr_link_tab'] = $event_monster_output_settings['em_spr_link_tab'];
6623 //social
6624 $data['em_social'] = $event_monster_output_settings['em_social'];
6625 $data['em_share_btn'] = stripslashes($event_monster_output_settings['em_share_btn']);
6626 //organizers
6627 $data['em_organizer'] = $event_monster_output_settings['em_organizer'];
6628 $em_organizer = $data['em_organizer'];
6629
6630 $data['em_artist'] = $event_monster_output_settings['em_artist'];
6631 $data['em_artist_tab'] = $event_monster_output_settings['em_artist_tab'];
6632
6633 //headings texts
6634 $data['em_organizer_heading'] = $event_monster_output_settings['em_organizer_heading'];
6635 $data['em_artist_heading'] = $event_monster_output_settings['em_artist_heading'];
6636 $data['em_date_time_heading'] = $event_monster_output_settings['em_date_time_heading'];
6637 $data['em_location_heading'] = $event_monster_output_settings['em_location_heading'];
6638 $data['em_gallery_heading'] = $event_monster_output_settings['em_gallery_heading'];
6639 $data['em_sponsor_heading'] = $event_monster_output_settings['em_sponsor_heading'];
6640 //Gallery
6641 $data['em_show_event_gallery'] = $event_monster_output_settings['em_show_event_gallery'];
6642 $data['em_gal_thumb_size'] = $event_monster_output_settings['em_gal_thumb_size'];
6643 $em_gal_thumb_size = $data['em_gal_thumb_size'];
6644 $data['em_gal_spacing'] = $event_monster_output_settings['em_gal_spacing'];
6645 $data['em_col_settings'] = $event_monster_output_settings['em_col_settings'];
6646 $data['em_col_large_desktops'] = $em_col_settings[0];
6647 $data['em_col_desktops'] = $em_col_settings[1];
6648 $data['em_col_tablets'] = $em_col_settings[2];
6649 $data['em_col_phones'] = $em_col_settings[3];
6650 $data['em_light_box'] = $event_monster_output_settings['em_light_box'];
6651 $data['em_hover_style'] = $event_monster_output_settings['em_hover_style'];
6652 //countdown
6653 $data['em_countdown'] = $event_monster_output_settings['em_countdown'];
6654 $data['em_prog_bar_thickness'] = $event_monster_output_settings['em_prog_bar_thickness'];
6655 $data['em_prog_bar_color'] = $event_monster_output_settings['em_prog_bar_color'];
6656 $data['em_prog_bar_bg_color'] = $event_monster_output_settings['em_prog_bar_bg_color'];
6657 $data['em_days_text'] = $event_monster_output_settings['em_days_text'];
6658 $data['em_hours_text'] = $event_monster_output_settings['em_hours_text'];
6659 $data['em_minutes_text'] = $event_monster_output_settings['em_minutes_text'];
6660 $data['em_seconds_text'] = $event_monster_output_settings['em_seconds_text'];
6661 $data['em_cancle_out_msg'] = $event_monster_output_settings['em_cancle_out_msg'];
6662 $data['em_coupon_show'] = $event_monster_output_settings['em_coupon_show'];
6663
6664 // common settings
6665 $em_common_settings = unserialize(base64_decode(get_option('awl_em_common_settings')));
6666 $data['em_cncl_notifi_msg'] = $em_common_settings['em_cncl_notifi_msg'];
6667 $data['em_form_heading_one'] = $em_common_settings['em_form_heading_one'];
6668 $data['em_form_sub_heading'] = $em_common_settings['em_form_sub_heading'];
6669 $data['em_form_sucsess_msg_field'] = $em_common_settings['em_form_sucsess_msg_field'];
6670 $data['em_booking_full_msg'] = $em_common_settings['em_booking_full_msg'];
6671
6672 $data['em_form_first_name_field'] = $em_common_settings['em_form_first_name_field'];
6673 $data['em_form_last_name_field'] = $em_common_settings['em_form_last_name_field'];
6674 $data['em_form_email_field'] = $em_common_settings['em_form_email_field'];
6675 $data['em_form_phone_field'] = $em_common_settings['em_form_phone_field'];
6676
6677 $data['em_desc'] = get_the_content();
6678
6679
6680 if($em_organizer) {
6681 for($i = 0; $i < $em_organizer; $i++) {
6682
6683 $organizers[$i]['name'] = $event_monster_output_settings['em_organizer_name'][$i];
6684 $organizers[$i]['phone'] = $event_monster_output_settings['em_organizer_phone'][$i];
6685 $organizers[$i]['email'] = $event_monster_output_settings['em_organizer_email'][$i];
6686 $organizers[$i]['website'] = $event_monster_output_settings['em_organizer_website'][$i];
6687 }
6688 }
6689
6690 $data['organizers'] = $organizers;
6691
6692 if(isset($event_monster_output_settings['em-artist-ids']) && count($event_monster_output_settings['em-artist-ids']) > 0) {
6693 $count=0;
6694 foreach($event_monster_output_settings['em-artist-ids'] as $attachment_sponsor_id) {
6695 $thumbnail = wp_get_attachment_image_src($attachment_sponsor_id, 'thumbnail', true);
6696 $medium = wp_get_attachment_image_src($attachment_sponsor_id, 'medium', true);
6697 $large = wp_get_attachment_image_src($attachment_sponsor_id, 'large', true);
6698 $postthumbnail = wp_get_attachment_image_src($attachment_sponsor_id, 'post-thumbnail', true);
6699 $speakers[$count]['name'] = $event_monster_output_settings['em-artist-names'][$count];
6700 $speakers[$count]['img'] = $thumbnail[0];
6701 $speakers[$count]['link'] = $event_monster_output_settings['em-artist-links'][$count];
6702 $speakers[$count]['details'] = $event_monster_output_settings['em-artist-details'][$count];
6703 $count++;
6704 }
6705
6706 }
6707
6708 $data['speakers'] = $speakers;
6709
6710
6711 if(isset($event_monster_output_settings['em-slide-ids']) && count($event_monster_output_settings['em-slide-ids']) > 0) {
6712 $count = 0;
6713 foreach($event_monster_output_settings['em-slide-ids'] as $attachment_id) {
6714 $thumb = wp_get_attachment_image_src($attachment_id, 'thumb', true);
6715 $thumbnail = wp_get_attachment_image_src($attachment_id, 'thumbnail', true);
6716 $medium = wp_get_attachment_image_src($attachment_id, 'medium', true);
6717 $large = wp_get_attachment_image_src($attachment_id, 'large', true);
6718 $full = wp_get_attachment_image_src($attachment_id, 'full', true);
6719 $postthumbnail = wp_get_attachment_image_src($attachment_id, 'post-thumbnail', true);
6720 $attachment_details = get_post( $attachment_id );
6721 $href = get_permalink( $attachment_details->ID );
6722 $src = $attachment_details->guid;
6723 $title = $attachment_details->post_title;
6724 $description = $attachment_details->post_content;
6725 //hover title
6726 $em_images_name = $event_monster_output_settings['em-images-name'][$count];
6727 if(isset($slidetext) == 'true') {
6728 if($slidetextopt == 'title') $text = $title;
6729 } else {
6730 $text = $title;
6731 }
6732 //set thumbnail size
6733 if($em_gal_thumb_size == "thumbnail") { $thumbnail_url = $thumbnail[0]; }
6734 if($em_gal_thumb_size == "medium") { $thumbnail_url = $medium[0]; }
6735 if($em_gal_thumb_size == "large") { $thumbnail_url = $large[0]; }
6736 if($em_gal_thumb_size == "full") { $thumbnail_url = $full[0]; }
6737
6738 $gallery[$count]['img'] = $full[0];
6739 $gallery[$count]['img_heading'] = $em_images_name;
6740 $count++;
6741 }// end of attachment foreach
6742 }
6743
6744 $data['gallery'] = $gallery;
6745
6746 if(isset($event_monster_output_settings['em-slide-spr-ids']) && count($event_monster_output_settings['em-slide-spr-ids']) > 0) {
6747 $count=0;
6748 foreach($event_monster_output_settings['em-slide-spr-ids'] as $attachment_sponsor_id) {
6749 $thumb = wp_get_attachment_image_src($attachment_sponsor_id, 'thumb', true);
6750 $thumbnail = wp_get_attachment_image_src($attachment_sponsor_id, 'thumbnail', true);
6751 $medium = wp_get_attachment_image_src($attachment_sponsor_id, 'medium', true);
6752 $large = wp_get_attachment_image_src($attachment_sponsor_id, 'large', true);
6753 $postthumbnail = wp_get_attachment_image_src($attachment_sponsor_id, 'post-thumbnail', true);
6754 $slide_spr_link = $event_monster_output_settings['em-slide-spr-links'][$count];
6755 $sponsors[$count]['splogo'] = $medium[0];
6756 $count++;
6757 }
6758 }
6759 $data['sponsors'] = $sponsors;
6760
6761
6762 $sum_booked_tickets_array = array();
6763 $booked_tickets = $wpdb->get_results( "SELECT * FROM `$booking_table_name` WHERE `event_id` = '$id' AND `status` LIKE 'Completed'" );
6764 if(count($booked_tickets)) {
6765 foreach($booked_tickets as $booked_ticket_val ) {
6766 $ticket_ids = unserialize($booked_ticket_val->ticket_id);
6767 if(is_array($ticket_ids)){
6768 foreach ($ticket_ids as $id=>$value) {
6769 $sum_booked_tickets_array[$id] += $value;
6770 }
6771 }
6772 }
6773 }
6774
6775 $data['em_tkt_max_range'] = $event_monster_output_settings['em_tkt_max_range'];
6776 $em_tkt_max_range = $event_monster_output_settings['em_tkt_max_range'];
6777 $data['em_tkt_selected'] = $event_monster_output_settings['em_tkt_select'];
6778 $em_tkt_selected = $data['em_tkt_selected'];
6779
6780 $count = 0;
6781 foreach($em_tkt_selected as $em_tkt_selected_val ) {
6782 $em_tickets_otp_result = $wpdb->get_results( "SELECT * FROM `$ticket_table_name` WHERE `id` LIKE '$em_tkt_selected_val'" );
6783 if(count($em_tickets_otp_result)) {
6784
6785 foreach($em_tickets_otp_result as $single_row ) {
6786 $tickets[$count]['inc'] = $count;
6787 $tickets[$count]['ticket_id'] = $single_row->id;
6788 $tickets[$count]['ticket_name'] = $single_row->name;
6789 $tickets[$count]['ticket_price'] = $single_row->price;
6790 $tickets[$count]['ticket_status'] = $single_row->status;
6791 $em_tkt_quantities = $event_monster_output_settings['em_tkt_quantity'][$tickets[$count]['ticket_id']];
6792 $tickets[$count]['ticket_quantity'] = $em_tkt_quantities;
6793 if(count($sum_booked_tickets_array)) {
6794 $tickets[$count]['booked'] = ($em_tkt_quantities - $sum_booked_tickets_array[$tickets[$count]['ticket_id']]);
6795 } else {
6796 $tickets[$count]['booked'] = $em_tkt_quantities;
6797 }
6798
6799 $em_remain_tkt = $em_tkt_quantities - $sum_booked_tickets_array[$tickets[$count]['ticket_id']];
6800 $em_tkt_max_range_val = $em_tkt_max_range[$tickets[$count]['ticket_id']];
6801 if($em_remain_tkt < $em_tkt_max_range_val) {
6802 $d =0;
6803 for($digit = 1; $digit <= $em_remain_tkt; $digit++){
6804 $ticket_remain_max_range[$j]['rang_key'] = $digit;
6805 $d++;
6806 }
6807 } else {
6808 $j=0;
6809 for($k = 1; $k <= $em_tkt_max_range_val; $k++) {
6810 $ticket_remain_max_range[$j]['rang_key'] = $k;
6811 $j++;
6812 }
6813 }
6814 $tickets[$count]['ticket_remain_max_range'] = $ticket_remain_max_range;
6815
6816 }
6817
6818 }
6819 $count++;
6820 }
6821 $data['tickets'] = $tickets;
6822
6823 endwhile;
6824 }
6825
6826 return $data;
6827 }
6828
6829 public function run_book_now( WP_REST_Request $request ){
6830
6831 $data = array();
6832 $post = (array) $request->get_params();
6833 $em_id = $post['event_id'];
6834 $em_tkt_ids = $post['em_tkt_ids'];
6835 $count = count($em_tkt_ids);
6836 $em_tkt_quantity = $post['em_tkt_quantity'];
6837
6838 $em_attendee_first_name = $post['first_name'];
6839 $em_attendee_last_name = $post['last_name'];
6840 $em_attendee_email = $post['email'];
6841 $em_attendee_phone = $post['phone_number'];
6842
6843 // Get table name
6844 global $wpdb;
6845 $booking_table_name = $wpdb->prefix."em_bookings";
6846 $payment_table_name = $wpdb->prefix."em_payments";
6847 $attendee_table_name = $wpdb->prefix."em_attendees";
6848 $ticket_table_name = $wpdb->prefix."em_tickets";
6849 $coupon_table_name = $wpdb->prefix . 'em_coupons';
6850 $data = array();
6851
6852 //calculate booking is paid or free
6853 $total_due = 0;
6854 foreach($em_tkt_ids as $em_tkt_single_key => $em_tkt_single_id){
6855 // get ticket price
6856 $em_ticket_price = $wpdb->get_row( "SELECT * FROM `$ticket_table_name` WHERE `id` LIKE '$em_tkt_single_id' AND `status` LIKE 'available'");
6857 if($em_ticket_price) {
6858 $ticket_price = $em_ticket_price->price;
6859 }
6860 $total_due = $total_due + ($ticket_price * $em_tkt_quantity[$em_tkt_single_key]);
6861 }
6862
6863 //combine ticket id (key) ticket quantity (value)
6864 $em_tkt_id_value = array_combine($em_tkt_ids,$em_tkt_quantity);
6865
6866 if($em_tkt_id_value == false){
6867 $data['success'] = false;
6868 $data['msg'] = 'Could not book ticket, please try again';
6869 return $data;
6870 }
6871
6872 //register attendee and get its id
6873 $wpdb->insert( $attendee_table_name, array(
6874 'event_id' => $em_id,
6875 'ticket_id' => serialize($em_tkt_id_value),
6876 'first_name' => $em_attendee_first_name,
6877 'last_name' => $em_attendee_last_name,
6878 'email' => $em_attendee_email,
6879 'phone' => $em_attendee_phone,
6880 'extra' => '',
6881 ));
6882 $last_attendee_id = $wpdb->insert_id;
6883 $booking_type = "free";
6884
6885 // booking table data insert
6886 $wpdb->insert( $booking_table_name, array(
6887 'event_id' => $em_id,
6888 'ticket_id' => serialize($em_tkt_id_value),
6889 'amount' => 0,
6890 'coupon_id' => '',
6891 'attendees_id' => $last_attendee_id,
6892 'booking_type' => $booking_type,
6893 'payment_id' => '',
6894 'status' => 'Completed',
6895 'extra' => '',
6896 ));
6897
6898 $em_booking_id = $wpdb->insert_id;
6899 $event_monster_output_settings = unserialize(base64_decode(get_post_meta( $em_id, 'awl_em_settings_'.$em_id, true)));
6900 $em_start_date = $event_monster_output_settings['em_start_date'];
6901 $em_end_date = $event_monster_output_settings['em_end_date'];
6902 $em_start_time = $event_monster_output_settings['em_start_time'];
6903 $em_end_time = $event_monster_output_settings['em_end_time'];
6904 $em_address = $event_monster_output_settings['em_address'];
6905 $em_venue_phone = $event_monster_output_settings['em_venue_phone'];
6906 $em_venue_email = $event_monster_output_settings['em_venue_email'];
6907 $em_common_settings = unserialize(base64_decode(get_option('awl_em_common_settings')));
6908
6909 include('ticket-email.php');
6910
6911 $data['success'] = true;
6912 $data['msg'] = 'Ticket booked successfully';
6913 return $data;
6914 }
6915
6916 public function run_token_transfer( WP_REST_Request $request ){
6917
6918 $data = array();
6919 global $wpdb;
6920 $post = (array) $request->get_params();
6921// $device_id = $post['device_id'];
6922 $access_token = $post['access_token'];
6923 $currency = $post['currency'];
6924 $sender_id = $post['sender_id'];
6925 $sender_email = $post['sender_email'];
6926 $receiver_email = $post['receiver_email'];
6927 $amount = $post['amount'];
6928 $transaction_details = 'WiToken Transfer';
6929
6930
6931 try {
6932
6933 $ch = curl_init();
6934 curl_setopt($ch, CURLOPT_URL,"http://account.witoken.global/wallet/api/transactions");
6935 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
6936 'Content-Type' => 'application/json',
6937 'Authorization: Bearer '.$access_token
6938 ));
6939 curl_setopt($ch, CURLOPT_POST, 1);
6940 curl_setopt($ch, CURLOPT_POSTFIELDS,
6941 "app_id=67&transaction_type_id=15&client_id=3&device_id=ADSFADFADFA525ADSF&transactionDetails[0][amount]=".$amount."&transactionDetails[0][fee]=0&transactionDetails[0][currency_exchange_rate]=0&transactionDetails[0][currency_id]=94&transactionDetails[0][transaction_details]=".$transaction_details."&user_email=".$sender_email."&end_user_email=".$receiver_email."");
6942 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6943 $server_output = curl_exec($ch);
6944 curl_close ($ch);
6945 $json_data = json_decode($server_output);
6946 $fetch_data = $json_data->data;
6947
6948 if( $fetch_data->status == "success" ){
6949 $table = $wpdb->prefix.'balance_transaction';
6950
6951 $user = get_user_by( 'email', $fetch_data->end_user_email );
6952 $receiver_id = $user->ID;
6953
6954 $wpdb->insert($table,
6955 array(
6956 'transaction_date' => date('Y-m-d'),
6957 'currency' => $currency,
6958 'sender_id' => $sender_id,
6959 'sender_email' => $fetch_data->user_email,
6960 'receiver_id' => $receiver_id,
6961 'receiver_email' => $fetch_data->end_user_email,
6962 'amount' => $amount,
6963 'transaction_id' =>$fetch_data->transaction_id,
6964 'transaction_detail' =>json_encode($fetch_data)
6965 )
6966 );
6967
6968 $data['success'] = true;
6969 $data['msg'] = $json_data;
6970
6971 } else {
6972 $data['success'] = false;
6973 $data['msg'] = $json_data;
6974 }
6975
6976 } catch (Exception $e){
6977 $data['success'] = false;
6978 $data['msg'] = $e->getMessage() ;
6979
6980 }
6981
6982 return $data;
6983 }
6984
6985 public function run_balance_transactions( WP_REST_Request $request ){
6986
6987 $data = array();
6988 global $wpdb;
6989 $params = $request->get_params();
6990 $user_id = $params['user_id'];
6991 $current_page = $params['page'];
6992 $limit = 10;
6993
6994 $table = $wpdb->prefix.'balance_transaction';
6995 $total_row_sql = "SELECT COUNT(id) as total_row FROM $table WHERE sender_id =".$user_id." OR receiver_id =".$user_id. " ORDER BY Id DESC";
6996 $total_row = $wpdb->get_results($total_row_sql);
6997 $total_row = $total_row[0]->total_row;
6998 $total_page = $total_row / $limit;
6999
7000 if( $total_page <= $limit ){
7001 $total_page = 1;
7002 }
7003 if( ($total_row % $limit) != 0 ) {
7004 $total_page = ceil($total_row / $limit);
7005 }
7006
7007 $start_from = ($current_page - 1) * $limit;
7008 $sql = "SELECT * FROM $table WHERE sender_id =".$user_id." OR receiver_id =".$user_id." ORDER BY Id DESC LIMIT $start_from, $limit";
7009 $result = $wpdb->get_results($sql, ARRAY_A);
7010
7011 $data['limit'] = $limit;
7012 $data['row'] = $total_row;
7013 $data['total_page'] = $total_page;
7014 $data['current_page'] = $current_page;
7015 $data['result'] = $result;
7016
7017 return $data;
7018 }
7019
7020 public function build_data_files($boundary, $fields, $files){
7021
7022 $data = '';
7023 $eol = "\r\n";
7024
7025 $delimiter = '-------------' . $boundary;
7026
7027 foreach ($fields as $name => $content) {
7028 $data .= "--" . $delimiter . $eol
7029 . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
7030 . $content . $eol;
7031 }
7032
7033
7034 foreach ($files as $name => $content) {
7035 $data .= "--" . $delimiter . $eol
7036 . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
7037 //. 'Content-Type: image/png'.$eol
7038 . 'Content-Transfer-Encoding: binary'.$eol
7039 ;
7040
7041 $data .= $eol;
7042 $data .= $content . $eol;
7043 }
7044 $data .= "--" . $delimiter . "--".$eol;
7045
7046
7047 return $data;
7048 }
7049
7050
7051 public function run_update_profile( WP_REST_Request $request ){
7052
7053 global $wpdb;
7054 $data = array();
7055 $params = $request->get_params();
7056 $user_id = $params['user_id'];
7057 $access_token = $params['access_token'];
7058 $device_id = $params['device_id'];
7059 $first_name = $params['first_name'];
7060 $last_name = $params['last_name'];
7061 $date_of_birth = $params['birth_date'];
7062 $mobile_no = $params['phone_number'];
7063 $mobile_code = $params['country_code'];
7064 $country_id = $params['country_id'];
7065 $country_name = $params['country_name'];
7066 $gender = $params['gender'];
7067 $address = $params['address'];
7068 $zip_code = $params['zip_code'];
7069 $nationality = $params['nationality'];
7070 $avatar = $params['file'];
7071 $nid = $params['nfile'];
7072 $old_password = $params['old_password'];
7073 $new_password = $params['password'];
7074
7075 require_once('vendor/autoload.php');
7076
7077
7078 // data fields for POST request
7079 $fields = [
7080 'first_name' => $first_name,
7081 'last_name' => $last_name,
7082 'date_of_birth' => $date_of_birth,
7083 'mobile_no' => $mobile_no,
7084 'mobile_code' => $mobile_code,
7085 'country_id' => $country_id,
7086 'zip_code' => $zip_code,
7087 'gender' => $gender,
7088 'address' => $address,
7089 'client_id' => 22,
7090 'device_id' => $device_id,
7091 ];
7092
7093 $files = [];
7094
7095
7096 if(!empty($new_password) && !empty($old_password) )
7097 {
7098 $fields['password'] = $old_password;
7099 $fields['new_password'] = $new_password;
7100 }
7101
7102 if (!empty($avatar)) {
7103 $fields['avatar'] = $avatar;
7104
7105 }
7106
7107 if (!empty($nid)) {
7108 $fields['nid'] = $nid;
7109 }
7110
7111
7112 try {
7113
7114 $userClient = new GuzzleHttp\Client();
7115 $response = $userClient->request('post', 'https://account.witoken.global/api/users', [
7116 'headers' => [
7117 'Accept' => 'application/json',
7118 'Authorization' => 'Bearer '.$access_token
7119 ],
7120 'form_params' => $fields,
7121 // 'multipart' => $files
7122 ]);
7123
7124 $respt = json_decode((string)$response->getBody(), true);
7125
7126 if($respt['status'] == 'success'){
7127
7128 if( !empty($first_name) ){
7129 update_user_meta($user_id, 'first_name', $first_name);
7130 }
7131
7132 if( !empty($last_name) ){
7133 update_user_meta($user_id, 'last_name', $last_name);
7134 }
7135
7136 if( !empty($date_of_birth) ){
7137 update_user_meta($user_id, 'birth_date', $date_of_birth);
7138 }
7139
7140 if( !empty($gender) ){
7141 update_user_meta($user_id, 'gender', $gender);
7142 }
7143
7144 if( !empty($mobile_no) ){
7145 update_user_meta($user_id, 'phone_number', $mobile_no);
7146 }
7147
7148 if( !empty($country_code) ){
7149 update_user_meta($user_id, 'country_code', $country_code);
7150 }
7151
7152 if( !empty($country_id) ){
7153 update_user_meta($user_id, 'country_id', $country_id);
7154 }
7155
7156 if( !empty($country_name) ){
7157 update_user_meta($user_id, 'country_name', $country_name);
7158 }
7159
7160 if( !empty($nationality) ){
7161 update_user_meta($user_id, 'nationality', $nationality);
7162 }
7163
7164 if( !empty($zip_code) ){
7165 update_user_meta($user_id, 'zip_code', $zip_code);
7166 }
7167
7168 if( !empty($address) ){
7169 update_user_meta($user_id, 'address', $address);
7170 }
7171
7172 if( !empty($_POST['new_password']) ){
7173 wp_set_password( $_POST['new_password'], $user_id );
7174 }
7175
7176 if( !empty($avatar) ){
7177 $avatar_id = $this->uploadImgforProfile($avatar);
7178 update_user_meta($user_id, 'avatar_id', $avatar_id);
7179 update_user_meta($user_id, 'jas_avatar', $avatar_id);
7180 }
7181
7182 if( !empty($nid)){
7183 $nid = $this->uploadImgforNid($nid);
7184 update_user_meta($user_id, 'nid', $nid);
7185 }
7186 $avatar = wp_get_attachment_url( get_user_meta($user_id, 'avatar_id', true) );
7187 $nid = wp_get_attachment_url( get_user_meta($user_id, 'nid', true) );
7188
7189 $data['success'] = true;
7190 $data['meta'] = $this->array_map_t($user_id);
7191 $data['avatar'] = $avatar;
7192 $data['nid'] = $nid;
7193 $data['msg'] = $respt['message'];
7194
7195 } else {
7196
7197 $data['success'] = false;
7198 $data['msg'] = $respt['message'];
7199 }
7200
7201 } catch (\Exception $e) {
7202 $data['success'] = false;
7203 $data['msg'] = $e->getMessage();
7204 }
7205
7206
7207 return $data;
7208
7209 }
7210
7211
7212 public function array_map_t($user_id){
7213 // Get all user meta data for $user_id
7214 $meta = get_user_meta($user_id);
7215 // Filter out empty meta data
7216 $data = array_filter(array_map(
7217 function ($a)
7218 {
7219 return $a[0];
7220 }
7221 , $meta));
7222
7223 return $data;
7224 }
7225
7226 public function run_password_reset( WP_REST_Request $request ){
7227
7228 global $wpdb;
7229 $data = array();
7230 $params = $request->get_params();
7231 $email = $params['email'];
7232 $string = $params['password'];
7233
7234 // you may change these values to your own
7235 $secret_key = 'x877VK9uoqoweb1c';
7236 $secret_iv = '9Hbbq18bcjow16we';
7237 $salt = 'Khela-Ghor-';
7238 $encrypt_method = "AES-128-CFB";
7239
7240 $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $secret_key, 0, $secret_iv );
7241 $userInfo = get_user_by('email', $email);
7242 if($userInfo === null) {
7243 return false;
7244 }
7245
7246
7247 if( strpos( $output, $salt ) !== false) {
7248 $password = str_replace($salt, "", $output);
7249 wp_set_password($password, $userInfo->ID );
7250
7251 }
7252 else {
7253 return false;
7254 }
7255
7256
7257 }
7258
7259 public function run_update_affiliate_rank_by_package( WP_REST_Request $request ){
7260
7261 $data = array();
7262 $params = $request->get_params();
7263 $user_id = $params['user_id'];
7264 $package_id = $params['package_id'];
7265
7266 $affiliate_id = $this->affiliate_get_id_by_uid($user_id);
7267 if( $affiliate_id !=0 ){
7268
7269 //Package rank id & rank order
7270 $rank_id = get_post_meta( $package_id, 'bounty_affiliate', true);
7271 $package_rank = $this->get_rank($rank_id);
7272 $package_rank_order = $package_rank['rank_order'];
7273
7274 //Retrive user present rank order and compare with package rank order
7275 $users_rank_id = $this->get_affiliate_rank($affiliate_id);
7276 $users_rank = $this->get_rank($users_rank_id);
7277 $users_rank_order = $users_rank['rank_order'];
7278
7279 //check user's rank order small than package rank order
7280 if( $users_rank_order < $package_rank_order){
7281 $this->update_affiliate_rank_by_uid($user_id, $rank_id);
7282 $data['success'] = true;
7283 $data['msg'] = "Rank updated successfully";
7284 } else {
7285 $data['success'] = true;
7286 $data['msg'] = "You already assigned higher or same rank";
7287 }
7288
7289 } else {
7290 $data['success'] = false;
7291 $data['msg'] = "Given affiliate id not found. ";
7292 }
7293
7294 return $data;
7295 }
7296
7297
7298 public function run_witoken_request( WP_REST_Request $request ){
7299
7300 global $wpdb;
7301 $table_name = $wpdb->prefix . 'witoken_purchase_request';
7302 $params = $request->get_params();
7303 $user_id = $params['user_id'];
7304 $witoken_qty = $params['request_wit_qty'];
7305 $check = $wpdb->insert($table_name,
7306 array(
7307 'user_id' => $user_id,
7308 'date' => date('Y-m-d'),
7309 'witoken_qty' => $witoken_qty
7310 )
7311 );
7312 $transaction_id = $wpdb->insert_id;
7313
7314 if($check){
7315 return $return = array( 'success' => true);
7316 } else {
7317 return $return = array( 'success' => false);
7318 }
7319
7320 }
7321
7322 public function run_witoken_request_list( WP_REST_Request $request ){
7323
7324 global $wpdb;
7325 $table = $wpdb->prefix . 'witoken_purchase_request';
7326 $params = $request->get_params();
7327 $user_id = $params['user_id'];
7328 $sql = "SELECT * FROM $table WHERE user_id =".$user_id." ORDER BY Id DESC";
7329 return $result = $wpdb->get_results($sql, ARRAY_A);
7330 }
7331
7332
7333 public function run_country_wise_payment_methods( WP_REST_Request $request ){
7334
7335 $data = array();
7336 $params = $request->get_params();
7337 $smart2pay_billing_country = $params['country'];
7338 $ch = curl_init();
7339 curl_setopt($ch, CURLOPT_URL,"https://paytest.smart2pay.com/v1/methods?Country=".$smart2pay_billing_country."");
7340 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7341 'Content-Type' => 'application/json',
7342 'Authorization: Basic MzAyMDE6aEo1Um9iWXg5cjdGZk53Q3ZIWTlMWEhxcXIrRkV6cmM3YUp2UVFrNEdhejFtZzdSeXk='
7343 ));
7344 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
7345 $server_output = curl_exec($ch);
7346 curl_close ($ch);
7347 return $json_data = json_decode($server_output);
7348 if( !empty($json_data) ){
7349
7350 $i=0;
7351 foreach( $json_data->Methods as $method ){
7352 $data[$i][$method->ID] = $method->DisplayName;
7353 $i++;
7354 }
7355 }
7356
7357 return $data;
7358
7359 }
7360
7361}