<?php

session_start();

include "include.php";
include "session.php";
$ajax_logout = 1;
$updte = 1;
///Start Env Value
$key = '6998398bea78f121dbca0acdf6a11b6e';
///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,
        "client_txn_id" => $o1->ref_number,
        "amount" => $o1->amount,
        "remark1" => "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',
    ];
    echo "<pre>";print_r($arr);die;
    //$json_data = json_encode($arr,true);
    
    if($o1->request_money_id){
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://tajapi.site/api/create-order',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => http_build_query($arr),
          CURLOPT_HTTPHEADER => array(
            'Content-Type: application/x-www-form-urlencoded'
          ),
        ));
        
        $response = curl_exec($curl);
        curl_close($curl);
        $res_data = json_decode($response,true);
        // $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);
?>