@@ -5,13 +5,26 @@ import { EmailService } from './EmailService.js'
55import { NotificationEngine } from './NotificationEngine.js'
66import { getNotificationConfig } from '../config/notifications.js'
77import type Stripe from 'stripe'
8+ import type { NotificationChannel , NotificationPayload } from '../types/notifications.js'
89
910// Shared notification engine instance
1011const getNotificationEngine = ( ) : NotificationEngine => {
1112 const config = getNotificationConfig ( )
1213 return new NotificationEngine ( config )
1314}
1415
16+ const sendNotification = async ( payload : NotificationPayload ) : Promise < void > => {
17+ try {
18+ console . log ( 'Sending notification:' , payload )
19+ const notificationEngine = getNotificationEngine ( )
20+ console . log ( 'sending notification with engine:' , notificationEngine )
21+ await notificationEngine . sendNotification ( payload , [ 'discord' ] )
22+ } catch ( error ) {
23+ console . error ( 'Failed to send notification:' , error )
24+ throw new ApiError ( 'Failed to send notification' , 500 )
25+ }
26+ }
27+
1528const handleCheckoutSessionCompleted = async ( session : Stripe . Checkout . Session ) : Promise < void > => {
1629 const customerId = session . customer as string
1730 const userId = session . client_reference_id || session . metadata ?. user_id
@@ -47,6 +60,25 @@ const handleCheckoutSessionCompleted = async (session: Stripe.Checkout.Session):
4760 expiration_date : null ,
4861 updated_at : new Date ( )
4962 } )
63+ const notificationEngine = getNotificationEngine ( )
64+
65+ await notificationEngine . sendNotification ( {
66+ type : 'checkout_completed' ,
67+ user : {
68+ id : userId ,
69+ email : session . customer_details ?. email || session . customer_email || 'N/A'
70+ } ,
71+ referenceId : `checkout_completed_${ session . id } ` ,
72+ data : {
73+ session_id : session . id ,
74+ customer_id : customerId ,
75+ subscription_id : session . subscription ,
76+ product_id : productId ,
77+ license_type : productConfig . licenseType ,
78+ amount_total : session . amount_total ,
79+ currency : session . currency
80+ }
81+ } , [ 'discord' ] )
5082 return
5183 }
5284
@@ -62,22 +94,6 @@ const handleCheckoutSessionCompleted = async (session: Stripe.Checkout.Session):
6294 updated_at : new Date ( )
6395 } )
6496 console . log ( 'Existing subscription license updated' )
65- return
66- }
67- console . log ( 'Creating new subscription license' )
68- await LicenseRepo . createLicense ( {
69- user_id : userId ,
70- status : 'active' ,
71- license_type : productConfig . licenseType ,
72- purchase_date : new Date ( ) ,
73- stripe_customer_id : customerId ,
74- stripe_payment_id : session . subscription as string ,
75- expiration_date : undefined ,
76- } )
77- console . log ( `License created for user ${ userId } with checkout session ${ session . id } ` )
78-
79- // Send Discord notification for successful checkout
80- try {
8197 const notificationEngine = getNotificationEngine ( )
8298
8399 await notificationEngine . sendNotification ( {
@@ -97,12 +113,41 @@ const handleCheckoutSessionCompleted = async (session: Stripe.Checkout.Session):
97113 currency : session . currency
98114 }
99115 } , [ 'discord' ] )
100-
101- console . log ( `Discord notification sent for successful checkout: ${ session . id } ` )
102- } catch ( error ) {
103- console . error ( 'Failed to send Discord notification for checkout completion:' , error )
104- // Don't throw - we don't want Discord failures to break webhook processing
116+ return
105117 }
118+ console . log ( 'Creating new subscription license' )
119+ await LicenseRepo . createLicense ( {
120+ user_id : userId ,
121+ status : 'active' ,
122+ license_type : productConfig . licenseType ,
123+ purchase_date : new Date ( ) ,
124+ stripe_customer_id : customerId ,
125+ stripe_payment_id : session . subscription as string ,
126+ expiration_date : undefined ,
127+ } )
128+ console . log ( `License created for user ${ userId } with checkout session ${ session . id } ` )
129+
130+ // Send Discord notification for successful checkout
131+ const notificationEngine = getNotificationEngine ( )
132+
133+ await notificationEngine . sendNotification ( {
134+ type : 'checkout_completed' ,
135+ user : {
136+ id : userId ,
137+ email : session . customer_details ?. email || session . customer_email || 'N/A'
138+ } ,
139+ referenceId : `checkout_completed_${ session . id } ` ,
140+ data : {
141+ session_id : session . id ,
142+ customer_id : customerId ,
143+ subscription_id : session . subscription ,
144+ product_id : productId ,
145+ license_type : productConfig . licenseType ,
146+ amount_total : session . amount_total ,
147+ currency : session . currency
148+ }
149+ } , [ 'discord' ] )
150+
106151 }
107152}
108153
@@ -236,10 +281,7 @@ const handleInvoicePaymentFailed = async (invoice: Stripe.Invoice): Promise<void
236281 // }
237282
238283 // Send Discord notification using NotificationEngine
239- try {
240- const notificationEngine = getNotificationEngine ( )
241-
242- await notificationEngine . sendNotification ( {
284+ await sendNotification ( {
243285 type : 'payment_failed' ,
244286 user : {
245287 id : invoice . customer as string || 'unknown' ,
@@ -253,13 +295,7 @@ const handleInvoicePaymentFailed = async (invoice: Stripe.Invoice): Promise<void
253295 customer_name : invoice . customer_name ,
254296 formatted_amount : `${ invoice . currency . toUpperCase ( ) } $${ ( invoice . amount_due / 100 ) . toFixed ( 2 ) } `
255297 }
256- } , [ 'discord' ] )
257-
258- console . log ( `Discord notification sent for payment failure: ${ invoice . id } ` )
259- } catch ( error ) {
260- console . error ( 'Failed to send Discord notification for payment failure:' , error )
261- // Don't throw - we don't want Discord failures to break webhook processing
262- }
298+ } )
263299}
264300
265301export const WebhookService = {
0 commit comments