A developer implements webhook handlers in a NestJS reservation service to process Stripe payment success and failure events. The process involves configuring TCP communication between microservices, updating database status using MongoDB operators, and utilizing event patterns for asynchronous state transitions.
Length 16:38
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #webhook #stripe #microservices #nestjs #tcp #programming #backend #reservation #typescript
0:00 Finish this webhook implementation off. 0:02 The first thing I wanna do is head back into the reservation service, 0:07 specifically into the reservations controller. 0:10 And in here, we want to now add a couple of event 0:13 patterns that 0:14 we can listen for events from the payment service. 0:17 Now, I think this is also a really cool example 0:20 because it can show you that 0:21 you can actually mix together HTTP controller 0:25 methods with 0:26 microservice TCP or any other controller methods. 0:30 So here we can simply add a new event 0:34 pattern decorator and listen for the paymentSucceeded 0:37 event, which remember we set up earlier in our 0:40 common DTO. 0:42 We'll set up our usePipe for ValidationPipe to make sure we have 0:46 validation, 0:48 and set up async 0:49 handlePaymentSucceeded. 0:54 And we'll destructure or extract the incoming TCP 0:58 payload, which will be of type PaymentSucceededDTO. 1:03 And now we'll call this 1:05 .reservationService.handlePaymentSucceeded 1:10 and pass through that payload, which of course we'll have to set up, 1:14 which 1:14 we'll do shortly. 1:16 Let's go ahead and do the same thing for payment failed event. 1:21 So I'll add paymentFailed, 1:23 use pipe, new ValidationPipe. 1:27 And now this will be async handlePaymentFailed. 1:31 Pass through that payload 1:34 of type PaymentFailedDTO, 1:38 and we'll return this 1:39 reservationService.handlePaymentFailed 1:44 with the payload. And so now let's add these inside of the 1:47 reservation service. So this is gonna be pretty straightforward now 1:51 in the reservation service. Let's go ahead and start off with 1:55 async handlePaymentSucceeded. 2:01 We'll go ahead and destructure the reservationId as well as the 2:05 paymentIntentId 2:09 from 2:10 the PaymentSucceededDTO. 2:14 And firstly, we can go ahead and add a log 2:18 statement just to make it more clear that we've actually received this event. 2:22 So let's go ahead and create 2:25 our private readonly logger 2:28 of type new Logger from Nest common, 2:32 pass through our 2:32 reservationService.name. 2:39 And now we can use this to log something out.
The rest of the transcript is for members.
Sign in