[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: process.php
<?php include("functions.php"); if (!isset($_SESSION['user_email'])) { header("location:index"); } else { // Database connection (replace with your PDO credentials) try { $db = new PDO('mysql:host=localhost;dbname=ticking', 'root', ''); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die(json_encode(['error' => 'Database connection failed: ' . $e->getMessage()])); } // Paystack secret key (replace with your live secret key for production) $secret_key = 'sk_test_30b390f2e210416e7c0efde4f3f369b195c2b1da'; // Replace with your Paystack secret key // Get the transaction reference from the URL $reference = isset($_GET['reference']) ? $_GET['reference'] : null; if (!$reference) { die(json_encode(['error' => 'No transaction reference provided'])); } // Initialize cURL to verify the transaction $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . urlencode($reference), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer $secret_key", "Content-Type: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { die(json_encode(['error' => 'cURL error: ' . $err])); } $result = json_decode($response, true); if (!$result || !isset($result['status']) || !$result['status']) { die(json_encode(['error' => 'Verification failed: ' . ($result['message'] ?? 'Unknown error')])); } // Check transaction status and amount $transaction_status = $result['data']['status']; $transaction_amount = $result['data']['amount'] / 100; // Convert pesewas to GHS // Check if transaction has already been processed function hasTransactionBeenProcessed($db, $reference) { try { $stmt = $db->prepare('SELECT COUNT(*) FROM tickets WHERE transaction_reference = :reference'); $stmt->bindValue(':reference', $reference, PDO::PARAM_STR); $stmt->execute(); $count = $stmt->fetchColumn(); return $count > 0; } catch (PDOException $e) { die(json_encode(['error' => 'Database error in hasTransactionBeenProcessed: ' . $e->getMessage()])); } } if (hasTransactionBeenProcessed($db, $reference)) { die(json_encode(['error' => 'Transaction has already been processed'])); } // Generate unique ticket code function generateUniqueTicketCode($db) { $prefix = 'TCK-'; $length = 6; $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $max_attempts = 10; for ($attempt = 1; $attempt <= $max_attempts; $attempt++) { $random = ''; for ($i = 0; $i < $length; $i++) { $random .= $characters[rand(0, strlen($characters) - 1)]; } $ticket_code = $prefix . $random; try { $stmt = $db->prepare('SELECT COUNT(*) FROM tickets WHERE ticket_code = :ticket_code'); $stmt->bindValue(':ticket_code', $ticket_code, PDO::PARAM_STR); $stmt->execute(); $count = $stmt->fetchColumn(); if ($count == 0) { return $ticket_code; } } catch (PDOException $e) { die(json_encode(['error' => 'Database error in generateUniqueTicketCode: ' . $e->getMessage()])); } } // Fallback unique code return 'TCK-' . substr(strtoupper(md5(uniqid(rand(), true))), 0, 6); } if ($transaction_status === 'success') { // Determine number of tickets based on amount $tickets_count = 0; switch ($transaction_amount) { case 20: $tickets_count = 1; break; case 50: $tickets_count = 3; break; case 80: $tickets_count = 5; break; case 150: $tickets_count = 10; break; default: die(json_encode(['error' => 'Invalid amount: ' . $transaction_amount . ' GHS'])); } // Generate and store ticket codes $ticket_codes = []; $email = $result['data']['customer']['email'] ?? 'afoakwastephen80@gmail.com'; // Use email from Paystack or fallback // Begin database transaction to ensure atomicity try { $db->beginTransaction(); $ticketused = 0; for ($i = 0; $i < $tickets_count; $i++) { $ticket_code = generateUniqueTicketCode($db); // Insert ticket into database $stmt = $db->prepare('INSERT INTO tickets (ticket_code, transaction_reference, amount, email, status,ticketvalue,ticketused) VALUES (:ticket_code, :reference, :amount, :email, :status,:tickets_count,:ticketused)'); $stmt->bindValue(':ticket_code', $ticket_code, PDO::PARAM_STR); $stmt->bindValue(':reference', $reference, PDO::PARAM_STR); $stmt->bindValue(':amount', $transaction_amount, PDO::PARAM_STR); $stmt->bindValue(':email', $email, PDO::PARAM_STR); $stmt->bindValue(':status', 'active', PDO::PARAM_STR); $stmt->bindValue(':ticketvalue', $tickets_count, PDO::PARAM_STR); $stmt->bindValue(':ticketused', $ticketused, PDO::PARAM_STR); $stmt->execute(); $ticket_codes[] = $ticket_code; } // Commit transaction $db->commit(); } catch (PDOException $e) { // Rollback on error $db->rollback(); die(json_encode(['error' => 'Failed to generate tickets: ' . $e->getMessage()])); } // Output success response echo json_encode([ 'status' => 'success', 'message' => 'Payment verified and tickets generated successfully', 'reference' => $reference, 'amount' => $transaction_amount, 'email' => $email, 'tickets' => $ticket_codes ]); } else { echo json_encode([ 'error' => 'Transaction failed or was abandoned', 'status' => $transaction_status ]); } } ?>
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.8 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002