Overview
Implement a `POST` handler that sends a test ping payload to a registered webhook URL so the user can verify their endpoint is working.
⚠️ Scope: Write your code only inside `app/api/routes-b/webhooks/[id]/ping/route.ts`. Do not touch any file outside this folder.
Create the route file
File: `app/api/routes-b/webhooks/[id]/ping/route.ts`
Handler logic
- Verify auth
- Find `UserWebhook` by `params.id` — verify ownership
- Build a test payload:
```typescript
const payload = {
event: 'ping',
timestamp: new Date().toISOString(),
webhookId: params.id,
}
```
- Sign it using HMAC-SHA256 with the webhook's `signingSecret`:
```typescript
import crypto from 'crypto'
const signature = crypto
.createHmac('sha256', webhook.signingSecret)
.update(JSON.stringify(payload))
.digest('hex')
```
-
`POST` the payload to `webhook.targetUrl` with headers:
- `Content-Type: application/json`
- `X-LancePay-Signature: sha256=${signature}`
-
Record success/failure and return the result
Expected response
```json
{
"success": true,
"statusCode": 200,
"targetUrl": "https://myapp.com/webhooks/lancepay"
}
```
If the ping fails (non-2xx or network error):
```json
{ "success": false, "statusCode": 500, "targetUrl": "https://myapp.com/webhooks/lancepay" }
```
Do not throw an error if the target URL returns a non-2xx — just record `success: false`.
Acceptance criteria
Overview
Implement a `POST` handler that sends a test ping payload to a registered webhook URL so the user can verify their endpoint is working.
Create the route file
File: `app/api/routes-b/webhooks/[id]/ping/route.ts`
Handler logic
```typescript
const payload = {
event: 'ping',
timestamp: new Date().toISOString(),
webhookId: params.id,
}
```
```typescript
import crypto from 'crypto'
const signature = crypto
.createHmac('sha256', webhook.signingSecret)
.update(JSON.stringify(payload))
.digest('hex')
```
`POST` the payload to `webhook.targetUrl` with headers:
Record success/failure and return the result
Expected response
```json
{
"success": true,
"statusCode": 200,
"targetUrl": "https://myapp.com/webhooks/lancepay"
}
```
If the ping fails (non-2xx or network error):
```json
{ "success": false, "statusCode": 500, "targetUrl": "https://myapp.com/webhooks/lancepay" }
```
Acceptance criteria