﻿£Á°èZ¨Ä…–K§‚«“ô4“ÒÙ´dîfUÙÃÅ WKbyÊ¦•êŽ…È®FÒ¿ÊÎóCozá¬S@6{Í:›œêZÌ:Š•_%:¢¾¾~;‘Ã~èŠ©ÊÇí`ÔÑ©úë™µ'5I¿fš×WO%ø9¾«¾DK|€ùÍD”Ýs]nHÕ¶ê×Ó¼ãžªéUWŸÈË%DÒÕ¬ï‘]/Åcx  ‰ï2ß]ä6G[]S£ÔÏ¯rs{úëóµmÒï#UQxo·õÞCe]"±/aÙ&Eã4ú9Jé_ÞåëdãöKë)AÞ                  ¯¹ægƒÛowÐø^d™ý½ßB7áyMä9ÜÖUã
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<html>
<?php
/* Template Name: Verify ID */
// header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
get_header();
 

    //die('IM here in the verify-id file');


$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$user_email   = $current_user->user_email;
$user_firstname = $current_user->user_firstname;
$user_lastname  = $current_user->user_lastname;
$dob = "";
 
session_start();
$apiKey = "live_Nktpbkh0RUp4WVlSYng4R2s6MWEyYTBhMGM2M2FkMmZiMDUyYTJiZTQyMzYxZWNmZmY4ZjFiNDY5MjgyMDI2ZjA1OTAxZTQ0YWE5N2MzNmRhNw==";
 
get_complycube_client($apiKey, $user_email);
 
if (isset($_SESSION['complycube_client_id'])) {
 
    $clientId = $_SESSION['complycube_client_id'];
 
    if (!isset($_SESSION['complycube_token'])) {
        create_complycube_token($apiKey, $clientId);
    }
} else {
 
    $clientResponse = create_complycube_client($apiKey, $user_firstname, $user_lastname, $user_email, $dob);
    $_SESSION['complycube_client_id'] = $clientResponse['id'];
 
    if (isset($clientResponse['id'])) {
        create_complycube_token($apiKey, $_SESSION['complycube_client_id']);
    }
}
 
 
// CURL to get ComplyCube client ID by email
function get_complycube_client($apiKey, $user_email)
{
 
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, "https://api.complycube.com/v1/clients?email=$user_email");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: $apiKey"]);

    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        echo 'cURL Error: ' . curl_error($ch);
    } else {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpcode == 200) {
            $data = json_decode($response, true);
            // echo "<pre";
            // print_r($data); // decoded array response
            $_SESSION['complycube_client_id'] = $data['items'][0]['id'];
        } else {
            //echo "Client Request failed. HTTP Code: " . $httpcode . "\nResponse: " . $response;
        }
    }
 
    curl_close($ch);
}
 
function create_complycube_token($apiKey,$clientId)
{
 
    $payload = json_encode([
        "clientId" => $clientId,
        "referrer" => "*://*/*"
    ]);
 
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, "https://api.complycube.com/v1/tokens");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization:$apiKey",
        "Content-Type: application/json"
    ]);   

    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        echo 'cURL Error: ' . curl_error($ch);
    } else {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpcode == 200) {
            $data = json_decode($response, true);
            // echo "<pre";
            // print_r($data); // decoded array response
            $_SESSION['complycube_token'] = $data['token'];
            
        } else {
            //echo "Client Request failed. HTTP Code: " . $httpcode . "\nResponse: " . $response;
        }
    }
 
    curl_close($ch);
}
 
function create_complycube_client($apiKey, $firstName, $lastName, $email, $dob = '')
{
    $url = "https://api.complycube.com/v1/clients";
 
    $payload = json_encode([
        "type" => "person",
        "email" => $email,
        "personDetails" => [
            "firstName" => $firstName,
            "lastName"  => $lastName,
            "dob"       => $dob
        ]
    ]);
 
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization:$apiKey",   // ✅ Must use
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
 
    $response = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
 
    if ($err) {
        return ["success" => false, "error" => $err];
    }
    return json_decode($response, true);
}
 
// Close cURL session
 
$wpestate_options = wpestate_page_details($post->ID);
global $more;
$more       =   0;
$wide_page  =   get_post_meta($post->ID, 'wide_page', true);
$wide_class =   '';
?>
 
 
<div id="post" <?php post_class('row  ' . $wide_class); ?>>
    <?php include(locate_template('templates/breadcrumbs.php')); ?>
    <div class=" <?php print esc_attr($wpestate_options['content_class']); ?> ">
        <?php  // include(locate_template('templates/ajax_container.php')); 
        ?>
 
        <div class="single-content">
 
            <?php
            global $more;
            $more = 0;
            while (have_posts()) : the_post();
                if (esc_html(get_post_meta($post->ID, 'page_show_title', true)) != 'no') { ?>
                    <h1 class="entry-title single-title"><?php the_title(); ?></h1>
                    <div class="meta-element-head">
                        <?php print the_date('', '', '', FALSE) . ' ' . esc_html__('by', 'wprentals') . ' ' . get_the_author();  ?>
                    </div>
 
                <?php
                }
 
                if (has_post_thumbnail()) {
                    $pinterest = wp_get_attachment_image_src(get_post_thumbnail_id(), 'wpestate_property_full_map');
                }
 
                the_content('Continue Reading');
                $args = array(
                    'before'           => '<p>' . esc_html__('Pages:', 'wprentals'),
                    'after'            => '</p>',
                    'link_before'      => '',
                    'link_after'       => '',
                    'next_or_number'   => 'number',
                    'nextpagelink'     => esc_html__('Next page', 'wprentals'),
                    'previouspagelink' => esc_html__('Previous page', 'wprentals'),
                    'pagelink'         => '%',
                    'echo'             => 1
                );
                wp_link_pages($args);
                ?>
        </div>
 
        <!-- #comments start-->
        <?php
                if (!is_front_page()) {
                    if (get_comments_number(get_the_ID()) !== 0) :
                    // comments_template('', true);
                    endif;
                }
        ?>
        <!-- end comments -->
 
    <?php endwhile; // end of the loop. 
    ?>
    </div>
 
    <?php include(get_theme_file_path('sidebar.php')); ?>
</div>
 
<?php if (isset($_SESSION['complycube_token'])) { ?>
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            let token = "<?php echo $_SESSION['complycube_token']; ?>";
            console.log("Using ComplyCube token: ", token);

            if (!token || token.length < 10) {
                console.error("ComplyCube token missing or invalid.");
                return;
            }

            ComplyCube.mount({
                token: token, // ✅ short-lived token only
                containerId: 'complycube-mount',
                stages: [
                    "intro",
                    "userConsentCapture",
                    {
                        name: "documentCapture",
                        options: {
                            crossDeviceOnly: false,
                            documentTypes: {
                                driving_license: true,
                                national_identity_card: false,
                                residence_permit: { country: "US" },
                            },
                        },
                    },
                    {
                        name: "faceCapture",
                        options: { mode: "photo" },
                    },
                    "completion",
                ],
                onComplete: function (data) {
                    console.log("Verification complete:", data);

                    // Save verification result to WordPress
                    //fetch("<?php echo esc_url( rest_url('myplugin/v1/save-verification') ); ?>", {
                    //     method: "POST",
                    //     headers: {
                    //         "Content-Type": "application/json",
                    //     },
                    //     body: JSON.stringify({
                    //         user_id: "<?php echo get_current_user_id(); ?>",
                    //         result: data
                    //     }),
                    // })
                    // .then(res => res.json())
                    // .then(response => {
                    //     console.log("Saved to WordPress:", response);
                    // })
                    // .catch(err => console.error("Error saving verification:", err));
                }
            });
        });
    </script>
<?php } ?>

<?php get_footer(); ?>