5 months ago
Hello, I have a project in NestJS that is deployed here on Railway.
I'm having problems with Stripe's webhook.
In local development, the WebHook works, but when deploying here on the Railway platform, errors occur:
Main.ts
import { ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { AppModule } from './app/app.module'
async function bootstrap() {
const app = await NestFactory.create(AppModule, { rawBody: true })
const config = new DocumentBuilder()
.setTitle('Core Service API Documentation')
.setDescription('API documentation for Core Service')
.setVersion('1.0')
.addBearerAuth()
.build()
const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup('api-docs', app, document)
const configService = app.get(ConfigService)
const port: number =
configService.get<number>('PORT') ||
configService.get<number>('SERVICE_PORT') ||
12005
app.useGlobalPipes(new ValidationPipe())
app.enableCors()
await app.listen(port)
}
void bootstrap()
`const app = await NestFactory.create(AppModule, { rawBody: true })`
My controller:
import {
Controller,
HttpCode,
Post,
Req,
type RawBodyRequest
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { Request } from 'express'
import { HandleStripeWebhookUseCase } from 'src/app/modules/checkout/core/useCases/stripe/handle-stripe-webhook.usecase'
@ApiTags('Webhooks')
@Controller('checkouts/webhooks')
export class WebhooksController {
constructor(
private readonly handleStripeWebhookUseCase: HandleStripeWebhookUseCase
) {}
@Post('stripe')
@HttpCode(200)
async handleStripeWebhook(@Req() req: RawBodyRequest<Request>) {
const signature = req.headers['stripe-signature']
const rawBody = req.rawBody
if (!rawBody) {
throw new Error('Raw body is undefined')
}
if (typeof signature !== 'string') {
throw new Error('Stripe signature is missing or invalid')
}
return this.handleStripeWebhookUseCase.execute(rawBody, signature)
}
@Post('test-raw')
@HttpCode(200)
testRawBody(@Req() req: Request & { rawBody: Buffer }) {
return {
rawBodyLength: req.rawBody?.length,
rawBodySample: req.rawBody?.toString('utf8').substring(0, 100),
regularBody: req.body
}
}
}
What could be the problem?
Is your proxy changing the request body?
Well, I followed the Stripe documentation for implementation and it's strange that it doesn't work after deployment.
The error:
StripeSignatureVerificationError: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
If a webhook request is being forwarded by a third-party tool, ensure that the exact request body, including JSON formatting and new line style, is preserved.
1 Replies
5 months ago
Hello,
Our proxy never touches the body data of the request.
You have an application or configuration level issue on your side, and unfortunately we cannot help with those as these forums are only for platform or product support.
Perhaps coding support forums such as StackOverflow would be able to help you here, or even Stripe's support?
Best,
Brody
Status changed to Awaiting User Response Railway • 5 months ago
Status changed to Closed brody • 5 months ago