![]() 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/ |
# Credit Purchase Behavior - What Happens When Credits Are Bought
## 🔍 Question
**What happens when Stephane bought 5 credit subscription, and he had 9 credits already?**
## 📋 Answer
### How Credit Addition Works
When a credit package is purchased, the system **ADDS** credits to the existing balance:
```php
// From webhooks/stripe.php - addCreditsToUser() function
UPDATE users
SET credits = credits + ?, // ← ADDS to existing credits
plan = ?,
subscription_expires = ?
WHERE id = ?
```
### Example Calculation
**Scenario:**
- Stephane has: **9 credits**
- Buys credit package: **+30 credits** (Starter package)
- **Result: 9 + 30 = 39 credits total**
### Available Credit Packages
The system has these credit packages (one-time purchases):
1. **Starter**: 30 credits for $19.99
2. **Pro**: 200 credits for $59
3. **Premium**: 500 credits for $129
**Note:** There is NO "5 credit" package. If Stephane "bought 5 credits", it might be:
- A free credit grant (not a purchase)
- Confusion with Essential subscription (5 tracks/month, not credits)
- Part of a different transaction
### Credit Purchase Flow
1. **User adds credit package to cart** (`credits.php`)
2. **Checkout via Stripe** (Payment Intent created)
3. **Webhook receives** `payment_intent.succeeded`
4. **`addCreditsToUser()` function:**
- Adds credits: `credits = credits + [package_credits]`
- Updates plan: `plan = [package_name]`
- Sets expiration: `subscription_expires = NOW() + 30 days`
- Records purchase in `credit_purchases` table
- Logs transaction in `credit_transactions` table
### Important Notes
1. **Credits are ADDITIVE**: Each purchase adds to existing balance
2. **No replacement**: Credits are never replaced, only added
3. **30-day expiration**: Purchased credits expire 30 days after purchase
4. **Plan update**: User's plan is updated to match the package (starter/pro/premium)
### Checking Stephane's Credits
To check Stephane's actual credit balance and purchases:
1. **Run the script**: `/check_stephane_credits.php` (requires admin login)
2. **Or query database directly**:
```sql
-- Current balance
SELECT credits, plan FROM users WHERE id = 5;
-- Purchase history
SELECT * FROM credit_purchases WHERE user_id = 5 ORDER BY created_at DESC;
-- Transaction history
SELECT * FROM credit_transactions WHERE user_id = 5 ORDER BY created_at DESC;
```
### Expected Behavior
If Stephane had **9 credits** and bought a **Starter package (30 credits)**:
✅ **Correct Result**: 9 + 30 = **39 credits**
If the result is different, possible causes:
- Credits were used to create tracks (1 credit per track)
- Credits expired (30-day expiration)
- Credits were granted from another source
- Database transaction issue
### Verification
The script `check_stephane_credits.php` will show:
- Current credit balance
- All credit purchases
- Credit transaction history
- Expected vs actual balance
- Any discrepancies