odeme sayfam:
<?php
// Hata raporlamayı aç (Geliştirme aşamasında)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include '../includes/db.php';
include '../includes/header.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Kullanıcı giriş kontrolü
if (!isset($_SESSION['user_id'])) {
header('Location: /giris-yap.php?error=giris_gerekli');
exit;
}
// CSRF Koruması
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
die("Geçersiz CSRF token.");
}
unset($_SESSION['csrf_token']);
// POST verilerini al ve doğrula
$sunucu_id = intval($_POST['sunucu_id'] ?? 0);
$paket_id = intval($_POST['paket_id'] ?? 0);
$paket_turu = $_POST['paket_turu'] ?? '';
if ($sunucu_id <= 0 || $paket_id <= 0) {
die("Geçersiz sunucu veya paket ID.");
}
// Kullanıcı bilgilerini veritabanından çekme
$user_id = $_SESSION['user_id'];
$kullanici_stmt = $conn->prepare("SELECT * FROM kullanicilar WHERE id = :id");
$kullanici_stmt->bindParam(':id', $user_id, PDO::PARAM_INT);
$kullanici_stmt->execute();
$kullanici = $kullanici_stmt->fetch(PDO::FETCH_ASSOC);
if (!$kullanici) {
die("Kullanıcı bilgileri bulunamadı.");
}
// Paket bilgilerini çekme
$paket_stmt = $conn->prepare("SELECT paket_adi, fiyat, paket_turu FROM premium_paketleri WHERE id = :paket_id");
$paket_stmt->bindParam(':paket_id', $paket_id, PDO::PARAM_INT);
$paket_stmt->execute();
$paket = $paket_stmt->fetch(PDO::FETCH_ASSOC);
if (!$paket) {
die("Geçersiz paket.");
}
// Ödeme bilgileri
$tutar = floatval($paket['fiyat']) * 100; // Kuruş cinsinden
$merchant_oid = time() . rand(1000, 9999); // Benzersiz OID
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_basket = base64_encode(json_encode([[$paket['paket_adi'], $paket['fiyat'], 1]]));
// PayTR için hash hesaplama
$merchant_id = '519960';
$merchant_key = 'SU85kXeWnTHPtwMY';
$merchant_salt = 'YcEGLyjmcN6c3yED';
$hash_str = $merchant_id . $user_ip . $merchant_oid . $kullanici['eposta'] . $tutar . $user_basket . '0' . '1' . 'TL' . '0';
$paytr_token = base64_encode(hash_hmac('sha256', $hash_str . $merchant_salt, $merchant_key, true));
// Veritabanına ödeme kaydı ekleme
try {
$conn->beginTransaction();
$odeme_ekle = $conn->prepare("INSERT INTO odemeler (sunucu_id, kullanici_id, paket_id, tutar, odendi_mi, merchant_oid) VALUES (?, ?, ?, ?, 0, ?)");
$odeme_ekle->execute([$sunucu_id, $user_id, $paket_id, $tutar, $merchant_oid]);
$conn->commit();
} catch (PDOException $e) {
$conn->rollBack();
die("Ödeme işlemi başlatılamadı.");
}
// PayTR API isteği
$post_vals = [
'merchant_id' => $merchant_id,
'user_ip' => $user_ip,
'merchant_oid' => $merchant_oid,
'email' => $kullanici['eposta'],
'payment_amount' => $tutar,
'paytr_token' => $paytr_token,
'user_basket' => $user_basket,
'debug_on' => 1,
'no_installment' => 0,
'max_installment' => 1,
'user_name' => $kullanici['kullanici_adi'],
'user_address' => 'Adres girilmedi',
'user_phone' => $kullanici['telefon_numarasi'],
'merchant_ok_url' => "https://metin2serverlar.com.tr/odeme/odeme-basarili.php?sunucu_id={$sunucu_id}",
'merchant_fail_url' => "https://metin2serverlar.com.tr/odeme/odeme-basarisiz.php",
'timeout_limit' => 30,
'currency' => 'TL',
'test_mode' => 1
];
// cURL ile PayTR API isteği
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.paytr.com/odeme/api/get-token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vals);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
if (curl_errno($ch)) {
die("PAYTR IFRAME bağlantı hatası: " . curl_error($ch));
}
curl_close($ch);
$result = json_decode($result, true);
if ($result['status'] !== 'success') {
die("PAYTR IFRAME hatası: " . htmlspecialchars($result['reason'], ENT_QUOTES, 'UTF-8'));
}
$token = htmlspecialchars($result['token'], ENT_QUOTES, 'UTF-8');
?>
<section class="container my-5">
<h2>Ödeme Yap</h2>
<p class="lead">Ödeme işlemini tamamlamak için aşağıdaki formu kullanın.</p>
<script src="https://www.paytr.com/js/iframeResizer.min.js"></script>
<iframe src="https://www.paytr.com/odeme/guvenli/<?php echo $token; ?>" id="paytriframe" frameborder="0" scrolling="no" style="width: 100%;"></iframe>
<script>iFrameResize({}, '#paytriframe');</script>
</section>
<?php include '../includes/footer.php'; ?>CallBack.php sayfam: <?php
// Veritabanı bağlantısı ve oturum başlatma
include 'includes/db.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Log: Callback başladı
file_put_contents('callback_log.txt', "Callback başladı: " . json_encode($_POST) . "\n", FILE_APPEND);
// PayTR'den gelen verileri al
$post_data = $_POST;
// Eksik veri kontrolü
if (!isset($post_data['merchant_oid'], $post_data['status'], $post_data['hash'])) {
file_put_contents('callback_log.txt', "Eksik veri: " . json_encode($post_data) . "\n", FILE_APPEND);
die("Eksik veri.");
}
// PayTR doğrulama bilgileri
$merchant_key = 'SU85kXeWnTHPtwMY';
$merchant_salt = 'YcEGLyjmcN6c3yED';
// Hash doğrulama
$hash = base64_encode(hash_hmac(
'sha256',
$post_data['merchant_oid'] . $merchant_salt . $post_data['status'],
$merchant_key,
true
));
if (!hash_equals($hash, $post_data['hash'])) {
file_put_contents('callback_log.txt', "Hash doğrulama hatası: Beklenen: $hash, Gelen: " . $post_data['hash'] . "\n", FILE_APPEND);
die('PAYTR hash doğrulama hatası.');
}
// Ödeme detaylarını al
$merchant_oid = $post_data['merchant_oid'];
$status = $post_data['status'];
// Ödeme kaydını merchant_oid ile sorgula
$stmt = $conn->prepare("SELECT * FROM merchant_oid_tablosu WHERE merchant_oid = :merchant_oid");
$stmt->bindParam(':merchant_oid', $merchant_oid, PDO::PARAM_STR);
$stmt->execute();
$merchant_record = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$merchant_record) {
file_put_contents('callback_log.txt', "Geçersiz merchant_oid: $merchant_oid\n", FILE_APPEND);
die("Geçersiz merchant_oid.");
}
$odeme_id = $merchant_record['odeme_id'];
// Ödeme kaydını al
$odeme_detayi = $conn->prepare("SELECT * FROM odemeler WHERE id = :odeme_id");
$odeme_detayi->bindParam(':odeme_id', $odeme_id, PDO::PARAM_INT);
$odeme_detayi->execute();
$odeme = $odeme_detayi->fetch(PDO::FETCH_ASSOC);
if (!$odeme) {
file_put_contents('callback_log.txt', "Ödeme kaydı bulunamadı: ID $odeme_id\n", FILE_APPEND);
die("Ödeme kaydı bulunamadı.");
}
// Ödeme başarılıysa, veritabanına kaydedin
if ($status === 'success') {
try {
// Ödeme kaydını güncellemeden önce, odendi_mi değerini 0 olarak kaydedin
$guncelle_odeme = $conn->prepare("UPDATE odemeler SET odendi_mi = 0 WHERE id = :odeme_id");
$guncelle_odeme->bindParam(':odeme_id', $odeme_id, PDO::PARAM_INT);
$guncelle_odeme->execute();
// Kullanıcıyı odeme-basarili.php sayfasına yönlendir
header("Location: /odeme/odeme-basarili.php?sunucu_id=" . $odeme['sunucu_id']);
exit;
} catch (PDOException $e) {
file_put_contents('callback_log.txt', "Veritabanı hatası: " . $e->getMessage() . "\n", FILE_APPEND);
die("Ödeme işlemi başlatılamadı.");
}
} else {
file_put_contents('callback_log.txt', "Başarısız ödeme: OID $merchant_oid\n", FILE_APPEND);
die("Başarısız ödeme.");
}
?>
