Repository navigation
Expand file tree
/
Copy pathprocess_payment.php
More file actions
81 lines (66 loc) · 2.79 KB
/
Copy pathprocess_payment.php
File metadata and controls
81 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once 'includes/session_config.php';
include 'conn.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'message' => 'Not logged in']);
exit();
}
// Check if user is a client
if ($_SESSION['user_type'] !== 'client') {
echo json_encode(['success' => false, 'message' => 'Only clients can release payments']);
exit();
}
// Get data from POST request
$project_id = isset($_POST['project_id']) ? intval($_POST['project_id']) : 0;
$amount = isset($_POST['amount']) ? floatval($_POST['amount']) : 0;
if ($project_id <= 0 || $amount <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid project ID or amount']);
exit();
}
// Verify that the client owns this project
$verify_sql = "SELECT * FROM projects WHERE id = ? AND user_id = ?";
$verify_stmt = $conn->prepare($verify_sql);
$verify_stmt->bind_param("ii", $project_id, $_SESSION['user_id']);
$verify_stmt->execute();
$verify_result = $verify_stmt->get_result();
if ($verify_result->num_rows === 0) {
echo json_encode(['success' => false, 'message' => 'You do not have permission to release payment for this project']);
exit();
}
// Get the freelancer ID for this project
$freelancer_sql = "SELECT freelancer_id FROM project_assignments WHERE project_id = ? AND is_accepted = 1";
$freelancer_stmt = $conn->prepare($freelancer_sql);
$freelancer_stmt->bind_param("i", $project_id);
$freelancer_stmt->execute();
$freelancer_result = $freelancer_stmt->get_result();
if ($freelancer_result->num_rows === 0) {
echo json_encode(['success' => false, 'message' => 'No freelancer assigned to this project']);
exit();
}
$freelancer_data = $freelancer_result->fetch_assoc();
$freelancer_id = $freelancer_data['freelancer_id'];
// Begin transaction
$conn->begin_transaction();
try {
// Record the payment in the project_payments table
$payment_sql = "INSERT INTO project_payments (project_id, client_id, freelancer_id, amount, payment_date)
VALUES (?, ?, ?, ?, NOW())";
$payment_stmt = $conn->prepare($payment_sql);
$payment_stmt->bind_param("iiid", $project_id, $_SESSION['user_id'], $freelancer_id, $amount);
$payment_stmt->execute();
// Update the payment_released column in the projects table to 'yes'
$update_sql = "UPDATE projects SET payment_released = 'yes' WHERE id = ?";
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param("i", $project_id);
$update_stmt->execute();
// Commit the transaction
$conn->commit();
echo json_encode(['success' => true]);
} catch (Exception $e) {
// Rollback the transaction in case of error
$conn->rollback();
echo json_encode(['success' => false, 'message' => 'Error processing payment: ' . $e->getMessage()]);
}
$conn->close();
?>