@@ -35,6 +35,8 @@ import { FastifyPluginAsync } from "fastify";
3535import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi" ;
3636import stripe , { Stripe } from "stripe" ;
3737import rawbody from "fastify-raw-body" ;
38+ import { AvailableSQSFunctions , SQSPayload } from "common/types/sqsMessage.js" ;
39+ import { SendMessageCommand , SQSClient } from "@aws-sdk/client-sqs" ;
3840
3941const stripeRoutes : FastifyPluginAsync = async ( fastify , _options ) => {
4042 await fastify . register ( rawbody , {
@@ -227,7 +229,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
227229 name : null ,
228230 } ;
229231 const paymentLinkId = event . data . object . payment_link . toString ( ) ;
230- if ( ! paymentLinkId ) {
232+ if ( ! paymentLinkId || ! paymentCurrency || ! paymentAmount ) {
231233 return reply
232234 . code ( 200 )
233235 . send ( { handled : false , requestId : request . id } ) ;
@@ -247,18 +249,60 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
247249 message : "Could not check for payment link in table." ,
248250 } ) ;
249251 }
250- if ( ! response . Count || response . Count !== 1 ) {
252+ if ( ! response . Items || response . Items ?. length !== 1 ) {
251253 return reply . status ( 200 ) . send ( {
252254 handled : false ,
253255 requestId : request . id ,
254256 } ) ;
255257 }
258+ const unmarshalledEntry = unmarshall ( response . Items [ 0 ] ) as {
259+ userId : string ;
260+ invoiceId : string ;
261+ } ;
262+ if ( ! unmarshalledEntry . userId || ! unmarshalledEntry . invoiceId ) {
263+ return reply . status ( 200 ) . send ( {
264+ handled : false ,
265+ requestId : request . id ,
266+ } ) ;
267+ }
268+ const withCurrency = new Intl . NumberFormat ( "en-US" , {
269+ style : "currency" ,
270+ currency : paymentCurrency . toUpperCase ( ) ,
271+ } )
272+ . formatToParts ( paymentAmount / 100 )
273+ . map ( ( val ) => val . value )
274+ . join ( "" ) ;
256275 request . log . info (
257- `Registered payment of ${ paymentAmount } ${ paymentCurrency } by ${ name } (${ email } ) for payment link ${ paymentLinkId } .` ,
276+ `Registered payment of ${ withCurrency } by ${ name } (${ email } ) for payment link ${ paymentLinkId } invoice ID ${ unmarshalledEntry . invoiceId } ).` ,
277+ ) ;
278+ const sqsPayload : SQSPayload < AvailableSQSFunctions . EmailNotifications > =
279+ {
280+ function : AvailableSQSFunctions . EmailNotifications ,
281+ metadata : {
282+ initiator : eventId ,
283+ reqId : request . id ,
284+ } ,
285+ payload : {
286+ to : [ unmarshalledEntry . invoiceId ] ,
287+ subject : `Payment Recieved for Invoice ${ unmarshalledEntry . invoiceId } ` ,
288+ content :
`Received payment of ${ paymentAmount } ${ paymentCurrency } by ${ name } (${ email } ) for invoice ID ${ unmarshalledEntry . invoiceId } . Please contact [email protected] with any questions.` , 289+ } ,
290+ } ;
291+ if ( ! fastify . sqsClient ) {
292+ fastify . sqsClient = new SQSClient ( {
293+ region : genericConfig . AwsRegion ,
294+ } ) ;
295+ }
296+ const result = await fastify . sqsClient . send (
297+ new SendMessageCommand ( {
298+ QueueUrl : fastify . environmentConfig . SqsQueueUrl ,
299+ MessageBody : JSON . stringify ( sqsPayload ) ,
300+ } ) ,
258301 ) ;
259302 return reply . status ( 200 ) . send ( {
260303 handled : true ,
261304 requestId : request . id ,
305+ queueId : result . MessageId ,
262306 } ) ;
263307 }
264308 return reply
0 commit comments