<?php
session_start();
    if(!isset($_SESSION['id'])) {
        header("location: ../index.php");
    }
?>

<?php

include 'connection.php';


$APP_ID = $_SESSION['id'];
// echo "$APP_ID ";

$sql = "SELECT * FROM AD_APP 
 WHERE APP_ID = '$APP_ID' ";
$parse = ociparse($conn, $sql);
oci_execute($parse);

// print_r($sql);
while ($row = oci_fetch_assoc($parse)) {
	$user_row[] = $row;
}




include 'token.php';

$credentials_json = file_get_contents('config.json');
$credentials_arr = json_decode($credentials_json, true);

function create()
{
    getToken();
    global $credentials_arr;
    $post_token = array(
        'mode' => '0011',
        'amount' => $_POST['amount'] ? $_POST['amount'] : 1,
        'payerReference' => $_POST['id'],
        // 'callbackURL' => "http://" . $_SERVER['SERVER_NAME']."/".basename(__DIR__)."/callback.php", // Your callback URL
        'callbackURL' => "http://localhost/ndc_2023/RAW_PHP_Bkash_july_2023/callback.php", // Your callback URL
        'currency' => 'BDT',
        'intent' => 'sale',
        'merchantInvoiceNumber' => 'INV_APP_ID_'.$_POST['id']
    );

    $url = curl_init($credentials_arr['base_url'] . "/checkout/create");
    $post_token = json_encode($post_token);
    $header = array(
        'Content-Type:application/json',
        'Authorization:' . $_SESSION["token"],
        'X-APP-Key:' . $credentials_arr['app_key']
    );

    curl_setopt($url, CURLOPT_HTTPHEADER, $header);
    curl_setopt($url, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($url, CURLOPT_POSTFIELDS, $post_token);
    curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);
    $result_data = curl_exec($url);
    curl_close($url);

    $response = json_decode($result_data, true);

    header("Location: " . $response['bkashURL']);
    exit;
}


if (isset($_POST['form_submitted'])) {
    echo create();
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bkash Payment</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<body>

    <div class="container" style="margin-top: 150px;margin-bottom: 100px;">
        <div class="card">
            <div class="row">
                <div class="col-md-12">
                    <div style="text-align: center;margin: 50px;">
                        <form action="payment.php" method="POST">



                            <label for="amount" style="font-weight: bold;">Paid Amount:</label>
                            <input type="hidden" name="id" value="<?PHP echo $user_row[0]['APP_ID'] ?>">
                            <input type="text" id="amount" name="amount" value="300" readonly><br><br>
                            <input type="hidden" name="form_submitted" value="1" />
                            <input type="submit" class="btn btn-primary" value="Pay With Bkash">
                        </form>



                    </div>

                </div>
            </div>
        </div>
    </div>

    <!-- <br><br>
    <form action="payment.php" method="POST">



        <label for="amount">Amount:</label>
        <input type="hidden" name="id" value="<?PHP echo $all_data[0]['ID'] ?>">
        <input type="text" id="amount" name="amount" value="<?PHP echo $all_data[0]['SUB_TOTAL'] ?>"><br><br>
        <input type="hidden" name="form_submitted" value="1" />
        <input type="submit" value="Pay With Bkash">
    </form> -->


    <!-- Latest compiled and minified JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>

</html>