@@ -23,11 +23,11 @@ class CheckoutError extends Error {
2323
2424// Helper function to find or create a constituent
2525async function findOrCreateConstituent ( user ) {
26- let constituent = await Constituent . query ( ) . where ( 'email' , user . email ) . first ( ) ;
26+ let constituent = await Constituent . query ( ) . where ( 'email' , user . email ) . first ( )
2727 if ( ! constituent ) {
28- constituent = await Constituent . query ( ) . insert ( user ) ;
28+ constituent = await Constituent . query ( ) . insert ( user )
2929 }
30- return constituent ;
30+ return constituent
3131}
3232
3333// Helper function to create a transaction
@@ -38,151 +38,134 @@ async function createTransaction(stripeTransactionId, constituentId, donation, s
3838 amount : donation ,
3939 currency : 'usd' ,
4040 paymentMethod : 'credit_card' ,
41- status,
42- } ) ;
41+ status
42+ } )
4343}
4444
4545// Helper function to render letter HTML
4646async function renderLetterHtml ( letter , user ) {
4747 letter . merge_variables = {
4848 ...letter . merge_variables ,
4949 firstName : user . firstName ,
50- lastName : user . lastName ,
51- } ;
52- const template = await LetterTemplate . query ( ) . findById ( letter . letter_template_id ) ;
53- return Handlebars . render ( letter . merge_variables , template . html ) ;
50+ lastName : user . lastName
51+ }
52+ const template = await LetterTemplate . query ( ) . findById ( letter . letter_template_id )
53+ return Handlebars . render ( letter . merge_variables , template . html )
5454}
5555
5656// Helper function to save letters for each delivery method
5757async function saveLetters ( transactionId , constituentId , letter , html , deliveryMethods ) {
5858 for ( const method of deliveryMethods ) {
59- letter . trackingNumber = uuidv4 ( ) ; // Generate unique tracking number
60- console . log ( letter . trackingNumber ) ;
59+ letter . trackingNumber = uuidv4 ( ) // Generate unique tracking number
60+ console . log ( letter . trackingNumber )
6161
6262 await Letter . query ( ) . insert ( {
6363 transactionId,
6464 constituentId,
6565 letterTemplate : html ,
6666 deliveryMethod : method ,
67- ...letter ,
68- } ) ;
67+ ...letter
68+ } )
6969 }
7070}
7171
7272router . post ( '/create-transaction' , async ( req , res ) => {
73- const stripe = new Stripe ( ) ;
74- const db = require ( '../../db/connection' ) ;
73+ const stripe = new Stripe ( )
74+ const db = require ( '../../db/connection' )
7575
7676 try {
77- const { sessionId } = req . body ;
77+ const { sessionId } = req . body
7878
7979 if ( ! sessionId ) {
80- return res . status ( 400 ) . json ( { error : 'Session ID is required' } ) ;
80+ return res . status ( 400 ) . json ( { error : 'Session ID is required' } )
8181 }
8282
83- const session = await stripe . checkout . sessions . retrieve ( sessionId ) ;
83+ const session = await stripe . checkout . sessions . retrieve ( sessionId )
8484
8585 if ( ! session ) {
86- return res . status ( 404 ) . json ( { error : 'Stripe session not found' } ) ;
86+ return res . status ( 404 ) . json ( { error : 'Stripe session not found' } )
8787 }
8888
8989 const formattedTransaction = {
9090 stripe_transaction_id : sessionId ,
9191 amount : session . amount_total ,
9292 currency : session . currency ,
9393 payment_method : session . payment_method_types [ 0 ] || 'unknown' ,
94- email : session . customer_details ?. email || 'unknown' ,
95- } ;
94+ email : session . customer_details ?. email || 'unknown'
95+ }
9696
97- await db ( 'transactions' ) . insert ( formattedTransaction ) ;
97+ await db ( 'transactions' ) . insert ( formattedTransaction )
9898
9999 return res . status ( 201 ) . json ( {
100100 message : 'Transaction created successfully' ,
101- transaction : formattedTransaction ,
102- } ) ;
101+ transaction : formattedTransaction
102+ } )
103103 } catch ( error ) {
104- console . error ( 'Error creating transaction:' , error ) ;
104+ console . error ( 'Error creating transaction:' , error )
105105
106- const statusCode = error instanceof CheckoutError ? 400 : 500 ;
106+ const statusCode = error instanceof CheckoutError ? 400 : 500
107107
108- return res . status ( statusCode ) . json ( { error : error . message } ) ;
108+ return res . status ( statusCode ) . json ( { error : error . message } )
109109 }
110- } ) ;
111-
112-
110+ } )
113111
114112// Refactored /create-checkout-session route
115113router . post ( '/create-checkout-session' , async ( req , res ) => {
116- const { donation, user, letter, deliveryMethods } = req . body ;
117- const origin = req . get ( 'origin' ) ;
114+ const { donation, user, letter, deliveryMethods } = req . body
115+ const origin = req . get ( 'origin' )
118116
119117 try {
120- const presenter = new PaymentPresenter ( ) ;
121- presenter . validatePaymentAmount ( donation ) ; // Validate donation amount
118+ const presenter = new PaymentPresenter ( )
119+ presenter . validatePaymentAmount ( donation ) // Validate donation amount
122120
123121 // Handle free transactions
124122 if ( donation === 0 && process . env . VUE_APP_EMPTY_TRANSACTIONS === 'on' ) {
125- const CHECKOUT_SESSION_ID = uuidv4 ( ) ;
126- const redirectUrl = `${ origin } /complete?session_id=${ CHECKOUT_SESSION_ID } ` ;
123+ const CHECKOUT_SESSION_ID = uuidv4 ( )
124+ const redirectUrl = `${ origin } /complete?session_id=${ CHECKOUT_SESSION_ID } `
127125
128- const constituent = await findOrCreateConstituent ( user ) ;
129- console . log ( constituent . id ) ;
126+ const constituent = await findOrCreateConstituent ( user )
127+ console . log ( constituent . id )
130128
131129 const transaction = await createTransaction (
132130 `no-stripe-${ uuidv4 ( ) } ` ,
133131 constituent . id ,
134132 donation ,
135133 'succeeded'
136- ) ;
134+ )
137135
138- const html = await renderLetterHtml ( letter , user ) ;
139- await saveLetters ( transaction . id , constituent . id , letter , html , deliveryMethods ) ;
136+ const html = await renderLetterHtml ( letter , user )
137+ await saveLetters ( transaction . id , constituent . id , letter , html , deliveryMethods )
140138
141- return res . status ( 200 ) . json ( { url : redirectUrl , sessionId : CHECKOUT_SESSION_ID } ) . end ( ) ;
139+ return res . status ( 200 ) . json ( { url : redirectUrl , sessionId : CHECKOUT_SESSION_ID } ) . end ( )
142140 }
143141
144142 // Handle paid transactions
145- const redirectUrl = `${ origin } /complete?session_id={CHECKOUT_SESSION_ID}` ;
146- const cancelUrl = origin ;
143+ const redirectUrl = `${ origin } /complete?session_id={CHECKOUT_SESSION_ID}`
144+ const cancelUrl = origin
147145
148- const stripe = new Stripe ( ) ;
149- const session = await stripe . createCheckoutSession ( donation , redirectUrl , cancelUrl ) ;
146+ const stripe = new Stripe ( )
147+ const session = await stripe . createCheckoutSession ( donation , redirectUrl , cancelUrl )
150148
151- const constituent = await findOrCreateConstituent ( user ) ;
152- console . log ( constituent . id ) ;
149+ const constituent = await findOrCreateConstituent ( user )
150+ console . log ( constituent . id )
153151
154- const transaction = await createTransaction ( session . paymentIntent , constituent . id , donation ) ;
152+ const transaction = await createTransaction ( session . paymentIntent , constituent . id , donation )
155153
156- const html = await renderLetterHtml ( letter , user ) ;
157- await saveLetters ( transaction . id , constituent . id , letter , html , deliveryMethods ) ;
154+ const html = await renderLetterHtml ( letter , user )
155+ await saveLetters ( transaction . id , constituent . id , letter , html , deliveryMethods )
158156
159- return res . status ( 200 ) . json ( { url : session . url , sessionId : session . id } ) . end ( ) ;
157+ return res . status ( 200 ) . json ( { url : session . url , sessionId : session . id } ) . end ( )
160158 } catch ( error ) {
161- console . error ( error ) ;
159+ console . error ( error )
162160
163- const statusCode = error instanceof PaymentPresenterError ? 400 : 500 ;
164- return res . status ( statusCode ) . json ( { error : error . message } ) . end ( ) ;
161+ const statusCode = error instanceof PaymentPresenterError ? 400 : 500
162+ return res . status ( statusCode ) . json ( { error : error . message } ) . end ( )
165163 }
166- } ) ;
164+ } )
167165
168166router . post ( '/process-transaction' , async ( req , res ) => {
169167 try {
170- // const stripe = new Stripe()
171-
172- // If livemode is false, disable signature checking
173- // and event reconstructionfor ease of testing.
174168 let event
175- /*
176- if (stripe.livemode) {
177- const signature = req.headers['stripe-signature']
178- if (!signature) throw new CheckoutError('No stripe signature on request!')
179-
180- event = stripe.validateEvent(signature, req.rawBody)
181- } else {
182- event = req.body
183- // console.log(event)
184- }
185- */
186169 event = req . body
187170
188171 if ( ! event ) throw new CheckoutError ( 'Unprocessable message' )
@@ -193,8 +176,6 @@ router.post('/process-transaction', async (req, res) => {
193176
194177 console . log ( paymentIntent , amount , eventOutcome )
195178
196- // We are not going to send letters from here just yet
197- // so we will record the transaction no matter the outcome.
198179 if ( eventType !== 'payment_intent' ) {
199180 throw new CheckoutError (
200181 `Unexpected event! Received ${ eventType } but it could not be processed.`
@@ -256,7 +237,6 @@ router.post('/process-transaction', async (req, res) => {
256237 }
257238
258239 if ( error instanceof StripeError ) {
259- // Don't leak Stripe logging.
260240 console . error ( error . message )
261241 error . message = 'Payment processing error'
262242 }
@@ -265,4 +245,4 @@ router.post('/process-transaction', async (req, res) => {
265245 }
266246} )
267247
268- module . exports = router
248+ module . exports = router
0 commit comments