T.ME/BIBIL_0DAY
CasperSecurity


Server : Apache/2
System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User : gositeme ( 1004)
PHP Version : 8.2.29
Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Directory :  /home/gositeme/domains/soundstudiopro.com/public_html/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/domains/soundstudiopro.com/public_html/invoices.php
<?php
/**
 * User Invoices Page
 * Display Stripe invoices for the logged-in user
 */

session_start();
require_once 'config/database.php';
require_once __DIR__ . '/includes/translations.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: /auth/login.php?redirect=' . urlencode('/invoices.php'));
    exit;
}

$pdo = getDBConnection();
$stripe_secret = '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';

// Get user info
$stmt = $pdo->prepare("SELECT id, name, email, stripe_customer_id FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

$invoices = [];
$error_message = null;

// Get credit purchases from database
$credit_purchases = [];
try {
    $credit_stmt = $pdo->prepare("
        SELECT 
            id,
            package,
            credits,
            amount,
            payment_intent_id,
            created_at
        FROM credit_purchases 
        WHERE user_id = ? 
        ORDER BY created_at DESC
    ");
    $credit_stmt->execute([$_SESSION['user_id']]);
    $credit_purchases = $credit_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    error_log("Error fetching credit purchases: " . $e->getMessage());
}

// Convert credit purchases to invoice format
foreach ($credit_purchases as $purchase) {
    $package_names = [
        'starter' => 'Starter Credits Package',
        'pro' => 'Pro Credits Package',
        'premium' => 'Premium Credits Package'
    ];
    
    $invoice = [
        'id' => 'credit_' . $purchase['id'],
        'number' => 'CREDIT-' . str_pad($purchase['id'], 6, '0', STR_PAD_LEFT),
        'created' => strtotime($purchase['created_at']),
        'amount_paid' => (int)($purchase['amount'] * 100), // Convert to cents
        'amount_due' => (int)($purchase['amount'] * 100),
        'status' => 'paid',
        'description' => ($package_names[$purchase['package']] ?? ucfirst($purchase['package']) . ' Package') . ' - ' . $purchase['credits'] . ' credits',
        'type' => 'credit_purchase',
        'payment_intent_id' => $purchase['payment_intent_id'],
        'package' => $purchase['package'],
        'credits' => $purchase['credits']
    ];
    $invoices[] = $invoice;
}

// Get Stripe invoices, payment intents, and charges if customer ID exists
if (!empty($user['stripe_customer_id'])) {
    try {
        // Fetch invoices from Stripe (invoices for subscriptions and one-time payments)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/invoices?customer=' . urlencode($user['stripe_customer_id']) . '&limit=100');
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code === 200) {
            $data = json_decode($response, true);
            $stripe_invoices = $data['data'] ?? [];
            
            // Add Stripe invoices to the list
            foreach ($stripe_invoices as $stripe_invoice) {
                $stripe_invoice['type'] = 'stripe_invoice';
                $invoices[] = $stripe_invoice;
            }
        } else {
            error_log("Stripe invoices API error: HTTP {$http_code} - {$response}");
        }
        
        // Also fetch payment intents (for one-time payments that might not have invoices)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/payment_intents?customer=' . urlencode($user['stripe_customer_id']) . '&limit=100');
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code === 200) {
            $data = json_decode($response, true);
            $payment_intents = $data['data'] ?? [];
            
            // Track which payment intents already have invoices
            $invoiced_payment_intents = [];
            foreach ($invoices as $inv) {
                if (!empty($inv['payment_intent'])) {
                    $invoiced_payment_intents[$inv['payment_intent']] = true;
                }
            }
            
            // Add payment intents that don't have invoices
            foreach ($payment_intents as $pi) {
                // Skip if already has an invoice, or if not succeeded
                if (isset($invoiced_payment_intents[$pi['id']]) || $pi['status'] !== 'succeeded') {
                    continue;
                }
                
                // Convert payment intent to invoice format
                $invoice = [
                    'id' => $pi['id'],
                    'number' => 'PAY-' . substr($pi['id'], 3),
                    'created' => $pi['created'],
                    'amount_paid' => $pi['amount'],
                    'amount_due' => $pi['amount'],
                    'status' => 'paid',
                    'description' => 'Payment - ' . ($pi['description'] ?? 'One-time payment'),
                    'type' => 'payment_intent',
                    'payment_intent_id' => $pi['id'],
                    'metadata' => $pi['metadata'] ?? []
                ];
                $invoices[] = $invoice;
            }
        } else {
            error_log("Stripe payment intents API error: HTTP {$http_code}");
        }
        
        // Also fetch charges (for direct charges that might not have payment intents)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/charges?customer=' . urlencode($user['stripe_customer_id']) . '&limit=100');
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code === 200) {
            $data = json_decode($response, true);
            $charges = $data['data'] ?? [];
            
            // Track which charges already have invoices or payment intents
            $tracked_charges = [];
            foreach ($invoices as $inv) {
                if (!empty($inv['charge'])) {
                    $tracked_charges[$inv['charge']] = true;
                }
                if (!empty($inv['payment_intent_id'])) {
                    // Get payment intent to find associated charge
                    // (We'll skip charges that are part of payment intents we already have)
                }
            }
            
            // Add charges that don't have invoices or payment intents
            foreach ($charges as $charge) {
                // Skip if already tracked, or if not succeeded
                if (isset($tracked_charges[$charge['id']]) || $charge['status'] !== 'succeeded') {
                    continue;
                }
                
                // Skip if this charge is part of a payment intent we already have
                if (!empty($charge['payment_intent'])) {
                    $has_pi = false;
                    foreach ($invoices as $inv) {
                        if (isset($inv['payment_intent_id']) && $inv['payment_intent_id'] === $charge['payment_intent']) {
                            $has_pi = true;
                            break;
                        }
                    }
                    if ($has_pi) {
                        continue;
                    }
                }
                
                // Convert charge to invoice format
                $invoice = [
                    'id' => $charge['id'],
                    'number' => 'CHG-' . substr($charge['id'], 3),
                    'created' => $charge['created'],
                    'amount_paid' => $charge['amount'],
                    'amount_due' => $charge['amount'],
                    'status' => 'paid',
                    'description' => 'Charge - ' . ($charge['description'] ?? 'Payment'),
                    'type' => 'charge',
                    'charge_id' => $charge['id'],
                    'payment_intent_id' => $charge['payment_intent'] ?? null,
                    'metadata' => $charge['metadata'] ?? []
                ];
                $invoices[] = $invoice;
            }
        } else {
            error_log("Stripe charges API error: HTTP {$http_code}");
        }
        
        if (empty($invoices) && empty($credit_purchases)) {
            $error_message = t('invoice.fetch_error');
        }
    } catch (Exception $e) {
        if (empty($credit_purchases)) {
            $error_message = t('invoice.load_error') . " " . htmlspecialchars($e->getMessage());
        }
        error_log("Invoice fetch error: " . $e->getMessage());
    }
} else {
    if (empty($credit_purchases)) {
        $error_message = t('invoice.no_payment_history');
    }
}

// Sort all invoices by date (newest first)
// Ensure proper date conversion and sorting
usort($invoices, function($a, $b) {
    // Handle different date formats
    $time_a = 0;
    $time_b = 0;
    
    // For credit purchases, created_at is already a timestamp from strtotime
    if (isset($a['created'])) {
        if (is_numeric($a['created'])) {
            $time_a = (int)$a['created'];
        } elseif (is_string($a['created'])) {
            $time_a = strtotime($a['created']);
        }
    }
    
    if (isset($b['created'])) {
        if (is_numeric($b['created'])) {
            $time_b = (int)$b['created'];
        } elseif (is_string($b['created'])) {
            $time_b = strtotime($b['created']);
        }
    }
    
    // Sort by date DESC (newest first), then by ID DESC as tiebreaker
    if ($time_b != $time_a) {
        return $time_b - $time_a;
    }
    
    // If dates are equal, sort by ID (newest first)
    $id_a = isset($a['id']) ? (is_numeric($a['id']) ? (int)$a['id'] : 0) : 0;
    $id_b = isset($b['id']) ? (is_numeric($b['id']) ? (int)$b['id'] : 0) : 0;
    return $id_b - $id_a;
});

$page_title = t('invoice.page_title');

include 'includes/header.php';
?>

<style>
    .invoices-page {
        max-width: 1000px;
        margin: 60px auto;
        padding: 40px 20px;
    }
    
    .invoices-page h1 {
        color: white;
        font-size: 3.5rem;
        font-weight: 800;
        margin-bottom: 50px;
        text-align: center;
    }
    
    .invoices-container {
        background: rgba(255, 255, 255, 0.05);
        border-radius: 20px;
        padding: 30px;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    .invoice-item {
        background: rgba(255, 255, 255, 0.05);
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 15px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        display: flex;
        justify-content: space-between;
        align-items: center;
        transition: all 0.3s ease;
    }
    
    .invoice-item:hover {
        background: rgba(255, 255, 255, 0.08);
        transform: translateY(-2px);
    }
    
    .invoice-info {
        flex: 1;
    }
    
    .invoice-number {
        font-weight: 600;
        color: white;
        font-size: 1.1rem;
        margin-bottom: 5px;
    }
    
    .invoice-date {
        color: rgba(255, 255, 255, 0.7);
        font-size: 0.9rem;
        margin-bottom: 5px;
    }
    
    .invoice-description {
        color: rgba(255, 255, 255, 0.8);
        font-size: 0.95rem;
        margin-top: 8px;
    }
    
    .invoice-amount {
        font-size: 1.5rem;
        font-weight: 700;
        color: #48bb78;
        margin-right: 20px;
    }
    
    .invoice-status {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.85rem;
        font-weight: 600;
        margin-top: 5px;
    }
    
    .status-paid {
        background: rgba(72, 187, 120, 0.2);
        color: #48bb78;
    }
    
    .status-open {
        background: rgba(255, 193, 7, 0.2);
        color: #ffc107;
    }
    
    .status-draft {
        background: rgba(255, 255, 255, 0.1);
        color: rgba(255, 255, 255, 0.7);
    }
    
    .status-void {
        background: rgba(220, 38, 38, 0.2);
        color: #dc2626;
    }
    
    .status-uncollectible {
        background: rgba(220, 38, 38, 0.2);
        color: #dc2626;
    }
    
    .invoice-actions {
        display: flex;
        gap: 10px;
        align-items: center;
    }
    
    .btn-download {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        padding: 10px 20px;
        border-radius: 8px;
        cursor: pointer;
        text-decoration: none;
        font-weight: 600;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    
    .btn-download:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
    }
    
    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: rgba(255, 255, 255, 0.7);
    }
    
    .empty-state i {
        font-size: 4rem;
        margin-bottom: 20px;
        opacity: 0.5;
    }
    
    .error-message {
        background: rgba(220, 38, 38, 0.2);
        border: 1px solid rgba(220, 38, 38, 0.5);
        color: #fca5a5;
        padding: 15px;
        border-radius: 8px;
        margin-bottom: 20px;
    }
    
    .empty-state h3 {
        color: rgba(255, 255, 255, 0.9);
        font-size: 1.8rem;
        margin-bottom: 15px;
        font-weight: 600;
    }
    
    .empty-state p {
        color: rgba(255, 255, 255, 0.7);
        font-size: 1.1rem;
        line-height: 1.6;
    }
    
    @media (max-width: 768px) {
        .invoices-page {
            margin: 20px auto;
            padding: 20px 15px;
        }
        
        .invoices-page h1 {
            font-size: 2rem;
            margin-bottom: 30px;
        }
        
        .invoices-container {
            padding: 20px 15px;
            border-radius: 16px;
        }
        
        .invoice-item {
            flex-direction: column;
            align-items: flex-start;
            padding: 18px 15px;
            gap: 15px;
        }
        
        .invoice-info {
            width: 100%;
        }
        
        .invoice-number {
            font-size: 1rem;
        }
        
        .invoice-date {
            font-size: 0.85rem;
        }
        
        .invoice-description {
            font-size: 0.9rem;
            margin-top: 10px;
        }
        
        .invoice-amount {
            font-size: 1.4rem;
            margin-right: 0;
            margin-bottom: 12px;
            text-align: center;
            width: 100%;
            padding: 8px 0;
        }
        
        .invoice-actions {
            width: 100%;
            flex-direction: column;
            align-items: stretch;
            gap: 12px;
        }
        
        .btn-download {
            width: 100%;
            justify-content: center;
            padding: 12px 20px;
            font-size: 1rem;
            min-height: 44px;
        }
        
        .empty-state {
            padding: 40px 15px;
        }
        
        .empty-state i {
            font-size: 3rem;
            margin-bottom: 15px;
        }
        
        .empty-state h3 {
            font-size: 1.5rem;
        }
        
        .empty-state p {
            font-size: 1rem;
        }
        
        .error-message {
            padding: 12px 15px;
            font-size: 0.95rem;
        }
    }
    
    @media (max-width: 480px) {
        .invoices-page {
            padding: 15px 10px;
        }
        
        .invoices-page h1 {
            font-size: 1.75rem;
            margin-bottom: 20px;
        }
        
        .invoices-container {
            padding: 15px 10px;
        }
        
        .invoice-item {
            padding: 15px 12px;
        }
        
        .invoice-number {
            font-size: 0.95rem;
        }
        
        .invoice-date {
            font-size: 0.8rem;
        }
        
        .invoice-description {
            font-size: 0.85rem;
        }
        
        .invoice-amount {
            font-size: 1.3rem;
        }
        
        .invoice-status {
            font-size: 0.8rem;
            padding: 3px 10px;
        }
        
        .btn-download {
            padding: 10px 16px;
            font-size: 0.95rem;
            min-height: 44px;
        }
        
        .empty-state i {
            font-size: 2.5rem;
        }
        
        .empty-state h3 {
            font-size: 1.3rem;
        }
        
        .empty-state p {
            font-size: 0.95rem;
        }
    }
</style>

<div class="invoices-page">
    <h1><?= t('invoice.page_title') ?></h1>
    
    <div class="invoices-container">
        <?php if ($error_message): ?>
            <div class="error-message">
                <i class="fas fa-exclamation-circle"></i> <?= htmlspecialchars($error_message) ?>
            </div>
        <?php endif; ?>
        
        <?php if (empty($invoices) && !$error_message): ?>
            <div class="empty-state">
                <i class="fas fa-file-invoice"></i>
                <h3><?= t('invoice.no_invoices') ?></h3>
                <p><?= t('invoice.no_invoices_desc') ?></p>
            </div>
        <?php elseif (!empty($invoices)): ?>
            <?php foreach ($invoices as $invoice): ?>
                <div class="invoice-item">
                    <div class="invoice-info">
                        <div class="invoice-number">
                            <?= t('invoice.invoice_number') ?> #<?= htmlspecialchars($invoice['number'] ?? $invoice['id']) ?>
                        </div>
                        <div class="invoice-date">
                            <?php
                            $invoice_date = $invoice['created'] ?? 0;
                            if (is_numeric($invoice_date)) {
                                echo date('F j, Y', $invoice_date);
                            } elseif (is_string($invoice_date)) {
                                echo date('F j, Y', strtotime($invoice_date));
                            } else {
                                echo 'Date unavailable';
                            }
                            ?>
                        </div>
                        <?php if (!empty($invoice['description'])): ?>
                            <div class="invoice-description">
                                <?= htmlspecialchars($invoice['description']) ?>
                            </div>
                        <?php elseif (isset($invoice['type']) && $invoice['type'] === 'credit_purchase'): ?>
                            <div class="invoice-description">
                                <i class="fas fa-coins" style="margin-right: 5px;"></i>
                                <?= htmlspecialchars($invoice['description'] ?? 'Credit Package Purchase') ?>
                            </div>
                        <?php elseif (!empty($invoice['lines']['data'][0]['description'])): ?>
                            <div class="invoice-description">
                                <?= htmlspecialchars($invoice['lines']['data'][0]['description']) ?>
                            </div>
                        <?php else: ?>
                            <div class="invoice-description">
                                <?php
                                if (!empty($invoice['subscription'])) {
                                    echo t('invoice.subscription_payment');
                                } else {
                                    echo t('invoice.payment');
                                }
                                ?>
                            </div>
                        <?php endif; ?>
                        <span class="invoice-status status-<?= htmlspecialchars($invoice['status']) ?>">
                            <?php
                            $status_key = 'invoice.status_' . $invoice['status'];
                            $status_translation = t($status_key);
                            echo ($status_translation !== $status_key) ? $status_translation : ucfirst($invoice['status']);
                            ?>
                        </span>
                    </div>
                    <div class="invoice-actions">
                        <div class="invoice-amount">
                            $<?= number_format(($invoice['amount_paid'] ?? $invoice['amount_due'] ?? 0) / 100, 2) ?>
                        </div>
                        <?php if (!empty($invoice['invoice_pdf'])): ?>
                            <a href="<?= htmlspecialchars($invoice['invoice_pdf']) ?>" target="_blank" class="btn-download">
                                <i class="fas fa-download"></i>
                                <?= t('invoice.download_pdf') ?>
                            </a>
                        <?php elseif (!empty($invoice['hosted_invoice_url'])): ?>
                            <a href="<?= htmlspecialchars($invoice['hosted_invoice_url']) ?>" target="_blank" class="btn-download">
                                <i class="fas fa-external-link-alt"></i>
                                <?= t('invoice.view_invoice') ?>
                            </a>
                        <?php elseif (isset($invoice['type']) && $invoice['type'] === 'credit_purchase' && !empty($invoice['payment_intent_id'])): ?>
                            <a href="https://dashboard.stripe.com/payments/<?= htmlspecialchars($invoice['payment_intent_id']) ?>" target="_blank" class="btn-download">
                                <i class="fas fa-external-link-alt"></i>
                                <?= t('invoice.view_payment', ['default' => 'View Payment']) ?>
                            </a>
                        <?php elseif (isset($invoice['type']) && ($invoice['type'] === 'payment_intent' || $invoice['type'] === 'charge') && !empty($invoice['payment_intent_id'])): ?>
                            <a href="https://dashboard.stripe.com/payments/<?= htmlspecialchars($invoice['payment_intent_id']) ?>" target="_blank" class="btn-download">
                                <i class="fas fa-external-link-alt"></i>
                                <?= t('invoice.view_payment', ['default' => 'View Payment']) ?>
                            </a>
                        <?php elseif (isset($invoice['type']) && $invoice['type'] === 'charge' && !empty($invoice['charge_id'])): ?>
                            <a href="https://dashboard.stripe.com/payments/<?= htmlspecialchars($invoice['charge_id']) ?>" target="_blank" class="btn-download">
                                <i class="fas fa-external-link-alt"></i>
                                <?= t('invoice.view_payment', ['default' => 'View Payment']) ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<?php include 'includes/footer.php'; ?>


CasperSecurity Mini