# 🏦 Midtrans Sandbox Setup Guide

## 📋 Prerequisites

1. **Midtrans Sandbox Account**: Sign up at https://dashboard.sandbox.midtrans.com/
2. **Get your credentials** from Midtrans Dashboard

## 🔧 Step 1: Environment Setup

Add these variables to your `.env` file:

```env
# Midtrans Sandbox Configuration
MIDTRANS_CLIENT_KEY=SB-Mid-client-your_sandbox_client_key_here
MIDTRANS_SERVER_KEY=SB-Mid-server-your_sandbox_server_key_here
MIDTRANS_IS_PRODUCTION=false
```

### How to get your keys:
1. Go to https://dashboard.sandbox.midtrans.com/
2. Login/Register with your account
3. Go to **Settings** → **Access Keys**
4. Copy **Client Key** and **Server Key**

## 🚀 Step 2: Postman Setup

1. **Import** the updated `LMS-Payment-Flow.postman_collection.json`
2. **Set Environment Variables**:
   - Go to **Variables** tab in Postman
   - Set `midtrans_server_key` to your actual Midtrans Server Key
   - Set `base_url` to your API URL (default: `http://localhost:8000`)

## 💳 Step 3: Test Payment Flow

### Option A: Full Browser Payment (Recommended)
1. **Run "Login"** request
2. **Run "Initiate Payment"** request
3. **Copy the `payment_url`** from response
4. **Open payment URL in browser**
5. **Use test cards**:
   - **Success**: `4811 1111 1111 1114`
   - **Failed**: `4911 1111 1111 1113`
   - **CVV**: `123`
   - **Expiry**: `12/25`
6. **Complete payment** in browser
7. **Webhook will be automatically sent** by Midtrans
8. **Check status** using "Check Payment Status" request

### Option B: Manual Webhook Simulation
1. **Run "Login"** request  
2. **Run "Initiate Payment"** request
3. **Run "Simulate Midtrans Success Webhook"** request
4. **Run "Check Payment Status"** request

## 🧪 Test Cards

| Purpose | Card Number | CVV | Expiry |
|---------|-------------|-----|--------|
| **Success** | `4811 1111 1111 1114` | `123` | `12/25` |
| **Failed** | `4911 1111 1111 1113` | `123` | `12/25` |
| **3D Secure** | `4481 1111 1111 1114` | `123` | `12/25` |

## 📊 Monitoring

- **Midtrans Dashboard**: https://dashboard.sandbox.midtrans.com/
- **Transaction Logs**: Check your dashboard for all transactions
- **Webhook Logs**: Monitor webhook delivery status

## 🔐 Security Notes

- **Sandbox Only**: Never use sandbox keys in production
- **Signature Verification**: Webhook signatures are verified for security
- **Server Key**: Keep your server key secret, never expose in frontend

## 🎯 Available Packages for Testing

1. **Ksatria PCPM** - Rp 1,350,000 (6 months) - Full access
2. **Pejuang PCPM** - Rp 900,000 (4 months) - Tahap 1 & 2
3. **Tahap 1 Only** - Rp 450,000 (3 months)
4. **Tahap 2 Only** - Rp 540,000 (3 months)
5. **Tahap 3 Only** - Rp 630,000 (2 months)
6. **Intensif** - Rp 1,800,000 (6 months) - Premium

## 🆘 Troubleshooting

### Common Issues:

1. **"Invalid signature" error**:
   - Check your `MIDTRANS_SERVER_KEY` in `.env`
   - Verify Postman environment variable `midtrans_server_key`

2. **"Failed to create payment token" error**:
   - Check your `MIDTRANS_CLIENT_KEY` in `.env`
   - Ensure you're using **Sandbox** credentials (start with `SB-Mid-`)

3. **Webhook not received**:
   - For local testing, webhook simulation is required
   - For real webhook testing, use tools like ngrok

### Debug Steps:
1. Check Laravel logs: `tail -f storage/logs/laravel.log`
2. Verify Midtrans configuration: `php artisan config:cache`
3. Test with Postman collection step by step

## 🔗 Useful Links

- **Midtrans Sandbox Dashboard**: https://dashboard.sandbox.midtrans.com/
- **Midtrans API Docs**: https://docs.midtrans.com/
- **Test Payment Simulator**: https://simulator.sandbox.midtrans.com/

---

Happy Testing! 🎉 