1- import { HttpException , HttpStatus , Injectable , Logger } from '@nestjs/common' ;
1+ import {
2+ BadRequestException ,
3+ HttpException ,
4+ HttpStatus ,
5+ Injectable ,
6+ Logger ,
7+ } from '@nestjs/common' ;
28import { ConfigService } from '@nestjs/config' ;
39import { CreateOrderDto } from '@projectx/models' ;
410import {
@@ -87,9 +93,9 @@ export class AppService {
8793 HttpStatus . CONFLICT
8894 ) ;
8995 } else {
90- throw new HttpException (
96+ this . logger . error ( `createOrder(${ user . email } ) - Error creating order` , error ) ;
97+ throw new BadRequestException (
9198 'Error creating order' ,
92- HttpStatus . INTERNAL_SERVER_ERROR ,
9399 {
94100 cause : error ,
95101 }
@@ -111,7 +117,7 @@ export class AppService {
111117 throw new HttpException ( 'No active order found' , HttpStatus . NOT_FOUND ) ;
112118 }
113119
114- if ( Date . now ( ) - description . startTime . getMilliseconds ( ) >= WORKFLOW_TTL ) {
120+ if ( Date . now ( ) - description . startTime . getTime ( ) >= WORKFLOW_TTL ) {
115121 throw new HttpException ( 'Order has expired' , HttpStatus . GONE ) ;
116122 }
117123
@@ -127,17 +133,25 @@ export class AppService {
127133 await handle . signal ( cancelWorkflowSignal ) ;
128134 }
129135
130- async handleWebhook ( payload : Buffer , signature : string ) {
131- this . logger . log ( 'Processing webhook event' ) ;
136+ async handleWebhook ( payload : string | Buffer , signature : string ) {
137+ if ( ! payload || ! signature ) {
138+ this . logger . error ( `handleWebhook(${ signature } ) - No payload received` ) ;
139+ throw new BadRequestException ( 'No payload received' ) ;
140+ }
141+ this . logger . log ( `handleWebhook(${ signature } ) - Processing webhook event` ) ;
132142 try {
133143 // Verify and construct the webhook event
134- const event = await this . stripeService . constructWebhookEvent (
144+ const event = this . stripeService . constructWebhookEvent (
135145 payload ,
136146 signature
137147 ) ;
138148
139149 // Extract payment intent data
140150 const paymentIntent = this . stripeService . handleWebhookEvent ( event ) ;
151+ if ( ! paymentIntent ?. metadata ) {
152+ this . logger . error ( `handleWebhook(${ signature } ) - Unhandled event type: ${ event . type } ` ) ;
153+ return { received : true } ;
154+ }
141155 const { userId, referenceId } = paymentIntent . metadata ;
142156
143157 if ( ! userId || ! referenceId ) {
@@ -174,8 +188,10 @@ export class AppService {
174188 // Return true to indicate the webhook was received
175189 return { received : true } ;
176190 } catch ( err ) {
177- this . logger . error ( 'Webhook Error:' , err . message ) ;
178- throw err ;
191+ this . logger . error ( `handleWebhook(${ signature } ) - Webhook Error: ${ err . message } ` ) ;
192+ throw new BadRequestException ( 'Webhook Error' , {
193+ cause : err ,
194+ } ) ;
179195 }
180196 }
181197}
0 commit comments