A developer updates a NestJS microservice to process Stripe webhooks. The process involves mapping HTTP ports in Docker, configuring raw body parsing for signature verification, and implementing a controller to handle events like checkout session completion and expiry within the payment service.
Length 15:12
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nestjs #stripe #webhooks #typescript #backend #microservices #docker #coding #api #software-development
0:00 All right, so now what we're gonna do is prepare our payment service to 0:04 be able to receive these webhooks from Stripe. 0:07 Of course, the first thing we need to do 0:09 is actually open up our payment 0:11 service for HTTP traffic. Remember, right now it 0:15 is only exposed as a TCP microservice, but now we 0:19 want to be able to actually receive HTTP requests from Stripe. 0:24 So the first step that I'm gonna do is firstly in our Docker 0:27 Compose, in our payments section, 0:30 let's go ahead and add a new ports key 0:34 and define a new ports array. In this case, we'll map 0:38 port 3005 on payments as the HTTP port. 0:43 And then I'll head back to payments.m. 0:46 Right now we have the single hard-coded port. 0:50 Let's change this to TCP port 3003, 0:54 and now we'll define the HTTP port as 3005. 0:58 So this looks good. So with this in place, we can now go back 1:02 to our main.ts where we're bootstrapping the server, and there's a few 1:06 changes we need to make. The first is actually on the Nest factory. 1:10 We need to pass a new option to 1:12 nestFactory.create. The second options here is 1:16 an object where we can configure the underlying Express server. 1:20 And here we wanna set a property called raw body to 1:24 true. Now, what this does is by default, the Express server 1:28 inside of NestJS uses 1:33 body parser middleware to automatically parse the incoming 1:36 request as a JSON object, which is very 1:40 useful because that's what populates the 1:43 JavaScript body object on the request, and that's how we are able to 1:47 destructure the addBody payload. 1:50 However, when we use Stripe webhooks, we need to actually 1:53 verify that the incoming request is in fact 1:57 coming from Stripe, right? We want some sort of authentication or
The rest of the transcript is for members.
Sign in