<!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 
session_start();
require_once '../config.php'; 
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;
$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)
{
	$userid = $_GET['userid'];
  $pdfid = $_GET['pdfid'];
 $CreatedDate = date('Y-m-d');
 $Amount = $_GET['amount'];
 $query55 = "SELECT Title,Files FROM tbl_pdf_sell WHERE id = '$pdfid'";
$row55 = getRecord($query55);
$PdfName = $row55['Title'];
$Files = $row55['Files'];
$Narration = "Puchasing PDF ".$PdfName;

  $query44 = "SELECT Fname,EmailId FROM tbl_users WHERE id = '$userid'";
$row44 = getRecord($query44);
$Fname = $row44['Fname'];
$EmailId = $row44['EmailId'];
$InvNo = "#".rand(1000,9999);
  $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);
 $sql = "INSERT INTO tbl_buy_pdf_details SET UserId='$userid',PdfId='$pdfid',CreatedDate='$CreatedDate'";
 $conn->query($sql);
 $sql3 = "INSERT INTO tbl_invoices SET InvNo='$InvNo',UserId='$userid',Amount='$Amount',Narration='$Narration',PaymentDate='$CreatedDate',PurchaseId='$pdfid',Type=2";
 $conn->query($sql3);
 $to = $EmailId;
include_once 'incpdfpurchase.php';
include_once '../sendmailsmtp.php';  
  }
     ?>
       <script type="text/javascript">
        setTimeout(function () { 
swal({
  title: "Thank you.",
  text: " For Buying Pdf.",
  type: "success",
  confirmButtonText: "ok"
},
function(isConfirm){
  if (isConfirm) {
      //window.location.href="../uploads/<?php echo $Files;?>";
   window.location.href="../buy-pdfs.php";
    
  }
}); });</script>
<?php
    //mail
    //include("incmailcontent.php");
    //include("sendmailsmtp.php");  
}
else
{
    $html = "<p>Your payment failed</p>
             <p>{$error}</p>";
}

 ?>