Skip to content

Commit 54d91eb

Browse files
committed
added put error handling and general error handling
1 parent 4474a03 commit 54d91eb

File tree

1 file changed

+62
-29
lines changed

1 file changed

+62
-29
lines changed

src/api/routes/stripe.ts

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -721,39 +721,72 @@ Please contact Officer Board with any questions.`,
721721

722722
const amount = intent.amount_received;
723723
const currency = intent.currency;
724-
const customerId = intent.customer?.toString() ?? "UNKNOWN";
725-
const email =
726-
intent.receipt_email ??
727-
intent.metadata?.billing_email ??
728-
"unknown@example.com";
729-
const acmOrg = intent.metadata?.acm_org ?? "ACM@UIUC";
730-
const domain = email.includes("@")
731-
? email.split("@")[1]
732-
: "unknown.com";
724+
const customerId = intent.customer?.toString();
725+
const email = intent.receipt_email ?? intent.metadata?.billing_email;
726+
const acmOrg = intent.metadata?.acm_org;
733727

734-
await fastify.dynamoClient.send(
735-
new PutItemCommand({
736-
TableName: genericConfig.StripePaymentsDynamoTableName,
737-
Item: marshall({
738-
primaryKey: `${acmOrg}#${domain}`,
739-
sortKey: event.id,
740-
amount,
741-
currency,
742-
status: "succeeded",
743-
billingEmail: email,
744-
createdAt: new Date().toISOString(),
745-
eventId: event.id,
728+
if (!customerId) {
729+
request.log.info("Skipping payment intent with no customer ID.");
730+
return reply
731+
.code(200)
732+
.send({ handled: false, requestId: request.id });
733+
}
734+
735+
if (!email) {
736+
request.log.warn("Missing email for payment intent.");
737+
return reply
738+
.code(200)
739+
.send({ handled: false, requestId: request.id });
740+
}
741+
742+
if (!acmOrg) {
743+
request.log.warn("Missing acm_org for payment intent.");
744+
return reply
745+
.code(200)
746+
.send({ handled: false, requestId: request.id });
747+
}
748+
749+
if (!email.includes("@")) {
750+
request.log.warn("Invalid email format for payment intent.");
751+
return reply
752+
.code(200)
753+
.send({ handled: false, requestId: request.id });
754+
}
755+
const domain = email.split("@")[1];
756+
757+
try {
758+
await fastify.dynamoClient.send(
759+
new PutItemCommand({
760+
TableName: genericConfig.StripePaymentsDynamoTableName,
761+
Item: marshall({
762+
primaryKey: `${acmOrg}#${domain}`,
763+
sortKey: event.id,
764+
amount,
765+
currency,
766+
status: "succeeded",
767+
billingEmail: email,
768+
createdAt: new Date().toISOString(),
769+
eventId: event.id,
770+
}),
746771
}),
747-
}),
748-
);
772+
);
749773

750-
request.log.info(
751-
`Recorded successful payment ${intent.id} from ${email} (${amount} ${currency})`,
752-
);
774+
request.log.info(
775+
`Recorded successful payment ${intent.id} from ${email} (${amount} ${currency})`,
776+
);
753777

754-
return reply
755-
.status(200)
756-
.send({ handled: true, requestId: request.id });
778+
return reply
779+
.status(200)
780+
.send({ handled: true, requestId: request.id });
781+
} catch (e) {
782+
if (e instanceof BaseError) {
783+
throw e;
784+
}
785+
request.log.error(e);
786+
throw new DatabaseInsertError({
787+
message: `Could not insert Stripe payment record: ${(e as Error).message}`,
788+
});
789+
}
757790
}
758791
default:
759792
request.log.warn(`Unhandled event type: ${event.type}`);

0 commit comments

Comments
 (0)