View file paypal/update_golden_plan.php

File size: 3.29Kb
<?php
include "../connection/connection.php";

// Verificar si el usuario está autenticado
if (!isset($_SESSION['correo_electronico'])) {
    die("<div class='alert alert-danger text-center'>Acceso denegado. Por favor, inicia sesión.</div>");
}

// Verificar si se recibió el ID de la orden
if (!isset($_GET['orderID']) || empty($_GET['orderID'])) {
    die("<div class='alert alert-danger text-center'>Falta el ID de la orden.</div>");
}

$orderID = $_GET['orderID'];
$correo = $_SESSION['correo_electronico'];


$apiURL = "https://api-m.sandbox.paypal.com/v2/checkout/orders/$orderID";

// Autenticación con PayPal
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "$clientID:$secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
$tokenData = json_decode($response, true);
if (!isset($tokenData['access_token'])) {
    die("<div class='alert alert-danger text-center'>Error al autenticar con PayPal.</div>");
}

$accessToken = $tokenData['access_token'];

// Validar el orderID con PayPal
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Bearer $accessToken"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
$orderData = json_decode($response, true);

// Verificar si la transacción es válida
if (!isset($orderData['status']) || $orderData['status'] !== "COMPLETED") {
    die("<div class='alert alert-danger text-center'>La transacción no es válida o no está completada.</div>");
}

// Verificar si la transacción ya fue procesada
$stmt = $conn->prepare("SELECT id FROM transacciones WHERE order_id = ?");
$stmt->bind_param("s", $orderID);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows > 0) {
    die("<div class='alert alert-warning text-center'>Esta transacción ya fue procesada.</div>");
}
$stmt->close();

// Verificar que el correo del comprador coincida
$payerEmail = $orderData['payer']['email_address'] ?? null;
if ($correo !== $payerEmail) {
    die("<div class='alert alert-danger text-center'>El correo no coincide con el comprador de la transacción.</div>");
}

// Registrar la transacción y actualizar el plan
$conn->begin_transaction();

try {
    // Registrar la transacción
    $stmt = $conn->prepare("INSERT INTO transacciones (order_id, correo_electronico, status) VALUES (?, ?, ?)");
    $status = "COMPLETED";
    $stmt->bind_param("sss", $orderID, $correo, $status);
    $stmt->execute();
    $stmt->close();

    // Actualizar el plan del usuario
    $stmt = $conn->prepare("UPDATE usuarios SET plan = 'golden', enlaces_restantes = 1500 WHERE correo_electronico = ?");
    $stmt->bind_param("s", $correo);
    $stmt->execute();
    $stmt->close();

    $conn->commit();
    header("Location: ../dashboard.php");
    exit();
} catch (Exception $e) {
    $conn->rollback();
    die("<div class='alert alert-danger text-center'>Error al procesar la transacción: " . $e->getMessage() . "</div>");
}

$conn->close();
?>