$npx -y skills add hookdeck/webhook-skills --skill chargebee-webhooksReceive and verify Chargebee webhooks. Use when setting up Chargebee webhook handlers, debugging Basic Auth verification, or handling subscription billing events.
| 1 | # Chargebee Webhooks |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | - Setting up Chargebee webhook handlers |
| 6 | - Debugging Basic Auth verification failures |
| 7 | - Understanding Chargebee event types and payloads |
| 8 | - Processing subscription billing events |
| 9 | |
| 10 | ## Essential Code |
| 11 | |
| 12 | Chargebee uses Basic Authentication for webhook verification. Here's how to implement it: |
| 13 | |
| 14 | ### Express.js |
| 15 | |
| 16 | ```javascript |
| 17 | // Verify Chargebee webhook with Basic Auth |
| 18 | // NOTE: Chargebee uses Basic Auth (not HMAC signatures), so raw body access |
| 19 | // is not required. Use express.json() for automatic JSON parsing: |
| 20 | app.post('/webhooks/chargebee', express.json(), (req, res) => { |
| 21 | // Extract Basic Auth credentials |
| 22 | const auth = req.headers.authorization; |
| 23 | if (!auth || !auth.startsWith('Basic ')) { |
| 24 | return res.status(401).send('Unauthorized'); |
| 25 | } |
| 26 | |
| 27 | // Decode and verify credentials |
| 28 | const encoded = auth.substring(6); |
| 29 | const decoded = Buffer.from(encoded, 'base64').toString('utf-8'); |
| 30 | const [username, password] = decoded.split(':'); |
| 31 | |
| 32 | const expectedUsername = process.env.CHARGEBEE_WEBHOOK_USERNAME; |
| 33 | const expectedPassword = process.env.CHARGEBEE_WEBHOOK_PASSWORD; |
| 34 | |
| 35 | if (username !== expectedUsername || password !== expectedPassword) { |
| 36 | return res.status(401).send('Invalid credentials'); |
| 37 | } |
| 38 | |
| 39 | // Access the parsed JSON directly |
| 40 | const event = req.body; |
| 41 | console.log(`Received ${event.event_type} event:`, event.id); |
| 42 | |
| 43 | // Handle specific event types |
| 44 | switch (event.event_type) { |
| 45 | case 'subscription_created': |
| 46 | case 'subscription_changed': |
| 47 | case 'subscription_cancelled': |
| 48 | // Process subscription events |
| 49 | break; |
| 50 | case 'payment_succeeded': |
| 51 | case 'payment_failed': |
| 52 | // Process payment events |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | res.status(200).send('OK'); |
| 57 | }); |
| 58 | |
| 59 | // Note: If you later need raw body access (e.g., for HMAC signature |
| 60 | // verification with other providers), use express.raw(): |
| 61 | // app.post('/webhooks/other', express.raw({ type: 'application/json' }), (req, res) => { |
| 62 | // const rawBody = req.body.toString(); |
| 63 | // // ... verify signature using rawBody ... |
| 64 | // }); |
| 65 | ``` |
| 66 | |
| 67 | ### Next.js (App Router) |
| 68 | |
| 69 | ```typescript |
| 70 | // app/webhooks/chargebee/route.ts |
| 71 | import { NextRequest } from 'next/server'; |
| 72 | |
| 73 | export async function POST(req: NextRequest) { |
| 74 | // Extract Basic Auth credentials |
| 75 | const auth = req.headers.get('authorization'); |
| 76 | if (!auth || !auth.startsWith('Basic ')) { |
| 77 | return new Response('Unauthorized', { status: 401 }); |
| 78 | } |
| 79 | |
| 80 | // Decode and verify credentials |
| 81 | const encoded = auth.substring(6); |
| 82 | const decoded = Buffer.from(encoded, 'base64').toString('utf-8'); |
| 83 | const [username, password] = decoded.split(':'); |
| 84 | |
| 85 | const expectedUsername = process.env.CHARGEBEE_WEBHOOK_USERNAME; |
| 86 | const expectedPassword = process.env.CHARGEBEE_WEBHOOK_PASSWORD; |
| 87 | |
| 88 | if (username !== expectedUsername || password !== expectedPassword) { |
| 89 | return new Response('Invalid credentials', { status: 401 }); |
| 90 | } |
| 91 | |
| 92 | // Process the webhook |
| 93 | const event = await req.json(); |
| 94 | console.log(`Received ${event.event_type} event:`, event.id); |
| 95 | |
| 96 | return new Response('OK', { status: 200 }); |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | ### FastAPI |
| 101 | |
| 102 | ```python |
| 103 | # main.py |
| 104 | from fastapi import FastAPI, Header, HTTPException, Depends |
| 105 | from typing import Optional |
| 106 | import base64 |
| 107 | import os |
| 108 | |
| 109 | app = FastAPI() |
| 110 | |
| 111 | def verify_chargebee_auth(authorization: Optional[str] = Header(None)): |
| 112 | """Verify Chargebee webhook Basic Auth""" |
| 113 | if not authorization or not authorization.startswith("Basic "): |
| 114 | raise HTTPException(status_code=401, detail="Unauthorized") |
| 115 | |
| 116 | # Decode credentials |
| 117 | encoded = authorization[6:] |
| 118 | decoded = base64.b64decode(encoded).decode('utf-8') |
| 119 | |
| 120 | # Split username:password (handle colons in password) |
| 121 | if ':' not in decoded: |
| 122 | raise HTTPException(status_code=401, detail="Invalid authorization format") |
| 123 | |
| 124 | colon_index = decoded.index(':') |
| 125 | username = decoded[:colon_index] |
| 126 | password = decoded[colon_index + 1:] |
| 127 | |
| 128 | expected_username = os.getenv("CHARGEBEE_WEBHOOK_USERNAME") |
| 129 | expected_password = os.getenv("CHARGEBEE_WEBHOOK_PASSWORD") |
| 130 | |
| 131 | if username != expected_username or password != expected_password: |
| 132 | raise HTTPException(status_code=401, detail="Invalid credentials") |
| 133 | |
| 134 | return True |
| 135 | |
| 136 | @app.post("/webhooks/chargebee") |
| 137 | async def handle_chargebee_webhook( |
| 138 | event: dict, |
| 139 | auth_valid: bool = Depends(verify_chargebee_auth) |
| 140 | ): |
| 141 | """Handle Chargebee webhook events""" |
| 142 | event_type = event.get("event_type") |
| 143 | print(f"Received {event_type} event: {event.get('id')}") |
| 144 | |
| 145 | # Process event based on type |
| 146 | if event_type in ["subscription_created", "subscription_changed", "subscription_cancelled"]: |
| 147 | # Handle subscription events |
| 148 | pass |
| 149 | elif event_type in |