<!DOCTYPE html>
<html>
<head>
	<title></title>
	 <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
</head>
</html>
<?php 
require_once '../config.php'; 
session_start();
include_once '../class.phpmailer.php';
include_once '../class.smtp.php';
require('razorpay-php/Razorpay.php');
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;
 $keyId = $keyId;
 $keySecret = $keySecret;
// $keyId = 'rzp_live_isgZOqgPKrfIRR';
// $keySecret = '0Lvq3bpha3lGlVmEBGxR6GEB';
$displayCurrency = 'INR';
$success = true;

$error = "Payment Failed";
$OrderId = $_POST['razorpay_order_id'];
$PaymentId = $_POST['razorpay_payment_id'];
$PaySign = $_POST['razorpay_signature'];
if (empty($_POST['razorpay_payment_id']) === false)
{
    $api = new Api($keyId, $keySecret);

    try
    {
        // Please note that the razorpay order ID must
        // come from a trusted source (session here, but
        // could be database or something else)
        $attributes = array(
            'razorpay_order_id' => $_POST['razorpay_order_id'],
            'razorpay_payment_id' => $_POST['razorpay_payment_id'],
            'razorpay_signature' => $_POST['razorpay_signature']
        );

        $api->utility->verifyPaymentSignature($attributes);
        
        
    }
    catch(SignatureVerificationError $e)
    {
        $success = false;
        $error = 'Razorpay Error : ' . $e->getMessage();
    }
}
$payment_id = $_POST['razorpay_payment_id'];
if ($success === true)
{
 $email = $response['email'];
    $user_id = $_GET["userid"];
    $UserId = $_GET["userid"];
    $id = $_GET["id"];
    $deptid = $_GET["deptid"];
    $BatchId = $_GET['BatchId'];
    $query44 = "SELECT Fname,EmailId FROM tbl_users WHERE id = '$user_id'";
$row44 = getRecord($query44);
$Fname = $row44['Fname'];
$EmailId = $row44['EmailId'];

    $query = "SELECT Type,Duration,CourseFees,Name FROM tbl_courses WHERE id = '$id'";
$row = getRecord($query);
$CourseFees = $row['CourseFees'];
$Duration = $row['Duration'];
$CourseName = $row['Name'];
$Narration = "Puchasing ".$CourseName." Course";
$query2 = "SELECT StartDate,BatchTime,Strength FROM tbl_batches WHERE id = '$BatchId'";
$row2 = getRecord($query2);
$StartDate = $row2['StartDate'];
$BatchTime = $row2['BatchTime'];
   $InvNo = "#".rand(1000,9999);
    $CurrDate = date('Y-m-d');
    $OrderTime = date('h:i a');
    $Amount = $_GET['amount'];
    // $Amount = $response['payments'][0]['amount'];
    //  $payment_id = $response['payments'][0]['payment_id'];
    //   $payment_status = $response['payments'][0]['status'];
    //   $buyer_name = $response['payments'][0]['buyer_name'];
    //   $buyer_phone = $response['payments'][0]['buyer_phone'];
    //   $buyer_email = $response['payments'][0]['buyer_email'];
    //   $instrument_type = $response['payments'][0]['instrument_type'];
    //   $billing_instrument = $response['payments'][0]['billing_instrument'];
    //   $created_at = $response['payments'][0]['created_at'];  
      $sql3 = "SELECT * FROM tbl_payment_details WHERE payment_id='$payment_id'";
      $row = getRecord($sql3);
      $oldpayid = $row['payment_id'];
      if($oldpayid == $payment_id){}
        else{
      $sql = "INSERT INTO tbl_payment_details SET UserId='$UserId',PkgId='$PkgId',payment_id='$payment_id',payment_status='$payment_status',amount='$Amount',buyer_name='$buyer_name',buyer_phone='$buyer_phone',buyer_email='$buyer_email',instrument_type='$instrument_type',billing_instrument='$billing_instrument',created_at='$created_at'";
      $conn->query($sql);
    
      $sql2 = "UPDATE tbl_users SET CourseType='Paid',PackageStatus=1,PkgAmount='$Amount' WHERE id='$UserId'";
      $conn->query($sql2);
      
      $sql22 = "SELECT * FROM tbl_allocate_courses WHERE UserId='$UserId' AND DeptId='$deptid' AND CourseId='$id'";
$rncnt22 = getRow($sql22);
if($rncnt22 > 0){}
else{
$sql4 = "INSERT INTO tbl_allocate_courses SET UserId='$UserId',DeptId='$deptid',CourseId='$id',BatchId='$BatchId',StartDate='$StartDate',BatchTime='$BatchTime',Duration='$Duration',CourseType='Paid',CourseFees='$CourseFees',Status='1',Main=1,CreatedBy='$user_id',CreatedDate='$CreatedDate'";
$conn->query($sql4);
 $sql3 = "INSERT INTO tbl_invoices SET InvNo='$InvNo',UserId='$UserId',Amount='$Amount',Narration='$Narration',PaymentDate='$CreatedDate',PurchaseId='$id',Type=1";
 $conn->query($sql3);
}

$to = $EmailId;
include_once 'inccoursepurchase.php';
include_once '../sendmailsmtp.php';  
  }

?>
       <script type="text/javascript">
        setTimeout(function () { 
swal({
  title: "Thank you",
  text: "for Subscribing Course",
  type: "success",
  confirmButtonText: "OK"
},
function(isConfirm){
  if (isConfirm) {
          window.location.href="../dashboard.php";
  }
}); });</script>
<?php
    //mail
    //include("incmailcontent.php");
    //include("sendmailsmtp.php");  
}
else
{
    $html = "<p>Your payment failed</p>
             <p>{$error}</p>";
}

 ?>