<?php
    session_start();
    error_reporting(1);
  include('../../includes/config.php');
    
   
    
    
    
    if (!isset($_SESSION['alogin'])) {
        header('location:login.php');
        die('LOGIN FIRST');
    }

    $amount = $_POST['amount'];
    if(!$amount){
        die('Amount Paramitre Required');
    }
    $amount = $amount;
    $email = $_SESSION['alogin'];
    $apiKey =  'akYMgp3HaSQalyhL0';
    $url = 'https://tzsmmpay.com/api/payment/create';
    
    $paymentData = [
        'api_key' => $apiKey,
        'cus_name' => 'CUS NAME',
        'cus_email' => $email,
        'cus_number' => $_SESSION['alogin'],
        'amount' => $amount,
        'currency' => 'BDT',
        'success_url' => 'https://bestpoint.site/recharge.php',
        'cancel_url' => 'https://bestpoint.site/recharge.php',
        'callback_url' => 'https://bestpoint.site/tzsmm_callback.php',
    ];

    $options = [
        'http' => [
            'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => http_build_query($paymentData),
        ],
    ];

    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    // Decode the JSON response
    $responseData = json_decode($response, true);

    if ($responseData && $responseData['success']) {
        DIE($responseData['payment_url']) ;
    } else {
        DIE($responseData['messages'] ?? 'An error occurred.');
    }
?>