diff --git a/subscription.php b/subscription.php index 26b1d881..c82a46d6 100644 --- a/subscription.php +++ b/subscription.php @@ -16,15 +16,777 @@ include('config.php'); require 'vendor/autoload.php'; +// Binance payment function +function createBinancePayment($amount, $currency) { + $apiKey = getenv('oerorywnqozkuillondw6i3agatww7ohql5tqkoiozhjra9fdzxui6xqvssbqgcl'); + $apiSecret = getenv('anadyqw1l3u4abjd3lu6xkpqf88pd5ik0hnxhrlnrnxgpn8rhjgbvqtk8yrrqaqi'); + + $url = "https://api.binance.com/binancepay/api/v3/order"; + $data = [ + "amount" => $amount, + "currency" => $currency, + // Additional payment data can be added here + ]; + + // Generate API signature + $signature = hash_hmac('sha256', json_encode($data), $apiSecret); + + // Headers for the API request + $headers = [ + "Content-Type: application/json", + "Binance-Api-Key: $apiKey", + "Binance-Api-Signature: $signature" + ]; + + // Setting up HTTP context for the API call + $options = [ + "http" => [ + "header" => $headers, + "method" => "POST", + "content" => json_encode($data) + ] + ]; + + $context = stream_context_create($options); + $result = file_get_contents($url, false, $context); + + // Return the response from Binance API + return json_decode($result, true); +} + +// Handle POST request for payment +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $amount = $_POST['amount']; + $currency = $_POST['currency']; + $response = createBinancePayment($amount, $currency); + echo json_encode($response); +} + +try { + // Establish database connection + $connection = new PDO($dsn, $username, $password, $options); + + if ($_SERVER["REQUEST_METHOD"] === "POST") { + // Process payment form submission + $amount = $_POST['amount'] ?? 0; + $method = $_POST['method'] ?? 'Cash'; // Default to 'Cash' if not provided + $status = 'Pending'; // Default status for the payment + + // Prepare SQL query to insert payment record + $stmt = $connection->prepare("INSERT INTO payments (amount, method, status) VALUES (?, ?, ?)"); + $stmt->execute([$amount, $method, $status]); + + $paymentId = $connection->lastInsertId(); // Get the last inserted payment ID + } + + // Fetch inventory notifications with product images + $inventoryQuery = $connection->prepare(" + SELECT i.product_name, i.available_stock, i.inventory_qty, i.sales_qty, p.image_path + FROM inventory i + JOIN products p ON i.product_id = p.id + WHERE i.available_stock < :low_stock OR i.available_stock > :high_stock + ORDER BY i.last_updated DESC + "); + $inventoryQuery->execute([ + ':low_stock' => 10, + ':high_stock' => 1000, + ]); + $inventoryNotifications = $inventoryQuery->fetchAll(PDO::FETCH_ASSOC); + + // Fetch reports notifications with product images + $reportsQuery = $connection->prepare(" + SELECT JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.product_name')) AS product_name, + JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.revenue')) AS revenue, + p.image_path + FROM reports r + JOIN products p ON JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.product_id')) = p.id + WHERE JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.revenue')) > :high_revenue + OR JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.revenue')) < :low_revenue + ORDER BY r.report_date DESC + "); + $reportsQuery->execute([ + ':high_revenue' => 10000, + ':low_revenue' => 1000, + ]); + $reportsNotifications = $reportsQuery->fetchAll(PDO::FETCH_ASSOC); + +} catch (PDOException $e) { + // Handle database connection errors + echo "Database connection failed: " . $e->getMessage(); + exit; +} + + +?> + + + + + + + Subscriptions + + + + + + + + + + + + + +
+ +
+
+ +
+
+ +
+

Renew Your Subscription

+
+
+ + +
+ +
+ + +
+ + +
+ + +
+ + + + + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + 86400, + 'cookie_secure' => true, + 'cookie_httponly' => true, + 'use_strict_mode' => true, + 'sid_length' => 48, +]); + // Check if user is logged in if (!isset($_SESSION["username"])) { - header("Location: loginpage.php"); + // Redirect to login page if session is not set + header("Location: login.php"); exit; } +// Fetch the logged-in user's information $username = htmlspecialchars($_SESSION["username"]); -// Fetch the logged-in user's information $user_query = "SELECT username, email, date, phone, location, user_image FROM users WHERE username = :username"; $stmt = $connection->prepare($user_query); $stmt->bindParam(':username', $username); @@ -41,6 +803,11 @@ $existing_image = htmlspecialchars($user_info['user_image']); $image_to_display = !empty($existing_image) ? $existing_image : 'uploads/user/default.png'; + +// Include database connection and autoload files +include('config.php'); // Includes database connection +require 'vendor/autoload.php'; + // Binance payment function function createBinancePayment($amount, $currency) { $apiKey = getenv('oerorywnqozkuillondw6i3agatww7ohql5tqkoiozhjra9fdzxui6xqvssbqgcl'); @@ -50,16 +817,20 @@ function createBinancePayment($amount, $currency) { $data = [ "amount" => $amount, "currency" => $currency, + // Additional payment data can be added here ]; + // Generate API signature $signature = hash_hmac('sha256', json_encode($data), $apiSecret); + // Headers for the API request $headers = [ "Content-Type: application/json", "Binance-Api-Key: $apiKey", "Binance-Api-Signature: $signature" ]; + // Setting up HTTP context for the API call $options = [ "http" => [ "header" => $headers, @@ -71,6 +842,7 @@ function createBinancePayment($amount, $currency) { $context = stream_context_create($options); $result = file_get_contents($url, false, $context); + // Return the response from Binance API return json_decode($result, true); } @@ -83,15 +855,20 @@ function createBinancePayment($amount, $currency) { } try { - // Process payment form submission + // Establish database connection + $connection = new PDO($dsn, $username, $password, $options); + if ($_SERVER["REQUEST_METHOD"] === "POST") { + // Process payment form submission $amount = $_POST['amount'] ?? 0; - $method = $_POST['method'] ?? 'Cash'; - $status = 'Pending'; + $method = $_POST['method'] ?? 'Cash'; // Default to 'Cash' if not provided + $status = 'Pending'; // Default status for the payment + // Prepare SQL query to insert payment record $stmt = $connection->prepare("INSERT INTO payments (amount, method, status) VALUES (?, ?, ?)"); $stmt->execute([$amount, $method, $status]); - $paymentId = $connection->lastInsertId(); + + $paymentId = $connection->lastInsertId(); // Get the last inserted payment ID } // Fetch inventory notifications with product images @@ -102,7 +879,10 @@ function createBinancePayment($amount, $currency) { WHERE i.available_stock < :low_stock OR i.available_stock > :high_stock ORDER BY i.last_updated DESC "); - $inventoryQuery->execute([':low_stock' => 10, ':high_stock' => 1000]); + $inventoryQuery->execute([ + ':low_stock' => 10, + ':high_stock' => 1000, + ]); $inventoryNotifications = $inventoryQuery->fetchAll(PDO::FETCH_ASSOC); // Fetch reports notifications with product images @@ -116,13 +896,19 @@ function createBinancePayment($amount, $currency) { OR JSON_UNQUOTE(JSON_EXTRACT(revenue_by_product, '$.revenue')) < :low_revenue ORDER BY r.report_date DESC "); - $reportsQuery->execute([':high_revenue' => 10000, ':low_revenue' => 1000]); + $reportsQuery->execute([ + ':high_revenue' => 10000, + ':low_revenue' => 1000, + ]); $reportsNotifications = $reportsQuery->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { + // Handle database connection errors echo "Database connection failed: " . $e->getMessage(); exit; } + + ?> @@ -651,25 +1437,28 @@ class="rounded-top img-fluid mb-4"> const paypalButtonContainer = document.getElementById('paypal-button-container'); const binancePayButton = document.getElementById('binancePayButton'); - // Display plan selection by default - planSelection.style.display = "block"; - - // Set initial visibility for payment buttons - paypalButtonContainer.style.display = "none"; - binancePayButton.style.display = "block"; // Display Binance Pay button by default - // Toggle payment options based on selected method methodSelect.addEventListener("change", function() { - const isPayPal = methodSelect.value === "PayPal"; - paypalButtonContainer.style.display = isPayPal ? "block" : "none"; - binancePayButton.style.display = isPayPal ? "none" : "block"; + planSelection.style.display = "block"; // Always display plan selection for both methods + + if (methodSelect.value === "PayPal") { + paypalButtonContainer.style.display = "block"; + binancePayButton.style.display = "none"; + } else if (methodSelect.value === "BinancePay") { + paypalButtonContainer.style.display = "none"; + binancePayButton.style.display = "block"; + } }); + // Initial state + paypalButtonContainer.style.display = "none"; + binancePayButton.style.display = "none"; + // Initialize PayPal subscription button paypal.Buttons({ style: { shape: 'pill', - color: 'gold', + color: 'silver', layout: 'horizontal', label: 'subscribe' }, @@ -679,7 +1468,7 @@ class="rounded-top img-fluid mb-4"> plan_id: selectedPlan }); }, - onApprove: function(data) { + onApprove: function(data, actions) { alert("Subscription successful! Your subscription ID is: " + data.subscriptionID); } }).render('#paypal-button-container');