JezK
Edit File: payment.php
<?php include "header.php"; include 'App/checkSession.php'; if (!isset($_SESSION['start_payment'])) { header('location: ' . $baseUrl . 'home.php'); } ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Include the Razorpay PHP library // require_once('../razorpayGateway/razorpay-php-master/Razorpay.php'); use Razorpay\Api\Api; // Razorpay API credentials $keyId = 'rzp_test_kpM12J0EwK1zNw'; // sushmitha test credentials $keySecret = 'O5A6dpKCfuYMJyNyw63GR6lW'; // Initialize the Razorpay API $api = new Api($keyId, $keySecret); // Generate Razorpay order $orderData = [ 'receipt' => '3456', 'amount' => $_SESSION['amount'] * 100, 'currency' => 'INR', 'payment_capture' => 1 // Auto capture payment ]; $razorpayOrder = $api->order->create($orderData); $razorpayOrderId = $razorpayOrder['id']; $_SESSION['razorpay_order_id'] = $razorpayOrderId; $userId = $_SESSION['id']; // Fetch user details $query = "SELECT phone,email,address FROM user WHERE id = $userId"; require_once dirname(__FILE__) . '/App/config.php'; $db = new DbConnect(); $conn = $db->connect(); $stmt = $conn->prepare($query); $stmt->execute(); $userDetails = $stmt->fetch(PDO::FETCH_ASSOC); $phone = $userDetails['phone']; $email = $userDetails['email']; $address = $userDetails['address']; // Redirect URL after payment $successUrl = $baseUrl . 'verify.php'; $cancelUrl = $baseUrl . 'cancle.php'; // Prepare JSON data for Razorpay checkout $data = [ "key" => $keyId, "amount" => $orderData['amount'], "name" => $_SESSION['user_name'], "description" => "User cart checkout", "order_id" => $razorpayOrderId, "prefill" => [ "name" => $_SESSION['user_name'], "email" => $email, "contact" => $phone, ], "notes" => [ "address" => $address, "merchant_order_id" => "12312321", "user_id" => $userId, ], "theme" => [ "color" => "#FFFFF" ], "handler" => "function (response) { document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id; document.getElementById('razorpay_signature').value = response.razorpay_signature; document.getElementById('razorpay_payment_status').value = response.status; document.getElementById('razorpayform').submit(); }", "modal" => [ "ondismiss" => "", "oncancel" => "window.location.href='$cancelUrl';" ] ]; // Encode data as JSON $json = json_encode($data); ?> <!-- HTML form for Razorpay checkout --> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <form name='razorpayform' action="<?= $successUrl ?>" method="POST"> <input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id"> <input type="hidden" name="razorpay_signature" id="razorpay_signature"> <input type="hidden" name="razorpay_payment_status" id="razorpay_payment_status"> </form> <script> // Checkout details as a json var options = <?= $json ?>; /** * The entire list of Checkout fields is available at * https://docs.razorpay.com/docs/checkout-form#checkout-fields */ options.handler = function(response) { document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id; document.getElementById('razorpay_signature').value = response.razorpay_signature; document.razorpayform.submit(); }; // Boolean whether to show image inside a white frame. (default: true) options.theme.image_padding = false; options.modal = { ondismiss: function() { console.log("This code runs when the popup is closed"); window.location.href = 'cart.php'; }, // Boolean indicating whether pressing escape key // should close the checkout form. (default: true) escape: true, // Boolean indicating whether clicking translucent blank // space outside checkout form should close the form. (default: false) backdropclose: false }; var rzp = new Razorpay(options); rzp.on('payment.failed', function(response) { // window.location.href = '<?= $cancelUrl ?>'; }); rzp.open(); </script> <?php unset($_SESSION['start_payment']);