Skip to content

Commit 5f891dd

Browse files
committed
send email notification to invoice owner
1 parent ecb93ef commit 5f891dd

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/api/routes/stripe.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { FastifyPluginAsync } from "fastify";
3535
import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi";
3636
import stripe, { Stripe } from "stripe";
3737
import rawbody from "fastify-raw-body";
38+
import { AvailableSQSFunctions, SQSPayload } from "common/types/sqsMessage.js";
39+
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
3840

3941
const 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

Comments
 (0)