File: /var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_wht_service_user_register.php
<?php
/***
* @purpose : Register user for SP whitelabel
* @author Rohit Kashyap
* @since 31/07/2020 >> dd/mm/yyyy
* @version 0.1
* @copyright Copyright (C) 2020 Graposs Edutech Ltd. All rights reserved.
* @license Graposs Edutech Ltd.
*/
/***
* define header and include config
*/
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
include("../../../../wp-config.php");
global $wpdb;
$PSY_USERS = $wpdb->prefix . PSY_USERS;
$PSY_USERS_DETAILS = $wpdb->prefix . PSY_USERS_DETAILS;
$PSY_SUBSCRIPTION = $wpdb->prefix . PSY_SUBSCRIPTION;
$PSY_PLANS = $wpdb->prefix . PSY_PLANS;
$PSY_STATES = $wpdb->prefix . PSY_STATES;
$date = date('Y-m-d H:i:s');
include("/var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_mail_sms_functions.php");
$json = file_get_contents('php://input');
$data = json_decode($json);
if (!empty($data)) {
$type = "";
$ref_id = $data->id;
$whitelabel_id = $data->whitelabel_id;
$name = $data->name;
$email = $data->email;
$mobile = $data->mobile;
$class_id = $data->class_id;
$plan_id = $data->plan_id;
$state_id = $data->state_id;
$city_id = $data->city_id;
$center_lab_id = $data->center_lab_id;
$password = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));
$userExist = $wpdb->get_var("SELECT count(ID) FROM $PSY_USERS WHERE user_login ='$mobile' OR user_email ='$email'");
if ($userExist > 0) {
$type = 'upgrade-user';
} else {
$type = 'register-user';
}
if ($type == 'register-user') {
if ($name != '' && $email != '' && $mobile != '' && $plan_id != '' && $plan_id != 0) {
if (username_exists($mobile)) {
$array = array("status" => 0, "message" => "This number is already registered", "type" =>$type, "ref_id" => $ref_id, "self_user_id" => 0 );
} else if (email_exists($email)) {
$array = array("status" => 0, "message" => "This email id is already registered", "type" =>$type, "ref_id" => $ref_id, "self_user_id" => 0 );
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$array = array("status" => 0, "message" => "Please enter valid email address", "type" =>$type, "ref_id" => $ref_id, "self_user_id" => 0 );
} else {
$user_id = wp_create_user($mobile, $password, $email);
$username = $email;
if (is_wp_error($user_id)) {
$error = $user->get_error_message();
$array = array("status" => 0, "message" => $error, "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => 0);
} else {
/**check ref */
$checkRef = $wpdb->get_var("SELECT count(subscription_id) FROM $PSY_SUBSCRIPTION WHERE user_id='$user_id' and sp_ref_id='$ref_id'");
$user_state = $wpdb->get_var("SELECT state_name FROM $PSY_STATES where state_id='$state_id'");
$insertUserDetails = $wpdb->query("INSERT INTO $PSY_USERS_DETAILS SET user_id='$user_id', first_name='$name', mobile_number='$mobile', email_address='$email', gender_id='1',state_id='$state_id', city_id='$city_id', current_education_level='$class_id', source='Whitelabel', source_id='$whitelabel_id', center_id='$center_lab_id'");
//insert plan
$get_exams = $wpdb->get_row("SELECT reference_id,plan_price FROM $PSY_PLANS WHERE plan_id='$plan_id'");
$exam_id = explode(",", $get_exams->reference_id);
$amount = $get_exams->plan_price;
$planSucess = 0;
if ($checkRef == 0) {
foreach ($exam_id as $e_id) {
if ($e_id == '48282') {
$test_type = 'APT';
} else if ($e_id == '48305') {
$test_type = 'PER';
} else if ($e_id == '48328') {
$test_type = 'INT';
}
$insertSubscription = "INSERT INTO $PSY_SUBSCRIPTION SET user_id='$user_id', plan_id='$plan_id', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='Whitelabel', source_id='$whitelabel_id',sp_ref_id='$ref_id'";
if ($wpdb->query($insertSubscription)) {
$planSucess += 1;
}
}
}
if ($planSucess == count($exam_id)) {
/********create invoice data */
include_once '/var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_functions.php';
invoice('Plan Purchase','WP',$plan_id,$ref_id,$amount,'',$user_id,$name,$email,'',$user_state,$date);
sendTestPurchaseSMS($mobile, $name);
sendRegEmail($username,$email,$name,$password);
sendTestPurchaseEmail($email, $name);
sendCredentials($mobile,$password);
$array = array("status" => 1, "message" => 'Success', "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => $user_id);
} else {
$array = array("status" => 0, "message" => 'Failed', "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => 0);
}
}
}
} else {
$array = array("status" => 0, "message" => 'Name,Email,Mobile is required', "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => 0);
}
}
if ($type == 'upgrade-user') {
if (username_exists($mobile)) {
$user = get_user_by('login', $mobile);
$user_id = $user->ID;
/**check ref */
$checkRef = $wpdb->get_var("SELECT count(subscription_id) FROM $PSY_SUBSCRIPTION where user_id='$user_id' and sp_ref_id='$ref_id'");
//insert plan
$get_exams = $wpdb->get_row("SELECT reference_id, plan_price FROM $PSY_PLANS where plan_id='$plan_id'");
$userData = $wpdb->get_row("SELECT first_name,email_address,mobile_number FROM $PSY_USERS_DETAILS where user_id='$user_id'");
$amount = $get_exams->plan_price;
$name = $userData->first_name;
$email = $userData->email_address;
$mobile = $userData->mobile_number;
$exam_id = explode(",", $get_exams->reference_id);
$planSucess = 0;
if ($checkRef == 0) {
foreach ($exam_id as $e_id) {
if ($e_id == '48282') {
$test_type = 'APT';
} else if ($e_id == '48305') {
$test_type = 'PER';
} else if ($e_id == '48328') {
$test_type = 'INT';
}
$insertSubscription = "INSERT into $PSY_SUBSCRIPTION set user_id='$user_id', plan_id='$plan_id', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='Whitelabel', source_id='$whitelabel_id',sp_ref_id='$ref_id'";
if ($wpdb->query($insertSubscription)) {
$planSucess += 1;
}
}
}
if ($planSucess == count($exam_id)) {
/********create invoice data */
include_once '/var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_functions.php';
invoice('Plan Purchase','WP',$plan_id,$ref_id,$amount,'',$user_id,$name,$email,'',$user_state,$date);
sendTestPurchaseSMS($mobile, $name);
sendRegEmail($username,$email,$name,$password);
sendTestPurchaseEmail($email, $name);
sendCredentials($mobile,$password);
$array = array("status" => 1, "message" => 'Success', "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => $user_id);
} else {
$array = array("status" => 0, "message" => 'Failed', "type" =>$type, "ref_id" => $ref_id , "upID" => $plan_id, "ucID" => $class_id, "self_user_id" => 0);
}
} else {
$array = array("status" => 0, "message" => 'No user found', "type" =>$type, "ref_id" => $ref_id, "self_user_id" => 0 );
}
}
echo json_encode($array);
} else {
$dataArray = array("status" => 0, "message" => "Invalid parameters");
}