File size: 2,708 Bytes
3eebcd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 🧪 Guía de Testing Local con Stripe CLI

## Descripción

Usa **Stripe CLI** para probar webhooks localmente sin necesidad de deploy a producción.

---

## 1️⃣ Instalar Stripe CLI

### Windows (con Chocolatey):
```bash
choco install stripe-cli
```

### Windows (Manual):
1. Descarga de: https://github.com/stripe/stripe-cli/releases
2. Descomprime en `C:\Program Files\stripe-cli`
3. Agrega a PATH (o usa ruta completa)

### Verificar instalación:
```bash
stripe --version
# Salida esperada: v1.x.x
```

---

## 2️⃣ Login con Stripe

```bash
stripe login
```

Te abrirá un navegador para autorizar. Haz click "Allow" y vuelve a la terminal.

Done ✅ cuando veas:
```
✓ Authenticated with Stripe
```

---

## 3️⃣ Configurar Webhook Local

En una terminal, ejecuta:

```bash
stripe listen --forward-to localhost:3000/api/payments/webhook
```

**Salida:**
```
Getting ready to listen for live events...
Ready! You are now listening for Stripe events.

Webhook signing secret for whsec_XXXXX
```

**IMPORTANTE**: Copia ese `whsec_XXXXX` y actualiza tu `.env`:

```env
STRIPE_WEBHOOK_SECRET="whsec_XXXXX"
```

---

## 4️⃣ Iniciar Servidor (otra terminal)

```bash
npm run dev
# Server running at localhost:3000
```

---

## 5️⃣ Crear un Evento de Prueba

En una **tercera terminal**, simula un evento:

### Test: Checkout Completado
```bash
stripe trigger checkout.session.completed
```

**Verás en la consola del servidor:**
```
✓ [Webhook] Received checkout.session.completed
✓ Created subscription in DB
```

### Test: Pago de Factura (Renovación)
```bash
stripe trigger invoice.payment_succeeded
```

### Test: Pago Fallido
```bash
stripe trigger invoice.payment_failed
```

---

## 6️⃣ Verificar Eventos

En la terminal donde ejecutaste `stripe listen`:

```
2025-01-15 10:30:00 → checkout.session.completed
2025-01-15 10:30:01 → invoice.payment_succeeded
```

---

## 🔍 Debugging

### Ver logs del webhook:
```bash
stripe logs tail
```

### Ver eventos recientes en dashboard:
https://dashboard.stripe.com/test/webhooks

---

## ✅ Test Flow Completo

```bash
# Terminal 1: Webhook listener
stripe listen --forward-to localhost:3000/api/payments/webhook

# Terminal 2: Servidor
npm run dev

# Terminal 3: Triggear eventos
stripe trigger checkout.session.completed
stripe trigger invoice.payment_succeeded

# Ver resultados en BD:
npx prisma studio
```

---

## 💡 Tips

- **Stripe CLI** se desconecta automáticamente cada 1 hora
  - Ejecuta nuevamente: `stripe listen --forward-to ...`
- Los eventos triggados son **reales** (van a tu BD)
- No requiere tarjeta de crédito
- Los webhooks se reciben en **tiempo real**

---

**¡Listo para testear!** 🚀