<?php
    include'connection.php';
    
     $rechId=$_GET['id'];
     if(isset($_POST['submit']))
     {
         $UseApi=$_POST['api'];
         
         $quGetRech="select * from recharge where Id='$rechId'";
         $reGetRech=mysqli_query($con,$quGetRech);
         $dataGetRech=mysqli_fetch_array($reGetRech);
         
          $mnum=$dataGetRech['Mobile'];
          $amount=$dataGetRech['Amount'];
          $operator=$dataGetRech['Companyname'];
          $postpaidOperator=substr($operator, -8);
            $opcodequery="select * from operator where Operator='$operator'";
			$opcoderesult=mysqli_query($con,$opcodequery);
			$opcodedata=mysqli_fetch_array($opcoderesult);
			$opcode=$opcodedata['Value'];
			$myApiOpCode=$opcodedata['MyApiValue'];
			$QuickApiOpCode=$opcodedata['Quick_API'];
			$KwikApiOpCode=$opcodedata['KwikAPI'];
			$UniqueApiOpCode=$opcodedata['UniqueAPI'];
			
			$newrechid=$rechId;
         
                                            if($UseApi=="My_API")
											{
											    $queryUpadateOrderId="update recharge set ReHit='0', API='My_API' where Id='$newrechid'";
												mysqli_query($con,$queryUpadateOrderId);
												
												$token="01dc7e7d-ec4d-42db-99b4-cdf62a80a72a";
												$url="https://www.vastwebindia.com/API/API/Recharge?Mobile=$mnum&OptCode=$myApiOpCode&Amount=$amount&Token=$token&Userid=sameermansuri111@gmail.com&rch_id=$newrechid";
												
												$curl = curl_init();

                                                curl_setopt_array($curl, array(
                                                  CURLOPT_URL => "https://www.vastwebindia.com/API/API/Recharge?Mobile=$mnum&OptCode=$myApiOpCode&Amount=$amount&Token=$token&Userid=sameermansuri111@gmail.com&rch_id=$newrechid",
                                                  CURLOPT_RETURNTRANSFER => true,
                                                  CURLOPT_ENCODING => "",
                                                  CURLOPT_MAXREDIRS => 10,
                                                  CURLOPT_TIMEOUT => 30,
                                                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                                  CURLOPT_CUSTOMREQUEST => "GET",
                                                  CURLOPT_POSTFIELDS => "",
                                                  CURLOPT_HTTPHEADER => array(
                                                "Postman-Token: 7d11312d-5948-4352-be19-b636152e59fd",
                                                    "cache-control: no-cache"
                                                                      ),
                                                                ));

                                                    $response = curl_exec($curl);
                                                    $err = curl_error($curl);

                                                    curl_close($curl);

                                                        if ($err) {
                                                          echo "cURL Error #:" . $err;
                                                        } else
                                                        {
                                                            echo   $response;
                                                        }
											}
											elseif($UseApi=="Quick_API")
											{
											    $queryUpadateOrderId="update recharge set ReHit='0', API='Quick_API' where Id='$newrechid'";
												mysqli_query($con,$queryUpadateOrderId);
											    
												$apiKey="FROY38amkREsr7mHOutxIou64PDe3t02Qr9";
												$apiUser="9897095971";
												$apiPwd="9897095971";
											echo	$url="https://www.rechargedaddy.in/RDRechargeAPI/RechargeAPI.aspx?MobileNo=$apiUser&APIKey=$apiKey&REQTYPE=RECH&REFNO=$newrechid&SERCODE=$QuickApiOpCode&CUSTNO=$mobile&REFMOBILENO=9897095971&AMT=$amount&STV=0&RESPTYPE=JSON";

                                                $curl = curl_init();

                                                curl_setopt_array($curl, array(
                                                CURLOPT_URL => $url,
                                                CURLOPT_RETURNTRANSFER => true,
                                                CURLOPT_ENCODING => "",
                                                CURLOPT_MAXREDIRS => 10,
                                                CURLOPT_TIMEOUT => 30,
                                                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                                CURLOPT_CUSTOMREQUEST => "POST",
                                                CURLOPT_POSTFIELDS => "",
                                                CURLOPT_HTTPHEADER => array(
                                               "Postman-Token: 79fe2baf-8d4d-4a22-abbd-ff11a569289a",
                                                "cache-control: no-cache"
                                                  ),
                                                    ));

                                                $response = curl_exec($curl);
                                                $err = curl_error($curl);

                                                curl_close($curl);
												if ($err)
												{
													echo "cURL Error #:" . $err;
												}
												else
												{
												    echo $response;
												}
											}
											elseif($UseApi=="Kwik_API")
											{
											    $queryUpadateOrderId="update recharge set ReHit='0', API='Kwik_API' where Id='$newrechid'";
												mysqli_query($con,$queryUpadateOrderId);
												
												
											    $apikey="8tyeubd-prieyeiw-854125-75yhr-8454adz";
											    if($postpaidOperator=="Postpaid")
											    {
											        $url="https://www.kwikapi.com/api/v2/bills/recharge.php?api_key=$apikey&number=$mnum&amount=$amount&opid=$KwikApiOpCode&state_code=0&order_id=$newrechid";
											    
											        
													$curl = curl_init();
													curl_setopt_array($curl, array(
													CURLOPT_URL => $url,
													CURLOPT_RETURNTRANSFER => true,
													CURLOPT_ENCODING => "",
													CURLOPT_MAXREDIRS => 10,
													CURLOPT_TIMEOUT => 0,
													CURLOPT_FOLLOWLOCATION => false,
													CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
													CURLOPT_CUSTOMREQUEST => "GET",
													));
	
													$response = curl_exec($curl);
													$err = curl_error($curl);
	
													curl_close($curl);
	
													if ($err)
													{
														echo "cURL Error #:" . $err;
													}
													else
													{
														echo $response;
													}
												
											    }
											    else
											    {
											        echo $url="https://www.kwikapi.com/api/v2/recharge.php?api_key=$apikey&number=$mnum&amount=$amount&opid=$KwikApiOpCode&state_code=0&order_id=$newrechid";
											    
											        
													$curl = curl_init();
													curl_setopt_array($curl, array(
													CURLOPT_URL => $url,
													CURLOPT_RETURNTRANSFER => true,
													CURLOPT_ENCODING => "",
													CURLOPT_MAXREDIRS => 10,
													CURLOPT_TIMEOUT => 0,
													CURLOPT_FOLLOWLOCATION => false,
													CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
													CURLOPT_CUSTOMREQUEST => "GET",
													));
	
													$response = curl_exec($curl);
													$err = curl_error($curl);
	
													curl_close($curl);
	
													if ($err)
													{
														echo "cURL Error #:" . $err;
													}
													else
													{
														echo $response;
														$data2=json_decode($response);
													
															$status1 = $data2->status;
															$resText = $data2->message;
															$txnId = $data2->opr_id;
															$kwikApiBal = $data2->balance;
													
															if($status1=="SUCCESS")
															{
																$queryUpadateOrderId="update recharge set Transactionid='$orderId', AdminOpId='S', Status='$status1', Operatorid='$txnId', API='Kwik_API', ApiBal='$kwikApiBal' where Id='$newrechid'";
																mysqli_query($con,$queryUpadateOrderId);
																echo "<h1 align='center'><font color='#008000'> &#x2714; Your Request Submit Successfull !</font></h1><br>";
															}
															elseif($status1=="FAILED")
															{
															    $UpdateRechApibal="update recharge set ReHit='1', AdminOpId='$resText' where Id='$newrechid'";
	                                                        	mysqli_query($con,$UpdateRechApibal);
	                                                        	
	                                                        	
															}
													}
											    }
											}
											elseif($UseApi=="Unique_API")
											{
											    $queryUpadateOrderId="update recharge set ReHit='0', API='Unique_API' where Id='$newrechid'";
												mysqli_query($con,$queryUpadateOrderId);
											    
												$apiUserId="9897095971";
												$apiPwd="123456";
												$curl = curl_init();

                                                curl_setopt_array($curl, array(
                                                  CURLOPT_URL => "http://uniqueebazaar.store/index.php/RechargeWebService/apitransaction?userid=9897095971&password=123456&mobile=$mnum&opt=$UniqueApiOpCode&amount=$amount&agentid=$newrechid&fmt=json",
                                                  CURLOPT_RETURNTRANSFER => true,
                                                  CURLOPT_ENCODING => "",
                                                  CURLOPT_MAXREDIRS => 10,
                                                  CURLOPT_TIMEOUT => 30,
                                                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                                  CURLOPT_CUSTOMREQUEST => "POST",
                                                  CURLOPT_POSTFIELDS => "",
                                                  CURLOPT_HTTPHEADER => array(
                                                    "Postman-Token: 12172b3e-8111-46ff-a491-71ca0f581b7e",
                                                    "cache-control: no-cache"
                                                          ),
                                                        ));

                                                    $response = curl_exec($curl);
                                                    $err = curl_error($curl);

                                                    curl_close($curl);
												
												if ($err)
												{
													echo "cURL Error #:" . $err;
												}
												else
												{
													echo $response;
													
												}
											}
											elseif($UseApi=="Rech_API")
											{	
											    $UpdateRechApibal="update recharge set ReHit='0', API='Rech_API' where Id='$newrechid'";
												mysqli_query($con,$UpdateRechApibal);
												
												$token="4x9tiI9Zz7Sxmi3G8qvhpczsijBLBt";
												$url="http://api.rechapi.com/recharge.php?format=xml&token=$token&mobile=$mnum&amount=$amount&opid=$opcode&urid=$newrechid";
												$request_timeout = 60; // 60 seconds timeout
												$ch = curl_init();
												curl_setopt($ch, CURLOPT_URL, $url);
													curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
												curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
												curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
												$output = curl_exec($ch);
												$curl_error = curl_errno($ch);
												$getserver= curl_getinfo($ch);
												curl_close($ch);
												if($getserver["http_code"]==200)
												{
												
													$xml = simplexml_load_string($output);
													$orderId=$xml->orderId;
													$status1=$xml->status;
													$mobile=$xml->mobile;
													$amount=$xml->amount;
													$operatorId=$xml->operatorId;
													$error_code=$xml->error_code;
													$service=$xml->service;
													$resText=$xml->resText;
													if($status1=="PENDING")
													{
														$queryUpadateOrderId="update recharge set Transactionid='$orderId', API='Rech_API' where Id='$newrechid'";
														mysqli_query($con,$queryUpadateOrderId);
														echo "<h1 align='center'><font color='#008000'> &#x2714; Your Request Submit Successfull !</font></h1><br>";
													}
													elseif($status1=="SUCCESS")
													{
													    $queryUpadateOrderId="update recharge set Transactionid='$orderId', Status='SUCCESS', API='Rech_API', AdminOpId='S' where Id='$newrechid'";
														mysqli_query($con,$queryUpadateOrderId);
														echo "<h1 align='center'><font color='#008000'> &#x2714; Your Request Submit Successfull !</font></h1><br>";
													}
													elseif($status1=="FAILED")
													{
													    $UpdateRechApibal="update recharge set ReHit='1', AdminOpId='$resText', Transactionid='$orderId' where Id='$newrechid'";
	                                                   	mysqli_query($con,$UpdateRechApibal);
	                                                	echo "<h1 align='center'><font color='#ff0000'> &#x2716; Your Recharge Request Has Been Failed Now !</font></h1>";
	                                                        	
													}
													
													
												}
												elseif($curl_error=="28")
												{
													$UpdateRechApibal="update recharge set ReHit='1', AdminOpId='$resText', Transactionid='$orderId' where Id='$newrechid'";
	                                                mysqli_query($con,$UpdateRechApibal);
	                                                echo "<h1 align='center'><font color='#ff0000'> &#x2716; Your Recharge Request Has Been Failed Now !</font></h1>";
												}
											
											}
     }

?>
<html>
<head>
    
</head>
<body>
<form method="post">
    <table border="1" cellpadding="10">
        <tr>
            <td>
                <select name="api">
                    <option value="My_API">MY API</option>
                    <option value="Quick_API">Quick API</option>
                    <option value="Unique_API">Unique API</option>
                    <option value="Kwik_API">Kwik API</option>
                    <option value="Rech_API">Rech API</option>
                </select>
            </td>
            <td>
                <input type="submit" name="submit" value="Re-Hit">
            </td>
    <table>
</form>
</body>
</html>