<?php

session_start();

include "include.php";
include "session.php";
$ajax_logout = 1;
$updte = 1;
///Start Env Value
$key = 'e96ee40b90d5df0f7b1ce9dbeb380682';
///End Env Value
if($_POST['amount'] < 1){
    $error['error'] = 1;
    $error['error_msg'] = "Please enter valid amount.";
    echo json_encode($error);die;
}
if ($updte == 1) {

    $o1->user_id = $o->user_id;
    $o1->amount = $_POST['amount'];
    $o1->cash_credit = "Cash";
    $o1->request_date = todaysDate();
    $o1->status = "Pending";
    $o1->ref_number = reference_number().$o->user_id;
    $o1->transfer_mode = "UPI";
    $o1->transaction_number = "";
    $o1->is_active = 1;
    $o1->request_to = 1;
    $o4 = $factory->get_object($o1->request_to, "users", "user_id");
    $o1->bank_id = 1;
    $old_amount = $o->amount_balance;
    $old_credit = $o->credit_amount;
    $o1->request_money_id = $insertor->insert_object($o1, "request_money");
    $id = $o1->request_money_id;
    // $arr = [
    //     "key" => $key,
    //     "client_txn_id" => $o1->ref_number,
    //     "amount" => $o1->amount,
    //     "p_info" => "UPI Add Money Instant",
    //     "customer_name" => $o->name,
    //     "customer_email" => $o->email,
    //     "customer_mobile" => $o->mobile,
    //     "redirect_url" => 'https://mrechargepay.shop/instant_add_money_check_satus.php',
    //     "udf1" => "",
    //     "udf2" => "",
    //     "udf3" => "",
    // ];
    
    $arr = [
        "user_token" => $key,
        "order_id" => $o1->ref_number,
        "amount" => $o1->amount,
        "remark1" => "UPI Add Money Instant",
        "customer_name" => $o->name,
        "customer_email" => $o->email,
        "customer_mobile" => $o->mobile,
        //"customer_mobile" => rand(111111111,999999999),
        "redirect_url" => 'https://anampay.com/instant_add_money_check_satus.php?client_txn_id='.$o1->ref_number,
    ];
    $postData = http_build_query($arr); // automatically URL-encodes
   // echo "<pre>";print_r($arr);die;
    //$json_data = json_encode($arr,true);
    
    if($o1->request_money_id){
        $ch = curl_init();
        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, 'https://tajapi.store/api/create-order'); // ✅ your target URL
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Set headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/x-www-form-urlencoded',
            //'Content-Length: ' . strlen($postData)
        ]);
        
        $response = curl_exec($ch);
        curl_close($ch);
        //echo "<pre>";print_r($response);die;
        $res_data = json_decode($response,true);
        //echo "<pre>";print_r($res_data);die;
        // $set_data = "INSERT INTO roffer (res_data) VALUES ('$response')";
        //$rr = setXbyY($set_data);
        if($res_data['status'] == true){
            $error['error'] = 0;
            $error['error_msg'] = "Payment Link Generate Successfuly.";
            $error['pay_link'] = $res_data['result']['payment_url'];
            $o3 = $factory->get_object($o1->request_money_id, "request_money", "request_money_id");
            $o3->transaction_number = $res_data['result']['orderId'];
            $o3->request_money_id = $updater->update_object($o3, "request_money");
            echo json_encode($error);die;
        }else{
            $o3 = $factory->get_object($o1->request_money_id, "request_money", "request_money_id");
            $o3->status = "Rejected";
            $o3->decision = $res_data['message'];
            //$o3->decision_by = 1;
            $o3->decision_date = todaysDate();
            $o3->request_money_id = $updater->update_object($o3, "request_money");
            $error['error'] = 1;
            $error['error_msg'] = $res_data['message']." API";
            echo json_encode($error);die;
        }
        ///echo "<pre>";print_r($res_data);die;
    }
    //echo "<pre>";print_r($o1);die;
   
}

echo json_encode($result);
?>