fix
This commit is contained in:
104
process_payment.php
Normal file
104
process_payment.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Process Payment
|
||||
*
|
||||
* Riceve la conferma del pagamento da PayPal e registra l'acquisto nel database
|
||||
*/
|
||||
|
||||
require_once 'includes/config.php';
|
||||
require_once 'includes/functions.php';
|
||||
|
||||
session_start();
|
||||
|
||||
// Verifica che l'utente sia loggato
|
||||
if (!is_logged_in()) {
|
||||
echo json_encode(['success' => false, 'error' => 'Not logged in']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Leggi i dati JSON
|
||||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (!$data) {
|
||||
echo json_encode(['success' => false, 'error' => 'Invalid data']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$order_id = $data['orderID'] ?? '';
|
||||
$lesson_id = $data['lessonID'] ?? 0;
|
||||
$payer_id = $data['payerID'] ?? '';
|
||||
$amount = $data['amount'] ?? 0;
|
||||
|
||||
$user_id = $_SESSION['user_id'];
|
||||
|
||||
// Validazione
|
||||
if (empty($order_id) || !$lesson_id || !$amount) {
|
||||
echo json_encode(['success' => false, 'error' => 'Missing required fields']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verifica che la lezione esista
|
||||
$lesson = get_lesson_by_id($lesson_id);
|
||||
if (!$lesson) {
|
||||
echo json_encode(['success' => false, 'error' => 'Lesson not found']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verifica che l'utente non abbia già acquistato questa lezione
|
||||
if (user_owns_lesson($user_id, $lesson_id)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Already purchased']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = get_db_connection();
|
||||
|
||||
// Registra l'acquisto nel database
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO purchases (
|
||||
user_id, lesson_id, amount, currency, payment_method,
|
||||
paypal_order_id, paypal_payer_id, paypal_status,
|
||||
status, purchased_at, created_at
|
||||
) VALUES (
|
||||
?, ?, ?, 'EUR', 'paypal',
|
||||
?, ?, 'completed',
|
||||
'completed', NOW(), NOW()
|
||||
)
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
$user_id,
|
||||
$lesson_id,
|
||||
$amount,
|
||||
$order_id,
|
||||
$payer_id
|
||||
]);
|
||||
|
||||
// Aggiorna il contatore acquisti della lezione
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE lessons
|
||||
SET purchase_count = purchase_count + 1
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([$lesson_id]);
|
||||
|
||||
// Log attività
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO activity_log (user_id, action, description, ip_address, user_agent, created_at)
|
||||
VALUES (?, 'purchase', ?, ?, ?, NOW())
|
||||
");
|
||||
$stmt->execute([
|
||||
$user_id,
|
||||
"Acquisto lezione: {$lesson['title']}",
|
||||
$_SERVER['REMOTE_ADDR'] ?? null,
|
||||
$_SERVER['HTTP_USER_AGENT'] ?? null
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Payment processing error: " . $e->getMessage());
|
||||
echo json_encode(['success' => false, 'error' => 'Database error']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user