<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
date_default_timezone_set("Asia/Calcutta");
// $con = mysqli_connect('localhost','lifelinehospital_user', 'YO77U^YFI3nS','lifelinehospital_db');
$con=mysqli_connect('localhost','login_lifeuser','(]OOR.?Mg~]r','login_lifedb');
header('Content-Type: application/json');
$valid_passwords = array("LifeLCT" => "LifLinCnslt");
$valid_users = array_keys($valid_passwords);
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);
if (!$validated) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    $responsefull = array('error_message' => 'Unathorized: please provide a valid password!', 'data' => []);
} else {
    $datas = json_decode(file_get_contents('php://input'), true);
    $consumerId = $datas['consumerId'];
    $amount = $datas['amount'];
    $amountType = $datas['amountType'];
    $maxAmount = $datas['maxAmount'];
    
    /**payment credentials */
    $pay_cred_qry = mysqli_query($con,"SELECT * FROM tbl_hims_payment_cred");
    $pay_cred_array = mysqli_fetch_array($pay_cred_qry);
    
    /**txn id generation */
    // Define the word
    $word = "LFLN";
    // Get the current date and time
    $date = date("Ymd"); // e.g., 20240729
    $time = date("His"); // e.g., 145301
    // Combine components to form the unique transaction number
    $txnId = $word.rand(10,100).$date.$time;
    
    $merchantId = 'WL0000000017532';

    function generateHash($data, $salt, $algorithm) {
        // Create the pipe-separated string from the consumer data
        $pipeSeparatedString = implode('|', $data) . '|' . $salt;

        // print_r($pipeSeparatedString);

        // Generate the hash using the specified algorithm
        $hashValue = hash($algorithm, $pipeSeparatedString);
        
        return $hashValue;
    }

    // Example consumer data
    // $consumerData = [
    //     'merchantId' => $pay_cred_array['lid'],
    //     'txnId' => $txnId,
    //     'totalamount' => $amount,
    //     'accountNo' => '',
    //     'consumerId' => $consumerId,
    //     'consumerMobileNo' => '',
    //     'consumerEmailId' => '',
    //     'debitStartDate' => '',
    //     'debitEndDate' => '',
    //     'maxAmount' => $maxAmount,
    //     'amountType' => $amountType,
    //     'frequency' => '',
    //     'cardNumber' => '',
    //     'expMonth' => '',
    //     'expYear' => '',
    //     'cvvCode' => ''
    // ];
    $consumerData = [
        'merchantId' => $pay_cred_array['lid'],
        'txnId' => $txnId,
        'totalamount' => $amount,
        'accountNo' => '',
        'consumerId' => $consumerId,
        'consumerMobileNo' => '',
        'consumerEmailId' => '',
        'debitStartDate' => '',
        'debitEndDate' => '',
        'maxAmount' => '',
        'amountType' => '',
        'frequency' => '',
        'cardNumber' => '',
        'expMonth' => '',
        'expYear' => '',
        'cvvCode' => ''
    ];    

    // Salt provided by Worldline
    $salt = $pay_cred_array['enc_key'];
    // $salt = '6514678892GJYSTA';

    // Hashing algorithm to use (e.g., 'sha1' or 'sha256')
    $algorithm = 'sha512';

    // Generate the hash value
    $hashValue = generateHash($consumerData, $salt, $algorithm);


    $data = array(
        "token" =>  $hashValue,
        // "merchantLogo" =>  "https://lifelinehospitalkerala.com/wp-content/uploads/2021/09/logo.png",
        "merchantLogo" =>  "https://lifelinehospitalkerala.com/wp-content/uploads/2024/08/Lifeline-Adoor-Logo-Grey-e1719202526331-1.png.webp",
        // "merchantId" =>  $pay_cred_array['merchant_id'],
        "merchantId" =>  $pay_cred_array['lid'],
        "consumerId" =>  $consumerId,
        "txnId" => ''.$txnId,
        'salt'=>''.$salt,
        "schemeCode" =>  $pay_cred_array['scheme_code'],
        "primary_color_code" =>  "#0C9DA3",
        "secondary_color_code" =>  "#F7A800",
        "button_color_code1" =>  "0C9DA3",
        "button_color_code2" =>  "0C9DA3"    
    );

    $response = array("status" => "true", "message" =>  "Success!", "data" => $data);
}
if (isset($response)) {
    echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
?>