Hallo
Ich habe einen ähnlichen Code, der aber lediglich eine IPN-Rückmeldung von PayPal in einer Datenbank speichert und zeitlich begrenzten Zugriff auf ein Verzeichnis gewährt.
https://forums.mobirise.com/discussion/ ... ent_122108
Code: Alles auswählen
<?php
// Ce script reçoit les notifications IPN de PayPal
// et met à jour la base de données si le paiement est validé
require_once "config.php";
// Lire les données brutes de PayPal
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode('=', $keyval);
if (count($keyval) == 2) {
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
}
// Ajouter 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
foreach ($myPost as $key => $value) {
$value = urlencode($value);
$req .= "&$key=$value";
}
// Poster la requête de validation à PayPal
$ch = curl_init('https://ipnpb.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cert/cacert.pem');
$res = curl_exec($ch);
curl_close($ch);
if (strcmp($res, "VERIFIED") == 0) {
// Paiement validé par PayPal
$payer_email = $_POST['payer_email'];
$custom = $_POST['custom']; // Contient l'id de l'utilisateur
$payment_status = $_POST['payment_status'];
if ($payment_status === 'Completed') {
$stmt = $conn->prepare("UPDATE pdfsecure SET has_paid = 1 WHERE id = ?");
$stmt->bind_param("i", $custom);
$stmt->execute();
}
} else {
// Erreur ou tentative frauduleuse
file_put_contents("ipn_error.log", date("Y-m-d H:i:s") . " : Échec IPN
", FILE_APPEND);
}
?>
Code: Alles auswählen
<?php
session_start();
require_once "config.php";
if (!isset($_SESSION["user_id"])) {
die("Accès refusé.");
}
$id = $_SESSION["user_id"];
$stmt = $conn->prepare("SELECT has_paid, date_achat FROM pdfsecure WHERE id = ?");
$stmt->bind_param("i", $_SESSION["user_id"]);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if (!$user["has_paid"]) {
die("Vous n'avez pas accès au téléchargement.");
}
// Calcul expiration
$date_achat = strtotime($user["date_achat"]);
$now = time();
$diff = $now - $date_achat;
if ($diff > DOWNLOAD_VALIDITY_DURATION) {
// Optionnel : désactive l'accès
$stmt = $conn->prepare("UPDATE pdfsecure SET has_paid = 0 WHERE id = ?");
$stmt->bind_param("i", $_SESSION["user_id"]);
$stmt->execute();
$date_expiration = date("d/m/Y H:i", strtotime($user["date_achat"]) + DOWNLOAD_VALIDITY_DURATION);
die("Your link expired on $date_expiration. Contact support");
}
$allowed_files = ['ebook.pdf', 'ebook2023.pdf', 'ebook2024.pdf'];
$file = basename($_GET['file']);
if (!in_array($file, $allowed_files)) {
die("Unauthorized file.");
}
$path = __DIR__ . '/fichiers/' . $file;
if (!file_exists($path)) {
die("File not found.");
}
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile($path);
exit;
Dies ist ein Add-on, das ich gerade für mein Admin-Panel (digitaler Einkauf) fertigstelle.