prepare($sql); $stmt->execute([$payment_return]); if (debug) { debuglog("Payment cancelled - Order ID: {$payment_return} marked as cancelled (999)"); } } catch (Exception $e) { if (debug) { debuglog("Error marking order as cancelled: " . $e->getMessage()); } } } // Handle PayPal return - capture the order directly if ($paypal_token && $payment_return) { try { // Get PayPal access token $ch = curl_init(PAYPAL_URL . '/v1/oauth2/token'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials'); curl_setopt($ch, CURLOPT_USERPWD, PAYPAL_CLIENT_ID . ':' . PAYPAL_CLIENT_SECRET); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); $response = curl_exec($ch); curl_close($ch); $token_data = json_decode($response, true); $access_token = $token_data['access_token'] ?? ''; if ($access_token) { // Capture the PayPal order $capture_url = PAYPAL_URL . "/v2/checkout/orders/{$paypal_token}/capture"; $ch = curl_init($capture_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $access_token ]); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if (debug) { debuglog("PayPal Capture: HTTP $http_code - $response"); } // Update transaction status based on capture result if ($http_code == 200 || $http_code == 201) { $capture_result = json_decode($response, true); $capture_status = $capture_result['status'] ?? ''; $payment_status = null; if ($capture_status === 'COMPLETED') { $payment_status = 1; // Paid } elseif ($capture_status === 'PENDING') { $payment_status = 101; // Pending } if ($payment_status !== null) { $pdo = dbConnect($dbname); $sql = 'UPDATE transactions SET payment_status = ? WHERE txn_id = ?'; $stmt = $pdo->prepare($sql); $stmt->execute([$payment_status, $payment_return]); } } } // Redirect to clean URL header("Location: ?page=softwaretool&payment_return=1&order_id={$payment_return}"); exit; } catch (Exception $e) { if (debug) { debuglog("PayPal Capture Error: " . $e->getMessage()); } } } template_header('Softwaretool', 'softwaretool','view'); // Show payment return message if returning from payment $view = ''; $payment_modal = ''; if ($payment_return && $payment_return_status) { // Check actual payment status in database $pdo = dbConnect($dbname); $sql = 'SELECT payment_status FROM transactions WHERE txn_id = ?'; $stmt = $pdo->prepare($sql); $stmt->execute([$payment_return]); $transaction = $stmt->fetch(PDO::FETCH_ASSOC); if ($transaction) { if ($transaction['payment_status'] == 1) { // Payment confirmed as paid $payment_modal = ' '; } else if ($transaction['payment_status'] == 0 || $transaction['payment_status'] == 101) { // Payment pending $payment_modal = ' '; } else if ($transaction['payment_status'] == 999) { // Payment cancelled $payment_modal = ' '; } else { // Payment failed/expired $payment_modal = ' '; } } } $view .= '

'.$softwaretool_h2 .'

'.$softwaretool_p.'

'; //SHOW BACK BUTTON ONLY FOR PORTAL USERS if (isAllowed($page ,$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'R') != 0){ $view .= '
'; } $view .= '
'; $view .= '
'; $view .= ' '; $view .= ''; //OUTPUT echo $view; // Output payment modal if exists echo $payment_modal; echo ' '; template_footer(); ?>