@@ -30,6 +30,9 @@ const {
3030 routerParam,
3131 fastifyResponseChannel,
3232 fastifyPathParams,
33+ stripeCheckoutSessionCreate,
34+ stripePaymentIntentCreate,
35+ stripeConstructEvent,
3336} = require ( './channels' )
3437const waf = require ( './waf' )
3538const addresses = require ( './addresses' )
@@ -92,6 +95,9 @@ function enable (_config) {
9295 fastifyResponseChannel . subscribe ( onResponseBody )
9396 responseWriteHead . subscribe ( onResponseWriteHead )
9497 responseSetHeader . subscribe ( onResponseSetHeader )
98+ stripeCheckoutSessionCreate . subscribe ( onStripeCheckoutSessionCreate )
99+ stripePaymentIntentCreate . subscribe ( onStripePaymentIntentCreate )
100+ stripeConstructEvent . subscribe ( onStripeConstructEvent )
95101
96102 isEnabled = true
97103 config = _config
@@ -382,6 +388,100 @@ function onResponseSetHeader ({ res, abortController }) {
382388 }
383389}
384390
391+ function onStripeCheckoutSessionCreate ( payload ) {
392+ if ( payload ?. mode !== 'payment' ) return
393+
394+ waf . run ( {
395+ persistent : {
396+ [ addresses . PAYMENT_CREATION ] : {
397+ integration : 'stripe' ,
398+ id : payload . id ,
399+ amount_total : payload . amount_total ,
400+ client_reference_id : payload . client_reference_id ,
401+ currency : payload . currency ,
402+ 'discounts.coupon' : payload . discounts ?. [ 0 ] ?. coupon ,
403+ 'discounts.promotion_code' : payload . discounts ?. [ 0 ] ?. promotion_code ,
404+ livemode : payload . livemode ,
405+ 'total_details.amount_discount' : payload . total_details ?. amount_discount ,
406+ 'total_details.amount_shipping' : payload . total_details ?. amount_shipping ,
407+ } ,
408+ } ,
409+ } )
410+ }
411+
412+ function onStripePaymentIntentCreate ( payload ) {
413+ if ( payload === null || typeof payload !== 'object' ) return
414+
415+ waf . run ( {
416+ persistent : {
417+ [ addresses . PAYMENT_CREATION ] : {
418+ integration : 'stripe' ,
419+ id : payload . id ,
420+ amount : payload . amount ,
421+ currency : payload . currency ,
422+ livemode : payload . livemode ,
423+ payment_method : payload . payment_method ,
424+ } ,
425+ } ,
426+ } )
427+ }
428+
429+ function onStripeConstructEvent ( payload ) {
430+ const object = payload ?. data ?. object
431+ if ( object === null || typeof object !== 'object' ) return
432+
433+ let persistent
434+
435+ switch ( payload . type ) {
436+ case 'payment_intent.succeeded' :
437+ persistent = {
438+ [ addresses . PAYMENT_SUCCESS ] : {
439+ integration : 'stripe' ,
440+ id : object . id ,
441+ amount : object . amount ,
442+ currency : object . currency ,
443+ livemode : object . livemode ,
444+ payment_method : object . payment_method ,
445+ } ,
446+ }
447+ break
448+
449+ case 'payment_intent.payment_failed' :
450+ persistent = {
451+ [ addresses . PAYMENT_FAILURE ] : {
452+ integration : 'stripe' ,
453+ id : object . id ,
454+ amount : object . amount ,
455+ currency : object . currency ,
456+ 'last_payment_error.code' : object . last_payment_error ?. code ,
457+ 'last_payment_error.decline_code' : object . last_payment_error ?. decline_code ,
458+ 'last_payment_error.payment_method.id' : object . last_payment_error ?. payment_method ?. id ,
459+ 'last_payment_error.payment_method.type' : object . last_payment_error ?. payment_method ?. type ,
460+ livemode : object . livemode ,
461+ } ,
462+ }
463+ break
464+
465+ case 'payment_intent.canceled' :
466+ persistent = {
467+ [ addresses . PAYMENT_CANCELLATION ] : {
468+ integration : 'stripe' ,
469+ id : object . id ,
470+ amount : object . amount ,
471+ cancellation_reason : object . cancellation_reason ,
472+ currency : object . currency ,
473+ livemode : object . livemode ,
474+ } ,
475+ }
476+ break
477+
478+ default :
479+ return
480+ }
481+
482+ waf . run ( { persistent } )
483+ }
484+
385485function handleResults ( actions , req , res , rootSpan , abortController ) {
386486 if ( ! actions || ! req || ! res || ! rootSpan || ! abortController ) return
387487
@@ -427,6 +527,9 @@ function disable () {
427527 if ( fastifyResponseChannel . hasSubscribers ) fastifyResponseChannel . unsubscribe ( onResponseBody )
428528 if ( responseWriteHead . hasSubscribers ) responseWriteHead . unsubscribe ( onResponseWriteHead )
429529 if ( responseSetHeader . hasSubscribers ) responseSetHeader . unsubscribe ( onResponseSetHeader )
530+ if ( stripeCheckoutSessionCreate . hasSubscribers ) stripeCheckoutSessionCreate . unsubscribe ( onStripeCheckoutSessionCreate )
531+ if ( stripePaymentIntentCreate . hasSubscribers ) stripePaymentIntentCreate . unsubscribe ( onStripePaymentIntentCreate )
532+ if ( stripeConstructEvent . hasSubscribers ) stripeConstructEvent . unsubscribe ( onStripeConstructEvent )
430533}
431534
432535// this is faster than Object.keys().length === 0
0 commit comments