<?php

$token = $_POST['token'];
$body = $_POST['body'];
$title = $_POST['title'];
$key = $_POST['key'];

$security_key = decrypData($key);

if ($security_key == 'Jobayer121415') {
    
    require 'get-access-token.php';
    $serviceAccountKeyFile = 'service-account-file.json';
    $accessToken = getAccessToken($serviceAccountKeyFile);

    $url = "https://fcm.googleapis.com/v1/projects/money---test/messages:send";


    $datamsg = array(
        'action' => 'Smart Bkash',
        'title' => $title,
        'body' => $body
    );

 
    $notification = array(
        'title' => $title,
        'body' => $body
      
    );


    $arrayToSend = array(
        'token' => $token,
        'notification' => $notification,
        'data' => $datamsg,
        'android' => array(
            'notification' => array(
                'channel_id' => 'vot_finity_channel' 
            )
        )
    );

    $json = json_encode(['message' => $arrayToSend]);

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: Bearer ' . $accessToken;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);

    if ($response === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
    }

    curl_close($ch);
}

//====================================

function decrypData($text) {
    $encoded = $text;
    $decoded = base64_decode($encoded);
    $decrypted = openssl_decrypt($decoded, 'AES-128-ECB', '0175526800812345', OPENSSL_RAW_DATA);
    return $decrypted;
}
?>
